Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edfwriter not copying arrays perfectly. Random elements, most do not differ, differ from the numpy array used to create edf file by exactly -0.0984668518066405 or 0.0984668518066405 every time. #254

Open
NathanTBurgess opened this issue Jun 12, 2024 · 2 comments

Comments

@NathanTBurgess
Copy link

NathanTBurgess commented Jun 12, 2024

edfwriter not copying arrays perfectly. Random elements, most do not differ, differ from the numpy array used to create edf file by exactly -0.0984668518066405 or 0.0984668518066405 every time.

Here is the most relevant function. Just know when I find the differences between the returned numpy array of the written file and the original numpy array you see those random differences by that exact amount scattered in the indicies.

def write_edf_slice(shm_original_names, signal_headers, f, output_edf_file_path, start_time, end_time, shm_concat_names, start_pos, n_elements, total_elements, channel_number):
    # Create a new EDF file writer for the time slice
    output_edf_writer = pyedflib.EdfWriter(output_edf_file_path, len(signal_headers), file_type=pyedflib.FILETYPE_EDFPLUS)

    original_signal_data = []
    shm_original_list = []

    for i in range(len(shm_original_names)):
        shm_original_list.append(shared_memory.SharedMemory(name=shm_original_names[i]))
        original_signal_data.append(np.ndarray((total_elements), dtype=np.float64, buffer=shm_original_list[i].buf))

    concatenated_signal = []
    shm_concat_list = []

    for i in range(len(shm_concat_names)):
        shm_concat_list.append(shared_memory.SharedMemory(name=shm_concat_names[i]))
        concatenated_signal.append(np.ndarray((n_elements), dtype=np.float64, buffer=shm_concat_list[i].buf, offset=start_pos * np.dtype(np.float64).itemsize))

    # Copy signal headers to the new EDF file
    for i in range(channel_number):
        signal_header = signal_headers[i]
        output_edf_writer.setSignalHeader(i, signal_header)
        start_index = int(start_time * f[i])
        end_index = int(end_time * f[i])
        original_data_current_channel = original_signal_data[i][start_index:end_index]
        np.copyto(concatenated_signal[i], original_data_current_channel)

    output_edf_writer.writeSamples(concatenated_signal)

    output_edf_writer.close()

    # Close shared memory segment
    for shm in shm_original_list:
        shm.close()

    for shm in shm_concat_list:
        shm.close()
@skjerns
Copy link
Collaborator

skjerns commented Jun 12, 2024

can you post a minimal reproducible sample with code?

else it's difficult to find the bug

@TrianecT-Wouter
Copy link

You are using a np.float64 ( (2^64 steps) datatype. EDF stores float16 (2^16 steps) ranging from -digital_minimum to +digital_minimum and using physical_minumum and physical_maximum. This might explain (some) of the differences you see?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants