Skip to content

Commit f08f1d3

Browse files
Remove assert statement from non-test files (#3745)
* Remove assert statement from non-test files * [MONAI] python code formatting Signed-off-by: monai-bot <[email protected]> Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> Co-authored-by: monai-bot <[email protected]>
1 parent db61a08 commit f08f1d3

File tree

20 files changed

+39
-38
lines changed

20 files changed

+39
-38
lines changed

monai/config/deviceconfig.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ def get_system_info() -> OrderedDict:
161161
),
162162
)
163163
mem = psutil.virtual_memory()
164-
_dict_append(output, "Total physical memory (GB)", lambda: round(mem.total / 1024 ** 3, 1))
165-
_dict_append(output, "Available memory (GB)", lambda: round(mem.available / 1024 ** 3, 1))
166-
_dict_append(output, "Used memory (GB)", lambda: round(mem.used / 1024 ** 3, 1))
164+
_dict_append(output, "Total physical memory (GB)", lambda: round(mem.total / 1024**3, 1))
165+
_dict_append(output, "Available memory (GB)", lambda: round(mem.available / 1024**3, 1))
166+
_dict_append(output, "Used memory (GB)", lambda: round(mem.used / 1024**3, 1))
167167

168168
return output
169169

@@ -209,7 +209,7 @@ def get_gpu_info() -> OrderedDict:
209209
_dict_append(output, f"GPU {gpu} Is integrated", lambda: bool(gpu_info.is_integrated))
210210
_dict_append(output, f"GPU {gpu} Is multi GPU board", lambda: bool(gpu_info.is_multi_gpu_board))
211211
_dict_append(output, f"GPU {gpu} Multi processor count", lambda: gpu_info.multi_processor_count)
212-
_dict_append(output, f"GPU {gpu} Total memory (GB)", lambda: round(gpu_info.total_memory / 1024 ** 3, 1))
212+
_dict_append(output, f"GPU {gpu} Total memory (GB)", lambda: round(gpu_info.total_memory / 1024**3, 1))
213213
_dict_append(output, f"GPU {gpu} CUDA capability (maj.min)", lambda: f"{gpu_info.major}.{gpu_info.minor}")
214214

215215
return output

monai/data/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def __init__(
513513
self.db_file = self.cache_dir / f"{db_name}.lmdb"
514514
self.lmdb_kwargs = lmdb_kwargs or {}
515515
if not self.lmdb_kwargs.get("map_size", 0):
516-
self.lmdb_kwargs["map_size"] = 1024 ** 4 # default map_size
516+
self.lmdb_kwargs["map_size"] = 1024**4 # default map_size
517517
# lmdb is single-writer multi-reader by default
518518
# the cache is created without multi-threading
519519
self._read_env = None

monai/data/dataset_summary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def calculate_statistics(self, foreground_threshold: int = 0):
150150

151151
self.data_max, self.data_min = max(voxel_max), min(voxel_min)
152152
self.data_mean = (voxel_sum / voxel_ct).item()
153-
self.data_std = (torch.sqrt(voxel_square_sum / voxel_ct - self.data_mean ** 2)).item()
153+
self.data_std = (torch.sqrt(voxel_square_sum / voxel_ct - self.data_mean**2)).item()
154154

155155
def calculate_percentiles(
156156
self,

monai/handlers/parameter_scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _exponential(initial_value: float, gamma: float, current_step: int) -> float
134134
Returns:
135135
float: new parameter value
136136
"""
137-
return initial_value * gamma ** current_step
137+
return initial_value * gamma**current_step
138138

139139
@staticmethod
140140
def _step(initial_value: float, gamma: float, step_size: int, current_step: int) -> float:

monai/losses/image_dissimilarity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
126126
if target.shape != pred.shape:
127127
raise ValueError(f"ground truth has differing shape ({target.shape}) from pred ({pred.shape})")
128128

129-
t2, p2, tp = target ** 2, pred ** 2, target * pred
129+
t2, p2, tp = target**2, pred**2, target * pred
130130
kernel, kernel_vol = self.kernel.to(pred), self.kernel_vol.to(pred)
131131
# sum over kernel
132132
t_sum = separable_filtering(target, kernels=[kernel.to(pred)] * self.ndim)
@@ -217,7 +217,7 @@ def __init__(
217217
self.num_bins = num_bins
218218
self.kernel_type = kernel_type
219219
if self.kernel_type == "gaussian":
220-
self.preterm = 1 / (2 * sigma ** 2)
220+
self.preterm = 1 / (2 * sigma**2)
221221
self.bin_centers = bin_centers[None, None, ...]
222222
self.smooth_nr = float(smooth_nr)
223223
self.smooth_dr = float(smooth_dr)
@@ -280,7 +280,7 @@ def parzen_windowing_b_spline(self, img: torch.Tensor, order: int) -> Tuple[torc
280280
weight = weight + (sample_bin_matrix < 0.5) + (sample_bin_matrix == 0.5) * 0.5
281281
elif order == 3:
282282
weight = (
283-
weight + (4 - 6 * sample_bin_matrix ** 2 + 3 * sample_bin_matrix ** 3) * (sample_bin_matrix < 1) / 6
283+
weight + (4 - 6 * sample_bin_matrix**2 + 3 * sample_bin_matrix**3) * (sample_bin_matrix < 1) / 6
284284
)
285285
weight = weight + (2 - sample_bin_matrix) ** 3 * (sample_bin_matrix >= 1) * (sample_bin_matrix < 2) / 6
286286
else:

monai/networks/blocks/selfattention.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, hidden_size: int, num_heads: int, dropout_rate: float = 0.0)
4646
self.drop_output = nn.Dropout(dropout_rate)
4747
self.drop_weights = nn.Dropout(dropout_rate)
4848
self.head_dim = hidden_size // num_heads
49-
self.scale = self.head_dim ** -0.5
49+
self.scale = self.head_dim**-0.5
5050

5151
def forward(self, x):
5252
q, k, v = einops.rearrange(self.qkv(x), "b h (qkv l d) -> qkv b l h d", qkv=3, l=self.num_heads)

monai/networks/blocks/upsample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def __init__(
219219
out_channels = out_channels or in_channels
220220
if not out_channels:
221221
raise ValueError("in_channels need to be specified.")
222-
conv_out_channels = out_channels * (scale_factor ** self.dimensions)
222+
conv_out_channels = out_channels * (scale_factor**self.dimensions)
223223
self.conv_block = Conv[Conv.CONV, self.dimensions](
224224
in_channels=in_channels, out_channels=conv_out_channels, kernel_size=3, stride=1, padding=1, bias=bias
225225
)
@@ -247,7 +247,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
247247
x: Tensor in shape (batch, channel, spatial_1[, spatial_2, ...).
248248
"""
249249
x = self.conv_block(x)
250-
if x.shape[1] % (self.scale_factor ** self.dimensions) != 0:
250+
if x.shape[1] % (self.scale_factor**self.dimensions) != 0:
251251
raise ValueError(
252252
f"Number of channels after `conv_block` ({x.shape[1]}) must be evenly "
253253
"divisible by scale_factor ** dimensions "

monai/networks/blocks/warp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def forward(self, dvf):
150150
Returns:
151151
a dense displacement field
152152
"""
153-
ddf: torch.Tensor = dvf / (2 ** self.num_steps)
153+
ddf: torch.Tensor = dvf / (2**self.num_steps)
154154
for _ in range(self.num_steps):
155155
ddf = ddf + self.warp_layer(image=ddf, ddf=ddf)
156156
return ddf

monai/networks/layers/convutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def gaussian_1d(
115115
out = out.clamp(min=0)
116116
elif approx.lower() == "sampled":
117117
x = torch.arange(-tail, tail + 1, dtype=torch.float, device=sigma.device)
118-
out = torch.exp(-0.5 / (sigma * sigma) * x ** 2)
118+
out = torch.exp(-0.5 / (sigma * sigma) * x**2)
119119
if not normalize: # compute the normalizer
120120
out = out / (2.5066282 * sigma)
121121
elif approx.lower() == "scalespace":

monai/networks/nets/dints.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(self, in_channel: int, out_channel: int, spatial_dims: int = 3):
124124
# s0 is upsampled 2x from s1, representing feature sizes at two resolutions.
125125
# in_channel * s0 (activation) + 3 * out_channel * s1 (convolution, concatenation, normalization)
126126
# s0 = s1 * 2^(spatial_dims) = output_size / out_channel * 2^(spatial_dims)
127-
self.ram_cost = in_channel / out_channel * 2 ** self._spatial_dims + 3
127+
self.ram_cost = in_channel / out_channel * 2**self._spatial_dims + 3
128128

129129

130130
class MixedOp(nn.Module):
@@ -330,7 +330,7 @@ def __init__(
330330
# define downsample stems before DiNTS search
331331
if use_downsample:
332332
self.stem_down[str(res_idx)] = StemTS(
333-
nn.Upsample(scale_factor=1 / (2 ** res_idx), mode=mode, align_corners=True),
333+
nn.Upsample(scale_factor=1 / (2**res_idx), mode=mode, align_corners=True),
334334
conv_type(
335335
in_channels=in_channels,
336336
out_channels=self.filter_nums[res_idx],
@@ -373,7 +373,7 @@ def __init__(
373373

374374
else:
375375
self.stem_down[str(res_idx)] = StemTS(
376-
nn.Upsample(scale_factor=1 / (2 ** res_idx), mode=mode, align_corners=True),
376+
nn.Upsample(scale_factor=1 / (2**res_idx), mode=mode, align_corners=True),
377377
conv_type(
378378
in_channels=in_channels,
379379
out_channels=self.filter_nums[res_idx],
@@ -789,7 +789,7 @@ def get_ram_cost_usage(self, in_size, full: bool = False):
789789
image_size = np.array(in_size[-self._spatial_dims :])
790790
sizes = []
791791
for res_idx in range(self.num_depths):
792-
sizes.append(batch_size * self.filter_nums[res_idx] * (image_size // (2 ** res_idx)).prod())
792+
sizes.append(batch_size * self.filter_nums[res_idx] * (image_size // (2**res_idx)).prod())
793793
sizes = torch.tensor(sizes).to(torch.float32).to(self.device) / (2 ** (int(self.use_downsample)))
794794
probs_a, arch_code_prob_a = self.get_prob_a(child=False)
795795
cell_prob = F.softmax(self.log_alpha_c, dim=-1)
@@ -807,7 +807,7 @@ def get_ram_cost_usage(self, in_size, full: bool = False):
807807
* (1 + (ram_cost[blk_idx, path_idx] * cell_prob[blk_idx, path_idx]).sum())
808808
* sizes[self.arch_code2out[path_idx]]
809809
)
810-
return usage * 32 / 8 / 1024 ** 2
810+
return usage * 32 / 8 / 1024**2
811811

812812
def get_topology_entropy(self, probs):
813813
"""

0 commit comments

Comments
 (0)