Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# part 1: do sorting
df = pd.read_csv(args.csv_name, index_col=False)
for index, row in df.iterrows():
if not np.isnan(row['score']) and (row['score']==1 or row['score']==0):
continue
reader = AICSImage(row['raw'])
im_full = reader.data
struct_img = im_full[0,args.input_channel,:,:,:]
raw_img = (struct_img- struct_img.min() + 1e-8)/(struct_img.max() - struct_img.min() + 1e-8)
raw_img = 255 * raw_img
raw_img = raw_img.astype(np.uint8)
reader_seg1 = AICSImage(row['seg1'])
im_seg1_full = reader_seg1.data
assert im_seg1_full.shape[0]==1
assert im_seg1_full.shape[1]==1 or im_seg1_full.shape[2]==1
if im_seg1_full.shape[1]==1:
seg1 = im_seg1_full[0,0,:,:,:]>0.1
else:
seg1 = im_seg1_full[0,:,0,:,:]>0.1
reader_seg2 = AICSImage(row['seg2'])
im_seg2_full = reader_seg2.data
assert im_seg2_full.shape[0]==1
assert im_seg2_full.shape[1]==1 or im_seg2_full.shape[2]==1
if im_seg2_full.shape[1]==1:
seg2 = im_seg2_full[0,0,:,:,:]>0
else:
seg2 = im_seg2_full[0,:,0,:,:]>0
# when the tif has only 1 channel, the loaded array may have falsely swaped dimensions (ZCYX). we want CZYX
# (This may also happen in different OS or different package versions)
# ASSUMPTION: we have more z slices than the number of channels
if label.shape[1]0.5, label[0,:,:,:]==args.OutputCh[2*vi+1], costmap)
else:
write = omeTifWriter.OmeTifWriter(args.OutputDir + pathlib.PurePosixPath(fn).stem + '_seg_'+ str(args.OutputCh[2*ch_idx])+'.ome.tif')
if args.Threshold<0:
write.save(output_img[ch_idx].astype(float))
else:
out = output_img[ch_idx] > args.Threshold
out = out.astype(np.uint8)
out[out>0]=255
write.save(out)
print(f'Image {fn} has been segmented')
elif args.mode == 'eval_file':
fn = args.InputFile
print(fn)
data_reader = AICSImage(fn)
img0 = data_reader.data
if args.timelapse:
assert data_reader.shape[0]>1
for tt in range(data_reader.shape[0]):
# Assume: TCZYX
img = img0[tt, args.InputCh,:,:,:].astype(float)
img = input_normalization(img, args)
if len(args.ResizeRatio)>0:
img = resize(img, (1, args.ResizeRatio[0], args.ResizeRatio[1], args.ResizeRatio[2]), method='cubic')
for ch_idx in range(img.shape[0]):
struct_img = img[ch_idx,:,:,:] # note that struct_img is only a view of img, so changes made on struct_img also affects img
struct_img = (struct_img - struct_img.min())/(struct_img.max() - struct_img.min())
img[ch_idx,:,:,:] = struct_img
# Even though the reader was n-dim,
# check if the actual data is similar to YXC ("YX-RGBA" or "YX-RGB") and return
if (len(img.reader.data.shape) == 3 and (
img.reader.data.shape[2] == 3 or img.reader.data.shape[2] == 4)):
return img.reader.data
# Check which dimensions are available
# AICSImage makes strong assumptions about dimension ordering
# Reduce the array down to 2D + Channels when possible
# Always choose first Scene
if "S" in img.reader.dims:
img = AICSImage(img.data[0, :, :, :, :, :])
# Always choose middle time slice
if "T" in img.reader.dims:
img = AICSImage(img.data[0, img.data.shape[1] / 2, :, :, :, :])
# Keep Channel data, but max project when possible
if "C" in img.reader.dims:
projections = []
for i in range(img.data.shape[2]):
if "Z" in img.reader.dims:
# Add padding to the top and left of the projection
padded = np.pad(
norm_img(img.data[0, 0, i, :, :, :].max(axis=0)),
((5, 0), (5, 0)),
mode="constant"
)
projections.append(padded)
else:
# Add padding to the top and the left of the projection
padded = np.pad(
img = reader.data.astype(np.float32)
struct_img = input_normalization(img[0,[args.input_channel],:,:,:], args)
struct_img= struct_img[0,:,:,:]
# load segmentation gt
reader = AICSImage(row['seg'])
img = reader.data
assert img.shape[0]==1 and img.shape[1]==1
seg = img[0,0,:,:,:]>0
seg = seg.astype(np.uint8)
seg[seg>0]=1
cmap = np.ones(seg.shape, dtype=np.float32)
if os.path.isfile(str(row['mask'])):
# load segmentation gt
reader = AICSImage(row['mask'])
img = reader.data
assert img.shape[0]==1 and img.shape[1]==1
mask = img[0,0,:,:,:]
cmap[mask>0]=0
writer = omeTifWriter.OmeTifWriter(args.train_path + os.sep + 'img_' + f'{training_data_count:03}' + '.ome.tif')
writer.save(struct_img)
writer = omeTifWriter.OmeTifWriter(args.train_path + os.sep + 'img_' + f'{training_data_count:03}' + '_GT.ome.tif')
writer.save(seg)
writer = omeTifWriter.OmeTifWriter(args.train_path + os.sep + 'img_' + f'{training_data_count:03}' + '_CM.ome.tif')
writer.save(cmap)
print('training data is ready')
for img_idx, fn in enumerate(valid_filenames):
# target
label_reader = AICSImage(fn+'_GT.ome.tif') #CZYX
label = label_reader.data
label = np.squeeze(label,axis=0) # 4-D after squeeze
# when the tif has only 1 channel, the loaded array may have falsely swaped dimensions (ZCYX). we want CZYX
# (This may also happen in different OS or different package versions)
# ASSUMPTION: we have more z slices than the number of channels
if label.shape[1]
def _run_benchmark(
resources_dir: Path,
extensions: List[str],
non_aicsimageio_reader: List[Callable],
iterations: int = 3,
):
# Collect files matching the extensions provided
files = []
for ext in extensions:
files += list(resources_dir.glob(ext))
# Run reads for each file and store details in results
results = []
for file in files:
info_read = aicsimageio.AICSImage(file)
yx_planes = np.prod(info_read.size("STCZ"))
for reader in [aicsimageio.imread, non_aicsimageio_reader]:
reader_path = f"{reader.__module__}.{reader.__name__}"
for i in tqdm(range(iterations), desc=f"{reader_path}: {file.name}"):
start = time.perf_counter()
reader(str(file))
results.append(
{
"file_name": file.name,
"file_size_gb": file.stat().st_size / 10e8,
"reader": (
"aicsimageio" if "aicsimageio" in reader_path else "other"
),
"yx_planes": int(yx_planes),
"read_duration": time.perf_counter() - start,
}
for img_idx, fn in enumerate(valid_filenames):
# target
label_reader = AICSImage(fn+'_GT.ome.tif') #CZYX
label = label_reader.data
label = np.squeeze(label,axis=0) # 4-D after squeeze
# when the tif has only 1 channel, the loaded array may have falsely swaped dimensions (ZCYX). we want CZYX
# (This may also happen in different OS or different package versions)
# ASSUMPTION: we have more z slices than the number of channels
if label.shape[1]
for _, fn in enumerate(filenames):
training_data_count += 1
# load raw
reader = AICSImage(fn)
img = reader.data.astype(np.float32)
assert img.shape[0]==1
img = img[0,:,:,:,:]
if img.shape[0] > img.shape[1]:
img = np.transpose(img,(1,0,2,3))
struct_img = input_normalization(img[[args.input_channel],:,:,:], args)
# load seg
seg_fn = args.seg_path + os.sep + os.path.basename(fn)[:-1*len(args.data_type)] + '_struct_segmentation.tiff'
reader = AICSImage(seg_fn)
img = reader.data
assert img.shape[0]==1 and img.shape[1]==1
seg = img[0,0,:,:,:]>0
seg = seg.astype(np.uint8)
seg[seg>0]=1
# excluding mask
cmap = np.ones(seg.shape, dtype=np.float32)
mask_fn = args.mask_path + os.sep + os.path.basename(fn)[:-1*len(args.data_type)] + '_mask.tiff'
if os.path.isfile(mask_fn):
reader = AICSImage(mask_fn)
img = reader.data
assert img.shape[0]==1 and img.shape[1]==1
mask = img[0,0,:,:,:]
cmap[mask==0]=0