0% found this document useful (0 votes)
5 views2 pages

Class ImagesData

Uploaded by

Nishchal Sapkota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Class ImagesData

Uploaded by

Nishchal Sapkota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

class ImagesData:

def __init__(self, df):


self.df = df

def get_vol_df(self, vol_id):


'''
input [str] : vol id (example: C57_E155_011)
output [df] : sorted dataframe by slice_index with each row
corresponding to image slice and metadata
'''
return self.df[self.df.vol_id==vol_id].sort_values(by='slice_index')

def get_vol_arr(self, vol_df):


'''
input [df] : volume df
output [numpy_array: z*h*w] : numpy array with all image slices
in the volume
'''
sample_row = vol_df.iloc[0]
z = sample_row.total_slices_count
h, w = imread(sample_row.img_path).shape
vol_arr = np.zeros((z,h,w))
index = 0
for _, row in vol_df.iterrows():
image = imread(row.img_path)
if image.max() != 0:
image = image/image.max()
vol_arr[index] = image
index += 1
return vol_arr

def process_vol_id(self, vol_id):


start = time.time()
vol_df = self.get_vol_df(vol_id)
vol_arr = self.get_vol_arr(vol_df)
print(f'{vol_id}: {time.time()-start} s.')
return vol_id, vol_arr

def get_vol_arr_dict(self):
vols_dict = {}
vols = self.df.vol_id.unique()
# for vol_id in vols:
# start = time.time()
# vols_dict[vol_id] = self.get_vol_arr(self.get_vol_df(vol_id))
# print(f'{time.time()-start} s.')
with mp.Pool(mp.cpu_count()) as pool:
results = pool.map(self.process_vol_id, vols)

start = time.time()
for vol_id, vol_arr in results:
vols_dict[vol_id] = vol_arr
print(f'{time.time()-start} s.')

return vols_dict

You might also like