-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
242 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "55e69c20-7b7e-449d-8740-07ea2079949c", | ||
"metadata": {}, | ||
"source": [ | ||
"# Extract nwb file contents and save as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "fbe7cf35-bc99-421d-bb1e-bc0938310a9a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pynwb import NWBHDF5IO\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"# Function to extract data from a TimeSeries object\n", | ||
"def extract_data(time_series):\n", | ||
" return {\n", | ||
" 'data': np.array(time_series.data[:]),\n", | ||
" 'timestamps': np.array(time_series.timestamps[:]) if time_series.timestamps else None,\n", | ||
" 'unit': time_series.unit,\n", | ||
" 'comments': time_series.comments\n", | ||
" }\n", | ||
"\n", | ||
"# Replace with the path to your .nwb file\n", | ||
"nwb_file_path = 'data1.nwb'\n", | ||
"\n", | ||
"# Open the .nwb file using PyNWB\n", | ||
"with NWBHDF5IO(nwb_file_path, 'r') as io:\n", | ||
" nwbfile = io.read()\n", | ||
"\n", | ||
" # Extract acquisition groups\n", | ||
" acquisition_data = {}\n", | ||
" for name, timeseries in nwbfile.acquisition.items():\n", | ||
" acquisition_data[name] = extract_data(timeseries)\n", | ||
"\n", | ||
" # Extract stimulus groups if present\n", | ||
" stimulus_data = {}\n", | ||
" if hasattr(nwbfile, 'stimulus'):\n", | ||
" for name, timeseries in nwbfile.stimulus.items():\n", | ||
" stimulus_data[name] = extract_data(timeseries)\n", | ||
"\n", | ||
"# Save the extracted data to .npy files\n", | ||
"for name, data in acquisition_data.items():\n", | ||
" np.save(f'{name}_acquisition.npy', data['data'])\n", | ||
"\n", | ||
"for name, data in stimulus_data.items():\n", | ||
" np.save(f'{name}_stimulus.npy', data['data'])\n", | ||
"\n", | ||
"print(\"Data extraction and saving completed.\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c22c1249-44b2-4afb-8685-61ea9f2b8ad5", | ||
"metadata": {}, | ||
"source": [ | ||
"# Print contents" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a300dca3-9b37-461b-ad1a-9014a9a17fef", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pynwb import NWBHDF5IO\n", | ||
"\n", | ||
"# Replace with the path to your .nwb file\n", | ||
"nwb_file_path = 'data1.nwb'\n", | ||
"\n", | ||
"# Function to print details of a TimeSeries object\n", | ||
"def print_timeseries_details(name, timeseries):\n", | ||
" print(f\"\\n{name}:\")\n", | ||
" print(f\" - Comments: {timeseries.comments}\")\n", | ||
" print(f\" - Description: {timeseries.description}\")\n", | ||
" print(f\" - Unit: {timeseries.unit}\")\n", | ||
" print(f\" - Data shape: {timeseries.data.shape}\")\n", | ||
" print(f\" - Timestamps shape: {timeseries.timestamps.shape if timeseries.timestamps else 'No timestamps'}\")\n", | ||
"\n", | ||
"# Open the .nwb file using PyNWB\n", | ||
"with NWBHDF5IO(nwb_file_path, 'r') as io:\n", | ||
" nwbfile = io.read()\n", | ||
"\n", | ||
" # Access and print details of the acquisition group\n", | ||
" print(\"Acquisition Groups:\")\n", | ||
" acquisition = nwbfile.acquisition\n", | ||
" for name, timeseries in acquisition.items():\n", | ||
" print_timeseries_details(name, timeseries)\n", | ||
"\n", | ||
" # Access and print details of the stimulus group\n", | ||
" if hasattr(nwbfile, 'stimulus'):\n", | ||
" print(\"\\nStimulus Groups:\")\n", | ||
" for name, timeseries in nwbfile.stimulus.items():\n", | ||
" print_timeseries_details(name, timeseries)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ded5da77-78b1-423a-b9c0-b9fa3f1ea4de", | ||
"metadata": {}, | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6a92f750-5b15-4deb-a902-583900418978", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from pynwb import NWBHDF5IO\n", | ||
"\n", | ||
"# Function to extract data from a TimeSeries object\n", | ||
"def extract_data(time_series):\n", | ||
" return {\n", | ||
" 'data': np.array(time_series.data[:]),\n", | ||
" 'timestamps': np.array(time_series.timestamps[:]) if time_series.timestamps else None,\n", | ||
" 'unit': time_series.unit,\n", | ||
" 'comments': time_series.comments\n", | ||
" }\n", | ||
"\n", | ||
"# Path to your NWB file\n", | ||
"nwb_file_path = 'data1.nwb'\n", | ||
"\n", | ||
"# Open the .nwb file using PyNWB\n", | ||
"with NWBHDF5IO(nwb_file_path, 'r') as io:\n", | ||
" nwbfile = io.read()\n", | ||
"\n", | ||
" # Extract acquisition groups\n", | ||
" acquisition_data = {}\n", | ||
" for name, timeseries in nwbfile.acquisition.items():\n", | ||
" acquisition_data[name] = extract_data(timeseries)\n", | ||
"\n", | ||
" # Extract stimulus groups if present\n", | ||
" stimulus_data = {}\n", | ||
" if hasattr(nwbfile, 'stimulus'):\n", | ||
" for name, timeseries in nwbfile.stimulus.items():\n", | ||
" stimulus_data[name] = extract_data(timeseries)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.