Data Loaders¶
See Data Loaders for how to write one.
ClassificationLoader¶
rac4os.dataloaders.BaseDataLoader.ClassificationLoader
¶
Bases: ABC
Template: subclasses provide read_raw() + to_records(); base handles the rest.
Source code in rac4os/dataloaders/BaseDataLoader.py
__init__
¶
Stores the source path and checks that it exists.
Failing here rather than at to_records means a wrong path is reported before any
parsing work or model call happens.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if the path does not exist. |
Source code in rac4os/dataloaders/BaseDataLoader.py
to_records
abstractmethod
¶
load
¶
Parse the source and return it as Code records.
Calls the subclass's to_records and converts each dict with Code.from_dict, which
drops keys Code does not define and leaves missing ones at their defaults.
Returns:
| Type | Description |
|---|---|
list[Code]
|
list[Code]: one record per category in the source. |
Source code in rac4os/dataloaders/BaseDataLoader.py
Registry¶
rac4os.dataloaders.registry
¶
register
¶
Class decorator that registers a ClassificationLoader implementation under a name.
The name is what a dataset refers to in mapping through its loader_name key, and
what get_loader resolves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
key to register the decorated class under. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
the decorator, which registers the class and returns it unchanged. |
Source code in rac4os/dataloaders/registry.py
get_loader
¶
Build a registered loader for a source path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
a registered loader name, e.g. "JSON", "COICOP" or one of your own. |
required |
path
|
str | Path
|
the file the loader should read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ClassificationLoader |
ClassificationLoader
|
the loader, constructed and ready for |
Raises:
| Type | Description |
|---|---|
ValueError
|
if no loader is registered under that name. |
Source code in rac4os/dataloaders/registry.py
Bundled loaders¶
rac4os.dataloaders.loaders.PredefinedJsonLoader
¶
PredefinedJsonLoader
¶
Bases: ClassificationLoader
Loader for a classification that is already in Code shape, registered under "JSON".
This is the loader to use for your own taxonomy once you have converted it once, and
the one that reads back what ClassificationsSystemAugmenter writes: both are a flat
JSON list of Code-shaped objects, so there is no parsing left to do.
Source code in rac4os/dataloaders/loaders/PredefinedJsonLoader.py
to_records
¶
Read the JSON file and return its contents unchanged.
The file must hold a list of Code-shaped objects. Nothing is validated here; keys
that Code does not define are dropped later, by Code.from_dict.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: the records as they stand in the file. |
Source code in rac4os/dataloaders/loaders/PredefinedJsonLoader.py
rac4os.dataloaders.loaders.KlassServerDataLoader
¶
KlassServerDataloader
¶
Bases: ClassificationLoader
Loader for the XML exports of the German Klassifikationsserver, registered under "KLASS_SERVER".
One loader covers every classification published there: the export format is the same
and only the file differs, so pointing it at another Klassifikationsserver export is
all it takes to load one. The explanatory
notes are parsed out of the nested detail elements into details, and empty detail
dicts are removed so that a category without notes does not carry an empty field into
the prompt.
Source code in rac4os/dataloaders/loaders/KlassServerDataLoader.py
__get_detail_dict
¶
Constructs a dictionary for all the relevant sea categories with the given class names from the xml file. Parameters: item:xml.etree.ElementTree.Element Returns: dict
Source code in rac4os/dataloaders/loaders/KlassServerDataLoader.py
__parse_xml
¶
Parses the sea-documentation xml file and extracts all the relevant information. Parameters: path:str Returns: list
Source code in rac4os/dataloaders/loaders/KlassServerDataLoader.py
__clean_dict
¶
Cleans the dictionary of empty values. Parameters: all_labels:list Returns: list
Source code in rac4os/dataloaders/loaders/KlassServerDataLoader.py
to_records
¶
Parse the XML export and return Code-shaped dicts.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one dict per category, with the classification's explanatory notes under |
Source code in rac4os/dataloaders/loaders/KlassServerDataLoader.py
rac4os.dataloaders.loaders.CoicopDataLoader
¶
CoicopDataLoader
¶
Bases: ClassificationLoader
Loader for the official COICOP 2018 structure as published in xlsx, registered under "COICOP".
The sheet's three note columns (includes, alsoIncludes, excludes) are folded into
the details dict rather than dropped, because the exclusion notes are what settle most
of the hard cases between neighbouring categories. The level is derived from the code
itself, by counting its dot-separated groups.
Source code in rac4os/dataloaders/loaders/CoicopDataLoader.py
to_records
¶
Read the xlsx and return Code-shaped dicts.
Non-breaking spaces and the _x000D_ carriage returns that the published file contains
are replaced with ordinary spaces first; left in, they end up in the embedded text and
in the prompt.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one dict per COICOP category, with |
Source code in rac4os/dataloaders/loaders/CoicopDataLoader.py
rac4os.dataloaders.loaders.ICATUSLoader
¶
ICATUSLaoder
¶
Bases: ClassificationLoader
Loader for the UNSD ICATUS 2016 time-use statistics csv, registered under "ICATUS".
The source spreads the code over three columns, one per level, with the deeper ones empty for higher categories. The loader collapses them into a single code and derives the level from its length.
Source code in rac4os/dataloaders/loaders/ICATUSLoader.py
to_records
¶
Read the csv and return Code-shaped dicts.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one dict per ICATUS category, with |