holgern / pyedflib

pyedflib is a python library to read/write EDF+/BDF+ files based on EDFlib.
http://pyedflib.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
223 stars 124 forks source link

OSError: Unknown error while calling blockWriteSamples: -26 #253

Closed NikolayGromov closed 6 months ago

NikolayGromov commented 7 months ago

Hi! I am trying to rewrite annotations in .edf file, but got this error, when trying to copy signals. Annotations are correct, I checked them in EdfBrowser. Could you please, help me, may be I didn't fill something?

f is a valid edf file, which is opened, using edfReader. Снимок экрана 2024-04-26 202449

skjerns commented 7 months ago

have you tried writing the samples beofre the annotations?

NikolayGromov commented 7 months ago

Yes, same error

skjerns commented 7 months ago

One error I see is

setDatarecorduration is set with FileDuration

Those are different things. One denotes the block size in which the EDF is written, one the length of the entire file (=all blocks).

Don't think you need to set FileDuration explicitly, it is inferedd automatically.

If that doesn't fix your problem I need the sample code and all variables to reproduce the problem on my pc.

NikolayGromov commented 7 months ago

Now I didn't set duration, got this error image

I am using this code to reproduce the problem:

import datetime
import pyedflib
import numpy as np

header = {'technician': '',
 'recording_additional': 'Neuroscope_6.3_r2497',
 'patientname': '1 A',
 'patient_additional': '',
 'patientcode': '',
 'equipment': '',
 'admincode': '',
 'sex': 'Male',
 'startdate': datetime.datetime(2024, 1, 28, 21, 54, 45, 91798),
 'birthdate': '',
 'gender': ''}

signal_header = {'label': 'EEG Fp1-F3',
 'dimension': 'uV',
 'sample_rate': 199.50274692224406,
 'sample_frequency': 199.50274692224406,
 'physical_max': 3276.7,
 'physical_min': -3276.8,
 'digital_max': 32767,
 'digital_min': -32768,
 'prefilter': 'HP:0.5Hz LP:70.0Hz N:on',
 'transducer': 'AgAgCl electrode'}

signals = np.zeros((20, 1551205))

annotations = [[6000.0, 6002.105234170855], [-1.0, 0.0451121608040201], ['StartPredictionTime', 'ModelPrediction']]

fw = pyedflib.EdfWriter('error.edf', 20, file_type=1)

fw.setHeader(header)

for i in range(20):
    fw.setSignalHeader(i, signal_header)

for i in range(len(annotations[0])):    
    fw.writeAnnotation(annotations[0][i], annotations[1][i], annotations[2][i])

fw.writeSamples(signals)
fw.close()
skjerns commented 7 months ago

try setting sample_frequency=199.5 instead of that odd number. If you really need this millisecond sample-frequency, you need to find a float block_size s between 1.0-60.0 such that s*sample_frequency results in an integer, because unfortunately that's how the edf specs work :-/ PS: You don't need to set sample_rate

NikolayGromov commented 6 months ago

Yes, this error was because of sample_frequency (and I must also set sample_rate to ''). And yes, i need more precise frequency, but for the problem in this topic - it's done. Thank you!