ods-tools 3.2.5__tar.gz → 3.2.7__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.
- {ods_tools-3.2.5 → ods_tools-3.2.7}/PKG-INFO +74 -1
- {ods_tools-3.2.5 → ods_tools-3.2.7}/README.md +73 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/__init__.py +1 -1
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/csv.py +24 -30
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/mapping/file.py +2 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/runner/pandas.py +53 -22
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/transformers/transform.py +15 -3
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/source.py +1 -1
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/validator.py +14 -3
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/PKG-INFO +74 -1
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/data/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/main.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/config.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/base.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/base.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/errors.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/mssql.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/postgres.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/connector/db/sqlite.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/controller.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/data/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/errors.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/files/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/files/csv.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/files/yaml.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/log.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/mapping/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/mapping/base.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/mapping/errors.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/notset.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/runner/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/runner/base.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/transformers/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/transformers/errors.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/transformers/grammar.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/transformers/transform_utils.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/validator_base.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/odtf/validator_pandas.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/__init__.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/common.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/exposure.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/forex.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/oed_schema.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools/oed/setting_schema.py +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/SOURCES.txt +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/dependency_links.txt +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/entry_points.txt +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/requires.txt +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/ods_tools.egg-info/top_level.txt +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/setup.cfg +0 -0
- {ods_tools-3.2.5 → ods_tools-3.2.7}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: ods_tools
|
|
3
|
-
Version: 3.2.
|
|
3
|
+
Version: 3.2.7
|
|
4
4
|
Summary: Tools to manage ODS files
|
|
5
5
|
Home-page: https://github.com/OasisLMF/OpenDataStandards
|
|
6
6
|
Author: Oasis LMF
|
|
@@ -32,6 +32,13 @@ ODS_Tools can be installed via pip by running the following command:
|
|
|
32
32
|
pip install ods-tools
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
If using the `transform` command, use instead
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
pip install ods-tools[extra]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
which will install some optional packages needed for it.
|
|
35
42
|
|
|
36
43
|
## command line interface
|
|
37
44
|
|
|
@@ -251,6 +258,72 @@ oed_exposure.currency_conversion = ods_tools.oed.forex.create_currency_rates(
|
|
|
251
258
|
oed_exposure.reporting_currency = 'EUR' # this line will trigger currency conversion
|
|
252
259
|
```
|
|
253
260
|
|
|
261
|
+
### Format Conversions
|
|
262
|
+
|
|
263
|
+
The `transform` command can be used to convert between OED and other formats.
|
|
264
|
+
To run transformations, and extra set of packages must be installed. This can be done using `pip install ods-tools[extra]` (or `pip install --upgrade ods-tools[extra]` if already installed).
|
|
265
|
+
|
|
266
|
+
**Basic csv conversion**
|
|
267
|
+
|
|
268
|
+
A simple csv-to-csv transformation can be run from the command line with
|
|
269
|
+
```
|
|
270
|
+
ods_tools transform -f air-oed --input-file source.csv --output-file output.csv
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
The flag -f instructs the tool on which are the origin and destination formats (currently air and oed).
|
|
274
|
+
|
|
275
|
+
**Complex conversions**
|
|
276
|
+
|
|
277
|
+
More complex transformations run with `ods_tools transform` requires a configuration file, passed with the option `--config-file`. Please see the docs [here](https://oasislmf.github.io/sections/ODTF.html)
|
|
278
|
+
|
|
279
|
+
A configuration file must contain:
|
|
280
|
+
|
|
281
|
+
- type of file to transform (location or account)
|
|
282
|
+
- input format
|
|
283
|
+
- output format
|
|
284
|
+
- batch size
|
|
285
|
+
- input file
|
|
286
|
+
- output file
|
|
287
|
+
|
|
288
|
+
For example:
|
|
289
|
+
|
|
290
|
+
```
|
|
291
|
+
transformations:
|
|
292
|
+
loc: # type of file to transform, can be "acc" or "loc"
|
|
293
|
+
input_format:
|
|
294
|
+
name: Cede_Location
|
|
295
|
+
version: 10.0.0
|
|
296
|
+
output_format:
|
|
297
|
+
name: OED_Location
|
|
298
|
+
version: 3.0.2
|
|
299
|
+
runner:
|
|
300
|
+
batch_size: 150000 # Number of rows to process in a single batch
|
|
301
|
+
extractor:
|
|
302
|
+
options:
|
|
303
|
+
path: /path/to/input.csv # Path to the input file
|
|
304
|
+
quoting: minimal
|
|
305
|
+
loader:
|
|
306
|
+
options:
|
|
307
|
+
path: /path/to/output.csv # Path to the output file
|
|
308
|
+
quoting: minimal
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
For a transformation to be run, the folder `./ods_tools/odtf/data/mappings` must contain the appropriate yaml configuration file that describes the transformations to perform on the input file to obtain the output (and, potentially, vice versa). Currently, only [mapping_loc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_loc_Cede-OED.yaml) and
|
|
313
|
+
[mapping_acc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_acc_Cede-OED.yaml) are provided; they describe the transformation between AIR Cede v10.0.0 and OED 3.0.2 for location and account files respectively.
|
|
314
|
+
|
|
315
|
+
The transformation can be run using:
|
|
316
|
+
|
|
317
|
+
```
|
|
318
|
+
ods_tools transform --config-file configuration.yaml
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Options**
|
|
322
|
+
|
|
323
|
+
The following options can be used to adjust the process:
|
|
324
|
+
|
|
325
|
+
`--nocheck` to skip the oed validation at the end of the conversion
|
|
254
326
|
|
|
327
|
+
`-v` to adjust the logging level (debug:10, info:20, warning:30, error:40, critical:50)
|
|
255
328
|
|
|
256
329
|
|
|
@@ -19,6 +19,13 @@ ODS_Tools can be installed via pip by running the following command:
|
|
|
19
19
|
pip install ods-tools
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
If using the `transform` command, use instead
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install ods-tools[extra]
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
which will install some optional packages needed for it.
|
|
22
29
|
|
|
23
30
|
## command line interface
|
|
24
31
|
|
|
@@ -238,4 +245,70 @@ oed_exposure.currency_conversion = ods_tools.oed.forex.create_currency_rates(
|
|
|
238
245
|
oed_exposure.reporting_currency = 'EUR' # this line will trigger currency conversion
|
|
239
246
|
```
|
|
240
247
|
|
|
248
|
+
### Format Conversions
|
|
249
|
+
|
|
250
|
+
The `transform` command can be used to convert between OED and other formats.
|
|
251
|
+
To run transformations, and extra set of packages must be installed. This can be done using `pip install ods-tools[extra]` (or `pip install --upgrade ods-tools[extra]` if already installed).
|
|
252
|
+
|
|
253
|
+
**Basic csv conversion**
|
|
254
|
+
|
|
255
|
+
A simple csv-to-csv transformation can be run from the command line with
|
|
256
|
+
```
|
|
257
|
+
ods_tools transform -f air-oed --input-file source.csv --output-file output.csv
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
The flag -f instructs the tool on which are the origin and destination formats (currently air and oed).
|
|
261
|
+
|
|
262
|
+
**Complex conversions**
|
|
263
|
+
|
|
264
|
+
More complex transformations run with `ods_tools transform` requires a configuration file, passed with the option `--config-file`. Please see the docs [here](https://oasislmf.github.io/sections/ODTF.html)
|
|
265
|
+
|
|
266
|
+
A configuration file must contain:
|
|
267
|
+
|
|
268
|
+
- type of file to transform (location or account)
|
|
269
|
+
- input format
|
|
270
|
+
- output format
|
|
271
|
+
- batch size
|
|
272
|
+
- input file
|
|
273
|
+
- output file
|
|
274
|
+
|
|
275
|
+
For example:
|
|
276
|
+
|
|
277
|
+
```
|
|
278
|
+
transformations:
|
|
279
|
+
loc: # type of file to transform, can be "acc" or "loc"
|
|
280
|
+
input_format:
|
|
281
|
+
name: Cede_Location
|
|
282
|
+
version: 10.0.0
|
|
283
|
+
output_format:
|
|
284
|
+
name: OED_Location
|
|
285
|
+
version: 3.0.2
|
|
286
|
+
runner:
|
|
287
|
+
batch_size: 150000 # Number of rows to process in a single batch
|
|
288
|
+
extractor:
|
|
289
|
+
options:
|
|
290
|
+
path: /path/to/input.csv # Path to the input file
|
|
291
|
+
quoting: minimal
|
|
292
|
+
loader:
|
|
293
|
+
options:
|
|
294
|
+
path: /path/to/output.csv # Path to the output file
|
|
295
|
+
quoting: minimal
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
For a transformation to be run, the folder `./ods_tools/odtf/data/mappings` must contain the appropriate yaml configuration file that describes the transformations to perform on the input file to obtain the output (and, potentially, vice versa). Currently, only [mapping_loc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_loc_Cede-OED.yaml) and
|
|
300
|
+
[mapping_acc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_acc_Cede-OED.yaml) are provided; they describe the transformation between AIR Cede v10.0.0 and OED 3.0.2 for location and account files respectively.
|
|
301
|
+
|
|
302
|
+
The transformation can be run using:
|
|
303
|
+
|
|
304
|
+
```
|
|
305
|
+
ods_tools transform --config-file configuration.yaml
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
**Options**
|
|
309
|
+
|
|
310
|
+
The following options can be used to adjust the process:
|
|
311
|
+
|
|
312
|
+
`--nocheck` to skip the oed validation at the end of the conversion
|
|
241
313
|
|
|
314
|
+
`-v` to adjust the logging level (debug:10, info:20, warning:30, error:40, critical:50)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import csv
|
|
2
1
|
from typing import Any, Dict, Iterable
|
|
3
2
|
|
|
3
|
+
import numpy as np
|
|
4
4
|
import pandas as pd
|
|
5
5
|
|
|
6
6
|
from .base import BaseConnector
|
|
@@ -63,39 +63,33 @@ class CsvConnector(BaseConnector):
|
|
|
63
63
|
|
|
64
64
|
self.file_path = config.absolute_path(options["path"])
|
|
65
65
|
self.write_header = options.get("write_header", True)
|
|
66
|
-
self.quoting = {
|
|
67
|
-
"all": csv.QUOTE_ALL,
|
|
68
|
-
"minimal": csv.QUOTE_MINIMAL,
|
|
69
|
-
"none": csv.QUOTE_NONE,
|
|
70
|
-
"nonnumeric": csv.QUOTE_NONNUMERIC,
|
|
71
|
-
}.get(options.get("quoting", "nonnumeric"))
|
|
72
66
|
|
|
73
67
|
def _data_serializer(self, row):
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
def process_value(v):
|
|
69
|
+
if v is None or isinstance(v, NotSetType):
|
|
70
|
+
return ""
|
|
71
|
+
# add quotes to values that contain special characters
|
|
72
|
+
if isinstance(v, str) and any(d in v for d in [',', ';', '\t', '\n', '"']):
|
|
73
|
+
return f'"{v}"'
|
|
74
|
+
return str(v)
|
|
75
|
+
|
|
76
|
+
return [process_value(v) for v in row.values()]
|
|
79
77
|
|
|
80
78
|
def load(self, data: Iterable[Dict[str, Any]]):
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
writer.writeheader()
|
|
96
|
-
|
|
97
|
-
writer.writerow(self._data_serializer(first_row))
|
|
98
|
-
writer.writerows(map(self._data_serializer, data))
|
|
79
|
+
first_batch = True
|
|
80
|
+
|
|
81
|
+
with open(self.file_path, "a", newline="") as f:
|
|
82
|
+
for batch in data:
|
|
83
|
+
fieldnames = list(batch.keys())
|
|
84
|
+
|
|
85
|
+
rows = np.array([self._data_serializer(batch)])
|
|
86
|
+
|
|
87
|
+
if first_batch and self.write_header:
|
|
88
|
+
header = ','.join(fieldnames)
|
|
89
|
+
np.savetxt(f, [], fmt='%s', delimiter=',', header=header, comments='')
|
|
90
|
+
first_batch = False
|
|
91
|
+
|
|
92
|
+
np.savetxt(f, rows, fmt='%s', delimiter=',', comments='')
|
|
99
93
|
|
|
100
94
|
def fetch_data(self, chunksize: int) -> Iterable[pd.DataFrame]:
|
|
101
95
|
"""
|
|
@@ -434,6 +434,8 @@ class FileMapping(BaseMapping):
|
|
|
434
434
|
|
|
435
435
|
self._raw_configs: Union[None, Dict[str, RawMappingConfig]] = None
|
|
436
436
|
self._hydrated_configs: Union[None, Dict[str, FileMappingSpec]] = None
|
|
437
|
+
if isinstance(search_paths, str):
|
|
438
|
+
search_paths = [search_paths]
|
|
437
439
|
self.search_paths = [
|
|
438
440
|
*(os.path.abspath(p) for p in (search_paths or [])),
|
|
439
441
|
os.path.abspath(standard_search_path),
|
|
@@ -413,8 +413,8 @@ class PandasRunner(BaseRunner):
|
|
|
413
413
|
self, extractor: BaseConnector, mapping: BaseMapping
|
|
414
414
|
) -> Iterable[Dict[str, Any]]:
|
|
415
415
|
"""
|
|
416
|
-
|
|
417
|
-
It receives
|
|
416
|
+
Orchestrates the data transformation.
|
|
417
|
+
It receives the data in batches from the extractor, calculates which
|
|
418
418
|
transformations can be applied to it, applies them, and yields the
|
|
419
419
|
transformed rows.
|
|
420
420
|
|
|
@@ -422,28 +422,59 @@ class PandasRunner(BaseRunner):
|
|
|
422
422
|
:param mapping: The mapping object defining transformations.
|
|
423
423
|
:return: An iterable of dictionaries, each representing a transformed row.
|
|
424
424
|
"""
|
|
425
|
-
|
|
425
|
+
# Initial search paths
|
|
426
|
+
search_paths = [os.path.dirname(self.config.path)] if self.config.path else []
|
|
427
|
+
|
|
428
|
+
# Add search paths from the config, if provided
|
|
429
|
+
config_search_paths = self.config.get('mapping', {}).get('options', {}).get('search_paths', [])
|
|
430
|
+
if isinstance(config_search_paths, str):
|
|
431
|
+
config_search_paths = [config_search_paths]
|
|
432
|
+
search_paths.extend(config_search_paths)
|
|
433
|
+
|
|
434
|
+
# Instantiate the validator with the updated search paths
|
|
435
|
+
validator = PandasValidator(search_paths=search_paths)
|
|
426
436
|
runner_config = self.config.config.get('runner', None)
|
|
427
437
|
batch_size = runner_config.get('batch_size', 100000)
|
|
428
438
|
total_rows = 0
|
|
429
439
|
transformations = []
|
|
430
440
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
441
|
+
try:
|
|
442
|
+
for batch in extractor.fetch_data(batch_size):
|
|
443
|
+
# Calculates the set of transformations from the columns in the
|
|
444
|
+
# first batch (to avoid double-querying)
|
|
445
|
+
if not transformations:
|
|
446
|
+
available_columns = set(batch.columns)
|
|
447
|
+
transformations = mapping.get_transformations(available_columns=available_columns)
|
|
448
|
+
logger.info(
|
|
449
|
+
f"Running transformation set {transformations[0].input_format} -> {transformations[-1].output_format} [{extractor.name}]")
|
|
450
|
+
|
|
451
|
+
# Validate input data
|
|
452
|
+
try:
|
|
453
|
+
validator.run(self.coerce_row_types(batch, transformations[0].types),
|
|
454
|
+
mapping.input_format.name, mapping.input_format.version, mapping.file_type)
|
|
455
|
+
except KeyError as e:
|
|
456
|
+
logger.warning(f"Validation failed due to a missing column: {e}")
|
|
457
|
+
except Exception as e:
|
|
458
|
+
logger.warning(f"Validation failed: {e}")
|
|
459
|
+
|
|
460
|
+
# Apply the transformations to the batch
|
|
461
|
+
for transformation in transformations:
|
|
462
|
+
batch = self.apply_transformation_set(batch, transformation)
|
|
463
|
+
|
|
464
|
+
# Validate output data
|
|
465
|
+
try:
|
|
466
|
+
validator.run(batch, mapping.output_format.name, mapping.output_format.version, mapping.file_type)
|
|
467
|
+
except KeyError as e:
|
|
468
|
+
logger.warning(f"Validation failed due to a missing column: {e}")
|
|
469
|
+
except Exception as e:
|
|
470
|
+
logger.warning(f"Validation failed: {e}")
|
|
471
|
+
|
|
472
|
+
# Log the transformation progress
|
|
473
|
+
total_rows += len(batch)
|
|
474
|
+
logger.info(f"Processed {len(batch)} rows in the current batch (total: {total_rows})")
|
|
475
|
+
|
|
476
|
+
yield from (r.to_dict() for idx, r in batch.iterrows())
|
|
477
|
+
except FileNotFoundError:
|
|
478
|
+
logger.error(f"File not found: {extractor.file_path}")
|
|
479
|
+
except Exception as e:
|
|
480
|
+
logger.error(f"Error processing batch: {e}")
|
|
@@ -6,7 +6,7 @@ from operator import truediv as div
|
|
|
6
6
|
from typing import Any, Callable, Iterable, List, Pattern, TypedDict, Union
|
|
7
7
|
|
|
8
8
|
from lark import Transformer as _LarkTransformer
|
|
9
|
-
from lark import Tree
|
|
9
|
+
from lark import Token, Tree
|
|
10
10
|
from lark import exceptions as lark_exceptions
|
|
11
11
|
from lark import v_args
|
|
12
12
|
from ..transformers.transform_utils import replace_multiple
|
|
@@ -452,7 +452,9 @@ class BaseTreeTransformer(_LarkTransformer):
|
|
|
452
452
|
|
|
453
453
|
:return: True if the value is "True", False otherwise
|
|
454
454
|
"""
|
|
455
|
-
|
|
455
|
+
if isinstance(value, bool):
|
|
456
|
+
return value
|
|
457
|
+
return str(value).lower() == "true"
|
|
456
458
|
|
|
457
459
|
def null(self, value):
|
|
458
460
|
"""
|
|
@@ -479,6 +481,16 @@ class BaseTreeTransformer(_LarkTransformer):
|
|
|
479
481
|
return float(value)
|
|
480
482
|
|
|
481
483
|
|
|
484
|
+
def safe_lookup(r, name):
|
|
485
|
+
if isinstance(name, Token):
|
|
486
|
+
name = name.value
|
|
487
|
+
if name == 'True':
|
|
488
|
+
return True
|
|
489
|
+
elif name == 'False':
|
|
490
|
+
return False
|
|
491
|
+
return r.get(name, name)
|
|
492
|
+
|
|
493
|
+
|
|
482
494
|
def create_transformer_class(row, transformer_mapping):
|
|
483
495
|
"""
|
|
484
496
|
Creates a transformer class from the provided mapping overrides.
|
|
@@ -489,7 +501,7 @@ def create_transformer_class(row, transformer_mapping):
|
|
|
489
501
|
:return: The new transformer class
|
|
490
502
|
"""
|
|
491
503
|
transformer_mapping = {
|
|
492
|
-
"lookup":
|
|
504
|
+
"lookup": safe_lookup,
|
|
493
505
|
"add": lambda r, lhs, rhs: add(lhs, rhs),
|
|
494
506
|
"subtract": lambda r, lhs, rhs: sub(lhs, rhs),
|
|
495
507
|
"multiply": lambda r, lhs, rhs: mul(lhs, rhs),
|
|
@@ -309,7 +309,7 @@ class OedSource:
|
|
|
309
309
|
for field_info in ods_fields.values():
|
|
310
310
|
col = field_info['Input Field Name']
|
|
311
311
|
if col not in present_field:
|
|
312
|
-
if field_info.get('Required Field') == 'R' and field_info.get("Allow blanks?").upper() == "YES":
|
|
312
|
+
if field_info.get('Required Field') == 'R' and field_info.get("Allow blanks?", '').upper() == "YES":
|
|
313
313
|
if field_info['pd_dtype'] == 'category':
|
|
314
314
|
df[col] = '' if field_info['Default'] == 'n/a' else field_info['Default']
|
|
315
315
|
df[col] = df[col].astype('category')
|
|
@@ -126,7 +126,9 @@ class Validator:
|
|
|
126
126
|
|
|
127
127
|
for field_info in input_fields.values():
|
|
128
128
|
if field_info['Input Field Name'] not in field_to_columns:
|
|
129
|
-
|
|
129
|
+
# OED v4 = 'Property field status' and OED v3 = 'Required Field'
|
|
130
|
+
requ_field_ref = 'Property field status' if 'Property field status' in field_info else 'Required Field'
|
|
131
|
+
if field_info.get(requ_field_ref) == 'R':
|
|
130
132
|
invalid_data.append({'name': oed_source.oed_name, 'source': oed_source.current_source,
|
|
131
133
|
'msg': f"missing required column {field_info['Input Field Name']}"})
|
|
132
134
|
continue
|
|
@@ -134,7 +136,11 @@ class Validator:
|
|
|
134
136
|
if isinstance(columns, str):
|
|
135
137
|
columns = [columns]
|
|
136
138
|
for column in columns:
|
|
137
|
-
|
|
139
|
+
blanks_not_allowed = any([
|
|
140
|
+
field_info.get("Allow blanks?", '').upper() == 'NO', # OED v3
|
|
141
|
+
field_info.get('Property field status', '').upper() == 'R' # OED v4
|
|
142
|
+
])
|
|
143
|
+
if blanks_not_allowed:
|
|
138
144
|
missing_value_df = oed_source.dataframe[is_empty(oed_source.dataframe, column)]
|
|
139
145
|
if not missing_value_df.empty:
|
|
140
146
|
invalid_data.append({'name': oed_source.oed_name, 'source': oed_source.current_source,
|
|
@@ -170,10 +176,15 @@ class Validator:
|
|
|
170
176
|
identifier_field = self.identifier_field_maps[oed_source]
|
|
171
177
|
for column, field_info in column_to_field.items():
|
|
172
178
|
valid_ranges = field_info['Valid value range']
|
|
179
|
+
blanks_allowed = any([
|
|
180
|
+
field_info.get('Allow blanks?', '').lower() == 'yes', # OED v3
|
|
181
|
+
field_info.get('Property field status', '').upper() != 'R', # OED v4
|
|
182
|
+
])
|
|
183
|
+
|
|
173
184
|
if valid_ranges != 'n/a':
|
|
174
185
|
is_valid_value = functools.partial(OedSchema.is_valid_value,
|
|
175
186
|
valid_ranges=valid_ranges,
|
|
176
|
-
allow_blanks=
|
|
187
|
+
allow_blanks=blanks_allowed)
|
|
177
188
|
invalid_range_data = oed_source.dataframe[~oed_source.dataframe[column].apply(is_valid_value)]
|
|
178
189
|
if not invalid_range_data.empty:
|
|
179
190
|
invalid_data.append({'name': oed_source.oed_name, 'source': oed_source.current_source,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: ods-tools
|
|
3
|
-
Version: 3.2.
|
|
3
|
+
Version: 3.2.7
|
|
4
4
|
Summary: Tools to manage ODS files
|
|
5
5
|
Home-page: https://github.com/OasisLMF/OpenDataStandards
|
|
6
6
|
Author: Oasis LMF
|
|
@@ -32,6 +32,13 @@ ODS_Tools can be installed via pip by running the following command:
|
|
|
32
32
|
pip install ods-tools
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
If using the `transform` command, use instead
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
pip install ods-tools[extra]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
which will install some optional packages needed for it.
|
|
35
42
|
|
|
36
43
|
## command line interface
|
|
37
44
|
|
|
@@ -251,6 +258,72 @@ oed_exposure.currency_conversion = ods_tools.oed.forex.create_currency_rates(
|
|
|
251
258
|
oed_exposure.reporting_currency = 'EUR' # this line will trigger currency conversion
|
|
252
259
|
```
|
|
253
260
|
|
|
261
|
+
### Format Conversions
|
|
262
|
+
|
|
263
|
+
The `transform` command can be used to convert between OED and other formats.
|
|
264
|
+
To run transformations, and extra set of packages must be installed. This can be done using `pip install ods-tools[extra]` (or `pip install --upgrade ods-tools[extra]` if already installed).
|
|
265
|
+
|
|
266
|
+
**Basic csv conversion**
|
|
267
|
+
|
|
268
|
+
A simple csv-to-csv transformation can be run from the command line with
|
|
269
|
+
```
|
|
270
|
+
ods_tools transform -f air-oed --input-file source.csv --output-file output.csv
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
The flag -f instructs the tool on which are the origin and destination formats (currently air and oed).
|
|
274
|
+
|
|
275
|
+
**Complex conversions**
|
|
276
|
+
|
|
277
|
+
More complex transformations run with `ods_tools transform` requires a configuration file, passed with the option `--config-file`. Please see the docs [here](https://oasislmf.github.io/sections/ODTF.html)
|
|
278
|
+
|
|
279
|
+
A configuration file must contain:
|
|
280
|
+
|
|
281
|
+
- type of file to transform (location or account)
|
|
282
|
+
- input format
|
|
283
|
+
- output format
|
|
284
|
+
- batch size
|
|
285
|
+
- input file
|
|
286
|
+
- output file
|
|
287
|
+
|
|
288
|
+
For example:
|
|
289
|
+
|
|
290
|
+
```
|
|
291
|
+
transformations:
|
|
292
|
+
loc: # type of file to transform, can be "acc" or "loc"
|
|
293
|
+
input_format:
|
|
294
|
+
name: Cede_Location
|
|
295
|
+
version: 10.0.0
|
|
296
|
+
output_format:
|
|
297
|
+
name: OED_Location
|
|
298
|
+
version: 3.0.2
|
|
299
|
+
runner:
|
|
300
|
+
batch_size: 150000 # Number of rows to process in a single batch
|
|
301
|
+
extractor:
|
|
302
|
+
options:
|
|
303
|
+
path: /path/to/input.csv # Path to the input file
|
|
304
|
+
quoting: minimal
|
|
305
|
+
loader:
|
|
306
|
+
options:
|
|
307
|
+
path: /path/to/output.csv # Path to the output file
|
|
308
|
+
quoting: minimal
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
For a transformation to be run, the folder `./ods_tools/odtf/data/mappings` must contain the appropriate yaml configuration file that describes the transformations to perform on the input file to obtain the output (and, potentially, vice versa). Currently, only [mapping_loc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_loc_Cede-OED.yaml) and
|
|
313
|
+
[mapping_acc_Cede-OED.yaml](./ods_tools/odtf/data/mappings/mapping_acc_Cede-OED.yaml) are provided; they describe the transformation between AIR Cede v10.0.0 and OED 3.0.2 for location and account files respectively.
|
|
314
|
+
|
|
315
|
+
The transformation can be run using:
|
|
316
|
+
|
|
317
|
+
```
|
|
318
|
+
ods_tools transform --config-file configuration.yaml
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Options**
|
|
322
|
+
|
|
323
|
+
The following options can be used to adjust the process:
|
|
324
|
+
|
|
325
|
+
`--nocheck` to skip the oed validation at the end of the conversion
|
|
254
326
|
|
|
327
|
+
`-v` to adjust the logging level (debug:10, info:20, warning:30, error:40, critical:50)
|
|
255
328
|
|
|
256
329
|
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|