Skip to content

Commit b4c1822

Browse files
committed
Initial font globally and disable progress when verbose mode enabled
1 parent 3bb24be commit b4c1822

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

rawtools/qualitycontrol.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
from tqdm import tqdm
116116
from rawtools import dat
117117

118+
font = None
119+
118120
def rawfp2datfp(fp):
119121
directory = os.path.dirname(fp)
120122
name = os.path.splitext(os.path.basename(fp))[0]
@@ -158,7 +160,8 @@ def get_top_down_projection(args, fp):
158160
buffer_size = x * y * np.dtype('uint16').itemsize
159161
logging.debug(f'Allocated memory for a slice (i.e., buffer_size): {buffer_size} bytes')
160162

161-
pbar = tqdm(total = z, desc="Generating top-down projection") # progress bar
163+
if not args.verbose:
164+
pbar = tqdm(total = z, desc="Generating top-down projection") # progress bar
162165
with open(fp, mode='rb', buffering=buffer_size) as ifp:
163166
# Load in the first slice
164167
byte_slice = ifp.read(buffer_size) # Byte sequence
@@ -173,8 +176,10 @@ def get_top_down_projection(args, fp):
173176
raw_image_data = np.maximum(raw_image_data, byte_sequence_max_values)
174177
# # Read the next slice & update progress bar
175178
byte_slice = ifp.read(buffer_size)
176-
pbar.update(1)
177-
pbar.close()
179+
if not args.verbose:
180+
pbar.update(1)
181+
if not args.verbose:
182+
pbar.close()
178183

179184
# Convert raw bytes to array of 16-bit values
180185
logging.debug(f"raw_image_data shape: {np.shape(raw_image_data)}")
@@ -202,6 +207,7 @@ def get_side_projection(args, fp):
202207
fp (str): filepath for a .RAW volume
203208
204209
"""
210+
global font
205211
# Extract the resolution from .DAT file
206212
dat_fp = rawfp2datfp(fp)
207213
logging.debug(f"{dat_fp=}")
@@ -223,13 +229,9 @@ def get_side_projection(args, fp):
223229
# NOTE(tparker): This assumes that a unsigned 16-bit .RAW volume
224230
buffer_size = x * y * np.dtype('uint16').itemsize
225231
logging.debug(f'Allocated memory for a slice (i.e., buffer_size): {buffer_size} bytes')
226-
227-
# Load font
228-
font_fp = '/'.join([os.path.dirname(os.path.realpath(__file__)), 'assets', 'OpenSans-Regular.ttf'])
229-
logging.debug(f"Font filepath: '{font_fp}'")
230-
font = ImageFont.truetype(font_fp, args.font_size)
231232

232-
pbar = tqdm(total = z, desc="Generating side-view projection") # progress bar
233+
if not args.verbose:
234+
pbar = tqdm(total = z, desc=f"Generating side-view projection for '{os.path.basename(fp)}'") # progress bar
233235
with open(fp, mode='rb', buffering=buffer_size) as ifp:
234236
# Load in the first slice
235237
byte_slice = ifp.read(buffer_size) # Byte sequence
@@ -249,8 +251,10 @@ def get_side_projection(args, fp):
249251
raw_image_data.extend(byte_sequence_max_values)
250252
# Read the next slice & update progress bar
251253
byte_slice = ifp.read(buffer_size)
252-
pbar.update(1)
253-
pbar.close()
254+
if not args.verbose:
255+
pbar.update(1)
256+
if not args.verbose:
257+
pbar.close()
254258

255259
# Convert raw bytes to array of 16-bit values
256260
logging.debug(f"raw_image_data length: {len(raw_image_data)}")
@@ -345,7 +349,8 @@ def get_slice(args, fp):
345349
logging.debug(f'Relative byte indices for extracted slice: <{start_byte}, {end_byte}>')
346350
logging.debug(f'Allocated memory for a slice (i.e., buffer_size): {buffer_size} bytes')
347351

348-
pbar = tqdm(total = z, desc=f"Extracting slice #{i}") # progress bar
352+
if not args.verbose:
353+
pbar = tqdm(total = z, desc=f"Extracting slice #{i}") # progress bar
349354
with open(fp, mode='rb', buffering=buffer_size) as ifp:
350355
byte_slice = ifp.read(buffer_size) # Byte sequence
351356
raw_byte_string = bytearray()
@@ -354,8 +359,10 @@ def get_slice(args, fp):
354359
ith_byte_sequence = byte_slice[start_byte : end_byte]
355360
raw_byte_string.extend(ith_byte_sequence)
356361
byte_slice = ifp.read(buffer_size)
357-
pbar.update(1)
358-
pbar.close()
362+
if not args.verbose:
363+
pbar.update(1)
364+
if not args.verbose:
365+
pbar.close()
359366

360367
# Convert raw bytes to array of 16-bit values
361368
arr = np.frombuffer(raw_byte_string, dtype=np.uint16)
@@ -374,6 +381,8 @@ def get_slice(args, fp):
374381

375382
def main(args):
376383
"""Begin processing"""
384+
global font
385+
377386
logging.debug(f'File(s) selected: {args.path}')
378387
# For each file provided...
379388
paths = args.path
@@ -432,7 +441,8 @@ def main(args):
432441
if 'index' not in args and not args.projection:
433442
logging.warning(f"No action specified.")
434443
else:
435-
total_pbar = tqdm(total = len(args.path), desc = "Total Progress")
444+
if not args.verbose:
445+
total_pbar = tqdm(total = len(args.path), desc = "Total Progress")
436446
for fp in args.path:
437447
# Set working directory for file
438448
args.cwd = os.path.dirname(os.path.abspath(fp))
@@ -446,7 +456,11 @@ def main(args):
446456
filesize = f"{n_bytes} B"
447457

448458
# Process files
449-
logging.info(f"Processing '{fp}' ({filesize})")
459+
# Load font
460+
font_fp = '/'.join([os.path.dirname(os.path.realpath(__file__)), 'assets', 'OpenSans-Regular.ttf'])
461+
logging.debug(f"Font filepath: '{font_fp}'")
462+
font = ImageFont.truetype(font_fp, args.font_size)
463+
logging.debug(f"Processing '{fp}' ({filesize})")
450464
if 'index' in args and args.index is not None:
451465
if args.index is True:
452466
args.index = None
@@ -458,5 +472,7 @@ def main(args):
458472
if 'top' in args.projection:
459473
get_top_down_projection(args, fp)
460474

461-
total_pbar.update()
462-
total_pbar.close()
475+
if not args.verbose:
476+
total_pbar.update()
477+
if not args.verbose:
478+
total_pbar.close()

0 commit comments

Comments
 (0)