ionique.core

adaptation from “core.py” by Jacob Scheriber https://github.com/jmschrei/PyPore

This holds the core data types which may be abstracted in many different applications. This module defines a tree-based framework for representing, parsing, and annotating segments of ionic current data.

class ionique.core.AbstractSegmentTree

Bases: object

Class for managing hierarchical segments.

This class supports a tree structure where each segment can contain multiple child segments, enabling recursive parsing and analysis of nested data. It provides utilities to manage relationships between segments, apply parsers, and extract subsegments by rank.

__init__() None

Initialize the segment

add_child(child: AnySegment) None

Add a single child

add_children(children: list[AnySegment]) None

Add multiple children if: 1. Child position is the within parent’s segment. 2. The length of the child’s segment is > 0 3. There is no overlap between consecutive children

Or add children with no check

children: list[AnySegment]
clear_children()

Clear the list of children

climb_to_rank(rank: str) AnySegment | None

Go up and find the segment with the specified rank :return: segment

end: int | None
get_feature(name: str)

Gets the ‘name’ feature of the current segment or its parent

get_top_parent() AnySegment

Recursively go up and find top most parent :return: parent

property n: int

Get the length of the segment :return: len(segment)

parent: AnySegment | None
parse(parser, newrank: str, at_child_rank: str | None = None, **kwargs) bool

Parses the data in the segment or at a particular rank of child segments into children segments with a new rank.

Parameters:
  • parser (Parser) – A parser object with a parse method and required input attributes.

  • newrank (str) – Rank to assign to the newly created child segments.

  • at_child_rank (str or None) – Determines whether to traverse the children tree down to a given rank.

  • kwargs – Additional arguments to pass to the parser.

Returns:

True if parsing was successful, otherwise raises an exception.

Return type:

bool

rank: str
property relative_end: int

Get end position relative to parent segment :return: end position

property relative_slice: ndarray

Slice the array to get the data between end-start segment :return: sliced np array

property relative_start: int

Get the start position relative to parent segment :return: start position

property slice: ndarray

Slice the array to get the data between start-end segment :return: sliced np array

start: int | None
traverse_to_rank(rank: str) list

Traverse the tree rank and get the list of the segments of the rank :param: rank :return: list of segments

class ionique.core.MetaSegment(start: int, end: int, parent: AnySegment | None = None, rank: str | None = None, unique_features: dict | None = {}, **kwargs)

Bases: AbstractSegmentTree

The metadata on an abstract segment of ionic current. All information about a segment can be loaded, without the expectation of the array of floats.

__init__(start: int, end: int, parent: AnySegment | None = None, rank: str | None = None, unique_features: dict | None = {}, **kwargs)
Parameters:
  • start (int) – Start index of the segment.

  • end (int) – End index of the segment.

  • parent (AbstractSegmentTree or None) – Parent segment in the tree (optional).

  • rank (str or None) – Rank identifier for this segment (optional).

  • unique_features (dict) – Dictionary of metadata features.

property current

Get the current data of the segment if the segment correlates to the file :return: current

delete()

Delete itself. There are no arrays with which to delete references for.

property duration: float

Get the duration of the segment = start - end :return: duration

classmethod from_json(filename=None, in_json=None)

Read in a metasegment from a JSON and return a metasegment object. Either pass in a file which has a segment stored, or an actual JSON object.

property max

Calculate the maximum value of the current array.

Returns:

Maximum value of the current array.

Return type:

float

property mean

Calculate the mean of the current array.

Returns:

Mean value of the current array.

Return type:

float

property min

Calculate the minimum value of the current array.

Returns:

Minimum value of the current array.

Return type:

float

property std

Calculate the standard deviation of the current array.

Returns:

Standard deviation of the current array.

Return type:

float

property time

Get the time data of the segment if the corresponding rank is in file :return: time

to_dict()

Return a dict representation of the metadata, usually used prior to converting the dict to a JSON.

to_json(filename=None)

Return a JSON representation of this, by reporting the important metadata.

to_meta()

Kept to allow for error handling, but since it’s already a metasegment it won’t actually do anything.

unique_features
class ionique.core.Segment(current, **kwargs)

Bases: AbstractSegmentTree

A segment of ionic current, and methods relevant for collecting metadata. The ionic current is expected to be passed as a numpy array of floats. Metadata methods (mean, std..) are decorated as properties to reduce overall computational time, making them calculated on the fly rather than during analysis.

__init__(current, **kwargs)

The segment must have a list of ionic current, of which it stores some statistics about. It may also take in as many keyword arguments as needed, such as start time or duration if already known. Cannot override statistical measurements. :param current: Numpy array of current data points. :type current: np.ndarray :param kwargs: Additional attributes like ‘start’, ‘end’, ‘rank’, etc.

children: list[AnySegment]
delete()

Deleting this segment requires deleting its reference to the ionic current array, and then deleting itself.

end: int | None
classmethod from_json(filename=None, json=None)

Read in a segment from a JSON and return a metasegment object. Either pass in a file which has a segment stored, or an actual JSON object.

property max

Calculate the maximum value of the current array.

Returns:

Maximum value of the current array.

Return type:

float

property mean

Calculate the mean of the current array.

Returns:

Mean value of the current array.

Return type:

float

property min

Calculate the minimum value of the current array.

Returns:

Minimum value of the current array.

Return type:

float

property n

Get the number of elements in the current array.

Returns:

Number of elements in the current array.

Return type:

int

parent: AnySegment | None
rank: str
scale(sampling_freq)

Rescale all of the values to go from samples to seconds.

start: int | None
property std

Calculate the standard deviation of the current array.

Returns:

Standard deviation of the current array.

Return type:

float

to_dict()

Return a dict representation of the metadata, usually used prior to converting the dict to a JSON.

to_json(filename=None)

Return a JSON representation of this, by reporting the important metadata.

to_meta()

Convert from a segment to a ‘metasegment’, which stores only metadata about the segment and not the full array of ionic current.

ionique.core.ignored(*exceptions)

Replace the “try, except: pass” paradigm by replacing those three lines with a single line. Taken from the latest 3.4 python update push by Raymond Hettinger, see: http://hg.python.org/cpython/rev/406b47c64480