metaspace_converter.utils module

metaspace_converter.utils.empty_columns_to_nan(df)[source]

Converts all empty columns from None (dtype object) to NaN (dtype float).

Parameters:

df (DataFrame) – A dataframe

Return type:

DataFrame

Returns:

The modified dataframe. The original dataframe is modified, not a copy.

metaspace_converter.utils.has_optical_image(dataset)[source]

Check whether a METASPACE dataset has an optical image

Return type:

bool

metaspace_converter.utils.is_non_str_sequence(obj)[source]

Returns whether an object is a sequence (list and similar), excluding strings.

Parameters:

obj (Any) – The object to check.

Return type:

bool

Returns:

True if the object is a sequence.

metaspace_converter.utils.iter_property_paths(obj, delimiter='/', include_none=True, dont_traverse_types=(), dont_descend_paths=(), _path='')[source]

Iterates recursively over properties of nested objects.

Parameters:
  • obj (Any) – A class instance, mapping or sequence.

  • delimiter (str) – The delimiter to use. Should not be contained in any name.

  • include_none (bool) – Whether to yield attributes which have None assigned, or skip them.

  • dont_traverse_types (tuple[type, ...]) – Iterable data types which should not be traversed any deeper. These are yielded as values.

  • dont_descend_paths (Collection[str]) – Paths with iterable objects that should be preserved.

Return type:

Iterator[tuple[str, Any]]

Returns:

An iterator for iterating over paths and objects found at each path. It can also be converted to a dictionary.

metaspace_converter.utils.stringify_list_columns(df, columns=(), all=False)[source]

If the dataframe column is a list, stringify it into JSON.

Parameters:
  • df (DataFrame) – A dataframe with a column that contains lists (e.g. of molecule names)

  • columns (Iterable[str]) – The names of the columns containing lists

  • all (bool) – Whether to check and stringify all columns containing list-like objects

Return type:

DataFrame

Returns:

A dataframe where all values in specified columns are JSON stringified.

metaspace_converter.utils.transform_coords(coords, transform_matrix_2d)[source]

Transform a list of 2D coordinates using a 3×3 transformation matrix.

Like in scikit-image, but without adding that dependency.

See skimage.transform._geometric.ProjectiveTransform.__call__

Parameters:
  • coords (ndarray) – An array of coordinates with shape (number of points, 2)

  • transform_matrix_2d (ndarray) – A transformation matrix of shape (3, 3)

Return type:

ndarray

Returns:

The list of transformed coordinates as Numpy array