cellmil.utils.tools

Functions

close_logger(logger)

Closing a logger savely

create_color_gradient(bins, initial_color, ...)

Create a color gradient from initial_color to final_color.

end_timer(start_time[, timed_event])

Prints the time passed from start_time.

get_cpu_count()

Get the number of available CPU cores.

get_size_of_dict(d)

module_exists(*names[, error, ...])

Try to import optional dependencies.

plot_graph(data[, title])

Plot a PyTorch Geometric graph using Plotly.

plot_vector(vector[, title])

Plot a 1D vector using matplotlib.

start_timer()

Returns the number of seconds passed since epoch.

to_float(x)

Convert tensor to float32

to_float_normalized(x)

Convert tensor to float32 and normalize to [0, 1]

unflatten_dict(d[, sep])

Unflatten a flattened dictionary (created a nested dictionary)

cellmil.utils.tools.to_float(x: Tensor) Tensor[source]

Convert tensor to float32

cellmil.utils.tools.to_float_normalized(x: Tensor) Tensor[source]

Convert tensor to float32 and normalize to [0, 1]

cellmil.utils.tools.start_timer() float[source]

Returns the number of seconds passed since epoch. The epoch is the point where the time starts, and is platform dependent.

Returns:

The number of seconds passed since epoch

Return type:

float

cellmil.utils.tools.end_timer(start_time: float, timed_event: str = 'Time usage') None[source]

Prints the time passed from start_time.

Parameters:
  • start_time (float) – The number of seconds passed since epoch when the timer started

  • timed_event (str, optional) – A string describing the activity being monitored. Defaults to “Time usage”.

cellmil.utils.tools.module_exists(*names: Union[List[str], str], error: str = 'ignore', warn_every_time: bool = False, __INSTALLED_OPTIONAL_MODULES: Dict[str, bool] = {}) Optional[Union[Tuple[module | None, ...], module]][source]

Try to import optional dependencies. Ref: https://stackoverflow.com/a/73838546/4900327

Parameters:
  • names (Union(List(str), str)) – The module name(s) to import. Str or list of strings.

  • error (str, optional) –

    What to do when a dependency is not found:
    • raise : Raise an ImportError.

    • warn: print a warning.

    • ignore: If any module is not installed, return None, otherwise, return the module(s).

    Defaults to “ignore”.

  • warn_every_time (bool, optional) – Whether to warn every time an import is tried. Only applies when error=”warn”. Setting this to True will result in multiple warnings if you try to import the same library multiple times. Defaults to False.

Raises:

ImportError – ImportError of Module

Returns:

The imported module(s), if all are found.

None is returned if any module is not found and error!=”raise”.

Return type:

Optional[ModuleType, Tuple[ModuleType…]]

cellmil.utils.tools.close_logger(logger: Logger) None[source]

Closing a logger savely

Parameters:

logger (logging.Logger) – Logger to close

cellmil.utils.tools.get_size_of_dict(d: dict[str, Any]) int[source]
cellmil.utils.tools.unflatten_dict(d: dict[str, Any], sep: str = '.') dict[str, Any][source]

Unflatten a flattened dictionary (created a nested dictionary)

Parameters:
  • d (dict) – Dict to be nested

  • sep (str, optional) – Seperator of flattened keys. Defaults to ‘.’.

Returns:

Nested dict

Return type:

dict

cellmil.utils.tools.get_cpu_count() int[source]

Get the number of available CPU cores.

cellmil.utils.tools.create_color_gradient(bins: int, initial_color: list[int], final_color: list[int]) list[list[int]][source]

Create a color gradient from initial_color to final_color.

Parameters:
  • bins (int) – Number of bins in the gradient.

  • initial_color (list[int]) – RGB values for the initial color.

  • final_color (list[int]) – RGB values for the final color.

Returns:

List of RGB colors representing the gradient.

Return type:

list[list[int]]

cellmil.utils.tools.plot_vector(vector: Tensor, title: str = 'Vector Plot') None[source]

Plot a 1D vector using matplotlib.

Parameters:
  • vector (torch.Tensor) – 1D tensor to plot.

  • title (str, optional) – Title of the plot. Defaults to “Vector Plot”.

cellmil.utils.tools.plot_graph(data: Data, title: str = 'Graph Visualization') None[source]

Plot a PyTorch Geometric graph using Plotly.

Parameters:
  • data (Data) – PyTorch Geometric Data object containing graph structure.

  • title (str, optional) – Title of the plot. Defaults to “Graph Visualization”.