Code Reference
kymata
kymata.plot.expression
Functions:
-
expression_plot–Generates a plot of transform expressions over time with optional display customizations.
-
legend_display_dict–Creates a dictionary for the
legend_displayparameter ofexpression_plot().
expression_plot
expression_plot(expression_set: ExpressionSet, show_only: Optional[str | Sequence[str]] = None, paired_axes: bool = True, alpha: float = p_threshold_for_sigmas(5), color: Optional[str | dict[str, str] | list[str]] = None, ylim: Optional[float] = None, xlims: Optional[tuple[float | None, float | None]] = None, hidden_transforms_in_legend: bool = True, title: Optional[str] = None, fig_size: tuple[float, float] = (12, 7), minimap: str | None = None, minimap_type: str = 'inflated', minimap_view: Optional[str] = None, show_only_sensors: Optional[Literal['eeg', 'meg']] = None, minimap_latency_range: Optional[tuple[float | None, float | None]] = None, plot_top_n: Optional[int] = None, save_to: Optional[Path] = None, overwrite: bool = True, show_legend: bool = True, legend_display: dict[str, str] | None = None, minimap_kwargs: Optional[dict] = None) -> Figure
Generates a plot of transform expressions over time with optional display customizations.
Parameters:
-
(expression_setExpressionSet) –The set of expressions to plot, containing transforms and associated data.
-
(show_onlyOptional[str | Sequence[str]], default:None) –A string or a sequence of strings specifying which transforms to plot. If None, all transforms in the expression_set will be plotted. Default is None.
-
(paired_axesbool, default:True) –When True, shows the expression plot split into left and right axes. When False, all points are shown on the same axis. Default is True.
-
(alphafloat, default:p_threshold_for_sigmas(5)) –Significance level for statistical tests, defaulting to a 5-sigma threshold.
-
(colorOptional[str | dict[str, str] | list[str]], default:None) –Color settings for the plot. Can be a single color, a dictionary mapping transform names to colors, or a list of colors. Default is None.
-
(ylimOptional[float], default:None) –The y-axis limit (p-value). Use log10 of the desired value — e.g. if the desired limit is 10^-100, supply ylim=-100. If None, it will be determined automatically. Default is None.
-
(xlimstuple[Optional[float], Optional[float]], default:None) –The x-axis limits as a tuple (in ms). None to use default values, or set either entry to None to use the default for that value. Default is (-100, 800).
-
(hidden_transforms_in_legendbool, default:True) –If True, includes non-plotted transforms in the legend. Default is True.
-
(titlestr, default:None) –Title over the top axis in the figure. Supply None for no title. Default is None.
-
(fig_sizetuple[float, float], default:(12, 7)) –Figure size in inches. Default is (12, 7).
-
(minimapstr, default:None) –If None, no minimap is shown. Other options are:
"standard": Show small minimal."large": Show a large minimal with smaller expression plot. Default is None. -
(minimap_typestr, default:'inflated') –The type of minimap to display. Options include:
"inflated", or any other valid keyword for a mne surface: This will plot on a 3D cortical mesh."volumetric": plot in a volumetric view à la MRICron. Default is "inflated". -
(minimap_viewOptional[str], default:None) –The view type for the minimap, either "lateral" or other specified views. Valid options are:
"lateral": From the left or right side such that the lateral (outside) surface of the given hemisphere is visible."medial": From the left or right side such that the medial (inside) surface of the given hemisphere is visible (at least when in split or single-hemi mode)."rostral": From the front."caudal": From the rear."dorsal": From above, with the front of the brain pointing up."ventral": From below, with the front of the brain pointing up."frontal": From the front and slightly lateral, with the brain slightly tilted forward (yielding a view from slightly above)."parietal": From the rear and slightly lateral, with the brain slightly tilted backward (yielding a view from slightly above)."axial": From above with the brain pointing up (same as 'dorsal')."sagittal": From the right side."coronal": From the rear. Only"axial"and"coronal"are valid for volumetric view; sagittal views will always be plotted. Default is None (lateral for cortical, axial for volumetric). -
(show_only_sensorsstr, default:None) –Show only one type of sensors. "meg" for MEG sensors, "eeg" for EEG sensors. None to show all sensors. Supplying this value with something other than a SensorExpressionSet causes will throw an exception. Default is None.
-
(minimap_latency_rangeOptional[tuple[float | None, float | None]], default:None) –Supply
(start_time, stop_time)to restrict the minimap view to only the specified time window, and highlight the time window on the expression plot. Bothstart_timeandstop_timeare in seconds. Setstart_timeorstop_timetoNonefor half-open intervals. -
(plot_top_nOptional[int], default:None) –If not None, show only the N most significant sources. If None, plot all significant sources. Default is None.
-
(save_toOptional[Path], default:None) –Path to save the generated plot. If None, the plot is not saved. Default is None.
-
(overwritebool, default:True) –If True, overwrite the existing file if it exists. Default is True.
-
(show_legendbool, default:True) –If True, displays the legend. Default is True.
-
(legend_displaydict[str, str] | None, default:None) –Allows grouping of multiple transforms under the same legend item. Provide a dictionary mapping true transform names to display names. None applies no grouping. Default is None.
-
(minimap_kwargsOptional[dict], default:None) –Keyword argument overrides for minimap plotting. Default is None.
Returns:
-
Figure–pyplot.Figure: The matplotlib figure object containing the generated plot.
Raises:
-
FileExistsError–If the file already exists at save_to and overwrite is set to False.
Notes
The function plots the expression data with options to customize the appearance and statistical significance thresholds. It supports different data types (e.g., HexelExpressionSet, SensorExpressionSet) and can handle paired axes for left/right hemisphere data.
legend_display_dict
legend_display_dict(transforms: list[str], display_name) -> dict[str, str]
Creates a dictionary for the legend_display parameter of expression_plot().
This function maps each transform name in the provided list to a single display name, which can be used to group multiple transforms under one legend item in the plot.
Parameters:
-
(transformslist[str]) –A list of transform names to be grouped under the same display name.
-
(display_namestr) –The display name to be used for all transforms in the list.
Returns:
-
dict[str, str]–dict[str, str]: A dictionary mapping each transform name to the provided display name.
kymata.gridsearch.plain
Functions:
-
do_gridsearch–Perform a grid search over all hexels for all latencies using EMEG data and a given transform.
do_gridsearch
do_gridsearch(emeg_values: NDArray, transform: Transform, channel_names: list, channel_space: str, start_latency: float, emeg_t_start: float, stimulus_shift_correction: float, stimulus_delivery_latency: float, emeg_sample_rate: float, emeg_layout: SensorLayout, plot_location: Optional[Path] = None, n_derangements: int = 1, seconds_per_split: float = 1, n_splits: int = 400, n_reps: int = 1, plot_top_five_channels: bool = False, overwrite: bool = True) -> ExpressionSet
Perform a grid search over all hexels for all latencies using EMEG data and a given transform.
This function processes EMEG data to compute the correlation between sensor or source signals and a specified transform across multiple latencies. The results include statistical significance testing and optional plotting.
Parameters:
-
(emeg_valuesNDArray) –A 2D array of EMEG values with shape (channels, reps, time).
-
(transformTransform) –The transform against which the EMEG data will be correlated. It should have a
valuesattribute representing the transform's values and asample_rateattribute indicating its sample rate. -
(channel_nameslist) –List of channel names corresponding to the EMEG data. For 'sensor' space, it is a flat list of sensor names. For 'source' space, it is a list containing two lists: left hemisphere and right hemisphere hexel names.
-
(channel_spacestr) –The type of channel space used, either 'sensor' or 'source'.
-
(start_latencyfloat) –The starting latency for the grid search in seconds.
-
(emeg_t_startfloat) –The starting time of the EMEG data in seconds.
-
(stimulus_shift_correctionfloat) –Correction factor for stimulus shift in seconds per second.
-
(stimulus_delivery_latencyfloat) –Correction offset for stimulus delivery in seconds.
-
(plot_locationOptional[Path], default:None) –Path to save the plot of the top five channels of the grid search. If None, plotting is skipped. Default is None.
-
(emeg_sample_ratefloat) –The sample rate of the EMEG data in Hertz.
-
(n_derangementsint, default:1) –Number of derangements (random permutations) used to create the null distribution. Default is 1.
-
(seconds_per_splitfloat, default:1) –Duration of each split in seconds. Default is 0.5 seconds.
-
(n_splitsint, default:400) –Number of splits used for analysis. Default is 800.
-
(n_repsint, default:1) –Number of repetitions for each split. Default is 1.
-
(plot_top_five_channelsbool, default:False) –Plots the p-values and correlation values of the top five channels in the gridsearch. Default is False.
-
(overwritebool, default:True) –Whether to overwrite existing plot files. Default is True.
Returns:
-
ExpressionSet(ExpressionSet) –An ExpressionSet object (either SensorExpressionSet or HexelExpressionSet)
-
ExpressionSet–containing the log p-values for each channel/hexel and latency.
Notes
- The function down-samples the EMEG data to match the transform's sample rate.
- The EMEG data is reshaped into segments of the specified duration (
seconds_per_split). - Cross-correlations between the EMEG data and the transform are computed using FFT.
- Statistical significance is assessed using a vectorized Welch's t-test.
- If specified, the results are plotted and saved to the given location.
kymata.io.nkg
Functions:
-
load_expression_set–Loads an ExpressionSet from the specified path(s) or open file.
-
save_expression_set–Save the given ExpressionSet to a specified path or an already open file.
load_expression_set
load_expression_set(from_path_or_file: PathType | FileType | list[PathType]) -> ExpressionSet
Loads an ExpressionSet from the specified path(s) or open file.
The function determines the type of ExpressionSet (HexelExpressionSet or SensorExpressionSet) based on the data loaded from the provided path or file. It then constructs and returns an instance of the appropriate ExpressionSet subclass.
Parameters:
-
(from_path_or_filePathType | FileType | list[PathType]) –The path, file, or list of paths from which to load the data.
Returns:
-
ExpressionSet(ExpressionSet) –An instance of either HexelExpressionSet or SensorExpressionSet, depending on the type identifier in the data.
Raises:
-
KeyError–If required keys are missing in the data dictionary.
-
ValueError–If the type identifier is not recognized.
save_expression_set
save_expression_set(expression_set: ExpressionSet, to_path_or_file: PathType | FileType, compression=ZIP_LZMA, overwrite: bool = False)
Save the given ExpressionSet to a specified path or an already open file.
This function saves the ExpressionSet data into a compressed file format. If a file path is provided, it creates and writes to the file. If an open file is supplied, it should be opened in "wb" mode. The overwrite flag is ignored if an open file is supplied.
Parameters:
-
(expression_setExpressionSet) –The ExpressionSet object to be saved.
-
(to_path_or_filePathType | FileType) –The path or open file where the ExpressionSet will be saved.
-
–compressionThe compression method to use (default is ZIP_LZMA).
-
(overwritebool, default:False) –If True, allows overwriting an existing file (default is False).
Raises:
-
FileExistsError–If the specified path already exists and overwrite is False.
-
TypeError–If the provided path or file type is invalid.
Notes
- The compression parameter should be compatible with the
ZipFileclass. - The function writes various metadata and data blocks in a structured format within the zip file.
kymata.io.config
Functions:
-
get_root_dir–Get the root directory based on the configuration parameters.
-
load_config–Load configuration parameters from a specified path or file.
-
modify_param_config–Modify a specific configuration parameter in the given configuration file.
get_root_dir
get_root_dir(config: dict) -> str
Get the root directory based on the configuration parameters.
This function returns the appropriate root directory path based on the 'data_location' parameter in the provided configuration dictionary.
Parameters:
-
(configdict) –The configuration dictionary containing the 'data_location' parameter.
Returns:
-
str(str) –The root directory path corresponding to the 'data_location' parameter.
Raises:
-
ValueError–If the 'data_location' parameter is not 'local', 'cbu', or 'cbu-local'.
load_config
load_config(config_location: PathType | FileType) -> dict
Load configuration parameters from a specified path or file.
This function reads the configuration parameters from a YAML file located at the given path or open file.
Parameters:
-
(config_locationPathType | FileType) –The path to the configuration file or an open file object.
Returns:
-
dict(dict) –The configuration parameters loaded from the file.
Raises:
-
FileNotFoundError–If the specified path does not exist.
-
YAMLError–If there is an error in parsing the YAML file.
modify_param_config
modify_param_config(config_location: str, key: str, value)
Modify a specific configuration parameter in the given configuration file.
This function updates the value of a specified key in the configuration file and saves the changes.
Parameters:
-
(config_locationstr) –The path to the configuration file.
-
(keystr) –The key of the configuration parameter to be modified.
-
–valueThe new value to be assigned to the specified key.
Raises:
-
FileNotFoundError–If the specified configuration file does not exist.
-
YAMLError–If there is an error in parsing the YAML file.
kymata.plot.ippm
Functions:
-
plot_ippm–Plots an IPPM graph, always including all available hemispheres.
-
xlims_from_expressionset–Get an appropriate set of xlims from an ExpressionSet.
plot_ippm
plot_ippm(ippm: IPPM, colors: dict[str, str], title: Optional[str] = None, xlims_s: tuple[Optional[float], Optional[float]] = (None, None), y_ordinate_style: str = centered, connection_style: str = last_to_first, scale_nodes: bool = False, figheight: int = 5, figwidth: int = 10, arrowhead_dims: tuple[float, float] = None, linewidth: float = 3, show_labels: bool = True, relabel: dict[str, str] | None = None, avoid_collinearity: bool = False, _test_hemisphere_colors: bool = False) -> Figure
Plots an IPPM graph, always including all available hemispheres.
Parameters:
-
(ippmIPPM) –IPPM object to plot.
-
(colorsdict[str, str]) –Dictionary with keys as node names and values as colors in hexadecimal. Contains the color for each transform. The nodes and edges are colored accordingly.
-
(titlestr, default:None) –Title of the plot.
-
(scale_nodesbool, default:False) –scales the node by the significance. Default is False
-
(figheightint, default:5) –Height of the plot. Defaults to 5.
-
(figwidthint, default:10) –Width of the plot. Defaults to 10.
-
(show_labelsbool, default:True) –Show transform names as labels on the graph. Defaults to True.
-
(relabeldict[str, str], default:None) –Dictionary to specify optional labels for each node. Dictionary should map original transform labels to desired labels. Missing keys will be ignored. Defaults to None (no change).
-
(avoid_collinearitybool, default:False) –Whether to apply a small offset to avoid collinearity between nodes in the same serial step. Defaults to False.
-
(_test_hemisphere_colorsbool, default:False) –If True, overrides the
colorsdict to color nodes according to their hemisphere, for testing vertical separation. Defaults to False.
Returns:
-
Figure–A figure of the IPPM graph.
xlims_from_expressionset
xlims_from_expressionset(es: ExpressionSet, padding: float = 0.05) -> tuple[float, float]
Get an appropriate set of xlims from an ExpressionSet.
Parameters:
-
(esExpressionSet) – -
(paddingfloat, default:0.05) –The amount of padding to add either side of the IPPM plot, in seconds. Default is 0.05 (50ms).
Returns:
-
tuple[float, float]–tuple[float, float]: xmin, xmax
kymata.ippm.ippm
Classes:
-
IPPM–IPPM container/constructor object. Use this class as an interface to build a single, unified IPPM graph
IPPM
IPPM(expression_set: ExpressionSet, candidate_transform_list: CandidateTransformList | TransformHierarchy, denoiser: str | None = _default_denoiser, merge_before_denoising: bool = False, **kwargs: dict[str, Any])
IPPM container/constructor object. Use this class as an interface to build a single, unified IPPM graph from an expression set.
Parameters:
-
(expression_setExpressionSet) –The ExpressionSet from which to build the IPPM.
-
(candidate_transform_listCandidateTransformList) –The CTL (i.e. underlying hypothetical IPPM) to be applied to the expression set.
-
(denoiserstr, default:_default_denoiser) –The denoising method to be applied to the expression set. Default is None.
-
(merge_before_denoisingbool, default:False) –If true, this will merge both hemispheres before running the denoiser.
-
(**kwargsdict[str, Any], default:{}) –Additional arguments passed to the denoiser.
Raises:
-
ValueError–If any transform in the hierarchy is not found in the expression set, or if the provided denoiser is invalid.
Methods:
-
set_kids–Adds KID attributes to edges and nodes in the IPPM graph based on transform mappings.
-
to_json–Serializes the IPPM into a JSON format suitable for sending to
.
set_kids
set_kids(transform_kids: dict[str, str], verify_against_api: bool = True) -> None
Adds KID attributes to edges and nodes in the IPPM graph based on transform mappings.
Parameters:
-
(transform_kidsdict[str, str]) –Dictionary mapping transform names to their KID values.
-
(verify_against_apibool, default:True) –If True (the default), verify all supplied KIDs against the Kymata Atlas API.
Raises:
-
ValueError–When verifying against the API (and assuming the API is reachable), if any of the KIDs are not valid.
Warns:
-
Issues a warning if–
kymata.entities.expression
Classes and functions for storing expression information.
Classes:
-
ExpressionPoint–A single point of transform expression evidence.
-
ExpressionSet–Brain data associated with expression of a single transform.
-
HexelExpressionSet–Brain data associated with the expression of a single transform in hexel space.
-
SensorExpressionSet–Brain data associated with the expression of a single transform in sensor space.
Functions:
-
combine–Combines a sequence of
ExpressionSets into a singleExpressionSet. -
get_n_channels–Returns the number of channels represented in an ExpressionSet.
-
group_points_by_transform–Groups a list of expression points by their associated transforms.
ExpressionPoint
Bases: NamedTuple
A single point of transform expression evidence.
ExpressionSet
ExpressionSet(transforms: str | Sequence[str], latencies: Sequence[Latency], data_blocks: dict[str, _InputDataArray | Sequence[_InputDataArray]], channel_coord_name: str, channel_coord_dtype, channel_coord_values: dict[str, Sequence])
Bases: ABC
Brain data associated with expression of a single transform. Data is log10 p-values.
Initializes the ExpressionSet with the provided data.
Parameters:
-
(transformsstr | Sequence[str]) –Transform name, or sequence of names.
-
(latenciesSequence[Latency]) –Latency values.
-
(data_blocksdict[str, _InputDataArray | Sequence[_InputDataArray]]) –Mapping of block names to data arrays (log10 p-values).
In general there are two possible formats for this argument.
In the first (safer, more explicit and flexible) format,
data_blockscontains a dict mapping block names to data arrays. E.g., in the case there are three transforms in a hexel setting:or in a sensor setting: (and where{ # each array is (channel, latency)-shaped # and there's one for each transform # ↓ "left": [array(...), array(...), array(...)], "right": [array(...), array(...), array(...)], }array(...)can be a numpy array or a sparse array). In this format, all data arrays should be the same size.In the second (more performant) format,
data_blockscontains a single data array whosetransformdimensions can be concatenated to achieve the desired resultant data block. E.g. -
(channel_coord_namestr) –Name of the channel coordinate.
-
–channel_coord_dtypeData type of the channel coordinate.
-
(channel_coord_valuesdict[str, Sequence]) –Dictionary mapping block names to channel coordinate values.
Raises:
-
ValueError–when arguments are invalid
Methods:
-
best_transforms–Note that channels for which the best p-value is 1 will be omitted.
-
crop–Returns a copy of the ExpressionSet with latencies cropped between the two endpoints (inclusive).
-
rename–Renames the transforms and channels within an ExpressionSet.
Attributes:
-
latencies(NDArray[LatencyDType]) –Latencies, in seconds.
-
transforms(list[TransformNameDType]) –Transform names.
best_transforms
abstractmethod
best_transforms() -> list[ExpressionPoint] | tuple[list[ExpressionPoint], ...]
Note that channels for which the best p-value is 1 will be omitted.
crop
abstractmethod
crop(latency_start: float | None, latency_stop: float | None) -> Self
Returns a copy of the ExpressionSet with latencies cropped between the two endpoints (inclusive).
Parameters:
-
(latency_startfloat | None) –Latency in seconds to start the cropped window. Use None for no cropping at the start (e.g. half-open crop).
-
(latency_stopfloat | None) –Latency in seconds to stop the cropped window. Use None for no cropping at the end (e.g. half-open crop).
Returns:
-
Self(Self) –A copy of the ExpressionSet with the latencies cropped between the specified start and stop.
rename
Renames the transforms and channels within an ExpressionSet.
Supply a dictionary mapping old values to new values.
Raises KeyError if one of the keys in the renaming dictionary is not a transform name in the expression set.
HexelExpressionSet
HexelExpressionSet(transforms: str | Sequence[str], hexels_lh: Sequence[Hexel], hexels_rh: Sequence[Hexel], latencies: Sequence[Latency], data_lh: _InputDataArray | Sequence[_InputDataArray], data_rh: _InputDataArray | Sequence[_InputDataArray])
Bases: ExpressionSet
Brain data associated with the expression of a single transform in hexel space. Includes lh, rh, flipped, non-flipped. Data is log10 p-values
Methods:
-
best_transforms–Return a pair of DataFrames (left, right), containing:
-
rename–Renames the transforms and channels within an ExpressionSet.
Attributes:
-
hexels_left(NDArray[HexelDType]) –Hexels, canonical ID.
-
hexels_right(NDArray[HexelDType]) –Hexels, canonical ID.
-
latencies(NDArray[LatencyDType]) –Latencies, in seconds.
-
left(DataArray) –Left-hemisphere data.
-
right(DataArray) –Right-hemisphere data.
-
transforms(list[TransformNameDType]) –Transform names.
best_transforms
best_transforms() -> tuple[list[ExpressionPoint], list[ExpressionPoint]]
Return a pair of DataFrames (left, right), containing: for each hexel, the best transform and latency for that hexel, and the associated log p-value
Note that channels for which the best p-value is 1 will be omitted.
rename
Renames the transforms and channels within an ExpressionSet.
Supply a dictionary mapping old values to new values.
Raises KeyError if one of the keys in the renaming dictionary is not a transform name in the expression set.
SensorExpressionSet
SensorExpressionSet(transforms: str | Sequence[str], sensors: Sequence[Sensor], latencies: Sequence[Latency], data: _InputDataArray | Sequence[_InputDataArray], sensor_layout: Optional[SensorLayout] = None)
Bases: ExpressionSet
Brain data associated with the expression of a single transform in sensor space. Includes left hemisphere (lh), right hemisphere (rh), flipped, and non-flipped data. Data is represented as log10 p-values.
Initialize the SensorExpressionSet with transform names, sensor metadata, latency information, and log p-value data.
Parameters:
-
(transformsstr | Sequence[str]) –The names of the transforms being evaluated.
-
(sensorsSequence[Sensor]) –Metadata about the sensors used in the study.
-
(latenciesSequence[Latency]) –Latency information corresponding to the data.
-
(data_InputDataArray | Sequence[_InputDataArray]) –Log p-values representing the data.
-
(sensor_layoutOptional[SensorLayout], default:None) –Layout of the EMEG sensors. None indicates that the layout is not specified. If not None, then expect at least one of MEG and EEG sensors to be specified
Methods:
-
best_transforms–Return a DataFrame containing:
-
rename–Renames the transforms and channels within an ExpressionSet.
Attributes:
-
latencies(NDArray[LatencyDType]) –Latencies, in seconds.
-
scalp(DataArray) –Get the left-hemisphere data.
-
sensors(NDArray[SensorDType]) –Get the sensor metadata.
-
transforms(list[TransformNameDType]) –Transform names.
sensors
property
Get the sensor metadata.
Returns:
-
NDArray[SensorDType]–NDArray[SensorDType]: Array of sensor metadata.
best_transforms
best_transforms() -> list[ExpressionPoint]
Return a DataFrame containing: for each sensor, the best transform and latency for that sensor, and the associated log p-value
Note that channels for which the best p-value is 1 will be omitted.
rename
Renames the transforms and channels within an ExpressionSet.
Supply a dictionary mapping old values to new values.
Raises KeyError if one of the keys in the renaming dictionary is not a transform name in the expression set.
combine
Combines a sequence of ExpressionSets into a single ExpressionSet.
All must be suitable for combination, e.g. same type, same channels, etc.
get_n_channels
get_n_channels(es: ExpressionSet) -> int
Returns the number of channels represented in an ExpressionSet.
group_points_by_transform
group_points_by_transform(points: Iterable[ExpressionPoint]) -> GroupedPoints
Groups a list of expression points by their associated transforms.
This function organizes expression points into a dictionary, where each key is a transform, and each value is a list of expression points associated with that transform.
Parameters:
-
(pointslist[ExpressionPoint]) –A list of expression points to be grouped.
Returns:
-
GroupedPoints(GroupedPoints) –A dictionary mapping transform names to lists of expression points associated with each transform.