Skip to main content

Kiana Is A Neural Aligner. A toolkit for synchronizing, integrating, and analyzing heterogeneous neuroscience data (ephys, behaviors, etc.).

Project description

KIANA

Kiana Is A Neural Aligner.

Kiana Logo

🇨🇳 中文说明 (Chinese Version)

Project Summary

KIANA is a Python toolkit designed for neural data alignment in neuroscience research. It provides tools for processing and aligning time-series data from various sources—such as electrophysiology (e.g., spike trains) and imaging (e.g., fMRI)—to experimental events or stimuli.

It serves as a comprehensive toolkit for synchronizing, integrating, and analyzing heterogeneous neuroscience data (behavioral, electrophysiological, etc.). kiana aims to make the tedious, error-prone workflow of data alignment simple, reliable, and reproducible.


💡 Why Kiana?

In neuroscience research, we often need to process data streams from different devices with distinct time bases:

  • Behavioral Control Systems (e.g., MonkeyLogic): Millisecond-level event markers.
  • High-Speed Cameras or Motion Capture Systems: Video frames.
  • Electrophysiology Systems (e.g., Plexon, Blackrock): Microsecond-level neural spikes and LFP signals.

Precise alignment of these "heterogeneous" timelines is a prerequisite for subsequent analysis, but the process is often painful, time-consuming, and prone to errors. kiana was created to solve this pain point. It provides a "Recipe-Driven" framework that allows you to elegantly define, execute, and verify complex data synchronization tasks.

✨ Core Features

  • Recipe-Driven: Define every step of data processing clearly using .add_segment() method chaining, just like writing a "recipe".
  • Multi-Source Loading: Built-in flexible loaders (MatLoader, DataFrameLoader) with support for easy extension.
  • Robust Alignment: The core uses Dynamic Time Warping (DTW) algorithms to effectively handle common issues in event sequences such as "clock drift", "missing events", or "extra artifacts".
  • Multi-Context Synchronization: Easily align a single behavioral timeline with multiple independent electrophysiological recording contexts (e.g., times from different probes or devices).
  • One-Stop Analysis & Visualization: Includes a powerful SpikeTrainAnalyzer, allowing you to go from data alignment to publication-quality PSTH/Raster plots in one step.

🚀 Installation

You can install directly from PyPI via pip:

pip install kiana

Alternatively, for development:

  1. Clone the repository to your local machine:

    git clone git+https://github.com/jnjnnjzch/kiana_aligner.git
    
  2. Enter the project root directory:

    cd kiana_aligner
    
  3. Install in editable mode:

    pip install -e .
    

🚀 Quick Start

Let's experience the core power of kiana in 5 minutes with a complete, runnable example: integrating two heterogeneous behavioral datasets (mock experiment logs and mock motion capture events) and aligning them with electrophysiological data.

import numpy as np
import pandas as pd

# Assuming kiana is installed via pip
from kiana import BehavioralProcessor, DataFrameLoader 

# --- 1. Prepare Mock Data (In reality, these come from your files) ---

# a) Mock behavioral logs from a .mat file (Loaded as DataFrame)
#    Contains TrialID and BehavioralCode info
mock_mat_events = pd.DataFrame({
    'EventTime': [10.1, 15.2, 19.8, 30.5, 35.8, 39.9],
    'BehavioralCode': [19, 45, 9, 19, 45, 9],
    'TrialID': [1, 1, 1, 2, 2, 2]
})

# b) Mock motion capture events from a camera (Timestamps only)
mock_motion_events = pd.DataFrame({
    'EventTime': [12.5, 33.1]
})

# c) Mock results from a single Ephys Controller after EphysProcessor
#    Contains timestamps in seconds (times) and indices in sample points (indices)
mock_ephys_data = {
    'times': np.array([10.0, 20.0, 30.0, 40.0]),
    'indices': np.array([300000, 600000, 900000, 1200000])
}


# --- 2. Initialize Processor and Add Segments using "Recipe" Mode ---

# Instantiate the processor
bhv_proc = BehavioralProcessor()

# Add the first segment: From behavioral logs, specifying anchors
bhv_proc.add_segment(
    segment_name='TrialLog',
    loader=DataFrameLoader(trial_id_col='TrialID'), # Tell loader which column is TrialID
    source=mock_mat_events
).with_anchors("BehavioralCode == 19") # Use Behavioral Code 19 as the "Anchor" for ephys alignment

# Add the second segment: From motion capture, all events are anchors
bhv_proc.add_segment(
    segment_name='MotionCapture',
    loader=DataFrameLoader(),
    source=mock_motion_events
) # Without .with_anchors(), all events in this segment are anchors by default

# Execute build to integrate all behavioral segments
bhv_proc.build()


# --- 3. Add Sync Context (Align Behavioral Data to Ephys) ---

bhv_proc.add_sync_context(
    context_name='A1', # Name for this ephys channel/probe
    ephys_times=mock_ephys_data['times'],
    ephys_indices=mock_ephys_data['indices'],
    sampling_rate=30000
)

# --- 4. Get and Display the Final Aligned DataFrame ---

final_df = bhv_proc.get_final_dataframe()

print("🎉 Kiana alignment complete! Final Event Timeline:")
# Selecting key columns for better display
display_cols = ['segment_name', 'EventTime', 'BehavioralCode', 
                'TrialID', 'is_anchor', 'EphysTime_A1', 'EphysIndice_A1']
print(final_df[display_cols])

📖 Understanding the Output

After running the code above, you will get a pandas DataFrame integrating all information. Please pay special attention to the last few columns:

  • EphysTime_[controller_name] (e.g., EphysTime_A1): This is one of the most critical columns. It represents the precise time (in seconds) of each behavioral event on the aligned electrophysiology timeline. Any subsequent analysis requiring timing comparison with neural signals should use this column.

  • EphysIndice_[controller_name] (e.g., EphysIndice_A1): This is the most precise and reliable column. It maps the aligned event time to the sample point index in the electrophysiology recording file. If you need to extract event-related neural signal fragments (Spike or LFP) from raw waveform data, please use this column as your "Gold Standard".

  • AbsoluteDateTime (Inferred): kiana also calculates an approximate real-world calendar time for each event. Note: Due to clock drift between different device systems, this time is for reference only to help you quickly locate the approximate experiment period. Do not use it for any precise scientific analysis.

This optimized example truly reflects the toolkit's powerful capabilities in handling heterogeneous data, anchor alignment, and multi-context synchronization, allowing any new user to immediately grasp the core value of kiana.

🤝 Contributing

We welcome contributions in any form! Whether it's submitting bug reports, suggesting new features, or directly contributing code. Please feel free to open an issue or pull request on our GitHub page.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiana-1.0.0b2.tar.gz (265.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kiana-1.0.0b2-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file kiana-1.0.0b2.tar.gz.

File metadata

  • Download URL: kiana-1.0.0b2.tar.gz
  • Upload date:
  • Size: 265.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiana-1.0.0b2.tar.gz
Algorithm Hash digest
SHA256 5fbc70f203dffbf5aa017c7eaaf6e753623f42c206b4061231d25eabc52212aa
MD5 64f5637a7d268c161400f6b52632b93f
BLAKE2b-256 ece4702e50d4db9fa6e9cdf05bafd562c526c7a191a43fc9e190dab8bb403482

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiana-1.0.0b2.tar.gz:

Publisher: publish-to-pypi.yml on jnjnnjzch/kiana_aligner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kiana-1.0.0b2-py3-none-any.whl.

File metadata

  • Download URL: kiana-1.0.0b2-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiana-1.0.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1158f39f2bae86f1c21943056f93af71b30b08b087926b62612cfdac8caeae8
MD5 1d783c4656d2602bce4354d67b8f721e
BLAKE2b-256 6a7cbf6923adbb8a8813fae2861191b1e0df99d8cb9c7880709ef2913407ba4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiana-1.0.0b2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on jnjnnjzch/kiana_aligner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page