Skip to content

Commit

Permalink
Apply black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kushalbakshi committed Aug 9, 2024
1 parent 686bed6 commit bcd6386
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
17 changes: 11 additions & 6 deletions element_interface/extract_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def __init__(
def write_matlab_run_script(self):
"""Compose a matlab script and save it with the name run_extract.m.
The composed script is basically the formatted version of the m_template attribute."""
The composed script is basically the formatted version of the m_template attribute.
"""

self.output_fullpath = (
self.output_dir / f"{self.scanfile.stem}_extract_output.mat"
Expand All @@ -53,11 +54,15 @@ def write_matlab_run_script(self):
**dict(
parameters_list_string="\n".join(
[
f"config.{k} = '{v}';"
if isinstance(v, str)
else f"config.{k} = {str(v).lower()};"
if isinstance(v, bool)
else f"config.{k} = {v};"
(
f"config.{k} = '{v}';"
if isinstance(v, str)
else (
f"config.{k} = {str(v).lower()};"
if isinstance(v, bool)
else f"config.{k} = {v};"
)
)
for k, v in self.parameters.items()
]
),
Expand Down
56 changes: 37 additions & 19 deletions element_interface/prairie_view_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def write_single_bigtiff(
overwrite=False,
gb_per_file=None,
):
logger.warning("Deprecation warning: `caiman_compatible` argument will no longer have any effect and will be removed in the future. `write_single_bigtiff` will return multi-page tiff, which is compatible with CaImAn.")
logger.warning(
"Deprecation warning: `caiman_compatible` argument will no longer have any effect and will be removed in the future. `write_single_bigtiff` will return multi-page tiff, which is compatible with CaImAn."
)

tiff_names, plane_idx, channel = self.get_prairieview_filenames(
plane_idx=plane_idx, channel=channel, return_pln_chn=True
Expand All @@ -126,7 +128,9 @@ def write_single_bigtiff(
output_tiff_list = []
if self.meta["is_multipage"]:
if gb_per_file is not None:
logger.warning("Ignoring `gb_per_file` argument for multi-page tiff (NotYetImplemented)")
logger.warning(
"Ignoring `gb_per_file` argument for multi-page tiff (NotYetImplemented)"
)
# For multi-page tiff - the pages are organized as:
# (channel x slice x frame) - each page is (height x width)
# - TODO: verify this is the case for Bruker multi-page tiff
Expand All @@ -137,36 +141,45 @@ def write_single_bigtiff(
slice_idx = self.meta["plane_indices"].index(plane_idx)
channel_idx = self.meta["channels"].index(channel)

page_indices = [frame_idx * frame_step + slice_idx * slice_step + channel_idx
for frame_idx in range(self.meta["num_frames"])]
page_indices = [
frame_idx * frame_step + slice_idx * slice_step + channel_idx
for frame_idx in range(self.meta["num_frames"])
]

combined_data = np.empty([self.meta["num_frames"],
self.meta["height_in_pixels"],
self.meta["width_in_pixels"]],
dtype=int)
combined_data = np.empty(
[
self.meta["num_frames"],
self.meta["height_in_pixels"],
self.meta["width_in_pixels"],
],
dtype=int,
)
start_page = 0
try:
for input_file in tiff_names:
with tifffile.TiffFile(self.prairieview_dir / input_file) as tffl:
# Get indices in this tiff file and in output array
final_page_in_file = start_page + len(tffl.pages)
is_page_in_file = lambda page: page in range(start_page, final_page_in_file)
is_page_in_file = lambda page: page in range(
start_page, final_page_in_file
)
pages_in_file = filter(is_page_in_file, page_indices)
file_indices = [page - start_page for page in pages_in_file]
global_indices = [is_page_in_file(page) for page in page_indices]
global_indices = [
is_page_in_file(page) for page in page_indices
]

# Read from this tiff file (if needed)
if len(file_indices) > 0:
# this line looks a bit ugly but is memory efficient. Do not separate
combined_data[global_indices] = tffl.asarray(key=file_indices)
combined_data[global_indices] = tffl.asarray(
key=file_indices
)
start_page += len(tffl.pages)
except Exception as e:
raise Exception(f"Error in processing tiff file {input_file}: {e}")

output_tiff_fullpath = (
output_dir
/ f"{output_tiff_stem}.tif"
)
output_tiff_fullpath = output_dir / f"{output_tiff_stem}.tif"
tifffile.imwrite(
output_tiff_fullpath,
combined_data,
Expand All @@ -177,8 +190,7 @@ def write_single_bigtiff(
else:
while len(tiff_names):
output_tiff_fullpath = (
output_dir
/ f"{output_tiff_stem}_{len(output_tiff_list):04}.tif"
output_dir / f"{output_tiff_stem}_{len(output_tiff_list):04}.tif"
)
with tifffile.TiffWriter(
output_tiff_fullpath,
Expand All @@ -203,8 +215,14 @@ def write_single_bigtiff(
tffl.close()
del tffl
except Exception as e:
raise Exception(f"Error in processing tiff file {input_file}: {e}")
if gb_per_file and output_tiff_fullpath.stat().st_size >= gb_per_file * 1024 ** 3:
raise Exception(
f"Error in processing tiff file {input_file}: {e}"
)
if (
gb_per_file
and output_tiff_fullpath.stat().st_size
>= gb_per_file * 1024**3
):
break
output_tiff_list.append(output_tiff_fullpath)

Expand Down
1 change: 1 addition & 0 deletions element_interface/run_caiman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

try:
import torch

cuda_is_available = torch.cuda.is_available()
except:
cuda_is_available = False
Expand Down
4 changes: 3 additions & 1 deletion element_interface/suite2p_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def __init__(self, suite2p_plane_dir: str):

@property
def curation_time(self):
print("DeprecationWarning: 'curation_time' is deprecated, set to be the same as 'creation time', no longer reliable.")
print(
"DeprecationWarning: 'curation_time' is deprecated, set to be the same as 'creation time', no longer reliable."
)
return self.creation_time

@property
Expand Down

0 comments on commit bcd6386

Please sign in to comment.