Skip to content

Commit

Permalink
Merge pull request #252 from RENCI/handle-byte-swapping
Browse files Browse the repository at this point in the history
Handle byte swapping
  • Loading branch information
hyi authored Jul 21, 2023
2 parents 2f6c8e3 + 88e8b56 commit 195f911
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 2 additions & 0 deletions girder-client/create_slice_intensity_range_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
tif = TIFF.open(file_name_with_path)
images = []
for image in tif.iter_images():
if tif.isbyteswapped():
image = image.byteswap()
images.append(image)
# imarray should be in order of ZYX
imarray = np.array(images)
Expand Down
36 changes: 18 additions & 18 deletions girder/plugins/ninjato_api/girder_ninjato_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ def _get_buffered_extent(minx, maxx, miny, maxy, minz, maxz, xrange, yrange, zra
return int(minx), int(maxx), int(miny), int(maxy), int(minz), int(maxz)


def _get_tif_image_array(tif_input):
images = []
for image in tif_input.iter_images():
if tif_input.isbyteswapped():
image = image.byteswap()
images.append(image)
return images


def create_region_files(region_item, whole_item):
"""
extract region files from the whole subvolume item based on bounding box extent and
Expand Down Expand Up @@ -151,14 +160,13 @@ def create_region_files(region_item, whole_item):
if not os.path.isdir(out_dir_path):
os.makedirs(out_dir_path)
output_tif = TIFF.open(out_path, mode="w")
counter = 0
for image in tif.iter_images():
images = _get_tif_image_array(tif)
for counter, image in enumerate(images):
if min_z <= counter <= max_z:
img = np.copy(image[min_y:max_y + 1, min_x:max_x + 1])
output_tif.write_image(img)
if counter > max_z:
break
counter += 1
assetstore_id = item_file['assetstoreId']
save_file(assetstore_id, region_item, out_path, admin_user, output_file_name)
return
Expand Down Expand Up @@ -252,9 +260,7 @@ def update_assignment_in_whole_item(whole_item, assign_item_id, mask_file_name=N
if mask_file_name and assign_item_file['name'] != mask_file_name:
continue
assign_item_tif, _ = _get_tif_file_content_and_path(assign_item_file)
assign_item_images = []
for assign_image in assign_item_tif.iter_images():
assign_item_images.append(assign_image)
assign_item_images = _get_tif_image_array(assign_item_tif)

item_files = File().find({'itemId': whole_item['_id']})
for item_file in item_files:
Expand All @@ -266,15 +272,14 @@ def update_assignment_in_whole_item(whole_item, assign_item_id, mask_file_name=N
out_dir_path = os.path.dirname(whole_path)
output_path = os.path.join(out_dir_path, f'{uuid.uuid4()}_{file_name}')
whole_out_tif = TIFF.open(output_path, mode='w')
counter = 0
whole_images = _get_tif_image_array(whole_tif)
# region_imarray should be in order of ZYX
for image in whole_tif.iter_images():
for counter, image in enumerate(whole_images):
if assign_item_coords['z_min'] <= counter <= assign_item_coords['z_max']:
image[assign_item_coords['y_min']: assign_item_coords['y_max']+1,
assign_item_coords['x_min']: assign_item_coords['x_max']+1] = \
assign_item_images[counter-assign_item_coords['z_min']]
whole_out_tif.write_image(np.copy(image))
counter += 1

assetstore_id = item_file['assetstoreId']
# remove the original file and create new file using updated TIFF mask
Expand Down Expand Up @@ -318,9 +323,7 @@ def get_region_extent(item, region_id, user_extent=True):
if substr_to_check not in item_file['name']:
continue
tif, _ = _get_tif_file_content_and_path(item_file)
images = []
for image in tif.iter_images():
images.append(image)
images = _get_tif_image_array(tif)
imarray = np.array(images)
level_indices = np.where(imarray == int(region_id))
z_min = min(level_indices[0])
Expand Down Expand Up @@ -619,7 +622,6 @@ def _update_user_mask(item_id, old_content, old_extent, new_extent=None):
}
item_files = File().find({'itemId': ObjectId(item_id)})
item_user_mask = _reshape_content_bytes_to_array(old_content, old_extent)
item_mask = []
user_mask_file_name = ''
mask_file_name = ''
assetstore_id = ''
Expand All @@ -636,8 +638,8 @@ def _update_user_mask(item_id, old_content, old_extent, new_extent=None):
assetstore_id = item_file['assetstoreId']
item_tif, _ = _get_tif_file_content_and_path(item_file)
# initial mask
for mask in item_tif.iter_images():
item_mask.append(mask)
item_mask = _get_tif_image_array(item_tif)
break
# check user mask and initial mask to combine the extent to update user mask
z = new_extent['min_z']
for mask in item_mask:
Expand Down Expand Up @@ -746,9 +748,7 @@ def remove_region_from_active_assignment(whole_item, assign_item_id, region_id,
if '_masks' not in item_file['name']:
continue
tif, _ = _get_tif_file_content_and_path(item_file)
images = []
for image in tif.iter_images():
images.append(image)
images = _get_tif_image_array(tif)
imarray = np.array(images)
levels = imarray[np.nonzero(imarray)]
min_level = min(levels)
Expand Down

0 comments on commit 195f911

Please sign in to comment.