generator
generator ¶
DataProductCollection
pydantic-model
¶
DataProductCollection(**kwargs: Any)
Bases: BaseModel
A collection of data products.
Use this class to serialize data products to JSON, de-serialized them from JSON, filter the products, etc.
Attributes:
| Name | Type | Description |
|---|---|---|
elements |
ProductList
|
A list of data products. |
Show JSON schema:
{
"$defs": {
"DataProduct": {
"additionalProperties": true,
"description": "Base class for data products to be generated and handled.\n\nAttributes:\n product_type (str): Product type should be the same as the class name.\n The product type is used to search for products from a [DataProductCollection][trendify.API.DataProductCollection].\n tags (Tags): Tags to be used for sorting data.\n metadata (dict[str, str]): A dictionary of metadata to be used as a tool tip for mousover in grafana",
"properties": {
"tags": {
"items": {
"anyOf": []
},
"title": "Tags",
"type": "array"
},
"metadata": {
"additionalProperties": {
"type": "string"
},
"default": {},
"title": "Metadata",
"type": "object"
}
},
"required": [
"tags"
],
"title": "DataProduct",
"type": "object"
}
},
"description": "A collection of data products.\n\nUse this class to serialize data products to JSON, de-serialized them from JSON, filter the products, etc.\n\nAttributes:\n elements (ProductList): A list of data products.",
"properties": {
"derived_from": {
"anyOf": [
{
"format": "path",
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Derived From"
},
"elements": {
"anyOf": [
{
"items": {
"$ref": "#/$defs/DataProduct"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Elements"
}
},
"title": "DataProductCollection",
"type": "object"
}
Fields:
-
derived_from(Path | None) -
elements(ProductList | None)
Source code in src/trendify/api/generator/data_product_collection.py
add_products ¶
add_products(*products: DataProduct)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
products
|
Tuple[DataProduct | ProductList, ...]
|
Products or lists of products to be appended to collection elements. |
()
|
Source code in src/trendify/api/generator/data_product_collection.py
collect_from_all_jsons
classmethod
¶
collect_from_all_jsons(
*dirs: Path, recursive: bool = False, data_products_filename: str | None = "*.json"
)
Loads all products from JSONs in the given list of directories.
If recursive is set to True, the directories will be searched recursively
(this could lead to double counting if you pass in subdirectories of a parent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dirs
|
Tuple[Path, ...]
|
Directories from which to load data product JSON files. |
()
|
recursive
|
bool
|
whether or not to search each of the provided directories recursively for data product json files. |
False
|
Returns:
| Type | Description |
|---|---|
Type[Self] | None
|
Data product collection if JSON files are found. Otherwise, returns None if no product JSON files were found. |
Source code in src/trendify/api/generator/data_product_collection.py
drop_products ¶
Removes products matching tag and/or object_type from collection elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag | None
|
Tag for which data products should be dropped |
None
|
object_type
|
Type | None
|
Type of data product to drop |
None
|
Returns:
| Type | Description |
|---|---|
DataProductCollection
|
A new collection from which matching elements have been dropped. |
Source code in src/trendify/api/generator/data_product_collection.py
from_iterable
classmethod
¶
from_iterable(*products: Tuple[ProductList, ...])
Returns a new instance containing all of the products provided in the *products argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
products
|
Tuple[ProductList, ...]
|
Lists of data products to combine into a collection |
()
|
Returns:
| Type | Description |
|---|---|
cls
|
A data product collection containing all of the provided products in the |
Source code in src/trendify/api/generator/data_product_collection.py
get_products ¶
Returns a new collection containing products matching tag and/or object_type.
Both tag and object_type default to None which matches all products.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag | None
|
Tag of data products to be kept. |
None
|
object_type
|
Type | None
|
Type of data product to keep. |
None
|
Returns:
| Type | Description |
|---|---|
DataProductCollection
|
A new collection containing matching elements. |
Source code in src/trendify/api/generator/data_product_collection.py
get_tags ¶
get_tags(data_product_type: Type[DataProduct] | None = None) -> set
Gets the tags related to a given type of DataProduct. Parent classes will match all child class types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_product_type
|
Type[DataProduct] | None
|
type for which you want to get the list of tags |
None
|
Returns:
| Type | Description |
|---|---|
set
|
set of tags applying to the given |
Source code in src/trendify/api/generator/data_product_collection.py
process_collection
classmethod
¶
process_collection(
dir_in: Path, dir_out: Path, no_tables: bool, no_xy_plots: bool, no_histograms: bool, dpi: int
)
Processes collection of elements corresponding to a single tag. This method should be called on a directory containing jsons for which the products have been sorted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_in
|
Path
|
Input directory for loading assets |
required |
dir_out
|
Path
|
Output directory for assets |
required |
no_tables
|
bool
|
Suppresses table asset creation |
required |
no_xy_plots
|
bool
|
Suppresses xy plot asset creation |
required |
no_histograms
|
bool
|
Suppresses histogram asset creation |
required |
dpi
|
int
|
Sets resolution of asset output |
required |
Source code in src/trendify/api/generator/data_product_collection.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
sort_by_tags
classmethod
¶
sort_by_tags(
dirs_in: List[Path], dir_out: Path, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT
)
Loads the data product JSON files from dirs_in sorts the products.
Sorted products are written to smaller files in a nested directory structure under dir_out.
A nested directory structure is generated according to the data tags.
Resulting product files are named according to the directory from which they were originally loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dirs_in
|
List[Path]
|
Directories from which the data product JSON files are to be loaded. |
required |
dir_out
|
Path
|
Directory to which the sorted data products will be written into a nested folder structure generated according to the data tags. |
required |
data_products_fname
|
str
|
Name of data products file |
DATA_PRODUCTS_FNAME_DEFAULT
|
Source code in src/trendify/api/generator/data_product_collection.py
sort_by_tags_single_directory
classmethod
¶
sort_by_tags_single_directory(
dir_in: Path, dir_out: Path, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT
)
Loads the data product JSON files from dir_in and sorts the products.
Sorted products are written to smaller files in a nested directory structure under dir_out.
A nested directory structure is generated according to the data tags.
Resulting product files are named according to the directory from which they were originally loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_in
|
List[Path]
|
Directories from which the data product JSON files are to be loaded. |
required |
dir_out
|
Path
|
Directory to which the sorted data products will be written into a nested folder structure generated according to the data tags. |
required |
data_products_fname
|
str
|
Name of data products file |
DATA_PRODUCTS_FNAME_DEFAULT
|
Source code in src/trendify/api/generator/data_product_collection.py
union
classmethod
¶
union(*collections: DataProductCollection)
Aggregates all of the products from multiple collections into a new larger collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collections
|
Tuple[DataProductCollection, ...]
|
Data product collections for which the products should be combined into a new collection. |
()
|
Returns:
| Type | Description |
|---|---|
Type[Self]
|
A new data product collection containing all products from
the provided |
Source code in src/trendify/api/generator/data_product_collection.py
DataProductGenerator ¶
DataProductGenerator(processor: ProductGenerator)
A wrapper for saving the data products generated by a user defined function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processor
|
ProductGenerator
|
A callable that receives a working directory and returns a list of data products. |
required |
Source code in src/trendify/api/generator/data_product_generator.py
process_and_save ¶
process_and_save(workdir: Path, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT)
Runs the user-defined processor method stored at instantiation.
Saves the returned products to a JSON file in the same directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workdir
|
Path
|
working directory on which to run the processor method. |
required |
data_products_fname
|
str
|
Name of data products file |
DATA_PRODUCTS_FNAME_DEFAULT
|
Source code in src/trendify/api/generator/data_product_generator.py
Histogrammer ¶
Class for loading data products and histogramming the [HistogramEntry][trendify.API.HistogramEntry]s
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dirs
|
List[Path]
|
Directories from which the data products are to be loaded. |
required |
out_dir
|
Path
|
Directory to which the generated histogram will be stored |
required |
dpi
|
int
|
resolution of plot |
required |
Source code in src/trendify/api/generator/histogrammer.py
handle_histogram_entries
classmethod
¶
handle_histogram_entries(
tag: Tag,
histogram_entries: List[HistogramEntry],
dir_out: Path,
dpi: int,
saf: SingleAxisFigure | None = None,
) -> SingleAxisFigure
Histograms the provided entries. Formats and saves the figure. Closes the figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag
|
Tag used to filter the loaded data products |
required |
histogram_entries
|
List[HistogramEntry]
|
A list of [ |
required |
dir_out
|
Path
|
Directory to which the generated histogram will be stored |
required |
dpi
|
int
|
resolution of plot |
required |
Source code in src/trendify/api/generator/histogrammer.py
TableBuilder ¶
Builds tables (melted, pivot, and stats) for histogramming and including in a report or Grafana dashboard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dirs
|
List[Path]
|
directories from which to load data products |
required |
out_dir
|
Path
|
directory in which tables should be saved |
required |
Source code in src/trendify/api/generator/table_builder.py
get_stats_table
classmethod
¶
get_stats_table(df: DataFrame)
Computes multiple statistics for each column
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame for which the column statistics are to be calculated. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe having statistics (column headers) for each of the columns
of the input |
Source code in src/trendify/api/generator/table_builder.py
load_table ¶
load_table(tag: Tag, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT)
Collects table entries from JSON files corresponding to given tag and processes them.
Saves CSV files for the melted data frame, pivot dataframe, and pivot dataframe stats.
File names will all use the tag with different suffixes
'tag_melted.csv', 'tag_pivot.csv', 'name_stats.csv'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag
|
product tag for which to collect and process. |
required |
Source code in src/trendify/api/generator/table_builder.py
process_table_entries
classmethod
¶
process_table_entries(tag: Tag, table_entries: List[TableEntry], out_dir: Path)
Saves CSV files for the melted data frame, pivot dataframe, and pivot dataframe stats.
File names will all use the tag with different suffixes
'tag_melted.csv', 'tag_pivot.csv', 'name_stats.csv'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag
|
product tag for which to collect and process. |
required |
table_entries
|
List[TableEntry]
|
List of table entries |
required |
out_dir
|
Path
|
Directory to which table CSV files should be saved |
required |
Source code in src/trendify/api/generator/table_builder.py
XYDataPlotter ¶
Plots xy data from user-specified directories to a single axis figure
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dirs
|
List[Path]
|
Directories in which to search for data products from JSON files |
required |
out_dir
|
Path
|
directory to which figure will be output |
required |
dpi
|
int
|
Saved image resolution |
500
|
Source code in src/trendify/api/generator/xy_data_plotter.py
handle_points_and_traces
classmethod
¶
handle_points_and_traces(
tag: Tag,
points: List[Point2D],
traces: List[Trace2D],
axlines: List[AxLine],
dir_out: Path,
dpi: int,
saf: SingleAxisFigure | None = None,
)
Plots points, traces, and axlines, formats figure, saves figure, and closes matplotlinb figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag
|
Tag corresponding to the provided points and traces |
required |
points
|
List[Point2D]
|
Points to be scattered |
required |
traces
|
List[Trace2D]
|
List of traces to be plotted |
required |
axlines
|
List[AxLine]
|
List of axis lines to be plotted |
required |
dir_out
|
Path
|
directory to output the plot |
required |
dpi
|
int
|
resolution of plot |
required |
Source code in src/trendify/api/generator/xy_data_plotter.py
plot ¶
plot(tag: Tag, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT)
- Collects data from json files in stored
self.in_dirs, - plots the relevant products,
- applies labels and formatting,
- saves the figure
- closes matplotlib figure
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Tag
|
data tag for which products are to be collected and plotted. |
required |
data_products_fname
|
str
|
Data products file name |
DATA_PRODUCTS_FNAME_DEFAULT
|
Source code in src/trendify/api/generator/xy_data_plotter.py
flatten ¶
flatten(obj: Iterable)
Recursively flattens iterable up to a point (leaves str, bytes, and DataProduct unflattened)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Iterable
|
Object to be flattened |
required |
Returns:
| Type | Description |
|---|---|
Iterable
|
Flattned iterable |