Generated banner for Nwb Conversion
Generated logo for Nwb Conversion

Nwb Conversion

K-DenseCommunityCreated by K-Dense Inc.

  • Skill
  • Workflow
  • Science and research
  • MIT
  • Low risk
  • Has scripts

From the author

Converts neuroscience acquisition data to Neurodata Without Borders files with NeuroConv and PyNWB, preserves metadata and timebases, checks evidence-based clock alignment, and produces schema validation, NWB Inspector findings and round-trip checks. Use for NWB conversion and synchronization of planar single-channel two-photon TIFF imaging plus timestamped behavioral position CSV; this skill does not perform spike sorting or claim tested support for arbitrary acquisition formats.
— the description by K-Dense Inc.

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a structured workflow for converting single-channel two-photon TIFF imaging and timestamped behavioral position CSV data into the Neurodata Without Borders (NWB) format. It emphasizes data integrity by requiring explicit metadata, performing evidence-based clock synchronization, and executing rigorous post-conversion validation using NWB Inspector and schema checks. The workflow is designed for researchers who need to standardize their acquisition data while maintaining strict provenance and temporal accuracy.

What it does

  • Converts grayscale two-photon TIFF files into NWB Imaging series.
  • Integrates behavioral position CSV data into NWB SpatialSeries.
  • Performs affine clock synchronization between imaging and behavioral streams.
  • Validates output files against NWB schema and Inspector rules.
  • Conducts round-trip checks to verify data and metadata integrity.
  • Generates a comprehensive validation report alongside the NWB file.

When to use it

  • When you need to convert planar, single-channel two-photon TIFF data to NWB.
  • When you have timestamped behavioral position data that requires synchronization with imaging.
  • When you need to ensure your converted data meets NWB schema and quality standards.
  • When you require a documented, reproducible conversion process with provenance tracking.

Advantages

  • Ensures temporal alignment through explicit, evidence-based clock mapping.
  • Provides automated schema and quality validation for every conversion.
  • Maintains strict metadata requirements to prevent data ambiguity.
  • Includes round-trip verification to confirm pixel and timestamp accuracy.

Usage examples

“Convert my two-photon imaging session using the provided template.”
The agent will guide you through populating the session JSON, verifying your TIFF and CSV inputs, and executing the conversion script to produce a validated NWB file.
“How do I synchronize my behavior CSV with my imaging timestamps?”
The agent will explain how to provide either shared clock evidence or matched pulse pairs in your configuration to enable the affine clock transformation.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/k-dense-ai/nwb-conversion/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip nwb-conversion.zip -d .agents/skills

Documentation by K-Dense Inc.

Validated NWB conversion

Supported streams

The executable workflow covers two explicit input streams in one session:

InputNWB representationTested constraints
Two-photon grayscale multi-page TIFF + frame timestamps CSVAcquisition TwoPhotonSeries named Imaging through NeuroConvOne channel, one plane, one 2D image per page, fixed shape and dtype
Calibrated position CSV (time_s,x,y)Behavior Position / SpatialSeries through PyNWBCoordinates in m, cm or mm; converted to meters without temporal resampling

Other acquisition readers require their own format-specific tests. In particular, this helper does not decode SpikeGLX, Open Ephys, multichannel TIFF, volumetric TIFF, compressed video, or pixel-to-world calibration. Do not rename an arbitrary numeric table to a supported stream.

Install the tested environment

uv venv --python 3.12 nwb-env
uv pip install --python nwb-env/bin/python 'neuroconv[tiff]==0.10.2' pynwb==4.2.0 \
  nwbinspector==0.7.2 roiextractors==0.10.0 tifffile==2026.9.20 \
  zarr==2.18.7 hdmf-zarr==0.11.3

Keep both Zarr pins even for an HDF5-only conversion: NeuroConv 0.10.2 imports its backend configuration modules at startup, and the tested unconstrained Zarr 3.4.0 installation failed on zarr.codec_registry. The pinned environment ran the real conversion, PyNWB validation and Inspector successfully on macOS ARM64. The dependency resolver supplies NumPy and HDF5 support. These are compatibility pins, not claims that Zarr 2 and hdmf-zarr 0.11.3 are the latest releases. Current interface checks and the tested dependency exception are recorded in references/upstream-review.md.

Workflow

  1. Inventory the actual inputs and acquisition metadata. Identify image plane/channel, optical settings, subject/session identifiers, timezone, behavior coordinate system, units and the timestamp clock for every stream. Preserve originals. Do not replace missing metadata with plausible defaults from a sample config.

  2. Copy assets/session-template.json beside the raw data and replace the explicitly synthetic values. Paths resolve from that JSON file. Read references/input-contract.md for the exact CSV and metadata contract and the pulse-pair variant. TIFF pixels are retained as acquired; a raw arbitrary-unit intensity does not become a photon count merely by changing its unit label.

  3. Establish the common timebase from acquisition evidence. Frame timestamps must already be reference-clock seconds since the timezone-aware session start. For position, provide either a documented shared clock or matched synchronization pulses. The helper fits one affine clock transform, checks its residual against a specified tolerance, and refuses extrapolation beyond the pulse range. It never estimates synchronization from coincident-looking neural/behavioral signals. Clock resets or nonlinear drift require an explicitly validated piecewise mapping.

  4. Execute the converter. Inputs must have finite, strictly increasing timestamps and matching image/timestamp counts. Explicitly declare one channel and one plane; known TIFF channel/plane metadata must agree. Grayscale pages alone cannot exclude undocumented interleaving. The acquisition samples stay intact; only coordinate units and, when evidenced, behavior timestamps are transformed.

  5. Read the .validation.json alongside the NWB file. Schema compliance, Inspector findings and data equality answer different questions. The script exits with an error for schema failures and flags critical Inspector findings for review in the report. Review all findings in context; successful validation cannot establish that anatomical labels, pulse pairing or calibration supplied by the user are correct.

  6. Deliver the NWB, validation JSON, original conversion config and an explanation of remaining metadata gaps or Inspector findings. No upload or archive submission is part of this workflow.

Execute

Run the following from the skill directory, with paths to the actual analysis files:

nwb-env/bin/python scripts/convert_session.py /path/to/session.json --output /path/to/session.nwb

nwb-env must point to the environment created above; the absolute example input paths are illustrative. The command requires a .nwb output and refuses to overwrite an existing NWB or validation report. Output contains source and converter checksums, package versions, full supplied metadata, units and clock-fit provenance in both a scratch record and the validation report. When adapting this command for large data, TIFF writes are iterative and equality checking loads one frame at a time; position CSV currently loads into memory. Round-trip checks also verify dtype, unit scaling, optical-channel links, subject metadata, position reference frame, common time origin and embedded provenance. Inspector findings requiring review appear in the CLI summary; exit zero alone does not mean the file is scientifically correct. An exception during writing or round-trip checks can leave an incomplete NWB without a report; retain the error and use a fresh output path after correcting the cause.

The real-library test converts eight non-square uint16 images with irregular frame timing plus four position samples, asserts exact pixel and timestamp round trips, and checks centimeter-to-meter conversion. A second integration test recovers a known 1000-ppm clock drift and 50-ms offset from three matched pulses. Duplicate timestamps, mismatched frame counts, absent clock evidence, nonlinear pulse disagreement and missing timezone are rejection cases. The mapping is TIFF (time,y,x) to NWB (time,x,y), explicitly checked against every transposed source page. NWB Inspector flags the short fixture with a critical orientation heuristic because width exceeds frame count; the report retains that finding and adds the exact frame/timestamp equality evidence. No transpose is performed merely to satisfy a longest-axis heuristic.

Primary references

Files

scripts/convert_session.py

"""Convert planar TIFF imaging and timestamped position CSV to validated NWB."""
from __future__ import annotations

import argparse
import csv
from datetime import datetime
import hashlib
import importlib.metadata
import json
from pathlib import Path


def digest(path):
    checksum = hashlib.sha256()
    with Path(path).open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            checksum.update(chunk)
    return checksum.hexdigest()


def read_numeric_csv(path, columns):
    import numpy as np
    with Path(path).open(newline="") as handle:
        reader = csv.DictReader(handle)
        if reader.fieldnames != columns:
            raise ValueError(f"{path}: expected exactly {columns}")
        try:
            rows = []
            for row in reader:
                if None in row or any(row[k] is None for k in columns):
                    raise ValueError("Row width differs from the header")
                rows.append([float(row[k]) for k in columns])
            values = np.asarray(rows, dtype=float)
        except (TypeError, ValueError) as error:
            raise ValueError(f"{path}: nonnumeric or missing data") from error
    if values.ndim != 2 or len(values) < 2 or not np.isfinite(values).all():
        raise ValueError(f"{path}: need at least two finite rows")
    if (np.diff(values[:, 0]) <= 0).any():
        raise ValueError(f"{path}: timestamps must be strictly increasing")
    return values


def align_behavior(times, synchronization, base):
    import numpy as np
    times = np.asarray(times, dtype=float)
    if times.ndim != 1 or len(times) < 2 or not np.isfinite(times).all() or (np.diff(times) <= 0).any():
        raise ValueError("Behavior timestamps must be finite and strictly increasing")
    shared = synchronization.get("shared_clock_evidence")
    pulses = synchronization.get("pulse_pairs_csv")
    if bool(shared) == bool(pulses):
        raise ValueError("Supply either shared_clock_evidence or pulse_pairs_csv")
    if shared:
        if not isinstance(shared, str) or not shared.strip():
            raise ValueError("Shared-clock evidence must be a nonempty description")
        return times.copy(), {"method": "shared_clock", "evidence": shared, "slope": 1., "offset_s": 0.}
    pairs = read_numeric_csv(base / pulses, ["device_time_s", "reference_time_s"])
    if len(pairs) < 3 or (np.diff(pairs[:, 1]) <= 0).any():
        raise ValueError("At least three ordered, matched pulse pairs are required")
    tolerance = float(synchronization["max_residual_s"])
    if not np.isfinite(tolerance) or tolerance <= 0:
        raise ValueError("max_residual_s must be finite and positive")
    # Center the device clock to avoid fitting against a large absolute origin.
    device_origin, reference_origin = pairs[0]
    slope, centered_offset = np.polyfit(pairs[:, 0] - device_origin, pairs[:, 1] - reference_origin, 1)
    offset = reference_origin + centered_offset - slope * device_origin
    residuals = slope * (pairs[:, 0] - device_origin) + centered_offset - (pairs[:, 1] - reference_origin)
    residual = float(np.max(np.abs(residuals)))
    if not np.isfinite([slope, offset, residual]).all() or slope <= 0 or residual > tolerance:
        raise ValueError(f"Clock fit failed: maximum residual {residual:g} s")
    if times[0] < pairs[0, 0] or times[-1] > pairs[-1, 0]:
        raise ValueError("Behavior times exceed pulse support; affine extrapolation is not validated")
    aligned = slope * (times - device_origin) + centered_offset + reference_origin
    if not np.isfinite(aligned).all() or (np.diff(aligned) <= 0).any():
        raise ValueError("Clock mapping produced invalid or indistinguishable timestamps")
    return aligned, {"method": "matched_pulse_affine", "slope": float(slope),
           "offset_s": float(offset), "max_residual_s": residual, "tolerance_s": tolerance,
           "device_origin_s": float(device_origin), "reference_origin_s": float(reference_origin),
           "centered_offset_s": float(centered_offset), "pulse_residuals_s": residuals.tolist(),
           "device_support_s": pairs[[0, -1], 0].tolist(), "reference_support_s": pairs[[0, -1], 1].tolist(),
           "pulse_pairs": len(pairs), "pulse_sha256": digest(base / pulses)}


def convert(config_path, output):
    import numpy as np
    import tifffile
    from neuroconv.datainterfaces import TiffImagingInterface
    from pynwb import NWBFile, NWBHDF5IO, validate
    from pynwb.behavior import Position, SpatialSeries
    from pynwb.file import Subject
    from nwbinspector import inspect_nwbfile

    config_path, output = Path(config_path), Path(output)
    config = json.loads(config_path.read_text())
    base = config_path.resolve().parent
    if output.suffix.lower() != ".nwb":
        raise ValueError("Output must use the .nwb extension")
    report_path = output.with_suffix(".validation.json")
    for path in (output, report_path):
        if path.exists():
            raise FileExistsError(f"Refusing to overwrite {path}")
    start = datetime.fromisoformat(config["session_start_time"])
    if start.utcoffset() is None:
        raise ValueError("session_start_time needs an explicit timezone")
    for field in ("identifier", "session_description", "institution", "lab", "position_reference_frame", "position_description"):
        if not isinstance(config.get(field), str) or not config[field].strip():
            raise ValueError(f"Missing {field}")
    if not isinstance(config.get("experimenter"), list) or not config["experimenter"] or any(
        not isinstance(name, str) or not name.strip() for name in config["experimenter"]
    ):
        raise ValueError("experimenter must be a nonempty list of names")
    optics = config["imaging"]
    for key in ("num_channels", "num_planes"):
        if type(optics.get(key)) is not int or optics[key] != 1:
            raise ValueError(f"imaging.{key} must explicitly equal 1; interleaved channels/planes are unsupported")
    tiff_path = base / config["tiff"]
    frame_times = read_numeric_csv(base / config["frame_times_csv"], ["time_s"])[:, 0]
    behavior = read_numeric_csv(base / config["position_csv"], ["time_s", "x", "y"])
    with tifffile.TiffFile(tiff_path) as tiff:
        if len(tiff.pages) != len(frame_times) or len(tiff.pages[0].shape) != 2:
            raise ValueError("Expected one grayscale 2D TIFF page per frame timestamp")
        shape, dtype = tiff.pages[0].shape, tiff.pages[0].dtype
        if any(p.shape != shape or p.dtype != dtype for p in tiff.pages):
            raise ValueError("TIFF pages differ in shape or dtype")
        if any(p.samplesperpixel != 1 or p.photometric not in (0, 1) for p in tiff.pages):
            raise ValueError("TIFF pages must contain grayscale intensity samples")
        if len(tiff.series) != 1 or any(
            size > 1 and axis in "CZS" for axis, size in zip(tiff.series[0].axes, tiff.series[0].shape)
        ):
            raise ValueError("TIFF metadata identifies multiple series, channels, samples or depth planes")
    position_units = {"m": 1., "cm": .01, "mm": .001}
    if config["position_unit"] not in position_units:
        raise ValueError("Position unit must be m, cm or mm; pixels require calibrated coordinates")
    position_m = behavior[:, 1:] * position_units[config["position_unit"]]
    aligned, alignment = align_behavior(behavior[:, 0], config["synchronization"], base)
    if min(frame_times[0], aligned[0]) < 0:
        raise ValueError("This converter requires nonnegative seconds since session_start_time")
    if optics.get("modality") != "two-photon":
        raise ValueError("imaging.modality must explicitly be two-photon for this converter")
    for key in ("device", "device_description", "description", "indicator", "location", "unit", "optical_channel_description"):
        if not isinstance(optics.get(key), str) or not optics[key].strip():
            raise ValueError(f"Missing imaging.{key}; do not invent acquisition metadata")
    for key in ("excitation_nm", "emission_nm"):
        if not np.isfinite(float(optics[key])) or float(optics[key]) <= 0:
            raise ValueError(f"imaging.{key} must be positive and finite")
    subject_metadata = dict(config["subject"])
    if "date_of_birth" in subject_metadata:
        birth = datetime.fromisoformat(subject_metadata["date_of_birth"])
        if birth.utcoffset() is None:
            raise ValueError("subject.date_of_birth needs an explicit timezone")
        subject_metadata["date_of_birth"] = birth
    nwb = NWBFile(session_description=config["session_description"], identifier=config["identifier"],
                  session_start_time=start, timestamps_reference_time=start, experimenter=config["experimenter"],
                  institution=config["institution"], lab=config["lab"], subject=Subject(**subject_metadata))
    interface = TiffImagingInterface(file_paths=[str(tiff_path)], sampling_frequency=float(1 / np.median(np.diff(frame_times))),
                                     num_channels=1, num_planes=1, metadata_key="imaging", verbose=False)
    interface.set_aligned_timestamps(aligned_timestamps=frame_times)
    metadata = interface.get_metadata()
    metadata["Devices"] = {"microscope": {"name": optics["device"], "description": optics["device_description"]}}
    metadata["Ophys"]["ImagingPlanes"] = {"plane": {
        "name": "ImagingPlane", "description": optics["description"], "device_metadata_key": "microscope",
        "excitation_lambda": float(optics["excitation_nm"]), "indicator": optics["indicator"], "location": optics["location"],
        "optical_channel": [{"name": "channel", "description": optics["optical_channel_description"],
                             "emission_lambda": float(optics["emission_nm"])}]}}
    metadata["Ophys"]["MicroscopySeries"]["imaging"].update(
        name="Imaging", description=optics["description"], unit=optics["unit"], imaging_plane_metadata_key="plane")
    interface.add_to_nwbfile(nwbfile=nwb, metadata=metadata, always_write_timestamps=True)
    series = SpatialSeries(name="Position", data=position_m, timestamps=aligned,
                           unit="meters", reference_frame=config["position_reference_frame"],
                           description=f"Source coordinates in {config['position_unit']}; converted to meters. {config['position_description']}")
    nwb.create_processing_module(name="behavior", description="Measured animal position").add(Position(spatial_series=series))
    provenance = {"source_sha256": {key: digest(base / config[key]) for key in ("tiff", "frame_times_csv", "position_csv")},
                  "config_sha256": digest(config_path), "configuration": config, "alignment": alignment,
                  "converter_sha256": digest(__file__), "timestamps_reference_time": start.isoformat(),
                  "axis_mapping": "TIFF (time,y,x) -> NWB (time,x,y)",
                  "versions": {name: importlib.metadata.version(name) for name in ("neuroconv", "pynwb", "nwbinspector", "roiextractors", "tifffile", "zarr", "hdmf-zarr")}}
    nwb.add_scratch(json.dumps(provenance), name="conversion_provenance", description="Source metadata, checksums and clock mapping")
    output.parent.mkdir(parents=True, exist_ok=True)
    with NWBHDF5IO(str(output), "w-") as io:
        io.write(nwb)
    errors = [str(error) for error in validate(path=str(output))]
    findings = []
    for message in inspect_nwbfile(output, skip_validate=True):
        if message is not None:
            findings.append({"importance": message.importance.name, "check": message.check_function_name,
                             "message": message.message, "location": message.location})
    with NWBHDF5IO(str(output), "r") as io, tifffile.TiffFile(tiff_path) as tiff:
        restored = io.read()
        image = restored.acquisition["Imaging"]
        np.testing.assert_array_equal(image.timestamps[:], frame_times)
        for index, page in enumerate(tiff.pages):
            np.testing.assert_array_equal(image.data[index], page.asarray().T)
        restored_position = restored.processing["behavior"]["Position"]["Position"]
        np.testing.assert_array_equal(restored_position.data[:], position_m)
        np.testing.assert_array_equal(restored_position.timestamps[:], aligned)
        if image.unit != optics["unit"] or restored_position.unit != "meters":
            raise ValueError("Units changed during round trip")
        if image.data.dtype != dtype or any(s.conversion != 1.0 or s.offset != 0.0 for s in (image, restored_position)):
            raise ValueError("Data dtype or unit scaling changed during round trip")
        plane = image.imaging_plane
        if (plane.device.name != optics["device"] or plane.location != optics["location"]
            or plane.device.description != optics["device_description"] or plane.description != optics["description"]
            or plane.indicator != optics["indicator"] or plane.excitation_lambda != float(optics["excitation_nm"])
            or len(plane.optical_channel) != 1
            or plane.optical_channel[0].description != optics["optical_channel_description"]
            or plane.optical_channel[0].emission_lambda != float(optics["emission_nm"])):
            raise ValueError("Optical metadata changed during round trip")
        if restored_position.reference_frame != config["position_reference_frame"]:
            raise ValueError("Position reference frame changed during round trip")
        if (restored.identifier != config["identifier"] or restored.session_start_time != start
            or restored.timestamps_reference_time != start):
            raise ValueError("Session metadata changed during round trip")
        for key in ("session_description", "institution", "lab"):
            if getattr(restored, key) != config[key]:
                raise ValueError(f"Session {key} changed during round trip")
        if list(restored.experimenter) != config["experimenter"]:
            raise ValueError("Experimenter metadata changed during round trip")
        for key in subject_metadata:
            if getattr(restored.subject, key) != getattr(nwb.subject, key):
                raise ValueError(f"Subject {key} changed during round trip")
        if json.loads(restored.scratch["conversion_provenance"].data) != provenance:
            raise ValueError("Conversion provenance changed during round trip")
    for finding in findings:
        if finding["check"] == "check_data_orientation" and finding["location"] == "/acquisition/Imaging":
            finding["review_evidence"] = "Time axis verified against all frame timestamps and every source TIFF page; short recordings can trigger the longest-axis heuristic"
    report = {**provenance, "schema_errors": errors, "inspector_findings": findings,
              "inspector_requires_review": any(f["importance"] in {"ERROR", "PYNWB_VALIDATION", "CRITICAL"} for f in findings),
              "roundtrip": {"all_frames_equal": True, "position_equal": True, "timestamps_equal": True,
                            "metadata_equal": True, "provenance_equal": True, "identity_unit_scaling": True,
                            "frame_count": len(frame_times), "position_samples": len(behavior)}}
    with report_path.open("x") as handle:
        handle.write(json.dumps(report, indent=2) + "\n")
    if errors:
        raise ValueError("NWB schema validation failed; inspect the validation JSON")
    return report


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("config", type=Path)
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args()
    report = convert(args.config, args.output)
    print(json.dumps({"output": str(args.output), "schema_errors": report["schema_errors"],
                      "inspector_requires_review": report["inspector_requires_review"],
                      "inspector_findings": len(report["inspector_findings"]), "roundtrip": report["roundtrip"]}, indent=2))


if __name__ == "__main__":
    main()

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Needs Python

The author says: Requires Python 3.12 with neuroconv[tiff] 0.10.2, PyNWB 4.2.0, NWB Inspector 0.7.2, roiextractors 0.10.0, tifffile 2026.9.20, zarr 2.18.7 and hdmf-zarr 0.11.3. Local HDF5 file access is required. Network is needed only for installation; no credentials.

Security

What our scanner noticed· Low risk

  • Instructions that use the network
  • Scripts that your agent may run

This skill includes scripts that your agent may run. Read them before you use it.

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
K-Dense Inc.
Publisher
K-Dense (Community)
Source
K-Dense-AI/scientific-agent-skills · skills/nwb-conversion · commit a25cf9e
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE.md)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by K-Dense Inc. or K-Dense.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download nwb-conversion.zip