catalog
Catalog
Catalog(
filename: str | PathLike,
key_column_name: str,
columns_types: dict,
rename_dict: dict,
units_dict: dict,
catalog_type: CatalogType = 'default_catalog',
remove_duplicates: bool = True,
user_filepath: str | PathLike = DATA_BASE_PATH,
separator: str = ',',
decimal: str = '.',
)
Generic wrapper for tabular data read from a csv file, indexed by a key column.
For now, we only support csv input files located in the data/ folder of the source code.
Please note that the responsibility of ensuring the "uniqueness" of the key column is left
to the user: no integrity check is performed on input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | PathLike
|
filename of the csv data source |
required |
|
str
|
name of the column used as key (i.e. row identifier) |
required |
|
Literal['default_catalog', 'cable_catalog']
|
type of the catalog. Used in the |
'default_catalog'
|
|
dict | None
|
dictionary of column names and their types. |
required |
|
dict | None
|
dictionary of column names to rename. The key is the original name, the value is the new name. |
required |
|
bool
|
whether to remove duplicate rows. Defaults to True. |
True
|
|
str | PathLike
|
path to the folder containing the csv file. Defaults to internal data. |
DATA_BASE_PATH
|
Source code in src/mechaphlowers/data/catalog/catalog.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
check_wrong_rows
check_wrong_rows(clean_catalog: bool = True) -> Set
Check if rows are causes pandera SchemaErrors when running get_as_object(), and eventually remove them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
bool
|
If True, removes invalid rows from the catalog. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Set |
Set
|
Set of indices of rows that are invalid according to the pandera schema. |
Source code in src/mechaphlowers/data/catalog/catalog.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
get
Get rows from a list of keys.
If a key is present several times in the keys argument, the returned dataframe
will contain the corresponding row as many times as requested.
If any of the requested keys were to match several rows, all matching rows would
be returned.
Raises:
| Type | Description |
|---|---|
KeyError
|
if any of the requested |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list
|
list of keys |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: requested rows |
Source code in src/mechaphlowers/data/catalog/catalog.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
get_as_object
Get rows from a list of keys.
If a key is present several times in the keys argument, the returned dataframe
will contain the corresponding row as many times as requested.
If any of the requested keys were to match several rows, all matching rows would
be returned.
The type of the object returned depends on catalog_type. The mapping between catalog_type and object type is made by dictionary catalog_to_object
Raises:
| Type | Description |
|---|---|
KeyError
|
if any of the requested |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list
|
list of keys |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
Any
|
requested object, that depends on |
Source code in src/mechaphlowers/data/catalog/catalog.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
keys
keys() -> list
Get the keys available in the catalog
Source code in src/mechaphlowers/data/catalog/catalog.py
251 252 253 | |
remove_duplicates
Remove duplicate rows, and warn if any duplicates found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | PathLike
|
filename of the csv data source (used only for logging a warning) |
required |
Source code in src/mechaphlowers/data/catalog/catalog.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
rename_columns
rename_columns(
key_column_name: str, rename_dict: dict
) -> None
Rename the columns and the index of the catalog
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
name of the key index |
required |
|
dict
|
dictionary of all column names that need to be renamed. This can include the key index. |
required |
Source code in src/mechaphlowers/data/catalog/catalog.py
128 129 130 131 132 133 134 135 136 137 138 | |
validate_types
validate_types(dtype_dict: dict) -> None
Validate the types of the dataframe. Boolean columns are not checked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
dictionary of column names and their types, without the key index and the boolean columns. |
required |
Source code in src/mechaphlowers/data/catalog/catalog.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
build_catalog_from_yaml
build_catalog_from_yaml(
yaml_filename: str | PathLike,
rename=True,
remove_duplicates=True,
user_filepath: str | PathLike = DATA_BASE_PATH,
separator: str = ',',
decimal: str = '.',
) -> Catalog
Build a catalog from a yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | PathLike
|
path to the yaml file |
required |
|
bool
|
whether to rename columns according to the yaml file. Defaults to True. |
True
|
|
bool
|
whether to remove duplicate rows. Defaults to True. |
True
|
|
str | PathLike
|
path to the folder containing the yaml file. Defaults to internal data. |
DATA_BASE_PATH
|
Returns:
| Name | Type | Description |
|---|---|---|
Catalog |
Catalog
|
a catalog instance with the data from the yaml file |
Source code in src/mechaphlowers/data/catalog/catalog.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
write_yaml_catalog_template
write_yaml_catalog_template(
user_filepath: str | PathLike,
template: Literal[
'cable_catalog', 'support_catalog'
] = 'support_catalog',
) -> None
Write a yaml catalog template file in the user_filepath folder provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | PathLike
|
path to the folder where the yaml file will be written. Defaults to internal data. |
required |
|
Literal['cable_catalog', 'support_catalog']
|
type of the catalog template. Defaults to "support_catalog". |
'support_catalog'
|
Source code in src/mechaphlowers/data/catalog/catalog.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |