You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to convert csv files into nwbs. The csv files become TimeInterval objects. I'm planning to link the nwb's I make back to the data they are derived from (hence all the fields being labelled 'refer to original file').
However, everytime I try to write the nwbs to file I get this error:
The nwbfile variable seems to have the data I would like:
I'm assuming there is an issue with how I'm writing the csv files to time intervals? Any help would be appreciated. Reduced version of the code I'm using is below:
def create_nwb_file(session_description, experimenter, lab, institution, experiment_description, session_id):
return NWBFile(
session_description=session_description,
identifier=session_id, # Use session ID as the identifier
session_start_time=datetime.now(tzlocal()), # TODO: replace with actual session start time
experimenter=experimenter,
lab=lab,
institution=institution,
experiment_description=experiment_description,
session_id=session_id,
)
def add_intervals_from_csv(nwbfile, df, name, description, probe_id, channel_id, additional_columns=None):
# Create a TimeIntervals object
time_intervals = TimeIntervals(
name=name,
description=description,
)
# Add columns for probe and channel IDs
time_intervals.add_column(name="probe_id", description="ID of the probe")
time_intervals.add_column(name="channel_id", description="ID of the channel")
# Add additional columns if provided
if additional_columns:
for col_name, col_desc in additional_columns.items():
time_intervals.add_column(name=col_name, description=col_desc)
# Add rows to the TimeIntervals object
for _, row in df.iterrows():
if additional_columns is not None:
kwargs = {col: row[col] for col in df.columns if col in additional_columns}
else:
kwargs = {}
time_intervals.add_row(
start_time=row['start_time'],
stop_time=row['end_time'],
probe_id=probe_id,
channel_id=channel_id,
**kwargs,
)
return time_intervals
session_id = '1234'
probe_id = '5678'
channel_id = '91011'
nwbfile = create_nwb_file(session_description, experimenter, lab, institution, experiment_description, session_id)
# Generate a random number of rows between 200 and 800
num_rows = np.random.randint(200, 801)
# Generate data for the dataframe
toy_data = {
'event_number': np.arange(1, num_rows + 1),
'start_time': np.random.uniform(0, 4000, num_rows),
'end_time': np.random.uniform(0, 4000, num_rows),
'duration': np.random.uniform(0, 1, num_rows),
'mean_zscore': np.random.uniform(0, 10, num_rows),
'median_zscore': np.random.uniform(0, 2, num_rows),
'max_zscore': np.random.uniform(0, 10, num_rows),
'min_zscore': np.random.uniform(0, 1, num_rows)
}
# Create the dataframe
toy_df = pd.DataFrame(toy_data)
probe_module = nwbfile.create_processing_module(name="probe_" + probe_id, description='Probe events')
csv_module = add_intervals_from_csv(nwbfile, toy_df,
name = 'movement_artifacts_channel_' + channel_id,
description="Motion artifact events",
probe_id=probe_id,
channel_id=channel_id,
additional_columns={
'mean_zscore': "Mean z-score of the event",
'median_zscore': "Median z-score of the event",
'max_zscore': "Max z-score of the event",
'min_zscore': "Min z-score of the event",
})
probe_module.add_data_interface(csv_module)
try:
# Save the NWB file
with NWBHDF5IO('test_nwb.nwb', 'w') as io:
io.write(nwbfile)
print("Saved NWB file for " + str(session_id))
except Exception as e:
print(e)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm trying to convert csv files into nwbs. The csv files become TimeInterval objects. I'm planning to link the nwb's I make back to the data they are derived from (hence all the fields being labelled 'refer to original file').
However, everytime I try to write the nwbs to file I get this error:
The nwbfile variable seems to have the data I would like:
I'm assuming there is an issue with how I'm writing the csv files to time intervals? Any help would be appreciated. Reduced version of the code I'm using is below:
Beta Was this translation helpful? Give feedback.
All reactions