mgnipy.V2.mgnifier.metadata module#
- class MGnifyMetadata(data=None, id_label=None)[source]#
Bases:
ResultsHandler- append_result(page_num, value)[source]#
Append a single metadata record to the results. This method adds a new record to the existing results, typically used when processing paginated responses.
- property data#
Get the data associated with the current instance.
- property downloads: list [dict ] | None #
Get the downloads information from the current results, if available. This property extracts the ‘downloads’ key from each record in the results.
- get_ids(label=None)#
Get a list of IDs/accessions from the current metadata.
- Parameters:
label (str , optional) – The key to extract IDs from. If None, uses the default key based on the resource type.
- Returns:
A list of IDs extracted from the metadata.
- Return type:
- Raises:
ValueError – If no data is available to extract IDs from.
- property ids: list [str ] | None #
Get the list of identifiers from the current results.
- Returns:
List of identifiers (accessions, etc.), or
Noneif no results.- Return type:
Examples
>>> from mgnipy.V2.mgnifier import MGnifier >>> query = MGnifier("studies") >>> query.get() >>> ids = query.search_results.ids
- property pages: int | None #
The pages available in the results, if any. This is determined by the keys of the results dictionary, which represent page numbers.
- property records: chain | None #
Get an iterator of individual metadata records from the retrieved results, if available. This property provides a convenient way to access the metadata records without needing to handle pagination.
Used by ResultsHandler mixin.
- Returns:
An iterator that yields individual metadata records if results are available, otherwise None.
- Return type:
chain or None
- property results: dict [int , list [dict ]]#
Get the retrieved metadata results, if available. Results are stored in a dictionary with request number (e.g. page number) as keys.
- to_json(data=None, orient='records', lines=True, **json_kwargs)#
Convert the current metadata to a JSON string or save it to a file.
- Parameters:
- Returns:
The JSON string representation of the metadata, or None if no data is available.
- Return type:
str or None
- Raises:
RuntimeError – If no data is available to convert.
- to_list(*, data=None, drop_duplicates=False)#
Convert the current or provided metadata to a list of dictionaries.
- Parameters:
data (optional) – The paginated data to convert. If
None, uses :pyattr:`data`.drop_duplicates (bool , default True) – Whether to drop duplicate records from the list.
- Returns:
A list of metadata records as dictionaries, or
Noneif no data is available.- Return type:
Examples
>>> handler = ResultsHandler(data=[{"x": 10}]) >>> handler.to_list() [{'x': 10}]
- to_pandas(data=None, expand_nested_dicts=False, rename_columns=None, drop_duplicates=False, **kwargs)#
Convert the current or provided metadata to a pandas DataFrame.
- Parameters:
data (list of dict , optional) – List of records to convert. If
None, uses :pyattr:`data`.expand_nested_dicts (list of str or bool , optional) – List of keys to expand into separate columns, or
Trueto expand defaults.rename_columns (dict of str to str, optional) – A dictionary mapping old column names to new column names.
**kwargs – Additional keyword arguments passed to
pd.DataFrame.drop_duplicates (bool )
- Returns:
DataFrame containing the metadata or
Nonewhen no data is available.- Return type:
pd.DataFrame or None
Examples
>>> handler = ResultsHandler(data=[{"a": 1, "b": 2}]) >>> df = handler.to_pandas() >>> list(df.columns) ['a', 'b'] >>> df.iloc[0]['a'] np.int64(1)
- to_polars(data=None, expand_nested_dicts=False, rename_columns=None, drop_duplicates=False, **polars_kwargs)#
Convert the current metadata to a Polars DataFrame.
- Parameters:
- Returns:
A Polars DataFrame containing the metadata.
- Return type:
pl.DataFrame
- Raises:
RuntimeError – If no data is available to convert.
- class ResultsHandler(data=None)[source]#
Bases:
objectMixin providing methods to handle and convert paginated results. This mixin provides methods to convert paginated results into various formats such as pandas DataFrames, lists of dictionaries, JSON strings, and Polars DataFrames.
- The mixin assumes the host class provides the following dependencies:
data: A property that returns an iterable of metadata records, typically a chain of dictionaries. This can be overridden by providing data directly to the conversion methods.
- get_ids(label=None)[source]#
Get a list of IDs/accessions from the current metadata.
- Parameters:
label (str , optional) – The key to extract IDs from. If None, uses the default key based on the resource type.
- Returns:
A list of IDs extracted from the metadata.
- Return type:
- Raises:
ValueError – If no data is available to extract IDs from.
- to_json(data=None, orient='records', lines=True, **json_kwargs)[source]#
Convert the current metadata to a JSON string or save it to a file.
- Parameters:
- Returns:
The JSON string representation of the metadata, or None if no data is available.
- Return type:
str or None
- Raises:
RuntimeError – If no data is available to convert.
- to_list(*, data=None, drop_duplicates=False)[source]#
Convert the current or provided metadata to a list of dictionaries.
- Parameters:
data (optional) – The paginated data to convert. If
None, uses :pyattr:`data`.drop_duplicates (bool , default True) – Whether to drop duplicate records from the list.
- Returns:
A list of metadata records as dictionaries, or
Noneif no data is available.- Return type:
Examples
>>> handler = ResultsHandler(data=[{"x": 10}]) >>> handler.to_list() [{'x': 10}]
- to_pandas(data=None, expand_nested_dicts=False, rename_columns=None, drop_duplicates=False, **kwargs)[source]#
Convert the current or provided metadata to a pandas DataFrame.
- Parameters:
data (list of dict , optional) – List of records to convert. If
None, uses :pyattr:`data`.expand_nested_dicts (list of str or bool , optional) – List of keys to expand into separate columns, or
Trueto expand defaults.rename_columns (dict of str to str, optional) – A dictionary mapping old column names to new column names.
**kwargs – Additional keyword arguments passed to
pd.DataFrame.drop_duplicates (bool )
- Returns:
DataFrame containing the metadata or
Nonewhen no data is available.- Return type:
pd.DataFrame or None
Examples
>>> handler = ResultsHandler(data=[{"a": 1, "b": 2}]) >>> df = handler.to_pandas() >>> list(df.columns) ['a', 'b'] >>> df.iloc[0]['a'] np.int64(1)
- to_polars(data=None, expand_nested_dicts=False, rename_columns=None, drop_duplicates=False, **polars_kwargs)[source]#
Convert the current metadata to a Polars DataFrame.
- Parameters:
- Returns:
A Polars DataFrame containing the metadata.
- Return type:
pl.DataFrame
- Raises:
RuntimeError – If no data is available to convert.