Utilities API¶
Logging, unit handling, and data utility functions.
Logging¶
hera.utils.logging
¶
Unit Handler¶
hera.utils.unitHandler
¶
Unit handling module for Hera.
Pint is the preferred unit system. Use ureg for all new code.
Unum is supported as a fallback for backward compatibility with older code.
Usage::
from hera.utils.unitHandler import ureg, Quantity
speed = 5.0 * ureg.m / ureg.s
area = 10 * ureg.dunam # custom: 1 dunam = 1000 m²
tonumber(x, theunit)
¶
Convert a value with units to a plain number in the given unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Unum, Quantity, or numeric
|
The value to convert. |
required |
theunit
|
unit
|
The target unit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Source code in hera/utils/unitHandler.py
tounit(x, theunit)
¶
Convert a value to the given unit, handling both pint and unum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Unum, Quantity, or numeric
|
The value to convert. |
required |
theunit
|
Unit, Unum, or str
|
The target unit. |
required |
Returns:
| Type | Description |
|---|---|
Quantity or Unum
|
|
Source code in hera/utils/unitHandler.py
convert_unum_units_to_eval_str(unit_str)
¶
Convert a unum-style unit string to an eval-safe expression.
Source code in hera/utils/unitHandler.py
unumToStr(obj)
¶
Convert a Unum object to a string representation.
Source code in hera/utils/unitHandler.py
strToUnum(value)
¶
Convert a string to a Unum object.
Source code in hera/utils/unitHandler.py
extractUnumUnitsFromPint(pint_quantity)
¶
Extract unum unit equivalent from a pint Quantity.
Source code in hera/utils/unitHandler.py
pintToUnum(pint_quantity)
¶
Convert a pint Quantity to a Unum object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pint_quantity
|
Quantity
|
The pint value to convert. |
required |
Returns:
| Type | Description |
|---|---|
Unum
|
|
Source code in hera/utils/unitHandler.py
unumToPint(unum_obj, value=1.0)
¶
Convert a Unum object to a pint Quantity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unum_obj
|
Unum
|
The unum value to convert. |
required |
value
|
float
|
Optional numerical value. |
1.0
|
Returns:
| Type | Description |
|---|---|
Quantity
|
|
Source code in hera/utils/unitHandler.py
unumToBaseUnits(unum_obj)
¶
JSON Utilities¶
hera.utils.jsonutils
¶
Unum
¶
JSONvariationItem
¶
An iterator that creates the variations of a single parameter block.
Each group is a list of parameters. { p1 : list of values p2 : list of values
}
where p1, p2 are json paths. (If $. is not specified in the path, add them).
Source code in hera/utils/jsonutils.py
__init__(base, variationItem, convetToBaseUnits=True)
¶
The base is the json that will be changes.
variationItem is a map of json paths -> values. All the paths change together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
|
required | |
variationItem
|
|
required | |
convetToBaseUnits
|
If true, and the item is pint.Quantity, then convety to base (mks) units. |
True
|
Source code in hera/utils/jsonutils.py
__iter__()
¶
__next__()
¶
Return the next variation mapping or raise StopIteration.
Source code in hera/utils/jsonutils.py
get_logger(instance, name=None)
¶
Return a class logger for the instance, or a named logger if name is given.
tonumber(x, theunit)
¶
Convert a value with units to a plain number in the given unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Unum, Quantity, or numeric
|
The value to convert. |
required |
theunit
|
unit
|
The target unit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Source code in hera/utils/unitHandler.py
tounit(x, theunit)
¶
Convert a value to the given unit, handling both pint and unum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Unum, Quantity, or numeric
|
The value to convert. |
required |
theunit
|
Unit, Unum, or str
|
The target unit. |
required |
Returns:
| Type | Description |
|---|---|
Quantity or Unum
|
|
Source code in hera/utils/unitHandler.py
convert_unum_units_to_eval_str(unit_str)
¶
Convert a unum-style unit string to an eval-safe expression.
Source code in hera/utils/unitHandler.py
unumToStr(obj)
¶
Convert a Unum object to a string representation.
Source code in hera/utils/unitHandler.py
strToUnum(value)
¶
Convert a string to a Unum object.
Source code in hera/utils/unitHandler.py
extractUnumUnitsFromPint(pint_quantity)
¶
Extract unum unit equivalent from a pint Quantity.
Source code in hera/utils/unitHandler.py
pintToUnum(pint_quantity)
¶
Convert a pint Quantity to a Unum object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pint_quantity
|
Quantity
|
The pint value to convert. |
required |
Returns:
| Type | Description |
|---|---|
Unum
|
|
Source code in hera/utils/unitHandler.py
unumToPint(unum_obj, value=1.0)
¶
Convert a Unum object to a pint Quantity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unum_obj
|
Unum
|
The unum value to convert. |
required |
value
|
float
|
Optional numerical value. |
1.0
|
Returns:
| Type | Description |
|---|---|
Quantity
|
|
Source code in hera/utils/unitHandler.py
unumToBaseUnits(unum_obj)
¶
compareJSONS(longFormat=False, changeDotToUnderscore=False, **kwargs)
¶
Recieves {
Returns:
| Type | Description |
|---|---|
DataFrame: df of the comparison results
|
|
Source code in hera/utils/jsonutils.py
ConfigurationToJSON(valueToProcess, standardize=False, splitUnits=False, keepOriginalUnits=True)
¶
Converts a configuration dict (that might include unum objects) to JSON dict (where all the values are strings). The unum objects are converted to Str in a way that allows for their retrieval. (see the JSONToConfiguration function)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conf
|
dict
|
A key-value dict where the values are string. Converts unum objects to string like representations: 1m/s --> '1m/s'. |
required |
standardize
|
bool
|
If true, converts the units to MKS. |
False
|
splitUnits
|
bool
|
If true splits the unum object to number and units list.
The units are [MKS units,original units].
The units are saved in field The JSONToConfiguration locates the |
False
|
keepOriginalUnits
|
bool
|
If true, and the splitUnits is true then saved in |
True
|
Returns:
| Type | Description |
|---|---|
dict with all the values as string
|
|
Source code in hera/utils/jsonutils.py
JSONToConfiguration(valueToProcess, returnUnum=False, returnStandardize=False)
¶
Converts a dictionary (all the values are string) to a JSON where all the values are string. Convert the JSON back to configuration object using the ConfigurationToJSON function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
JSON
|
dict
|
A key-value where all the values are strings.
The unum objects has the format '1 |
required |
returnUnum
|
bool [Default True]
|
If true convert a quantity to Unum. |
False
|
returnStandardize
|
If true, return the units in MKS. If False return the original units. |
False
|
Returns:
| Type | Description |
|---|---|
A dict with the values convected to unum.
|
|
Source code in hera/utils/jsonutils.py
stripConfigurationUnits(valueToProcess, returnStandardize=False, ignoreStandardization=[])
¶
Converts a dictionary to a JSON where all the values with Unum or Pint units get converted to their magnitude
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
JSON
|
dict
|
The JSON to strip the units from |
required |
returnStandardize
|
If true, return the units in MKS. If False return the original units. |
False
|
Returns:
| Type | Description |
|---|---|
Same json with all units stripped leaving just magnitudes
|
|
Source code in hera/utils/jsonutils.py
loadJSON(jsonData)
¶
Reads the json object to the memory. Could be: * file object: any file-like object with the property 'read'. * str: either the JSON or a path to the directory. * dict: the JSON object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jsonData
|
str, object file, path to disk, dict
|
The object that contains the dict. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The loaded JSON. |
Source code in hera/utils/jsonutils.py
processJSONToPandas(jsonData, nameColumn='parameterName', valueColumn='value')
¶
Trnasforms a JSON to pandas, flattens a list items and names them according to their order
The example the JSON :
{
"nodes": {
"a" : {
"x" : 1,
"y" : 2,
"z" : 3
},
"b" : {
"r" : 1,
"t" : 2,
"y" : [3,2,4,5,6]
}
}
}
will be converted to
parameterName value
parameterName value
0 nodes.a.x 1 1 nodes.a.y 2 2 nodes.a.z 3 3 nodes.b.r 1 4 nodes.b.t 2 5 nodes.b.y_0 3 6 nodes.b.y_1 2 7 nodes.b.y_2 4 8 nodes.b.y_3 5 9 nodes.b.y_4 6
Notes:
- Currently does not support JSON whose root is a list.
[
{ "a" : 1 },
{ "b" : 2}
]
It will be supported if needed in the future.
Parameters
Parameters
jsonData : dict
the JSON data (a dict)
nameColumn: str
The name of the parameter column name
valueColumn : str
The name of the value
Source code in hera/utils/jsonutils.py
254 255 256 257 258 259 260 261 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
convertJSONtoPandas(jsonData, nameColumn='parameterNameFullPath', valueColumn='value')
¶
converts a JSON (either in file or loaded, or json str) to pandas. The pandas flattens the JSON using the json path convection. e.g { "a" : { "b" : 1, "c" : [1,2,3] } } will be converted to a.b 1 a.c_0 1 a.c_1 2 a.c_2 3 Does not support (currently) JSON whose root is a list but only supports dict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jsonData
|
(str, dict)
|
A json data either a file name, a json dict string, or a dict. |
required |
nameColumn
|
The name of the parameter column name |
'parameterNameFullPath'
|
|
valueColumn
|
str
|
The name of the value |
'value'
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame
|
with the fields nameColumn (the path of the json) and valueColumn |
Source code in hera/utils/jsonutils.py
setJSONPath(base, valuesDict, inPlace=False)
¶
Sets the variables in the values dict to the base.
That is the base is a regular JSON file:
{
"a" : {
"b" : 1
},
"c" : {
"d" : 2
}
}
And the values dict is a path-> value.
{
"a.b" : 2
}
so it will return
{
"a" : {
"b" : 2
},
"c" : {
"d" : 2
}
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
|
required | |
valuesDict
|
|
required | |
inPlace
|
bool
|
If false create a copy, else change the input. |
False
|
Source code in hera/utils/jsonutils.py
JSONVariations(base, variationJSON, convetToBaseUnits=True)
¶
The JSONVariations creates variations of the cartesian product of all the values between the variation groups. Parameters within the variation group change together. Hence, all the members of one variation group must an identical number of values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
base json to apply variations to
|
|
required |
variationJSON
|
list of variation groups
|
|
required |
Source code in hera/utils/jsonutils.py
Data Utilities¶
hera.utils.data.toolkit.dataToolkit
¶
Bases: abstractToolkit
Toolkit for managing data repositories (replacing the old hera-data).
It is initialized only with the DEFAULT project.
The structure of a datasource file is:
{
"<toolkit name>": {
"<datasource name>": {
"resource": "<location of datasource>",
"dataFormat": "<type of data source>",
"desc": {
... metadata ...
}
},
...
},
...
}
Source code in hera/utils/data/toolkit.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 188 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 224 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 250 251 252 253 254 255 256 257 258 259 260 261 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 331 332 333 334 335 336 337 338 339 340 341 342 343 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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 | |
__init__(connectionName=None)
¶
Initialize the dataToolkit on the default project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connectionName
|
str
|
The DB connection name. If None, uses the current OS username. |
None
|
Source code in hera/utils/data/toolkit.py
addRepository(repositoryName, repositoryPath, overwrite=False)
¶
Register a repository JSON file as a data source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repositoryName
|
str
|
The name to register the repository under. |
required |
repositoryPath
|
str
|
Path to the repository JSON file. |
required |
overwrite
|
bool
|
If True, overwrite an existing repository with the same name. |
False
|
Source code in hera/utils/data/toolkit.py
getRepositoryTable()
¶
Return a DataFrame listing all registered repositories.
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
getRepository(repositoryName)
¶
Load and return a repository's JSON content by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repositoryName
|
str
|
The name of the registered repository. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The parsed repository JSON. |
Source code in hera/utils/data/toolkit.py
loadAllDatasourcesInAllRepositoriesToProject(projectName, overwrite=False)
¶
Load all data sources from all registered repositories into a project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectName
|
str
|
The target project name. |
required |
overwrite
|
bool
|
If True, overwrite existing data sources. |
False
|
Source code in hera/utils/data/toolkit.py
loadAllDatasourcesInRepositoryToProject(projectName, repositoryName, overwrite=False)
¶
Load all data sources from a specific repository into a project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
projectName
|
str
|
The target project name. |
required |
repositoryName
|
str
|
The name of the registered repository to load from. |
required |
overwrite
|
bool
|
If True, overwrite existing data sources. |
False
|
Source code in hera/utils/data/toolkit.py
getToolkitDocument(toolkit_name: str)
¶
Find a dynamic toolkit document by name (either desc.datasourceName or desc.toolkit). Returns the mongoengine document or None.
Source code in hera/utils/data/toolkit.py
loadAllDatasourcesInRepositoryJSONToProject(projectName: str, repositoryJSON: dict, basedir: str = '', overwrite: bool = False, auto_register_missing: bool = True)
¶
Iterate through the repository JSON and for each toolkit: - Try to get an instance via ToolkitHome.getToolkit. - If missing and auto_register_missing=True, attempt auto-register ONLY if there is a clear classpath hint in the JSON (Registry.classpath or Registry.cls). - After we have a valid instance, dispatch to the appropriate handler per section.
Source code in hera/utils/data/toolkit.py
184 185 186 187 188 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 224 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
resolveDataSourcePaths(repositoryJSON, basedir='')
staticmethod
¶
Walk a repository JSON dict and resolve every resource field to an
absolute path, respecting the isRelativePath flag on each entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repositoryJSON
|
dict
|
The parsed repository JSON (toolkit-name -> section dict). |
required |
basedir
|
str
|
The base directory against which relative paths are resolved. Typically the directory that contains the repository JSON file. |
''
|
Returns:
| Type | Description |
|---|---|
dict
|
A deep copy of |
Source code in hera/utils/data/toolkit.py
loadRepositoryFromPath(json_path)
staticmethod
¶
Read a repository JSON file directly from disk, resolve all relative
resource paths to absolute paths based on the JSON file's directory,
and return the resulting dict.
This allows tests (and lightweight scripts) to work with repository
data without going through addRepository + MongoDB storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_path
|
str
|
Path to the repository JSON file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The repository dict with all resource paths resolved to absolute. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If json_path does not exist. |