noether.core.utils.common

Submodules

Classes

Stopwatch

A stopwatch class to measure elapsed time.

SizedIterable

Sampler needs to implement __len__ and be iterable such that the type checking doesn't complain.

Functions

lower_type_name(obj)

Return the type name of an object in lowercase.

pascal_to_snake(pascal_case)

Convert pascal/camel to snake case using Regex.

snake_type_name(obj)

Return the type name of an object in snake case.

select_with_path(obj, path)

Access values of an object, a list or a dictionary using a string path.

validate_path(path[, exists, suffix, mkdir])

Converts a string to a Path, validates it, and optionally creates it.

check_all_none(*args)

Checks if all arguments are None.

check_at_least_one(*args)

Checks if at least one of the arguments is not None.

check_at_most_one(*args)

Checks if at most one of the arguments is not None.

check_exclusive(*args)

Checks if exactly one of the arguments is not None.

check_inclusive(*args)

Checks if either all arguments are not None or if all are None.

float_to_integer_exact(f)

Converts floats without decimals to int.

Package Contents

noether.core.utils.common.lower_type_name(obj)

Return the type name of an object in lowercase.

Parameters:

obj (object)

Return type:

str

noether.core.utils.common.pascal_to_snake(pascal_case)

Convert pascal/camel to snake case using Regex.

Handles acronyms and numbers correctly. .. rubric:: Example

XMLParser -> xml_parser HTTPClient -> http_client V2Model -> v2_model

Parameters:

pascal_case (str)

Return type:

str

noether.core.utils.common.snake_type_name(obj)

Return the type name of an object in snake case.

Parameters:

obj (object)

Return type:

str

noether.core.utils.common.select_with_path(obj, path)

Access values of an object, a list or a dictionary using a string path.

Parameters:
  • obj (dict[str, Any] | list[Any] | object) – The object to access.

  • path (str | None) – The path to the value, e.g. “a.b.c” or “a[0].b.c”.

Return type:

object

noether.core.utils.common.validate_path(path, exists='must', suffix=None, mkdir=False)

Converts a string to a Path, validates it, and optionally creates it.

Parameters:
  • path (str | pathlib.Path) – The path string to validate.

  • exists (Literal['must', 'must_not', 'any']) –

    • “must”: Raises FileNotFoundError if the path doesn’t exist.

    • ”must_not”: Raises FileExistsError if the path already exists.

    • ”any”: Performs no existence check.

  • suffix (str | None) – If provided, checks if the path ends with this suffix.

  • mkdir (bool) – If True, creates the directory path (like mkdir -p).

Returns:

The validated path.

Return type:

Path

class noether.core.utils.common.Stopwatch(device=None)

A stopwatch class to measure elapsed time.

Supports two timing backends depending on the provided device:

  • CPU (device=None): plain time.perf_counter() wall-clock timing.

  • GPU (CUDA or MPS device): device event-based timing using torch.cuda.Event / torch.mps.Event for accurate GPU measurements. Events are recorded non-blocking in stop() / lap() and resolved lazily in elapsed_seconds, which synchronizes each pending event pair individually before calling elapsed_time().

Parameters:

device (torch.device | None) – Optional device that selects the timing backend.

start()

Start the stopwatch.

Return type:

Stopwatch

stop()

Stop the stopwatch and return the elapsed time since the last lap.

For GPU devices the return value is always float("nan"); access elapsed_seconds after stopping to obtain the resolved time.

Return type:

float

lap()

Record a lap time and return the elapsed time since the last lap.

For GPU devices the return value is always float("nan"); access elapsed_seconds after stopping to obtain the resolved time.

Return type:

float

static sync(device)

Synchronize the given GPU device. No-op for CPU devices.

Parameters:

device (torch.device)

Return type:

None

property last_lap_time: float

Return the last lap time.

Return type:

float

property lap_count: int

Return the number of laps recorded.

Return type:

int

property average_lap_time: float

Return the average lap time.

Return type:

float

property elapsed_seconds: float

Return the total elapsed time since the stopwatch was started.

For GPU stopwatches, flushes any pending event pairs first, synchronizing each end event before calling elapsed_time().

Return type:

float

property elapsed_milliseconds: float

Return the total elapsed time since the stopwatch was started in milliseconds.

Return type:

float

class noether.core.utils.common.SizedIterable

Bases: Protocol

Sampler needs to implement __len__ and be iterable such that the type checking doesn’t complain.

noether.core.utils.common.check_all_none(*args)

Checks if all arguments are None.

Parameters:

args (Any)

Return type:

bool

noether.core.utils.common.check_at_least_one(*args)

Checks if at least one of the arguments is not None.

Parameters:

args (Any)

Return type:

bool

noether.core.utils.common.check_at_most_one(*args)

Checks if at most one of the arguments is not None.

Parameters:

args (Any)

Return type:

bool

noether.core.utils.common.check_exclusive(*args)

Checks if exactly one of the arguments is not None.

Parameters:

args (Any)

Return type:

bool

noether.core.utils.common.check_inclusive(*args)

Checks if either all arguments are not None or if all are None.

Parameters:

args (Any)

Return type:

int

noether.core.utils.common.float_to_integer_exact(f)

Converts floats without decimals to int.

Parameters:

f (float)

Return type:

int