immlib 1.0.0.dev2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. immlib/__init__.py +131 -0
  2. immlib/_init.py +108 -0
  3. immlib/_version.py +235 -0
  4. immlib/doc/__init__.py +38 -0
  5. immlib/doc/_core.py +311 -0
  6. immlib/iolib/__init__.py +29 -0
  7. immlib/iolib/_core.py +720 -0
  8. immlib/pathlib/__init__.py +69 -0
  9. immlib/pathlib/_cache.py +152 -0
  10. immlib/pathlib/_core.py +869 -0
  11. immlib/pathlib/_osf.py +538 -0
  12. immlib/test/__init__.py +16 -0
  13. immlib/test/__main__.py +10 -0
  14. immlib/test/doc/__init__.py +6 -0
  15. immlib/test/doc/test_core.py +91 -0
  16. immlib/test/iolib/__init__.py +7 -0
  17. immlib/test/iolib/test_core.py +81 -0
  18. immlib/test/pathlib/__init__.py +11 -0
  19. immlib/test/pathlib/test_core.py +146 -0
  20. immlib/test/pathlib/test_osf.py +54 -0
  21. immlib/test/types/__init__.py +5 -0
  22. immlib/test/types/test_core.py +110 -0
  23. immlib/test/util/__init__.py +11 -0
  24. immlib/test/util/test_core.py +681 -0
  25. immlib/test/util/test_numeric.py +1374 -0
  26. immlib/test/util/test_quantity.py +218 -0
  27. immlib/test/util/test_url.py +51 -0
  28. immlib/test/workflow/__init__.py +9 -0
  29. immlib/test/workflow/test_core.py +418 -0
  30. immlib/test/workflow/test_plantype.py +248 -0
  31. immlib/types/__init__.py +29 -0
  32. immlib/types/_core.py +333 -0
  33. immlib/util/__init__.py +283 -0
  34. immlib/util/_core.py +2524 -0
  35. immlib/util/_numeric.py +2651 -0
  36. immlib/util/_quantity.py +523 -0
  37. immlib/util/_url.py +114 -0
  38. immlib/workflow/__init__.py +48 -0
  39. immlib/workflow/_core.py +1635 -0
  40. immlib/workflow/_plantype.py +334 -0
  41. immlib-1.0.0.dev2.dist-info/METADATA +76 -0
  42. immlib-1.0.0.dev2.dist-info/RECORD +45 -0
  43. immlib-1.0.0.dev2.dist-info/WHEEL +5 -0
  44. immlib-1.0.0.dev2.dist-info/licenses/LICENSE +21 -0
  45. immlib-1.0.0.dev2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,218 @@
1
+ # -*- coding: utf-8 -*-
2
+ ################################################################################
3
+ # immlib/test/util/test_quantity.py
4
+ #
5
+ # Tests of the quantity module in immlib: i.e., tests for the code in the
6
+ # immlib.util._quantity module.
7
+
8
+
9
+ # Dependencies #################################################################
10
+
11
+ from unittest import TestCase
12
+
13
+ class TestUtilQuantity(TestCase):
14
+ """Tests the immlib.util._quantity module."""
15
+ # Pint Utilities ###########################################################
16
+ def test_is_ureg(self):
17
+ from immlib import (units, is_ureg)
18
+ from pint import UnitRegistry
19
+ # immlib.units is a registry.
20
+ self.assertTrue(is_ureg(units))
21
+ # So is any new UnitsRegistry we create.
22
+ self.assertTrue(is_ureg(UnitRegistry()))
23
+ # Other objects are not.
24
+ self.assertFalse(is_ureg(None))
25
+ def test_is_unit(self):
26
+ from immlib import (units, is_unit)
27
+ from pint import UnitRegistry
28
+ # We will use an alternate unit registry in some tests.
29
+ alt_units = UnitRegistry()
30
+ # Units from any unit registry are allowed by default.
31
+ self.assertTrue(is_unit(units.mm))
32
+ self.assertTrue(is_unit(units.gram))
33
+ self.assertTrue(is_unit(alt_units.mm))
34
+ self.assertTrue(is_unit(alt_units.gram))
35
+ # Things that aren't units are never units.
36
+ self.assertFalse(is_unit('mm'))
37
+ self.assertFalse(is_unit(10.0))
38
+ self.assertFalse(is_unit(None))
39
+ self.assertFalse(is_unit(10.0 * units.mm))
40
+ # If the ureg parameter is ..., then only units from the immlib units
41
+ # registry are allowed.
42
+ self.assertTrue(is_unit(units.mm, ureg=...))
43
+ self.assertFalse(is_unit(alt_units.mm, ureg=...))
44
+ # Alternately, the ureg parameter may be a specific unit registry.
45
+ self.assertFalse(is_unit(units.mm, ureg=alt_units))
46
+ self.assertTrue(is_unit(alt_units.mm, ureg=alt_units))
47
+ def test_is_quant(self):
48
+ from immlib import (units, is_quant)
49
+ from pint import UnitRegistry
50
+ # We will use an alternate unit registry in some tests.
51
+ alt_units = UnitRegistry()
52
+ # By default, it does not matter what registry a quantity comes from;
53
+ # it is considered a quantity.
54
+ q = 10.0 * units.mm
55
+ alt_q = 10.0 * alt_units.mm
56
+ self.assertTrue(is_quant(q))
57
+ self.assertTrue(is_quant(alt_q))
58
+ # Other objects aren't quantities.
59
+ self.assertFalse(is_quant(10.0))
60
+ self.assertFalse(is_quant(units.mm))
61
+ self.assertFalse(is_quant(None))
62
+ # We can require that a quantity meet a certain kind of unit type.
63
+ self.assertTrue(is_quant(q, unit=units.mm))
64
+ self.assertTrue(is_quant(q, unit='inches'))
65
+ self.assertFalse(is_quant(q, unit=units.grams))
66
+ self.assertFalse(is_quant(q, unit='seconds'))
67
+ # The ureg parameter changes whether any unit registry is allowed (the
68
+ # default, or ureg=None), only immlib.units is allowed (ureg=Ellipsis),
69
+ # or a specific unit registry is allowed.
70
+ self.assertTrue(is_quant(q, ureg=...))
71
+ self.assertFalse(is_quant(alt_q, ureg=...))
72
+ self.assertFalse(is_quant(q, ureg=alt_units))
73
+ self.assertTrue(is_quant(alt_q, ureg=alt_units))
74
+ def test_default_ureg(self):
75
+ from immlib import default_ureg
76
+ from pint import UnitRegistry
77
+ # You can set the immlib default units registry (immlib.units) temporarily
78
+ # in an execution context using the default_ureg function:
79
+ ureg = UnitRegistry()
80
+ with default_ureg(ureg):
81
+ from immlib import units
82
+ self.assertIs(units, ureg)
83
+ # This only affects the code inside the with-block.
84
+ from immlib import units
85
+ self.assertIsNot(units, ureg)
86
+ def test_like_unit(self):
87
+ from immlib import (like_unit, units)
88
+ # like_unit returns True when its argument is like a unit. This can be,
89
+ # for one, objects that already are units.
90
+ self.assertTrue(like_unit(units.mm))
91
+ self.assertTrue(like_unit(units.count))
92
+ # Otherwise, only strings may be unit-like.
93
+ self.assertFalse(like_unit(None))
94
+ self.assertFalse(like_unit(10))
95
+ self.assertFalse(like_unit([]))
96
+ # Strings that name units are unit-like.
97
+ self.assertTrue(like_unit('mm'))
98
+ self.assertTrue(like_unit('count'))
99
+ def test_unit(self):
100
+ from immlib import (unit, units)
101
+ from immlib.util import unitregistry
102
+ from pint.errors import UndefinedUnitError
103
+ from pint import UnitRegistry
104
+ # unit converts its argument into a unit. Units themselves are returned
105
+ # as-is.
106
+ u = units.mm
107
+ self.assertIs(u, unit(u))
108
+ u = units.count
109
+ self.assertIs(u, unit(u))
110
+ # Strings that name units can be converted into units.
111
+ self.assertEqual(units.mm, unit('mm'))
112
+ self.assertEqual(units.count, unit('count'))
113
+ # If the argument isn't a valid unit, then an error is raised.
114
+ with self.assertRaises(ValueError): unit(None)
115
+ with self.assertRaises(ValueError): unit(10)
116
+ with self.assertRaises(ValueError): unit([])
117
+ with self.assertRaises(UndefinedUnitError): unit('fdjsklfajdk')
118
+ # If Ellipsis is passed for the ureg argument of unit, it converts the
119
+ # unit from another unit registry into the immlib.units registry.
120
+ ureg = UnitRegistry()
121
+ self.assertIsNot(ureg.mm, unit(ureg.mm, ...))
122
+ self.assertIs(unitregistry(unit(ureg.count, ...)), units)
123
+ # Similarly we can convert back to the other ureg.
124
+ self.assertIsNot(units.mm, unit(units.mm, ureg))
125
+ self.assertIs(unitregistry(unit(units.count, ureg)), ureg)
126
+ def test_alike_units(self):
127
+ from immlib import (alike_units, units)
128
+ # alike_units tells us if two units are of the same unit category, like
129
+ # meters and feet both being lengths.
130
+ self.assertTrue(alike_units(units.mm, units.feet))
131
+ self.assertTrue(alike_units(units.seconds, units.days))
132
+ self.assertTrue(alike_units(units.rad, units.degree))
133
+ self.assertFalse(alike_units(units.rad, units.feet))
134
+ self.assertFalse(alike_units(units.days, units.mm))
135
+ self.assertFalse(alike_units(units.mm, units.degree))
136
+ def test_quant(self):
137
+ from immlib import (default_ureg, units, quant)
138
+ from pint import (UnitRegistry, Quantity)
139
+ import torch, numpy as np
140
+ # The quant function lets you create pint quantities; by default these
141
+ # are registered in the immlib.units UnitRegistry.
142
+ circ = np.linspace(0, 1, 25)
143
+ self.assertIsInstance(quant(10, 'mm'), Quantity)
144
+ self.assertIsInstance(quant([10, 30, 40], 'days'), Quantity)
145
+ self.assertIsInstance(quant(circ, 'turns'), Quantity)
146
+ self.assertEqual(quant(10, 'mm').m, 10)
147
+ self.assertTrue(np.array_equal(quant(circ, 'turns').m, circ))
148
+ # Iterables are upgraded to numpy arrays when applicable.
149
+ self.assertIsInstance(quant([10, 30, 40], 'days').m, np.ndarray)
150
+ self.assertTrue(np.array_equal(quant([10, 30, 40], 'days').m,
151
+ [10, 30, 40]))
152
+ # Units are registered in the immlib.units registry by default.
153
+ self.assertEqual(quant(10, 'mm').u, units.mm)
154
+ self.assertEqual(quant([10, 30, 40], 'days').u, units.days)
155
+ self.assertEqual(quant(circ, 'turns').u, units.turns)
156
+ # Tensors also work as quantities.
157
+ t = torch.linspace(0,1,5)
158
+ tq = quant(t, 'mm')
159
+ self.assertIsInstance(tq.m, torch.Tensor)
160
+ self.assertIs(tq.m, t)
161
+ # Changing the default unit registry changes how these are registered.
162
+ ureg = UnitRegistry()
163
+ with default_ureg(ureg):
164
+ q = quant(10, 'mm')
165
+ self.assertIsInstance(q, ureg.Quantity)
166
+ self.assertFalse(isinstance(q, units.Quantity))
167
+ # This can also be done with the ureg option.
168
+ q = quant(10, 'mm', ureg=ureg)
169
+ self.assertIsInstance(q, ureg.Quantity)
170
+ self.assertFalse(isinstance(q, units.Quantity))
171
+ def test_mag(self):
172
+ from immlib import (mag, units, quant)
173
+ import numpy as np
174
+ from pint.errors import DimensionalityError
175
+ # The mag function extracts the magnitude from a quantity.
176
+ m = np.linspace(0, 1, 5)
177
+ q = quant(m, 'second')
178
+ self.assertIs(m, mag(q))
179
+ self.assertEqual(10, mag(10 * units.mm))
180
+ # mag can extract in the quantity's native units if none are given, or
181
+ # in another unit, if requested.
182
+ self.assertTrue(np.array_equal(m * 1000, mag(q, 'ms')))
183
+ # If the value passed to mag is not a quantity, it is returned as-is,
184
+ # and is assumed to be in the correct unit.
185
+ self.assertIs(m, mag(m))
186
+ # The same is true, even if a unit is passed: non-quantities are always
187
+ # assumed to be in the correct units.
188
+ self.assertIs(m, mag(m, 'ms'))
189
+ self.assertIs(m, mag(m, 'days'))
190
+ self.assertIs(m, mag(m, 'feet'))
191
+ # If unit=None, then the argument must not be a quantity.
192
+ self.assertIs(m, mag(m, None))
193
+ with self.assertRaises(ValueError): mag(q, None)
194
+ # If the unit doesn't match, a DimensionalityError is raised.
195
+ with self.assertRaises(DimensionalityError): mag(q, 'miles')
196
+ def test_promote(self):
197
+ from immlib import (promote, units, quant)
198
+ import torch, numpy as np
199
+ # promote converts all arguments into quantities.
200
+ a = np.linspace(0, 1, 5)
201
+ b = np.arange(10)
202
+ (qa, qb) = promote(a, b)
203
+ self.assertNotIsInstance(qa, units.Quantity)
204
+ self.assertNotIsInstance(qb, units.Quantity)
205
+ self.assertIs(qa, a)
206
+ self.assertIs(qb, b)
207
+ # If one of the arguments is a tensor, all results will be tensors.
208
+ c = quant(torch.linspace(0, 1, 5), 'mm')
209
+ (qa, qb, qc) = promote(a, b, c)
210
+ self.assertNotIsInstance(qa, units.Quantity)
211
+ self.assertNotIsInstance(qb, units.Quantity)
212
+ self.assertIsInstance(qc, units.Quantity)
213
+ self.assertIsInstance(qa, torch.Tensor)
214
+ self.assertIsInstance(qb, torch.Tensor)
215
+ self.assertIsInstance(qc.m, torch.Tensor)
216
+ self.assertTrue(np.array_equal(qa.numpy(), a))
217
+ self.assertTrue(np.array_equal(qb.numpy(), b))
218
+ self.assertIs(qc, c)
@@ -0,0 +1,51 @@
1
+ # -*- coding: utf-8 -*-
2
+ ################################################################################
3
+ # immlib/test/util/test_url.py
4
+
5
+ """Tests of the URL utilities module in immlib: i.e., tests for the code in the
6
+ immlib.util._url module.
7
+ """
8
+
9
+
10
+ # Dependencies #################################################################
11
+
12
+ from unittest import TestCase
13
+
14
+
15
+ # Tests ########################################################################
16
+
17
+ lic_url = (
18
+ 'https://raw.githubusercontent.com/noahbenson/immlib'
19
+ '/refs/heads/main/LICENSE')
20
+
21
+ class TestUtilURL(TestCase):
22
+ """Tests the immlib.util._url module."""
23
+ def test_is_url(self):
24
+ "Tests the is_url function."
25
+ from immlib.util import is_url
26
+ self.assertTrue(is_url(lic_url))
27
+ self.assertTrue(is_url('https://github.com/noahbenson/immlib.git'))
28
+ self.assertTrue(is_url('s3://natural-scenes-dataset/'))
29
+ self.assertTrue(is_url('file:///etc/groups'))
30
+ self.assertFalse(is_url(None))
31
+ self.assertFalse(is_url(10))
32
+ self.assertFalse(is_url('test'))
33
+ def test_can_download_url(self):
34
+ "Tests the can_download_url function."
35
+ from immlib.util import can_download_url
36
+ self.assertTrue(can_download_url(lic_url))
37
+ self.assertTrue(can_download_url('https://github.com/noahbenson'))
38
+ badurl = 'https://github.com/noahbenson/no-repo-here'
39
+ self.assertFalse(can_download_url(badurl))
40
+ def test_url_download(self):
41
+ from tempfile import TemporaryDirectory
42
+ from pathlib import Path
43
+ from immlib.util import url_download
44
+ b = url_download(lic_url)
45
+ self.assertIn(b'MIT License', b)
46
+ with TemporaryDirectory() as tmpdir:
47
+ p = Path(tmpdir)
48
+ url_download(lic_url, p / "LICENSE")
49
+ with (p / "LICENSE").open('rt') as fl:
50
+ dat = fl.readlines()
51
+ self.assertEqual(dat[0].strip(), "MIT License")
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ ################################################################################
3
+ # immlib/test/workflow/__init__.py
4
+ #
5
+ # Tests of the immlib.workflow module.
6
+
7
+
8
+ from .test_core import TestWorkflowCore
9
+ from .test_plantype import TestWorkflowPlanType