I have 3D volumes of data (x,y,z) and I want to save 2D slices (xy, yz, xz planes) and save them for future use.
The way I tried to do so is by having a function (slice_data
) to take the slices and another one (save_slices
) to call slice_data
and then use numpy.save
to save the slices.
If I am not saving the slices, the time to take the slices is similar irrespective of whether I extract the xy, yz, xz planes. However, if I save the slices, the time to save slices depends on the direction of the slice, and is different for xy, yz, xz planes
Why is that? Using my full data, this difference goes from minutes to hours...
I have written below a dummy code, similar to the one I use in my full dataset to demonstrate the problem. The time difference between is shown below:
Not saving , just slicing
mean time x-slice: 0.00011536836624145507 sec
mean time y-slice: 0.00011417627334594726 sec
mean time z-slice: 0.00011371374130249023 sec
Slicing and saving:
mean time x-slice: 0.04629791975021362 sec
mean time y-slice: 0.06096100091934204 sec
mean time z-slice: 0.08996494293212891 sec
Code:
import os
import numpy as np
import time
import matplotlib.pyplot as plt
# take a slice of the data
def slice_data(roi):
dic = {}
data = np.zeros((512,512,256))
dic['data'] = np.squeeze( data[roi[0]:roi[1]+1, roi[2]:roi[3]+1, roi[4]:roi[5]+1] )
return dic
# save slices if the data
def save_slices(roi, save=False):
var = 'data'
for i in range(0,6):
# iterate to simulate a time series of data
a = slice_data(roi)[var]
var_dir = 'save_test/'
if not os.path.exists(var_dir): os.makedirs(var_dir)
file = var_dir + '{0:04d}{1}'.format(i,'.npy')
if save is True:
np.save(file, a)
## define slices
roix=[256, 256, 0, 512, 0, 256] # yz plane slice
roiy=[0, 512, 256, 256, 0, 256] # xz plane slice
roiz=[0, 512, 0, 512, 128, 128] # xy plane slice
## Calculate slices and do not save the results
dtx = []
dty = []
dtz = []
for i in range(100):
time0 = time.time()
save_slices(roix)
time1 = time.time()
dtx.append(time1-time0)
time0 = time.time()
save_slices(roiy)
time1 = time.time()
dty.append(time1-time0)
time0 = time.time()
save_slices(roiz)
time1 = time.time()
dtz.append(time1-time0)
plt.figure(1)
plt.plot(dtx)
plt.plot(dty)
plt.plot(dtz)
plt.title('time to run code without saving data')
print('mean time x-slice: {} sec'.format(np.mean(dtx)))
print('mean time y-slice: {} sec'.format(np.mean(dty)))
print('mean time z-slice: {} sec'.format(np.mean(dtz)))
## Calculate slices and do save the results
dtx = []
dty = []
dtz = []
for i in range(100):
time0 = time.time()
save_slices(roix, save=True)
time1 = time.time()
dtx.append(time1-time0)
time0 = time.time()
save_slices(roiy, save=True)
time1 = time.time()
dty.append(time1-time0)
time0 = time.time()
save_slices(roiz, save=True)
time1 = time.time()
dtz.append(time1-time0)
plt.figure(2)
plt.plot(dtx)
plt.plot(dty)
plt.plot(dtz)
plt.title('time to run code and save data')
print('mean time x-slice: {} sec'.format(np.mean(dtx)))
print('mean time y-slice: {} sec'.format(np.mean(dty)))
print('mean time z-slice: {} sec'.format(np.mean(dtz)))