Skip to content

Commit

Permalink
Disabling progress bars (#2763)
Browse files Browse the repository at this point in the history
* utils.py: Create sct_progress_bar

As suggested in #2724 it can be disabled by setting the
SCT_PROGRESS_BAR to no, off, or false (case insensitive)

* progress bars: replace all tqdm bars with sct_progress_bar

* sct_run_batch: Disable progress bars in batch runner

Co-authored-by: Julien Cohen-Adad <jcohen@polymtl.ca>
  • Loading branch information
cfhammill and jcohenadad committed Jun 29, 2020
1 parent 4483f3e commit c470421
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
10 changes: 5 additions & 5 deletions scripts/msct_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import sys, os, logging
from math import asin, cos, sin, acos
import numpy as np
from tqdm import tqdm

from scipy import ndimage
from scipy.signal import argrelmax, medfilt
from scipy.io import loadmat
from nibabel import load, Nifti1Image, save

from spinalcordtoolbox.image import Image, find_zmin_zmax, spatial_crop
from spinalcordtoolbox.utils import sct_progress_bar

import sct_utils as sct
import sct_apply_transfo
Expand Down Expand Up @@ -796,8 +796,8 @@ def register2d_centermassrot(fname_src, fname_dest, paramreg=None, fname_warp='w
th_max_angle *= np.pi / 180

# Loop across slices
for iz in tqdm(range(0, nz), unit='iter', unit_scale=False, desc="Estimate cord angle for each slice",
ascii=False, ncols=100):
for iz in sct_progress_bar(range(0, nz), unit='iter', unit_scale=False, desc="Estimate cord angle for each slice",
ascii=False, ncols=100):
try:
# compute PCA and get center or mass based on segmentation
coord_src[iz], pca_src[iz], centermass_src[iz, :] = compute_pca(data_src[:, :, iz])
Expand Down Expand Up @@ -884,8 +884,8 @@ def register2d_centermassrot(fname_src, fname_dest, paramreg=None, fname_warp='w
warp_inv_y = np.zeros(data_src.shape)

# construct 3D warping matrix
for iz in tqdm(z_nonzero, unit='iter', unit_scale=False, desc="Build 3D deformation field",
ascii=False, ncols=100):
for iz in sct_progress_bar(z_nonzero, unit='iter', unit_scale=False, desc="Build 3D deformation field",
ascii=False, ncols=100):
# get indices of x and y coordinates
row, col = np.indices((nx, ny))
# build 2xn array of coordinates in pixel space
Expand Down
5 changes: 2 additions & 3 deletions scripts/sct_analyze_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
import itertools
import argparse

import tqdm
from skimage.feature import greycomatrix, greycoprops

import sct_utils as sct
import spinalcordtoolbox.image as msct_image
from spinalcordtoolbox.image import Image
from spinalcordtoolbox.utils import Metavar, SmartFormatter, ActionCreateFolder
from spinalcordtoolbox.utils import Metavar, SmartFormatter, ActionCreateFolder, sct_progress_bar

def get_parser():
# Initialize the parser
Expand Down Expand Up @@ -251,7 +250,7 @@ def compute_texture(self):
dct_metric[m] = im_2save
# dct_metric[m] = Image(self.fname_metric_lst[m])

with tqdm.tqdm() as pbar:
with sct_progress_bar() as pbar:
for im_z, seg_z, zz in zip(self.dct_im_seg['im'], self.dct_im_seg['seg'], range(len(self.dct_im_seg['im']))):
for xx in range(im_z.shape[0]):
for yy in range(im_z.shape[1]):
Expand Down
3 changes: 2 additions & 1 deletion scripts/sct_run_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def run_single(subj_dir):
'PATH_RESULTS': path_results,
'PATH_LOG': path_log,
'PATH_QC': path_qc,
'ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS': str(args.itk_threads)
'ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS': str(args.itk_threads),
'SCT_PROGRESS_BAR': 'off'
})

# Ship the job out, merging stdout/stderr and piping to log file
Expand Down
4 changes: 2 additions & 2 deletions spinalcordtoolbox/deepseg_sc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def segment_2d(model_fname, contrast_type, input_size, im_in):
seg_crop = zeros_like(im_in, dtype=np.float32)

data_norm = im_in.data
# TODO: use tqdm
# TODO: use sct_progress_bar
for zz in range(im_in.dim[2]):
# 2D CNN prediction
pred_seg = seg_model.predict(np.expand_dims(np.expand_dims(data_norm[:, :, zz], -1), 0),
Expand All @@ -374,7 +374,7 @@ def segment_3d(model_fname, contrast_type, im_in):
# segment the spinal cord
z_patch_size = dct_patch_sc_3d[contrast_type]['size'][2]
z_step_keep = list(range(0, im_in.data.shape[2], z_patch_size))
# TODO: use tqdm
# TODO: use sct_progress_bar
for zz in z_step_keep:
if zz == z_step_keep[-1]: # deal with instances where the im.data.shape[2] % patch_size_z != 0
patch_im = np.zeros(dct_patch_sc_3d[contrast_type]['size'])
Expand Down
8 changes: 4 additions & 4 deletions spinalcordtoolbox/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util import Retry
from tqdm import tqdm
from spinalcordtoolbox.utils import sct_progress_bar

import spinalcordtoolbox as sct
import spinalcordtoolbox.utils
Expand Down Expand Up @@ -64,15 +64,15 @@ def download_data(urls):

with open(tmp_path, 'wb') as tmp_file:
total = int(response.headers.get('content-length', 1))
tqdm_bar = tqdm(total=total, unit='B', unit_scale=True, desc="Status", ascii=False, position=0)
sct_bar = sct_progress_bar(total=total, unit='B', unit_scale=True, desc="Status", ascii=False, position=0)

for chunk in response.iter_content(chunk_size=8192):
if chunk:
tmp_file.write(chunk)
dl_chunk = len(chunk)
tqdm_bar.update(dl_chunk)
sct_bar.update(dl_chunk)

tqdm_bar.close()
sct_bar.close()
return tmp_path

except Exception as e:
Expand Down
10 changes: 5 additions & 5 deletions spinalcordtoolbox/moco.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import os
from shutil import copyfile
import glob
from tqdm import tqdm
import numpy as np
import math
import scipy.interpolate
Expand All @@ -26,6 +25,7 @@
import csv

from spinalcordtoolbox.image import Image
from spinalcordtoolbox.utils import sct_progress_bar

import sct_utils as sct
import sct_dmri_separate_b0_and_dwi
Expand Down Expand Up @@ -261,8 +261,8 @@ def moco_wrapper(param):
_, file_dwi_basename, file_dwi_ext = sct.extract_fname(file_datasub)
# Group data
list_file_group = []
for iGroup in tqdm(range(nb_groups), unit='iter', unit_scale=False, desc="Merge within groups", ascii=False,
ncols=80):
for iGroup in sct_progress_bar(range(nb_groups), unit='iter', unit_scale=False, desc="Merge within groups", ascii=False,
ncols=80):
# get index
index_moco_i = group_indexes[iGroup]
n_moco_i = len(index_moco_i)
Expand Down Expand Up @@ -580,8 +580,8 @@ def moco(param):
failed_transfo = [0 for i in range(nt)]

# Motion correction: Loop across T
for indice_index in tqdm(range(nt), unit='iter', unit_scale=False,
desc="Z=" + str(iz) + "/" + str(len(file_data_splitZ)-1), ascii=False, ncols=80):
for indice_index in sct_progress_bar(range(nt), unit='iter', unit_scale=False,
desc="Z=" + str(iz) + "/" + str(len(file_data_splitZ)-1), ascii=False, ncols=80):

# create indices and display stuff
it = index[indice_index]
Expand Down
6 changes: 3 additions & 3 deletions spinalcordtoolbox/process_seg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import platform
import numpy as np
from skimage import measure, transform
from tqdm import tqdm
import logging
import nibabel

from spinalcordtoolbox.image import Image
from spinalcordtoolbox.aggregate_slicewise import Metric
from spinalcordtoolbox.centerline.core import ParamCenterline, get_centerline
from spinalcordtoolbox.resampling import resample_nib
from spinalcordtoolbox.utils import sct_progress_bar


def compute_shape(segmentation, angle_correction=True, param_centerline=None, verbose=1):
Expand Down Expand Up @@ -67,8 +67,8 @@ def compute_shape(segmentation, angle_correction=True, param_centerline=None, ve
_, arr_ctl, arr_ctl_der, fit_results = get_centerline(im_segr, param=param_centerline, verbose=verbose)

# Loop across z and compute shape analysis
for iz in tqdm(range(min_z_index, max_z_index + 1), unit='iter', unit_scale=False, desc="Compute shape analysis",
ascii=True, ncols=80):
for iz in sct_progress_bar(range(min_z_index, max_z_index + 1), unit='iter', unit_scale=False, desc="Compute shape analysis",
ascii=True, ncols=80):
# Extract 2D patch
current_patch = im_segr.data[:, :, iz]
if angle_correction:
Expand Down
6 changes: 3 additions & 3 deletions spinalcordtoolbox/straightening.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import os, time, logging, inspect
import bisect
import numpy as np
from tqdm import tqdm
from nibabel import Nifti1Image, save

from spinalcordtoolbox.types import Centerline
import spinalcordtoolbox.image as msct_image
from spinalcordtoolbox.image import Image
from spinalcordtoolbox.centerline.core import ParamCenterline, get_centerline
from spinalcordtoolbox.utils import sct_progress_bar

import sct_utils as sct
from sct_image import pad_image
Expand Down Expand Up @@ -436,7 +436,7 @@ def straighten(self):
# sct.printv(nx * ny * nz, nx_s * ny_s * nz_s)

if self.curved2straight:
for u in tqdm(range(nz_s)):
for u in sct_progress_bar(range(nz_s)):
x_s, y_s, z_s = np.mgrid[0:nx_s, 0:ny_s, u:u + 1]
indexes_straight = np.array(list(zip(x_s.ravel(), y_s.ravel(), z_s.ravel())))
physical_coordinates_straight = image_centerline_straight.transfo_pix2phys(indexes_straight)
Expand Down Expand Up @@ -465,7 +465,7 @@ def straighten(self):
= -displacements_straight

if self.straight2curved:
for u in tqdm(range(nz)):
for u in sct_progress_bar(range(nz)):
x, y, z = np.mgrid[0:nx, 0:ny, u:u + 1]
indexes = np.array(list(zip(x.ravel(), y.ravel(), z.ravel())))
physical_coordinates = image_centerline_pad.transfo_pix2phys(indexes)
Expand Down
11 changes: 11 additions & 0 deletions spinalcordtoolbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import subprocess
import shutil
import tqdm
from enum import Enum

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -386,6 +387,16 @@ def _version_string():
return "{install_type}-{sct_branch}-{sct_commit}".format(**locals())


def sct_progress_bar(*args, **kwargs):
"""Thin wrapper around `tqdm.tqdm` which checks `SCT_PROGRESS_BAR` muffling the progress
bar if the user sets it to `no`, `off`, or `false` (case insensitive)."""
do_pb = os.environ.get('SCT_PROGRESS_BAR', 'yes')
if do_pb.lower() in ['off', 'no', 'false']:
kwargs['disable'] = True

return tqdm.tqdm(*args, **kwargs)


__sct_dir__ = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
__version__ = _version_string()
__data_dir__ = os.path.join(__sct_dir__, 'data')
Expand Down

0 comments on commit c470421

Please sign in to comment.