Skip to content

Instantly share code, notes, and snippets.

@h-mayorquin
Created May 1, 2023 13:05
Show Gist options
  • Save h-mayorquin/88421fc0fff78338c6c375232901fa95 to your computer and use it in GitHub Desktop.
Save h-mayorquin/88421fc0fff78338c6c375232901fa95 to your computer and use it in GitHub Desktop.
Segmentation fault with memmaps examples
import mmap
import os
from pathlib import Path
import numpy as np
def count_int16_elements(file_path):
file_size = os.path.getsize(file_path)
int16_size = np.dtype(np.int16).itemsize
return file_size // int16_size
folder_path = Path("/home/heberto/spikeinterface_datasets/ephy_testing_data/spikeglx/Noise4Sam_g0/Noise4Sam_g0_imec0")
file_path = folder_path / "Noise4Sam_g0_t0.imec0.ap.bin"
number_of_elements = count_int16_elements(file_path)
num_channels = 385
shape = (number_of_elements // num_channels, num_channels)
file = open(file_path, 'rb')
memmap_obj = mmap.mmap(file.fileno(), length=0, access=mmap.ACCESS_READ)
array = np.ndarray.__new__(np.ndarray, shape=shape, dtype="int16", buffer=memmap_obj, order="C")
print("---------------------")
print(array)
memmap_obj.close()
print(array)
# This will throw a segmentation fault
print("---------------------")
file.close()
print(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment