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
Oftentimes it is useful to crop/truncate the EDF file to specified start and/or stop times (e.g. time in bed). I suggest adding a new function pyedflib.highlevel.truncate_edf (or "crop_edf")
This is an initial working implementation tested on pyedflib 0.1.30
"""Function to crop an EDF file to specified start/end times."""frompathlibimportPathimportpyedflibaspedfimportdatetimeasdtdeftruncate_edf(path_edf, new_start=None, new_stop=None):
"""Truncate an EDF file to desired start/stop times. Parameters ---------- path_edf : str The path the the EDF file new_start : datetime.datetime The new start timestamp new_stop : datetime.datetime The new stop timestamp """assertisinstance(new_start, (dt.datetime, type(None)))
assertisinstance(new_stop, (dt.datetime, type(None)))
path_edf=Path(path_edf)
assertpath_edf.exists(), "File does not exist."# Open the original EDF fileedf=pedf.EdfReader(str(path_edf))
signals_headers=edf.getSignalHeaders()
header=edf.getHeader()
# Define start/stop in samplescurrent_start=edf.getStartdatetime()
ifnew_startisNone:
new_start=current_startstart_diff_seconds= (new_start-current_start).total_seconds()
assertcurrent_start<=new_startcurrent_stop=current_start+dt.timedelta(seconds=edf.getFileDuration())
current_duration=current_stop-current_startifnew_stopisNone:
new_stop=current_stopassertnew_stop<=current_stopstop_diff_from_start= (new_stop-current_start).total_seconds()
# Crop each signalsignals= []
foriinrange(len(edf.getSignalHeaders())):
sf=edf.getSampleFrequency(i)
start_idx=int(start_diff_seconds*sf)
stop_idx=int(stop_diff_from_start*sf)
signals.append(edf.readSignal(i, start=start_idx, n=stop_idx-start_idx))
edf.close()
# Update header startdate and save fileheader["startdate"] =new_startoutpath=str(path_edf).replace(".edf", "cropped.edf")
pedf.highlevel.write_edf(outpath, signals, signals_headers, header)
# Get new EDF start, stop and durationedf=pedf.EdfReader(outpath)
start=edf.getStartdatetime()
stop=start+dt.timedelta(seconds=edf.getFileDuration())
duration=stop-startedf.close()
# Verboseprint(f"Original: {current_start} to {current_stop} ({current_duration})")
print(f"Truncated: {start} to {stop} ({duration})")
print(f"Succesfully written file: {outpath}")
The text was updated successfully, but these errors were encountered:
Sure thing, I'll submit a PR in the new couple of weeks. Is there anything that you'd modify from the initial code that I sent? Are there general contributing guidelines for this library?
Hi,
Oftentimes it is useful to crop/truncate the EDF file to specified start and/or stop times (e.g. time in bed). I suggest adding a new function
pyedflib.highlevel.truncate_edf
(or "crop_edf")This is an initial working implementation tested on pyedflib 0.1.30
The text was updated successfully, but these errors were encountered: