mkt.schema.io_utils
Serialize and deserialize KinaseInfo objects to/from json/yaml/toml and tar archives.
Provides serialize_kinase_dict() and deserialize_kinase_dict() for
round-tripping per-kinase files, plus helpers for reading packaged .tar.gz data
in memory. TOML serialization is skipped on Windows.
Module Attributes
Dictionary of serialization and deserialization functions supported. |
|
Basename (no suffix) of the persisted KLIFS conservation-data artifact. |
Functions
|
Remove unzipped files. |
|
Deserialize KinaseInfo object from files. |
|
Extract tar.gz files. |
Get the root of the git repository. |
|
|
Load a |
Return FileNotFoundError for the given path. |
|
|
Return the path to the package data directory or of a user-provided directory. |
|
Save the current matplotlib figure in the requested vector/raster formats. |
|
Serialize a |
|
Serialize KinaseInfo object to files. |
|
Untar files exclusively in memory. |
|
Unzip the file if it is a zip file. |
- mkt.schema.io_utils.DICT_FUNCS = {'json': {'deserialize_file': <function load>, 'deserialize_str': <function loads>, 'kwargs_deserialize': {}, 'kwargs_serialize': {'default': <class 'list'>, 'indent': 4}, 'serialize': <function dumps>}, 'toml': {'deserialize_file': <function load>, 'deserialize_str': <function loads>, 'kwargs_deserialize': {}, 'kwargs_serialize': {}, 'serialize': <function dumps>}, 'yaml': {'deserialize_file': <function safe_load>, 'deserialize_str': <function safe_load>, 'kwargs_deserialize': {}, 'kwargs_serialize': {'sort_keys': False}, 'serialize': <function safe_dump>}}
Dictionary of serialization and deserialization functions supported.
- Type:
dict[str, dict[str, Callable]]
- mkt.schema.io_utils.STR_CONSERVATION_FILENAME = 'KLIFSConservationData'
Basename (no suffix) of the persisted KLIFS conservation-data artifact.
- Type:
str
- mkt.schema.io_utils._conservation_cache = {}
Module-level cache of loaded
KLIFSConservationDataobjects keyed bystr_name(mirrors_deserialization_cache).- Type:
dict
- mkt.schema.io_utils.clean_files_and_delete_directory(list_files: list[str]) None[source]
Remove unzipped files.
- Parameters:
list_files (list[str]) – List of files to remove.
- Returns:
None
- Return type:
None
- mkt.schema.io_utils.deserialize_kinase_dict(suffix: str = 'json', deserialization_kwargs: dict[str, Any] | None = None, str_path: str | None = None, bool_remove: bool = True, list_ids: list[str] | None = None, str_name: str | None = None, bool_verbose: bool = True) dict[str, BaseModel][source]
Deserialize KinaseInfo object from files.
- Parameters:
suffix (str) – Deserialization types supported: json, yaml, toml.
deserialization_kwargs (dict[str, Any], optional) – Additional keyword arguments for deserialization function, by default None.
str_path (str | None, optional) – Path from which to load files, by default None.
bool_remove (bool, optional) – If True, remove the files after deserialization, by default True.
list_ids (list[str] | None, optional) – List of IDs to filter the files if reading from memory, by default None.
str_name (str | None, optional) – Name of a variable in the global scope that contains the kinase dictionary to prevent reloading, by default None.
- Returns:
Dictionary of KinaseInfo objects.
- Return type:
dict[str, KinaseInfo]
- mkt.schema.io_utils.extract_tarfiles(path_from, path_to)[source]
Extract tar.gz files.
- Parameters:
path_from (str) – Path to the tar.gz file
path_to (str) – Pth to extract the files to
- Returns:
None
- Return type:
None
- mkt.schema.io_utils.get_repo_root()[source]
Get the root of the git repository.
- Returns:
Path to the root of the git repository; if not found, return current directory
- Return type:
str
- mkt.schema.io_utils.load_conservation_data(suffix: str = 'json', str_path: str | None = None, str_name: str | None = None)[source]
Load a
KLIFSConservationDataartifact from a single file.- Parameters:
suffix (str) – Deserialization type supported: json, yaml, toml.
str_path (str | None) – Path to the artifact file, by default None (the packaged
mkt/schema/KLIFSConservationData.json).str_name (str | None) – If provided, cache the loaded object under this name in
_conservation_cacheto prevent reloading.
- Returns:
The deserialized conservation-data object.
- Return type:
- mkt.schema.io_utils.return_filenotfound_error_if_empty_or_missing(str_path_in: str) FileNotFoundError | None[source]
Return FileNotFoundError for the given path.
- Parameters:
str_path_in (str) – Path that was not found.
- Returns:
FileNotFoundError for the given path. If the path is not empty, return None.
- Return type:
FileNotFoundError | None
- mkt.schema.io_utils.return_str_path_from_pkg_data(str_path: str | None = None, pkg_name: str | None = None, pkg_resource: str | None = None) str[source]
Return the path to the package data directory or of a user-provided directory.
- Parameters:
str_path (str | None, optional) – Path to the KinaseInfo directory, by default None.
pkg_name (str | None, optional) – Package name, by default None and will use mkt.schema.
pkg_resource (str | None, optional) – Package resource, by default None and will use KinaseInfo.
- Returns:
Path to the package data or user-provided directory.
- Return type:
str
- mkt.schema.io_utils.save_plot(fig, output_filename: str, plot_type: str = 'Plot', bool_force_local: bool = True, bool_image_subdir=True, output_path: str | None = None, bool_svg: bool = True, bool_png: bool = True, bool_pdf: bool = False, **kwargs) None[source]
Save the current matplotlib figure in the requested vector/raster formats.
Parameters:
- figmatplotlib.figure.Figure
The figure object to save.
- output_filenamestr
Name of the output file to save the plot. Any extension is stripped; the format suffixes are appended per the
bool_svg/bool_png/bool_pdfflags.- plot_typestr
Description of the plot type for logging purposes (e.g., “Dynamic range plot”)
- bool_force_localbool
If True, forces saving to the local dir (repo root or cwd) regardless of env var; default is True.
- bool_image_subdirbool
If True, saves images to a subdirectory named “images” within the output path; default is True.
- output_pathstr | None
Optional path to save the plot. If None, saves to the current working directory.
- bool_svgbool
If True, write an SVG copy; default is True.
- bool_pngbool
If True, write a PNG copy; default is True.
- bool_pdfbool
If True, write a PDF copy; default is False.
- **kwargs
Additional keyword arguments to pass to plt.savefig (e.g., {“dpi”: 300}). Default is empty dict.
- mkt.schema.io_utils.serialize_conservation_data(conservation_data: BaseModel, suffix: str = 'json', str_path: str | None = None) str | None[source]
Serialize a
KLIFSConservationDataartifact to a single file.Unlike
serialize_kinase_dict()(one file per kinase), this writes the whole single-object artifact to{str_path}/KLIFSConservationData.{suffix}, reusing theDICT_FUNCSserialization registry.- Parameters:
conservation_data (BaseModel) – The
mkt.schema.conservation_schema.KLIFSConservationDataobject.suffix (str) – Serialization type supported: json, yaml, toml.
str_path (str | None) – Directory to write into, by default None (the
mkt.schemapackage directory).
- Returns:
Path written, or None if the suffix is unsupported.
- Return type:
str | None
- mkt.schema.io_utils.serialize_kinase_dict(kinase_dict: dict[str, BaseModel], suffix: str = 'json', serialization_kwargs: dict[str, Any] | None = None, str_path: str | None = None) None[source]
Serialize KinaseInfo object to files.
- Parameters:
kinase_dict (dict[str, BaseModel]) – Dictionary of KinaseInfo objects.
suffix (str) – Serialization types supported: json, yaml, toml.
serialization_kwargs (dict[str, Any], optional) –
- Additional keyword arguments for serialization function, by default None;
(e.g., {“indent”: 2} for json.dumps, {“sort_keys”: False} for yaml.safe_dump).
str_path (str | None = None) – Path to save the serialized file, by default None will use package data or Github repo data.
- mkt.schema.io_utils.untar_files_in_memory(str_path: str, bool_extract: bool = True, list_ids: list[str] | None = None) dict[str, str][source]
Untar files exclusively in memory.
- Parameters:
str_path (str) – Path to the tar.gz file.
bool_extract (bool, optional) – If True, extract the files to memory, by default True.
list_ids (list[str] | None, optional) – List of IDs to filter the files if reading from memory, by default None. If None, all files will be extracted.
- Returns:
Dictionary of file names and their contents as strings.
- Return type:
dict[str, str]