Skip to content

Commit

Permalink
improved conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsyrup committed Nov 30, 2023
1 parent a0bdd66 commit 75ee06a
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "55e69c20-7b7e-449d-8740-07ea2079949c",
"metadata": {},
"source": [
"# Read contents of .nwb file"
"# Extract nwb file contents and save as np"
]
},
{
Expand All @@ -16,37 +16,96 @@
"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",
"\n",
"# Open the .nwb file using PyNWB\n",
"with NWBHDF5IO(nwb_file_path, 'r') as io:\n",
" nwbfile = io.read()\n",
"\n",
" # Access the acquisition group\n",
" acquisition = nwbfile.acquisition\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",
" # Print the names of all groups within the acquisition group\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(f\" - {name}: {timeseries}\")\n",
" \n",
" # Print all groups in stimulus (if any)\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, sgroup in nwbfile.stimulus.items():\n",
" print(f\" - {name}: {sgroup}\")"
" 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": [
"# Extract nwb file contents"
]
"source": []
},
{
"cell_type": "code",
Expand Down Expand Up @@ -85,74 +144,6 @@
" for name, timeseries in nwbfile.stimulus.items():\n",
" stimulus_data[name] = extract_data(timeseries)"
]
},
{
"cell_type": "markdown",
"id": "d5b99136-99ed-47a4-8097-c7d6c9ac6855",
"metadata": {},
"source": [
"# Save extracted contents of nwb file as numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8084e8ff-c96c-4981-adc3-05a7abcbfbf1",
"metadata": {},
"outputs": [],
"source": [
"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'])"
]
},
{
"cell_type": "markdown",
"id": "114a8f14-bfe7-49fa-a8f5-2b3f011e6d71",
"metadata": {},
"source": [
"# Save extracted contents of nwb file as dataframe csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6d160ab-6151-4e08-9fc5-60479f167514",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"for name, data in acquisition_data.items():\n",
" # Check if data is two-dimensional\n",
" if data['data'].ndim == 2:\n",
" # Create column names for each dimension\n",
" column_names = [f'{name}_dim_{i}' for i in range(data['data'].shape[1])]\n",
" else:\n",
" # For one-dimensional data, use a single column\n",
" column_names = [f'{name}_value']\n",
"\n",
" df = pd.DataFrame(data['data'], columns=column_names)\n",
" if data['timestamps'] is not None:\n",
" df['timestamps'] = data['timestamps']\n",
" df.to_csv(f'{name}_acquisition.csv', index=False)\n",
"\n",
"for name, data in stimulus_data.items():\n",
" # Check if data is two-dimensional\n",
" if data['data'].ndim == 2:\n",
" # Create column names for each dimension\n",
" column_names = [f'{name}_dim_{i}' for i in range(data['data'].shape[1])]\n",
" else:\n",
" # For one-dimensional data, use a single column\n",
" column_names = [f'{name}_value']\n",
"\n",
" df = pd.DataFrame(data['data'], columns=column_names)\n",
" if data['timestamps'] is not None:\n",
" df['timestamps'] = data['timestamps']\n",
" df.to_csv(f'{name}_stimulus.csv', index=False)"
]
}
],
"metadata": {
Expand Down
170 changes: 170 additions & 0 deletions convert_nwb_to_npy.ipynb
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
}
Loading

0 comments on commit 75ee06a

Please sign in to comment.