elements 3.2.0__tar.gz → 3.4.0__tar.gz

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 (38) hide show
  1. {elements-3.2.0/elements.egg-info → elements-3.4.0}/PKG-INFO +5 -5
  2. {elements-3.2.0 → elements-3.4.0}/README.md +4 -4
  3. {elements-3.2.0 → elements-3.4.0}/elements/__init__.py +1 -1
  4. {elements-3.2.0 → elements-3.4.0}/elements/path.py +57 -5
  5. elements-3.4.0/elements/tree.py +41 -0
  6. {elements-3.2.0 → elements-3.4.0/elements.egg-info}/PKG-INFO +5 -5
  7. {elements-3.2.0 → elements-3.4.0}/elements.egg-info/SOURCES.txt +2 -1
  8. elements-3.4.0/elements.egg-info/requires.txt +5 -0
  9. {elements-3.2.0 → elements-3.4.0}/requirements-optional.txt +0 -3
  10. elements-3.4.0/requirements.txt +5 -0
  11. elements-3.4.0/tests/test_tree.py +70 -0
  12. elements-3.2.0/elements/tree.py +0 -31
  13. elements-3.2.0/elements.egg-info/requires.txt +0 -1
  14. elements-3.2.0/requirements.txt +0 -1
  15. {elements-3.2.0 → elements-3.4.0}/LICENSE +0 -0
  16. {elements-3.2.0 → elements-3.4.0}/MANIFEST.in +0 -0
  17. {elements-3.2.0 → elements-3.4.0}/elements/agg.py +0 -0
  18. {elements-3.2.0 → elements-3.4.0}/elements/checkpoint.py +0 -0
  19. {elements-3.2.0 → elements-3.4.0}/elements/config.py +0 -0
  20. {elements-3.2.0 → elements-3.4.0}/elements/counter.py +0 -0
  21. {elements-3.2.0 → elements-3.4.0}/elements/flags.py +0 -0
  22. {elements-3.2.0 → elements-3.4.0}/elements/fps.py +0 -0
  23. {elements-3.2.0 → elements-3.4.0}/elements/logger.py +0 -0
  24. {elements-3.2.0 → elements-3.4.0}/elements/plotting.py +0 -0
  25. {elements-3.2.0 → elements-3.4.0}/elements/printing.py +0 -0
  26. {elements-3.2.0 → elements-3.4.0}/elements/rwlock.py +0 -0
  27. {elements-3.2.0 → elements-3.4.0}/elements/timer.py +0 -0
  28. {elements-3.2.0 → elements-3.4.0}/elements/usage.py +0 -0
  29. {elements-3.2.0 → elements-3.4.0}/elements/utils.py +0 -0
  30. {elements-3.2.0 → elements-3.4.0}/elements/uuid.py +0 -0
  31. {elements-3.2.0 → elements-3.4.0}/elements/when.py +0 -0
  32. {elements-3.2.0 → elements-3.4.0}/elements.egg-info/dependency_links.txt +0 -0
  33. {elements-3.2.0 → elements-3.4.0}/elements.egg-info/top_level.txt +0 -0
  34. {elements-3.2.0 → elements-3.4.0}/setup.cfg +0 -0
  35. {elements-3.2.0 → elements-3.4.0}/setup.py +0 -0
  36. {elements-3.2.0 → elements-3.4.0}/tests/test_basics.py +0 -0
  37. {elements-3.2.0 → elements-3.4.0}/tests/test_flags.py +0 -0
  38. {elements-3.2.0 → elements-3.4.0}/tests/test_path.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: elements
3
- Version: 3.2.0
3
+ Version: 3.4.0
4
4
  Summary: Building blocks for productive research.
5
5
  Home-page: http://github.com/danijar/elements
6
6
  Classifier: Intended Audience :: Science/Research
@@ -228,14 +228,14 @@ print(cp.model)
228
228
  ### `elements.Timer`
229
229
 
230
230
  Collect timing statistics about the run time of different parts of a program.
231
- Measures code inside scopes and can wrap methods into scopes. Returns execution
232
- count, execution time, fraction of overall program time, and more. The
233
- resulting statisticse can be added to the logger.
231
+ Measures code inside sections and can wrap methods into sections. Returns
232
+ execution count, execution time, fraction of overall program time, and more.
233
+ The resulting statisticse can be added to the logger.
234
234
 
235
235
  ```python
236
236
  timer = Timer()
237
237
 
238
- timer.scope('foo'):
238
+ timer.section('foo'):
239
239
  time.sleep(10)
240
240
 
241
241
  timer.wrap('name', obj, ['method1', 'method2'])
@@ -217,14 +217,14 @@ print(cp.model)
217
217
  ### `elements.Timer`
218
218
 
219
219
  Collect timing statistics about the run time of different parts of a program.
220
- Measures code inside scopes and can wrap methods into scopes. Returns execution
221
- count, execution time, fraction of overall program time, and more. The
222
- resulting statisticse can be added to the logger.
220
+ Measures code inside sections and can wrap methods into sections. Returns
221
+ execution count, execution time, fraction of overall program time, and more.
222
+ The resulting statisticse can be added to the logger.
223
223
 
224
224
  ```python
225
225
  timer = Timer()
226
226
 
227
- timer.scope('foo'):
227
+ timer.section('foo'):
228
228
  time.sleep(10)
229
229
 
230
230
  timer.wrap('name', obj, ['method1', 'method2'])
@@ -1,4 +1,4 @@
1
- __version__ = '3.2.0'
1
+ __version__ = '3.4.0'
2
2
 
3
3
  from .agg import Agg
4
4
  from .checkpoint import Checkpoint
@@ -166,7 +166,60 @@ class LocalPath(Path):
166
166
  shutil.move(self, dest)
167
167
 
168
168
 
169
- class GFilePath(Path):
169
+ class GCSPath(Path):
170
+
171
+ __slots__ = ('_path',)
172
+
173
+ fs = None
174
+
175
+ def __init__(self, path):
176
+ path = str(path)
177
+ if not (path.startswith('/') or '://' in path):
178
+ path = os.path.abspath(os.path.expanduser(path))
179
+ super().__init__(path)
180
+ if not type(self).fs:
181
+ import gcsfs
182
+ type(self).fs = gcsfs.GCSFileSystem()
183
+
184
+ @contextlib.contextmanager
185
+ def open(self, mode='r'):
186
+ yield self.fs.open(str(self), mode)
187
+
188
+ def absolute(self):
189
+ return self
190
+
191
+ def glob(self, pattern):
192
+ for path in self.fs.glob(f'{str(self)}/{pattern}'):
193
+ yield type(self)(path)
194
+
195
+ def exists(self):
196
+ return self.fs.exists(str(self))
197
+
198
+ def isfile(self):
199
+ return self.fs.isfile(str(self))
200
+
201
+ def isdir(self):
202
+ return self.fs.isdir(str(self))
203
+
204
+ def mkdirs(self):
205
+ self.fs.makedirs(str(self), exist_ok=True)
206
+
207
+ def remove(self):
208
+ self.fs.rm(str(self), recursive=False)
209
+
210
+ def rmtree(self):
211
+ self.fs.rm(str(self), recursive=True)
212
+
213
+ def copy(self, dest):
214
+ dest = Path(dest)
215
+ self.fs.copy(str(self), str(dest), recursive=True)
216
+
217
+ def move(self, dest):
218
+ dest = Path(dest)
219
+ self.fs.mv(self, str(dest), recursive=True)
220
+
221
+
222
+ class TFPath(Path):
170
223
 
171
224
  __slots__ = ('_path',)
172
225
 
@@ -220,7 +273,7 @@ class GFilePath(Path):
220
273
  self.gfile.rmtree(str(self))
221
274
 
222
275
  def copy(self, dest):
223
- dest = type(self)(dest)
276
+ dest = Path(dest)
224
277
  if self.isfile():
225
278
  self.gfile.copy(str(self), str(dest), overwrite=True)
226
279
  else:
@@ -232,12 +285,11 @@ class GFilePath(Path):
232
285
 
233
286
  def move(self, dest):
234
287
  dest = Path(dest)
235
- if dest.isdir():
236
- dest.rmtree()
237
288
  self.gfile.rename(self, str(dest), overwrite=True)
238
289
 
239
290
 
240
291
  Path.filesystems = [
241
- (GFilePath, lambda path: path.startswith('gs://')),
292
+ (GCSPath, lambda path: path.startswith('gs://')),
293
+ (TFPath, lambda path: path.startswith('/cns/')),
242
294
  (LocalPath, lambda path: True),
243
295
  ]
@@ -0,0 +1,41 @@
1
+ from . import printing
2
+
3
+
4
+ def map(fn, *trees, isleaf=None):
5
+ assert trees, 'Provide one or more nested Python structures'
6
+ kw = dict(isleaf=isleaf)
7
+ first = trees[0]
8
+ try:
9
+ assert all(isinstance(x, type(first)) for x in trees)
10
+ if isleaf and isleaf(trees[0]):
11
+ return fn(*trees)
12
+ if isinstance(first, list):
13
+ assert all(len(x) == len(first) for x in trees)
14
+ return [map(
15
+ fn, *[t[i] for t in trees], **kw) for i in range(len(first))]
16
+ if isinstance(first, tuple):
17
+ assert all(len(x) == len(first) for x in trees)
18
+ return tuple([map(
19
+ fn, *[t[i] for t in trees], **kw) for i in range(len(first))])
20
+ if isinstance(first, dict):
21
+ assert all(set(x.keys()) == set(first.keys()) for x in trees)
22
+ return {k: map(fn, *[t[k] for t in trees], **kw) for k in first}
23
+ if hasattr(first, 'keys') and hasattr(first, 'get'):
24
+ assert all(set(x.keys()) == set(first.keys()) for x in trees)
25
+ return type(first)(
26
+ {k: map(fn, *[t[k] for t in trees], **kw) for k in first})
27
+ except AssertionError:
28
+ raise TypeError(printing.format_(trees))
29
+ return fn(*trees)
30
+
31
+
32
+ def flatten(tree, isleaf=None):
33
+ leaves = []
34
+ map(lambda x: leaves.append(x), tree, isleaf=isleaf)
35
+ structure = map(lambda x: None, tree, isleaf=isleaf)
36
+ return tuple(leaves), structure
37
+
38
+
39
+ def unflatten(leaves, structure):
40
+ leaves = iter(tuple(leaves))
41
+ return map(lambda x: next(leaves), structure)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: elements
3
- Version: 3.2.0
3
+ Version: 3.4.0
4
4
  Summary: Building blocks for productive research.
5
5
  Home-page: http://github.com/danijar/elements
6
6
  Classifier: Intended Audience :: Science/Research
@@ -228,14 +228,14 @@ print(cp.model)
228
228
  ### `elements.Timer`
229
229
 
230
230
  Collect timing statistics about the run time of different parts of a program.
231
- Measures code inside scopes and can wrap methods into scopes. Returns execution
232
- count, execution time, fraction of overall program time, and more. The
233
- resulting statisticse can be added to the logger.
231
+ Measures code inside sections and can wrap methods into sections. Returns
232
+ execution count, execution time, fraction of overall program time, and more.
233
+ The resulting statisticse can be added to the logger.
234
234
 
235
235
  ```python
236
236
  timer = Timer()
237
237
 
238
- timer.scope('foo'):
238
+ timer.section('foo'):
239
239
  time.sleep(10)
240
240
 
241
241
  timer.wrap('name', obj, ['method1', 'method2'])
@@ -29,4 +29,5 @@ elements.egg-info/requires.txt
29
29
  elements.egg-info/top_level.txt
30
30
  tests/test_basics.py
31
31
  tests/test_flags.py
32
- tests/test_path.py
32
+ tests/test_path.py
33
+ tests/test_tree.py
@@ -0,0 +1,5 @@
1
+ colored
2
+ gcsfs
3
+ numpy
4
+ psutil
5
+ ruamel.yaml
@@ -1,9 +1,6 @@
1
- colored
2
1
  gputil
3
2
  matplotlib
4
3
  mlflow
5
- psutil
6
4
  pytest
7
- ruamel.yaml
8
5
  tensorflow-cpu
9
6
  wandb
@@ -0,0 +1,5 @@
1
+ colored
2
+ gcsfs
3
+ numpy
4
+ psutil
5
+ ruamel.yaml
@@ -0,0 +1,70 @@
1
+ import elements
2
+ import pytest
3
+
4
+
5
+ class TestTree:
6
+
7
+ def test_map_identity(self):
8
+ tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
9
+ copy = elements.tree.map(lambda x: x, tree)
10
+ assert copy == tree
11
+ assert copy is not tree
12
+
13
+ def test_map_double(self):
14
+ tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
15
+ ref = {'a': 24, 'b': ['cc', (2, 4, 6)]}
16
+ res = elements.tree.map(lambda x: x * 2, tree)
17
+ assert ref == res
18
+
19
+ def test_map_multiple(self):
20
+ tree1 = {'a': 1, 'b': ['foo', (1, 2, 3)]}
21
+ tree2 = {'a': 2, 'b': ['bar', (10, 20, 30)]}
22
+ ref = {'a': 3, 'b': ['foobar', (11, 22, 33)]}
23
+ res = elements.tree.map(lambda x, y: x + y, tree1, tree2)
24
+ assert ref == res
25
+ with pytest.raises(TypeError):
26
+ tree1 = {'a': 1, 'b': ['foo', (1, 2, 3)]}
27
+ tree2 = {'b': 2}
28
+ res = elements.tree.map(lambda x, y: x + y, tree1, tree2)
29
+
30
+ def test_map_isleaf(self):
31
+ tree = {'a': 1, 'b': ['foo', (1, 2, 3)]}
32
+ isleaf = lambda x: isinstance(x, list)
33
+ leaves = []
34
+ elements.tree.map(lambda x: leaves.append(x), tree, isleaf=isleaf)
35
+ assert leaves == [1, ['foo', (1, 2, 3)]]
36
+
37
+ def test_map_degenerate(self):
38
+ assert elements.tree.map(lambda x: 2 * x, ()) == ()
39
+ assert elements.tree.map(lambda x: 2 * x, 12) == 24
40
+ assert elements.tree.map(lambda x: 2 * x, [[{}, ()]]) == [[{}, ()]]
41
+
42
+ def test_flatten_basic(self):
43
+ tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
44
+ leaves, structure = elements.tree.flatten(tree)
45
+ assert leaves == (12, 'c', 1, 2, 3)
46
+ assert structure == {'a': None, 'b': [None, (None, None, None)]}
47
+
48
+ def test_flatten_isleaf(self):
49
+ tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
50
+ isleaf = lambda x: isinstance(x, list)
51
+ leaves, structure = elements.tree.flatten(tree, isleaf=isleaf)
52
+ assert leaves == (12, ['c', (1, 2, 3)])
53
+ assert structure == {'a': None, 'b': None}
54
+
55
+ def test_flatten_degenerate(self):
56
+ assert elements.tree.flatten(()) == ((), ())
57
+ assert elements.tree.flatten(12) == ((12,), None)
58
+ assert elements.tree.flatten([[{}, ()]]) == ((), [[{}, ()]])
59
+
60
+ def test_unflatten(self):
61
+ trees = [
62
+ {'a': 12, 'b': ['c', (1, 2, 3)]},
63
+ (),
64
+ 12,
65
+ [[{}, ()]],
66
+ ]
67
+ for tree in trees:
68
+ leaves, structure = elements.tree.flatten(tree)
69
+ copy = elements.tree.unflatten(leaves, structure)
70
+ assert copy == tree
@@ -1,31 +0,0 @@
1
- from . import printing
2
-
3
-
4
- def map_(fn, *trees, isleaf=None):
5
- assert trees, 'Provide one or more nested Python structures'
6
- kw = dict(isleaf=isleaf)
7
- first = trees[0]
8
- assert all(isinstance(x, type(first)) for x in trees)
9
- if isleaf and isleaf(trees):
10
- return fn(*trees)
11
- if isinstance(first, list):
12
- assert all(len(x) == len(first) for x in trees), printing.format_(trees)
13
- return [map_(
14
- fn, *[t[i] for t in trees], **kw) for i in range(len(first))]
15
- if isinstance(first, tuple):
16
- assert all(len(x) == len(first) for x in trees), printing.format_(trees)
17
- return tuple([map_(
18
- fn, *[t[i] for t in trees], **kw) for i in range(len(first))])
19
- if isinstance(first, dict):
20
- assert all(set(x.keys()) == set(first.keys()) for x in trees), (
21
- printing.format_(trees))
22
- return {k: map_(fn, *[t[k] for t in trees], **kw) for k in first}
23
- if hasattr(first, 'keys') and hasattr(first, 'get'):
24
- assert all(set(x.keys()) == set(first.keys()) for x in trees), (
25
- printing.format_(trees))
26
- return type(first)(
27
- {k: map_(fn, *[t[k] for t in trees], **kw) for k in first})
28
- return fn(*trees)
29
-
30
-
31
- map = map_
@@ -1 +0,0 @@
1
- numpy
@@ -1 +0,0 @@
1
- numpy
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes