bw-processing 0.8.5__tar.gz → 0.9.1__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.
- {bw-processing-0.8.5/bw_processing.egg-info → bw_processing-0.9.1}/PKG-INFO +17 -5
- {bw-processing-0.8.5 → bw_processing-0.9.1}/README.md +3 -3
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/__init__.py +8 -2
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/constants.py +17 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/datapackage.py +172 -30
- bw_processing-0.9.1/bw_processing/examples/parquet_files.py +109 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/io_helpers.py +64 -8
- bw_processing-0.9.1/bw_processing/io_parquet_helpers.py +154 -0
- bw_processing-0.9.1/bw_processing/io_pyarrow_helpers.py +289 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/merging.py +2 -2
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/unique_fields.py +3 -2
- {bw-processing-0.8.5 → bw_processing-0.9.1/bw_processing.egg-info}/PKG-INFO +17 -5
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing.egg-info/SOURCES.txt +6 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing.egg-info/requires.txt +1 -0
- bw_processing-0.9.1/docs/conf.py +109 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/setup.cfg +1 -0
- bw_processing-0.9.1/tests/fixtures/basic_arrays.py +42 -0
- bw_processing-0.9.1/tests/helpers/__init__.py +6 -0
- bw_processing-0.9.1/tests/helpers/basic_array_helpers.py +33 -0
- bw-processing-0.8.5/docs/conf.py +0 -48
- {bw-processing-0.8.5 → bw_processing-0.9.1}/LICENSE +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/MANIFEST.in +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/array_creation.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/errors.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/examples/__init__.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/examples/interfaces.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/examples/simple.zip +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/filesystem.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/indexing.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/proxies.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing/utils.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing.egg-info/dependency_links.txt +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing.egg-info/not-zip-safe +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/bw_processing.egg-info/top_level.txt +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/dev/calculation_package.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/dev/loading.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/dev/processed_package.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/dev/resources.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/dev/speed_tests.py +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/pyproject.toml +0 -0
- {bw-processing-0.8.5 → bw_processing-0.9.1}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: bw-processing
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9.1
|
|
4
4
|
Summary: Tools to create structured arrays in a common format
|
|
5
5
|
Home-page: https://github.com/brightway-lca/bw_processing
|
|
6
6
|
Author: Chris Mutel
|
|
@@ -22,10 +22,22 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.11
|
|
23
23
|
Requires-Python: >=3.8
|
|
24
24
|
Description-Content-Type: text/markdown; charset=UTF-8
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: fs
|
|
27
|
+
Requires-Dist: numpy
|
|
28
|
+
Requires-Dist: pandas
|
|
25
29
|
Provides-Extra: testing
|
|
30
|
+
Requires-Dist: setuptools; extra == "testing"
|
|
31
|
+
Requires-Dist: pytest; extra == "testing"
|
|
32
|
+
Requires-Dist: pytest-cov; extra == "testing"
|
|
33
|
+
Requires-Dist: pyarrow; extra == "testing"
|
|
26
34
|
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
36
|
+
Requires-Dist: pylint; extra == "dev"
|
|
27
37
|
Provides-Extra: docs
|
|
28
|
-
|
|
38
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
39
|
+
Requires-Dist: myst_parser; extra == "docs"
|
|
40
|
+
Requires-Dist: furo; extra == "docs"
|
|
29
41
|
|
|
30
42
|
# bw-processing
|
|
31
43
|
|
|
@@ -34,10 +46,10 @@ Library for storing numeric data for use in matrix-based calculations. Designed
|
|
|
34
46
|
[][pypi status]
|
|
35
47
|
[][pypi status]
|
|
36
48
|
[][pypi status]
|
|
37
|
-
[][license]
|
|
38
50
|
|
|
39
51
|
[][read the docs]
|
|
40
|
-
[][tests]
|
|
41
53
|
[][codecov]
|
|
42
54
|
|
|
43
55
|
[][pre-commit]
|
|
@@ -45,7 +57,7 @@ Library for storing numeric data for use in matrix-based calculations. Designed
|
|
|
45
57
|
|
|
46
58
|
[pypi status]: https://pypi.org/project/bw-processing/
|
|
47
59
|
[read the docs]: https://bw-processing.readthedocs.io/
|
|
48
|
-
[tests]: https://github.com/brightway-lca/
|
|
60
|
+
[tests]: https://github.com/brightway-lca/bw_processing/actions/workflows/python-test.yml
|
|
49
61
|
[codecov]: https://app.codecov.io/gh/brightway-lca/bw-processing
|
|
50
62
|
[pre-commit]: https://github.com/pre-commit/pre-commit
|
|
51
63
|
[black]: https://github.com/psf/black
|
|
@@ -5,10 +5,10 @@ Library for storing numeric data for use in matrix-based calculations. Designed
|
|
|
5
5
|
[][pypi status]
|
|
6
6
|
[][pypi status]
|
|
7
7
|
[][pypi status]
|
|
8
|
-
[][license]
|
|
9
9
|
|
|
10
10
|
[][read the docs]
|
|
11
|
-
[][tests]
|
|
12
12
|
[][codecov]
|
|
13
13
|
|
|
14
14
|
[][pre-commit]
|
|
@@ -16,7 +16,7 @@ Library for storing numeric data for use in matrix-based calculations. Designed
|
|
|
16
16
|
|
|
17
17
|
[pypi status]: https://pypi.org/project/bw-processing/
|
|
18
18
|
[read the docs]: https://bw-processing.readthedocs.io/
|
|
19
|
-
[tests]: https://github.com/brightway-lca/
|
|
19
|
+
[tests]: https://github.com/brightway-lca/bw_processing/actions/workflows/python-test.yml
|
|
20
20
|
[codecov]: https://app.codecov.io/gh/brightway-lca/bw-processing
|
|
21
21
|
[pre-commit]: https://github.com/pre-commit/pre-commit
|
|
22
22
|
[black]: https://github.com/psf/black
|
|
@@ -15,6 +15,7 @@ __all__ = (
|
|
|
15
15
|
"generic_zipfile_filesystem",
|
|
16
16
|
"INDICES_DTYPE",
|
|
17
17
|
"load_datapackage",
|
|
18
|
+
"MatrixSerializeFormat",
|
|
18
19
|
"md5",
|
|
19
20
|
"merge_datapackages_with_mask",
|
|
20
21
|
"reindex",
|
|
@@ -25,11 +26,16 @@ __all__ = (
|
|
|
25
26
|
"UndefinedInterface",
|
|
26
27
|
)
|
|
27
28
|
|
|
28
|
-
__version__ = "0.
|
|
29
|
+
__version__ = "0.9.1"
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
from .array_creation import create_array, create_structured_array
|
|
32
|
-
from .constants import
|
|
33
|
+
from .constants import (
|
|
34
|
+
DEFAULT_LICENSES,
|
|
35
|
+
INDICES_DTYPE,
|
|
36
|
+
UNCERTAINTY_DTYPE,
|
|
37
|
+
MatrixSerializeFormat,
|
|
38
|
+
)
|
|
33
39
|
from .datapackage import (
|
|
34
40
|
Datapackage,
|
|
35
41
|
DatapackageBase,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import re
|
|
2
|
+
from enum import Enum
|
|
2
3
|
|
|
3
4
|
import numpy as np
|
|
4
5
|
|
|
@@ -30,3 +31,19 @@ DEFAULT_LICENSES = [
|
|
|
30
31
|
"title": "Open Data Commons Public Domain Dedication and License v1.0",
|
|
31
32
|
}
|
|
32
33
|
]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class MatrixSerializeFormat(str, Enum):
|
|
37
|
+
"""
|
|
38
|
+
Enum with the serializing formats for the vectors and matrices.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
NUMPY = "numpy" # numpy .npy format
|
|
42
|
+
PARQUET = "parquet" # Apache .parquet format
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# FILE EXTENSIONS
|
|
46
|
+
NUMPY_SERIALIZE_FORMAT_EXTENSION = ".npy"
|
|
47
|
+
NUMPY_SERIALIZE_FORMAT_NAME = "npy"
|
|
48
|
+
PARQUET_SERIALIZE_FORMAT_EXTENSION = ".parquet"
|
|
49
|
+
PARQUET_SERIALIZE_FORMAT_NAME = "pqt"
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import uuid
|
|
3
|
+
from abc import ABC
|
|
3
4
|
from functools import partial
|
|
4
5
|
from typing import Any, Dict, List, Optional, Union
|
|
5
6
|
|
|
@@ -9,7 +10,15 @@ from fs.base import FS
|
|
|
9
10
|
from fs.errors import ResourceNotFound
|
|
10
11
|
from fs.memoryfs import MemoryFS
|
|
11
12
|
|
|
12
|
-
from .constants import
|
|
13
|
+
from .constants import (
|
|
14
|
+
DEFAULT_LICENSES,
|
|
15
|
+
INDICES_DTYPE,
|
|
16
|
+
NUMPY_SERIALIZE_FORMAT_EXTENSION,
|
|
17
|
+
NUMPY_SERIALIZE_FORMAT_NAME,
|
|
18
|
+
PARQUET_SERIALIZE_FORMAT_EXTENSION,
|
|
19
|
+
PARQUET_SERIALIZE_FORMAT_NAME,
|
|
20
|
+
MatrixSerializeFormat,
|
|
21
|
+
)
|
|
13
22
|
from .errors import (
|
|
14
23
|
Closed,
|
|
15
24
|
InvalidMimetype,
|
|
@@ -25,10 +34,13 @@ from .proxies import Proxy, UndefinedInterface
|
|
|
25
34
|
from .utils import check_name, check_suffix, load_bytes, resolve_dict_iterator
|
|
26
35
|
|
|
27
36
|
|
|
28
|
-
class DatapackageBase:
|
|
37
|
+
class DatapackageBase(ABC):
|
|
29
38
|
"""Base class for datapackages. Not for normal use - you should use either `Datapackage` or `FilteredDatapackage`."""
|
|
30
39
|
|
|
31
40
|
def __init__(self):
|
|
41
|
+
# This is the global serialization format used for numpy.ndarray for the whole object.
|
|
42
|
+
# It can be overwritten locally for each numpy.ndarray introduced.
|
|
43
|
+
self._matrix_serialize_format_type = MatrixSerializeFormat.NUMPY
|
|
32
44
|
self._finalized = False
|
|
33
45
|
self._modified = set()
|
|
34
46
|
|
|
@@ -333,6 +345,7 @@ class Datapackage(DatapackageBase):
|
|
|
333
345
|
seed: Optional[int] = None,
|
|
334
346
|
sum_intra_duplicates: bool = True,
|
|
335
347
|
sum_inter_duplicates: bool = False,
|
|
348
|
+
matrix_serialize_format_type: MatrixSerializeFormat = MatrixSerializeFormat.NUMPY,
|
|
336
349
|
) -> None:
|
|
337
350
|
"""Start a new data package.
|
|
338
351
|
|
|
@@ -344,6 +357,11 @@ class Datapackage(DatapackageBase):
|
|
|
344
357
|
check_name(name)
|
|
345
358
|
|
|
346
359
|
self.fs = fs or MemoryFS()
|
|
360
|
+
if not isinstance(matrix_serialize_format_type, MatrixSerializeFormat):
|
|
361
|
+
raise TypeError(
|
|
362
|
+
f"Matrix serialize format type ({matrix_serialize_format_type}) not recognized!"
|
|
363
|
+
)
|
|
364
|
+
self._matrix_serialize_format_type = matrix_serialize_format_type
|
|
347
365
|
|
|
348
366
|
self.metadata = {
|
|
349
367
|
"profile": "data-package",
|
|
@@ -351,12 +369,13 @@ class Datapackage(DatapackageBase):
|
|
|
351
369
|
"id": id_ or uuid.uuid4().hex,
|
|
352
370
|
"licenses": (metadata or {}).get("licenses", DEFAULT_LICENSES),
|
|
353
371
|
"resources": [],
|
|
354
|
-
"created": datetime.datetime.
|
|
372
|
+
"created": datetime.datetime.now(datetime.UTC).isoformat("T") + "Z",
|
|
355
373
|
"combinatorial": combinatorial,
|
|
356
374
|
"sequential": sequential,
|
|
357
375
|
"seed": seed,
|
|
358
376
|
"sum_intra_duplicates": sum_intra_duplicates,
|
|
359
377
|
"sum_inter_duplicates": sum_inter_duplicates,
|
|
378
|
+
"matrix_serialize_format_type": matrix_serialize_format_type.value,
|
|
360
379
|
}
|
|
361
380
|
for k, v in (metadata or {}).items():
|
|
362
381
|
if k not in self.metadata:
|
|
@@ -380,6 +399,7 @@ class Datapackage(DatapackageBase):
|
|
|
380
399
|
mimetype="application/json",
|
|
381
400
|
)
|
|
382
401
|
self.fs.close()
|
|
402
|
+
self._finalized = True
|
|
383
403
|
|
|
384
404
|
def _prepare_modifications(self) -> None:
|
|
385
405
|
self._check_length_consistency()
|
|
@@ -406,6 +426,7 @@ class Datapackage(DatapackageBase):
|
|
|
406
426
|
name: Optional[str] = None,
|
|
407
427
|
dict_iterator: Any = None,
|
|
408
428
|
nrows: Optional[int] = None,
|
|
429
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
409
430
|
**kwargs,
|
|
410
431
|
) -> None:
|
|
411
432
|
"""Create a persistant vector from an iterator. Uses the utility function ``resolve_dict_iterator``.
|
|
@@ -426,6 +447,7 @@ class Datapackage(DatapackageBase):
|
|
|
426
447
|
indices_array=indices_array,
|
|
427
448
|
flip_array=flip_array,
|
|
428
449
|
distributions_array=distributions_array,
|
|
450
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
429
451
|
**kwargs,
|
|
430
452
|
)
|
|
431
453
|
|
|
@@ -439,6 +461,7 @@ class Datapackage(DatapackageBase):
|
|
|
439
461
|
flip_array: Optional[np.ndarray] = None,
|
|
440
462
|
distributions_array: Optional[np.ndarray] = None,
|
|
441
463
|
keep_proxy: bool = False,
|
|
464
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
442
465
|
**kwargs,
|
|
443
466
|
) -> None:
|
|
444
467
|
""" """
|
|
@@ -458,6 +481,9 @@ class Datapackage(DatapackageBase):
|
|
|
458
481
|
group=name,
|
|
459
482
|
kind="indices",
|
|
460
483
|
keep_proxy=keep_proxy,
|
|
484
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
485
|
+
meta_object="vector",
|
|
486
|
+
meta_type="indices",
|
|
461
487
|
**kwargs,
|
|
462
488
|
)
|
|
463
489
|
if data_array is not None:
|
|
@@ -480,6 +506,9 @@ class Datapackage(DatapackageBase):
|
|
|
480
506
|
name=name + ".data",
|
|
481
507
|
kind="data",
|
|
482
508
|
keep_proxy=keep_proxy,
|
|
509
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
510
|
+
meta_object="vector",
|
|
511
|
+
meta_type="generic",
|
|
483
512
|
**kwargs,
|
|
484
513
|
)
|
|
485
514
|
if distributions_array is not None:
|
|
@@ -500,6 +529,9 @@ class Datapackage(DatapackageBase):
|
|
|
500
529
|
group=name,
|
|
501
530
|
kind="distributions",
|
|
502
531
|
keep_proxy=keep_proxy,
|
|
532
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
533
|
+
meta_object="vector",
|
|
534
|
+
meta_type="distributions",
|
|
503
535
|
**kwargs,
|
|
504
536
|
)
|
|
505
537
|
if flip_array is not None:
|
|
@@ -524,6 +556,9 @@ class Datapackage(DatapackageBase):
|
|
|
524
556
|
name=name + ".flip",
|
|
525
557
|
kind="flip",
|
|
526
558
|
keep_proxy=keep_proxy,
|
|
559
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
560
|
+
meta_object="vector",
|
|
561
|
+
meta_type="generic",
|
|
527
562
|
**kwargs,
|
|
528
563
|
)
|
|
529
564
|
|
|
@@ -536,6 +571,7 @@ class Datapackage(DatapackageBase):
|
|
|
536
571
|
name: Optional[str] = None,
|
|
537
572
|
flip_array: Optional[np.ndarray] = None,
|
|
538
573
|
keep_proxy: bool = False,
|
|
574
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
539
575
|
**kwargs,
|
|
540
576
|
) -> None:
|
|
541
577
|
""" """
|
|
@@ -553,6 +589,9 @@ class Datapackage(DatapackageBase):
|
|
|
553
589
|
kind="indices",
|
|
554
590
|
group=name,
|
|
555
591
|
keep_proxy=keep_proxy,
|
|
592
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
593
|
+
meta_object="vector",
|
|
594
|
+
meta_type="indices",
|
|
556
595
|
**kwargs,
|
|
557
596
|
)
|
|
558
597
|
|
|
@@ -575,6 +614,9 @@ class Datapackage(DatapackageBase):
|
|
|
575
614
|
group=name,
|
|
576
615
|
kind="data",
|
|
577
616
|
keep_proxy=keep_proxy,
|
|
617
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
618
|
+
meta_object="matrix",
|
|
619
|
+
meta_type="generic",
|
|
578
620
|
**kwargs,
|
|
579
621
|
)
|
|
580
622
|
if flip_array is not None:
|
|
@@ -598,17 +640,63 @@ class Datapackage(DatapackageBase):
|
|
|
598
640
|
name=name + ".flip",
|
|
599
641
|
kind="flip",
|
|
600
642
|
keep_proxy=keep_proxy,
|
|
643
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
644
|
+
meta_object="vector",
|
|
645
|
+
meta_type="generic",
|
|
601
646
|
**kwargs,
|
|
602
647
|
)
|
|
603
648
|
|
|
604
649
|
def write_modified(self):
|
|
605
650
|
"""Write the data in modified files to the filesystem (if allowed)."""
|
|
606
651
|
for index in self._modified:
|
|
652
|
+
# get resource
|
|
653
|
+
resource = self.resources[index]
|
|
654
|
+
mediatype = resource["mediatype"]
|
|
655
|
+
path = resource["path"]
|
|
656
|
+
matrix_serialize_format_type = MatrixSerializeFormat.NUMPY # by default
|
|
657
|
+
# only used for parquet format
|
|
658
|
+
meta_object = None
|
|
659
|
+
meta_type = None
|
|
660
|
+
if mediatype == "application/octet-stream":
|
|
661
|
+
format = resource.get("format", None)
|
|
662
|
+
if format == PARQUET_SERIALIZE_FORMAT_NAME:
|
|
663
|
+
# parquet format
|
|
664
|
+
matrix_serialize_format_type = MatrixSerializeFormat.PARQUET
|
|
665
|
+
# try to guess object and type
|
|
666
|
+
kind = resource["kind"]
|
|
667
|
+
category = resource.get("category", None)
|
|
668
|
+
if kind == "indices":
|
|
669
|
+
meta_object = "vector"
|
|
670
|
+
meta_type = "indices"
|
|
671
|
+
elif kind == "flip":
|
|
672
|
+
meta_object = "vector"
|
|
673
|
+
meta_type = "generic"
|
|
674
|
+
elif kind == "distributions":
|
|
675
|
+
meta_object = "vector"
|
|
676
|
+
meta_type = "distributions"
|
|
677
|
+
elif kind == "data":
|
|
678
|
+
meta_type = "generic"
|
|
679
|
+
if category == "vector":
|
|
680
|
+
meta_object = "vector"
|
|
681
|
+
elif category == "array":
|
|
682
|
+
meta_object = "matrix"
|
|
683
|
+
else:
|
|
684
|
+
raise NotImplementedError(
|
|
685
|
+
f"Parquet format not available for resource with kind={kind} and category={category}!"
|
|
686
|
+
)
|
|
687
|
+
else:
|
|
688
|
+
raise NotImplementedError(
|
|
689
|
+
f"Parquet format not available for resource with kind={kind}!"
|
|
690
|
+
)
|
|
691
|
+
|
|
607
692
|
file_writer(
|
|
608
693
|
data=self.data[index],
|
|
609
694
|
fs=self.fs,
|
|
610
|
-
resource=
|
|
611
|
-
mimetype=
|
|
695
|
+
resource=path,
|
|
696
|
+
mimetype=mediatype,
|
|
697
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
698
|
+
meta_object=meta_object,
|
|
699
|
+
meta_type=meta_type,
|
|
612
700
|
)
|
|
613
701
|
|
|
614
702
|
self._modified = set()
|
|
@@ -621,9 +709,35 @@ class Datapackage(DatapackageBase):
|
|
|
621
709
|
matrix: str,
|
|
622
710
|
kind: str,
|
|
623
711
|
keep_proxy: bool = False,
|
|
712
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
713
|
+
meta_object: Optional[str] = None,
|
|
714
|
+
meta_type: Optional[str] = None,
|
|
624
715
|
**kwargs,
|
|
625
716
|
) -> None:
|
|
626
|
-
|
|
717
|
+
assert (
|
|
718
|
+
array.ndim <= 2
|
|
719
|
+
), f"Numpy array should be of dim 2 or less instead of {array.ndim}!"
|
|
720
|
+
|
|
721
|
+
if matrix_serialize_format_type is None:
|
|
722
|
+
# use instance default serialization format
|
|
723
|
+
matrix_serialize_format_type = self._matrix_serialize_format_type
|
|
724
|
+
else:
|
|
725
|
+
assert isinstance(
|
|
726
|
+
matrix_serialize_format_type, MatrixSerializeFormat
|
|
727
|
+
), f"Matrix serialization format {matrix_serialize_format_type} not recognized!"
|
|
728
|
+
|
|
729
|
+
filename = None
|
|
730
|
+
format = None
|
|
731
|
+
if matrix_serialize_format_type == MatrixSerializeFormat.NUMPY:
|
|
732
|
+
filename = check_suffix(name, NUMPY_SERIALIZE_FORMAT_EXTENSION)
|
|
733
|
+
format = NUMPY_SERIALIZE_FORMAT_NAME
|
|
734
|
+
elif matrix_serialize_format_type == MatrixSerializeFormat.PARQUET:
|
|
735
|
+
filename = check_suffix(name, PARQUET_SERIALIZE_FORMAT_EXTENSION)
|
|
736
|
+
format = PARQUET_SERIALIZE_FORMAT_NAME
|
|
737
|
+
else:
|
|
738
|
+
raise TypeError(
|
|
739
|
+
f"Matrix serialize format type {matrix_serialize_format_type} is not recognized!"
|
|
740
|
+
)
|
|
627
741
|
|
|
628
742
|
if not isinstance(self.fs, MemoryFS):
|
|
629
743
|
file_writer(
|
|
@@ -631,6 +745,9 @@ class Datapackage(DatapackageBase):
|
|
|
631
745
|
fs=self.fs,
|
|
632
746
|
resource=filename,
|
|
633
747
|
mimetype="application/octet-stream",
|
|
748
|
+
matrix_serialize_format_type=matrix_serialize_format_type, # NIKO
|
|
749
|
+
meta_object=meta_object,
|
|
750
|
+
meta_type=meta_type,
|
|
634
751
|
)
|
|
635
752
|
|
|
636
753
|
if keep_proxy:
|
|
@@ -649,7 +766,7 @@ class Datapackage(DatapackageBase):
|
|
|
649
766
|
resource = {
|
|
650
767
|
# Datapackage generic
|
|
651
768
|
"profile": "data-resource",
|
|
652
|
-
"format":
|
|
769
|
+
"format": format,
|
|
653
770
|
"mediatype": "application/octet-stream",
|
|
654
771
|
"name": name,
|
|
655
772
|
# Brightway specific
|
|
@@ -669,6 +786,7 @@ class Datapackage(DatapackageBase):
|
|
|
669
786
|
name: Optional[str] = None,
|
|
670
787
|
flip_array: Optional[np.ndarray] = None, # Not interface
|
|
671
788
|
keep_proxy: bool = False,
|
|
789
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
672
790
|
**kwargs,
|
|
673
791
|
) -> None:
|
|
674
792
|
self._prepare_modifications()
|
|
@@ -685,6 +803,9 @@ class Datapackage(DatapackageBase):
|
|
|
685
803
|
group=name,
|
|
686
804
|
kind="indices",
|
|
687
805
|
keep_proxy=keep_proxy,
|
|
806
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
807
|
+
meta_object="vector",
|
|
808
|
+
meta_type="indices",
|
|
688
809
|
**kwargs,
|
|
689
810
|
)
|
|
690
811
|
if flip_array is not None:
|
|
@@ -708,6 +829,9 @@ class Datapackage(DatapackageBase):
|
|
|
708
829
|
name=name + ".flip",
|
|
709
830
|
kind="flip",
|
|
710
831
|
keep_proxy=keep_proxy,
|
|
832
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
833
|
+
meta_object="vector",
|
|
834
|
+
meta_type="generic",
|
|
711
835
|
**kwargs,
|
|
712
836
|
)
|
|
713
837
|
|
|
@@ -730,6 +854,7 @@ class Datapackage(DatapackageBase):
|
|
|
730
854
|
name: Optional[str] = None,
|
|
731
855
|
flip_array: Optional[np.ndarray] = None,
|
|
732
856
|
keep_proxy: bool = False,
|
|
857
|
+
matrix_serialize_format_type: Optional[MatrixSerializeFormat] = None,
|
|
733
858
|
**kwargs,
|
|
734
859
|
) -> None:
|
|
735
860
|
"""`interface` must support the presamples API."""
|
|
@@ -750,6 +875,9 @@ class Datapackage(DatapackageBase):
|
|
|
750
875
|
group=name,
|
|
751
876
|
kind="indices",
|
|
752
877
|
keep_proxy=keep_proxy,
|
|
878
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
879
|
+
meta_object="vector",
|
|
880
|
+
meta_type="indices",
|
|
753
881
|
**kwargs,
|
|
754
882
|
)
|
|
755
883
|
if flip_array is not None:
|
|
@@ -773,6 +901,9 @@ class Datapackage(DatapackageBase):
|
|
|
773
901
|
name=name + ".flip",
|
|
774
902
|
kind="flip",
|
|
775
903
|
keep_proxy=keep_proxy,
|
|
904
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
905
|
+
meta_object="vector",
|
|
906
|
+
meta_type="generic",
|
|
776
907
|
**kwargs,
|
|
777
908
|
)
|
|
778
909
|
|
|
@@ -911,6 +1042,7 @@ def create_datapackage(
|
|
|
911
1042
|
seed: Optional[int] = None,
|
|
912
1043
|
sum_intra_duplicates: bool = True,
|
|
913
1044
|
sum_inter_duplicates: bool = False,
|
|
1045
|
+
matrix_serialize_format_type: MatrixSerializeFormat = MatrixSerializeFormat.NUMPY,
|
|
914
1046
|
) -> Datapackage:
|
|
915
1047
|
"""Create a new data package.
|
|
916
1048
|
|
|
@@ -921,16 +1053,16 @@ def create_datapackage(
|
|
|
921
1053
|
Licenses are specified as a list in ``metadata``. The default license is the `Open Data Commons Public Domain Dedication and License v1.0 <http://opendatacommons.org/licenses/pddl/>`__.
|
|
922
1054
|
|
|
923
1055
|
Args:
|
|
924
|
-
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
*
|
|
928
|
-
*
|
|
929
|
-
*
|
|
930
|
-
*
|
|
931
|
-
*
|
|
932
|
-
*
|
|
933
|
-
*
|
|
1056
|
+
* fs: A ``Filesystem``, optional. A new ``MemoryFS`` is used if not provided.
|
|
1057
|
+
* name: ``str``, optional. A new uuid is used if not provided.
|
|
1058
|
+
* `id_`: ``str``, optional. A new uuid is used if not provided.
|
|
1059
|
+
* metadata: ``dict``, optional. Metadata dictionary following datapackage specification; see above.
|
|
1060
|
+
* combinatorial: ``bool``, default ``False`` .: Policy on how to sample columns across multiple data arrays; see readme.
|
|
1061
|
+
* sequential: ``bool``, default ``False`` .: Policy on how to sample columns in data arrays; see readme.
|
|
1062
|
+
* seed: ``int``, optional. Seed to use in random number generator.
|
|
1063
|
+
* sum_intra_duplicates: ``bool``, default ``True``. Should duplicate elements in a single data resource be summed together, or should the last value replace previous values.
|
|
1064
|
+
* sum_inter_duplicates: ``bool``, default ``False``. Should duplicate elements in across data resources be summed together, or should the last value replace previous values. Order of data resources is given by the order they are added to the data package.
|
|
1065
|
+
* matrix_serialize_format_type: ``MatrixSerializeFormat``, default ``MatrixSerializeFormat.NUMPY``. Matrix serialization format type.
|
|
934
1066
|
|
|
935
1067
|
Returns:
|
|
936
1068
|
|
|
@@ -948,6 +1080,7 @@ def create_datapackage(
|
|
|
948
1080
|
seed=seed,
|
|
949
1081
|
sum_intra_duplicates=sum_intra_duplicates,
|
|
950
1082
|
sum_inter_duplicates=sum_inter_duplicates,
|
|
1083
|
+
matrix_serialize_format_type=matrix_serialize_format_type,
|
|
951
1084
|
)
|
|
952
1085
|
return obj
|
|
953
1086
|
|
|
@@ -965,9 +1098,9 @@ def load_datapackage(
|
|
|
965
1098
|
|
|
966
1099
|
Args:
|
|
967
1100
|
|
|
968
|
-
* fs_or_obj
|
|
969
|
-
* mmap_mode
|
|
970
|
-
* proxy
|
|
1101
|
+
* fs_or_obj: A `Filesystem` or an instance of `DatapackageBase`.
|
|
1102
|
+
* mmap_mode: `str`, optional. Define memory mapping mode to use when loading Numpy arrays.
|
|
1103
|
+
* proxy: bool, default `False`. Load proxies instead of complete Numpy arrays; see above.
|
|
971
1104
|
|
|
972
1105
|
Returns:
|
|
973
1106
|
|
|
@@ -982,27 +1115,36 @@ def load_datapackage(
|
|
|
982
1115
|
return obj
|
|
983
1116
|
|
|
984
1117
|
|
|
985
|
-
def simple_graph(data: dict, fs: Optional[FS]=None, **metadata):
|
|
1118
|
+
def simple_graph(data: dict, fs: Optional[FS] = None, **metadata)->Datapackage:
|
|
986
1119
|
"""Easy creation of simple datapackages with only persistent vectors.
|
|
987
1120
|
|
|
988
|
-
|
|
1121
|
+
Args:
|
|
1122
|
+
* data: is a dictionary.
|
|
1123
|
+
The data dictionary has the form::
|
|
989
1124
|
|
|
990
|
-
|
|
1125
|
+
{
|
|
1126
|
+
matrix_name: [
|
|
1127
|
+
(row_id, col_id, value, flip)
|
|
1128
|
+
]
|
|
1129
|
+
}
|
|
991
1130
|
|
|
992
|
-
matrix_name (str): [
|
|
993
|
-
(row id (int), col id (int), value (float), flip (bool, default False))
|
|
994
|
-
]
|
|
995
1131
|
|
|
996
|
-
|
|
1132
|
+
Where `row_id` and `col_id are an `int` s, value is a `float` and flip is a `bool` (False by default).
|
|
997
1133
|
|
|
998
|
-
|
|
1134
|
+
* fs: is a filesystem.
|
|
1135
|
+
* metadata: are passed as kwargs to ``create_datapackage()``.
|
|
999
1136
|
|
|
1000
|
-
Returns
|
|
1137
|
+
Returns:
|
|
1138
|
+
the datapackage.
|
|
1139
|
+
|
|
1140
|
+
"""
|
|
1001
1141
|
dp = create_datapackage(fs=fs, **metadata)
|
|
1002
1142
|
for key, value in data.items():
|
|
1003
1143
|
indices_array = np.array([row[:2] for row in value], dtype=INDICES_DTYPE)
|
|
1004
1144
|
data_array = np.array([row[2] for row in value])
|
|
1005
|
-
flip_array = np.array(
|
|
1145
|
+
flip_array = np.array(
|
|
1146
|
+
[row[3] if len(row) > 3 else False for row in value], dtype=bool
|
|
1147
|
+
)
|
|
1006
1148
|
dp.add_persistent_vector(
|
|
1007
1149
|
matrix=key,
|
|
1008
1150
|
data_array=data_array,
|