API Reference¶
All of the following can be imported from datalogs.
Logger¶
- class Logger(root_directory: str, param_db: ParamDB[Any] | None = None)[source]¶
- class Logger(*, parent: Logger, description: str, timestamp: bool = True)
Logger corresponding to a directory that generates log files and sub-
Loggerobjects corresponding to subdirectories.If
root_directoryis given, that will be used as the directory, and thisLoggerwill function as a root. Optionally,param_dbcan be given to enable commit tagging.Otherwise,
parentanddescriptionmust be given, and this will be a sub-Loggerobject that corresponds to a subdirectory within its parent’s directory (and uses its parent’s ParamDB, if given). SeeLogger.sub_logger()for an explanation of thetimestampoption.- sub_logger(description: str, timestamp: bool = True) Logger[source]¶
Create a new sub-
Loggerwith the given description corresponding to a subdirectory within the parentLogger.By default,
timestampis True, meaning that the directory name will include a timestamp corresponding to when it was created. (Note that the directory will be created when first needed so that the timestamp more accurately reflects when its content was created.)If
timestampis False, the directory name will not include a timestamp. If there is an existing directory, it will be used. If not, a new directory will be created immediately.
- property directory: str¶
Directory where this logger saves subdirectories or files.
If the directory does not yet exist (i.e. if this is a sub-
Loggerwith a timestamp), it is created.
- file_path(filename: str) str[source]¶
Generate a path to a file or directory with the given name within the directory of this
Logger.Note that this simply generates the path, with no checks for whether a file or directory with that path exists.
- log_data(description: str, coords: Coord | Sequence[Coord], data_vars: DataVar | Sequence[DataVar], *, commit_id: int | None = None) DataLog[source]¶
Construct an Xarray from the given data and corresponding metadata, save it in a NetCDF file, and return a
DataLogwith this data and metadata.The log will be tagged with the given commit ID, or the latest commit ID if none is given (and if this Logger has a corresponding ParamDB).
- classmethod convert_to_json(obj: Any, convert: Callable[[Any], Any] | None = None) Any[source]¶
Return a JSON-serializable version of the given object. This function is used to convert objects to JSON for
Logger.log_dict()andLogger.log_props().If provided,
convert()will be used to convert the object.Numpy scalars will be unpacked and Pandas DataFrames will be converted to dictionaries.
MappingandCollectionobjects will be converted to dictionaries and lists, with keys converted to strings and values converted according to these rules.Other non-JSON-serializable values will be converted to
repr()strings.
- log_dict(description: str, dict_data: dict[str, Any], *, commit_id: int | None = None, convert: Callable[[Any], Any] | None = None) DictLog[source]¶
Save the given dictionary data and corresponding metadata in a JSON file, and return a
DictLogwith this data and metadata.Objects will be converted according to
Logger.convert_to_json(), withconvert()passed to that function.The log will be tagged with the given commit ID, or the latest commit ID if none is given (and if this Logger has a corresponding ParamDB).
- log_props(description: str, obj: Any, *, commit_id: int | None = None, convert: Callable[[Any], Any] | None = None) DictLog[source]¶
Save a dictionary of the given object’s properties and corresponding metadata in a JSON file, and return a
DictLogwith this data and metadata.Only properties that have been marked with a
LoggedProptype hint at the top of the class definition will be saved. For example:class Example: value: LoggedProp number: LoggedProp[float]
Objects will be converted according to
Logger.convert_to_json(), withconvert()passed to that function.The log will be tagged with the given commit ID, or the latest commit ID if none is given (and if this Logger has a corresponding ParamDB).
- LoggedProp¶
Used as a type hint to indicate that properties of a class should be logged by
Logger.log_props().
Load Log¶
Log Objects¶
- class DataLog(metadata: LogMetadata, dataset: xarray.Dataset, path: str | None = None)[source]¶
Log containing an Xarray
Datasetwhich can be saved to a NetCDF (“.nc”) file.See https://docs.xarray.dev/en/stable/generated/xarray.Dataset.html.
- classmethod from_variables(metadata: LogMetadata, coords: Coord | Sequence[Coord], data_vars: DataVar | Sequence[DataVar]) DataLog[source]¶
Create a data log containing an Xarray
Datasetconstructed from the given coordinate and data variables.
- property data: xarray.Dataset¶
Data stored in this log.
- property metadata: LogMetadata¶
Metadata associated with this log.
- property path: str¶
Path to the log file.
- save() None¶
Save log to a file.
- class DictLog(metadata: LogMetadata, data_dict: dict[str, Any], path: str | None = None)[source]¶
Log containing a dictionary which can be saved to a JSON (“.json”) file.
- property metadata: LogMetadata¶
Metadata associated with this log.
- property path: str¶
Path to the log file.
- save() None¶
Save log to a file.
- property data: dict[str, Any]¶
Data stored in this log.
- class LogMetadata(directory: str, timestamp: datetime.datetime, description: str, commit_id: int | None, param_db_path: str | None)[source]¶
Metadata for a log, including the directory path, commit ID, description, created timestamp, and ParamDB path.
- directory: str¶
Directory this log was created in.
- timestamp: datetime¶
When the log was created.
- description: str¶
Log description.
- commit_id: int | None¶
Commit ID in the ParamDB.
- param_db_path: str | None¶
Path to the ParamDB.
Data Log Variables¶
- class Coord(name: str, data: numpy.typing.ArrayLike, long_name: str | None = None, units: str | None = None)[source]¶
Wrapper for an Xarray
Variablerepresenting a dimensional coordinate.nameis used as the coordinate and dimension name.datashould be a 1D array labeling points along the dimension.long_nameandunitsare stored in the underlying variable’s attribute dictionary.
See https://docs.xarray.dev/en/stable/user-guide/data-structures.html#coordinates for more information on coordinates in Xarray.
- property name: str¶
Name of this coordinate or data.
- property variable: xarray.Variable¶
Underlying Xarray
Variableobject.See https://docs.xarray.dev/en/stable/generated/xarray.Variable.html.
- class DataVar(name: str, dims: str | Sequence[str], data: numpy.typing.ArrayLike, long_name: str | None = None, units: str | None = None)[source]¶
Wrapper for an Xarray
Variablerepresenting a data variable along particular dimensions.datashould be an array that aligns with the named dimensions.long_nameandunitsare stored in the underlying variable’s attribute dictionary.
- property name: str¶
Name of this coordinate or data.
- property variable: xarray.Variable¶
Underlying Xarray
Variableobject.See https://docs.xarray.dev/en/stable/generated/xarray.Variable.html.