I have a large image stack with 853 images, each image has the dimensions (11779, 15394) pixels and 3 channels. The images have 8bit depth and are saved in BigTiff format. My data is stored on an external SSD which I connect to my computer.
What I want to do is to change the values of some pixels depending on the initial value. Since I cannot load the full stack into memory, I use tifffile.memmap to memory map the pixels I want to investigate and change.
Here is a minimal example of how my code looks like:
import numpy as np
from tifffile import memmap
memmap_img_HSV = memmap('img_HSV.tif', mode='r') #original image stack I want to investigate
memmap_img_result = memmap('img_result.tif', mode='r+') #another image which I want to overwrite with the results
img_hue = memmap_img_HSV[:,:,:,0] #first channels of original image
memmap_img_result[:,:,:] = 0 #set all pixels = 0, before I save the results into it
fill_value = 11 #define the variable "fill_value" which will be used later
sigma_list = [] #create a list where I later save coordinates in
array_label = memmap_img_result[z1:z2, y1:y2, x1:x2] #only memmap indicated pixels
array_hue = img_hue[z1:z2, y1:y2, x1:x2]
count = array_label == fill_value #these are the pixels which have already been investigated and replaced by the "fill_value"
orig_value_hue = np.median(array_hue[count]) #determine the median value of the pixel values from "count"
org = memmap_img_result[zv1:zv2, yv1:yv2, xv1:xv2] #original values of the investigated pixels
if (orig_value_hue+1) < 10: #condition, which pixels should be replaced with "fill_value": the rest stays how it was before
res = np.where((memmap_img_HSV[zv1:zv2, yv1:yv2, xv1:xv2] == 0), fill_value, org)
if (orig_value_hue+1) < 10: #another condition; if this is True, write the coordinates into list
res_sigma = np.argwhere(memmap_img_HSV[zv1:zv2, yv1:yv2, xv1:xv2] == 1)
if len(res_sigma)>0:
sigma_list.append([zv1,yv1,xv1])
sigma_list = np.asarray(sigma_list[1])
if len(sigma_list[sigma+1])>0:
np.savetxt('list.txt', np.column_stack([sigma_list[1]]))
memmap_img_result.flush()
del memmap_img_HSV
del memmap_img_result
I apologize if the code looks a bit chaotic; it is just a simplification of the much more complicated code. But I hope, the aim gets clear.
Now my problem is the following: When I run the code, it sometimes happens that the code crashes. When writing the file 'list.txt' to the disk, I either get the error message "No space left on device", although there has been a lot of free space on disk (enough for saving the list). Or I get the error message "Invalid argument: 'list.txt'". Additionally, the SSD shows 0 kB size after the error message. I first have to repair the disk and reboot before I can use the disk again.
I think it's not a problem with the memmap, but somehow with saving the list as a text-file.
I could already run the code with other (smaller) data without any error message, but it crashes with this data. Does anybody have an idea what might cause the crash or what I could try out?