Skip to content

Commit 1d83377

Browse files
committed
Refactor to re-use library supported functionality
1 parent c4c3f38 commit 1d83377

File tree

2 files changed

+15
-50
lines changed

2 files changed

+15
-50
lines changed

raw_utils/core/metadata.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ def bitdepth(name):
1818
TypeError: If requested type is not supported.
1919
2020
"""
21+
name = str(name)
22+
logging.warning(name)
2123
supported_types = {
2224
'uint8': 'UCHAR',
2325
'uint16': 'USHORT',
24-
'float32': 'FLOAT'
26+
'float32': 'FLOAT',
27+
'8': 'UCHAR',
28+
'16': 'USHORT',
29+
'32': 'FLOAT'
2530
}
2631
if name in supported_types:
2732
return supported_types[name]

raw_utils/nsihdr2raw/nsihdr2raw.py

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,16 @@
1111
import numpy as np
1212
from tqdm import tqdm
1313

14-
from util.extract import get_maximum_slice_projection, get_slice
14+
from raw_utils.core.metadata import write_dat, bitdepth
15+
from raw_utils.core.convert.convert import scale
1516

1617
# Global bounds for initial and target ranges per NSI project file
1718
INITIAL_LOWER_BOUND = None
1819
INITIAL_UPPER_BOUND = None
1920
TARGET_LOWER_BOUND = None
2021
TARGET_UPPER_BOUND = None
2122

22-
def scale(vi):
23-
"""Scales a value from one range to another range, inclusive.
24-
25-
This functions uses globally assigned values, min and max, of N given .nsidat
26-
files
27-
28-
Args:
29-
vi (numeric): input value
30-
31-
Returns:
32-
numeric: The equivalent value of the input value within a new target range
33-
"""
34-
return (TARGET_LOWER_BOUND + ((TARGET_UPPER_BOUND-TARGET_LOWER_BOUND)/(INITIAL_UPPER_BOUND-INITIAL_LOWER_BOUND)) * (vi-INITIAL_LOWER_BOUND))
35-
23+
# TODO(tparker): Replace with library version of this
3624
def write_metadata(args, metadata):
3725
"""Generates a .dat file from information gathered from an .nsihdr file
3826
@@ -50,42 +38,17 @@ def write_metadata(args, metadata):
5038
dat_filepath = f'{os.path.splitext(args.output)[0]}.dat'
5139
output_string = f"""ObjectFileName: {ObjectFileName}\nResolution: {resolution}\nSliceThickness: {slice_thickness}\nFormat: {metadata['bit_depth_type']}\nObjectModel: {metadata['ObjectModel']}"""
5240

53-
with open(dat_filepath, 'w') as ofp:
54-
print(f'Generating {dat_filepath}')
55-
ofp.write(output_string)
41+
write_dat(dat_filepath, metadata['dimensions'], metadata['resolution_rounded'])
42+
# with open(dat_filepath, 'w') as ofp:
43+
# print(f'Generating {dat_filepath}')
44+
# ofp.write(output_string)
5645

5746
bounds_filepath = os.path.join(args.cwd, f'{os.path.splitext(args.output)[0]}.float32.range')
5847
with open(bounds_filepath, 'w') as ofp:
5948
print(f'Generating {bounds_filepath}')
6049
bounds = f'{INITIAL_LOWER_BOUND} {INITIAL_UPPER_BOUND}'
6150
ofp.write(bounds)
6251

63-
# TODO(tparker): Replace with raw_utils.core.metadata.bitdepth function
64-
# DUPLICATE METHOD
65-
def bit_depth_to_string(bit_count):
66-
"""Convert an integer to a string representation of bit depth
67-
68-
These values have been hard-coded because there can be more than one type for
69-
each bit depth, but these are the ones we currently use.
70-
71-
Args:
72-
bit_count (integer): bit depth listed in .nsihdr
73-
74-
Returns:
75-
str: name of bit-depth
76-
"""
77-
# Hard-coded values because I'm not sure how NSI encodes them in their
78-
# .nsihdr files
79-
if bit_count == 8:
80-
return 'UCHAR'
81-
elif bit_count == 16:
82-
return 'USHORT'
83-
# Assume 32-bit floating point number
84-
elif bit_count == 32:
85-
return 'FLOAT'
86-
else:
87-
return None
88-
8952
def read_nsihdr(args, fp):
9053
"""Collects relative metadata from .nsihdr file
9154
@@ -160,7 +123,7 @@ def read_nsihdr(args, fp):
160123
"resolution_rounded": [resolution_rounded]*3,
161124
"bit_depth": bit_depth,
162125
"zoom_factor": round(source_to_detector_distance / source_to_table_distance, 2),
163-
"bit_depth_type": bit_depth_to_string(bit_depth),
126+
"bit_depth_type": bitdepth(bit_depth),
164127
"ObjectModel": ObjectModel,
165128
"dimensions": dimensions
166129
}
@@ -226,7 +189,7 @@ def process(args, metadata):
226189
with open (input_filepath, mode='rb') as ifp:
227190
df = np.fromfile(ifp, dtype='float32') # Assume 32-bit floating point value, but this may not be true for all volumes
228191

229-
sdf = scale(df).astype('uint16')
192+
sdf = scale(df, INITIAL_LOWER_BOUND, INITIAL_UPPER_BOUND, TARGET_LOWER_BOUND, TARGET_UPPER_BOUND).astype('uint16')
230193
with open(args.output, 'ab') as ofp:
231194
sdf.tofile(ofp)
232195
pbar.update(1)
@@ -299,9 +262,6 @@ def parseOptions():
299262
process(args, project_metadata)
300263
# Create .dat & .range files
301264
write_metadata(args, project_metadata)
302-
# Extract QC files
303-
get_slice(args, args.output)
304-
get_maximum_slice_projection(args, args.output)
305265
except Exception as err:
306266
logging.error(err)
307267
raise

0 commit comments

Comments
 (0)