Skip to content

Commit 2f7e488

Browse files
[Fix] Replace os.path.isdir with len (#830)
* replace os.path.isdir with len * enumerate from 0
1 parent 326dcc0 commit 2f7e488

File tree

6 files changed

+10
-13
lines changed

6 files changed

+10
-13
lines changed

ppsci/arch/mlp.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,9 @@ def forward_tensor(self, x):
300300
u = self.embed_u(x)
301301
v = self.embed_v(x)
302302

303-
x = self.linears[0](x)
304-
x = self.acts[0](x)
305-
306303
y = x
307304
skip = None
308-
for i, linear in enumerate(self.linears[1:], 1):
305+
for i, linear in enumerate(self.linears):
309306
y = linear(y)
310307
y = self.acts[i](y)
311308
y = (1 - y) * u + y * v

ppsci/utils/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def init_logger(
104104
# add file_handler, output to log_file(if specified), only for rank 0 device
105105
if log_file is not None and dist.get_rank() == 0:
106106
log_file_folder = os.path.dirname(log_file)
107-
if os.path.isdir(log_file_folder):
107+
if len(log_file_folder):
108108
os.makedirs(log_file_folder, exist_ok=True)
109109
file_formatter = logging.Formatter(
110110
"[%(asctime)s] %(name)s %(levelname)s: %(message)s",

ppsci/utils/symbolic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def add_edge(u: str, v: str, u_color: str = C_DATA, v_color: str = C_DATA):
615615
graph.layout()
616616
image_path = f"{graph_filename}.png"
617617
dot_path = f"{graph_filename}.dot"
618-
if os.path.isdir(os.path.dirname(image_path)):
618+
if len(os.path.dirname(image_path)):
619619
os.makedirs(os.path.dirname(image_path), exist_ok=True)
620620
graph.draw(image_path, prog="dot")
621621
graph.write(dot_path)

ppsci/utils/writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def save_tecplot_file(
182182
nx, ny = num_x, num_y
183183
assert nx * ny == nxy, f"nx({nx}) * ny({ny}) != nxy({nxy})"
184184

185-
if os.path.isdir(os.path.dirname(filename)):
185+
if len(os.path.dirname(filename)):
186186
os.makedirs(os.path.dirname(filename), exist_ok=True)
187187

188188
if filename.endswith(".dat"):

ppsci/visualize/plot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _save_plot_from_1d_array(filename, coord, value, value_keys, num_timestamps=
8080
value_keys (Tuple[str, ...]): Value keys.
8181
num_timestamps (int, optional): Number of timestamps coord/value contains. Defaults to 1.
8282
"""
83-
if os.path.isdir(os.path.dirname(filename)):
83+
if len(os.path.dirname(filename)):
8484
os.makedirs(os.path.dirname(filename), exist_ok=True)
8585
fig, a = plt.subplots(len(value_keys), num_timestamps, squeeze=False)
8686
fig.subplots_adjust(hspace=0.8)
@@ -188,7 +188,7 @@ def _save_plot_from_2d_array(
188188
xticks (Optional[Tuple[float, ...]]): Tuple of xtick locations. Defaults to None.
189189
yticks (Optional[Tuple[float, ...]]): Tuple of ytick locations. Defaults to None.
190190
"""
191-
if os.path.isdir(os.path.dirname(filename)):
191+
if len(os.path.dirname(filename)):
192192
os.makedirs(os.path.dirname(filename), exist_ok=True)
193193

194194
plt.close("all")
@@ -336,7 +336,7 @@ def _save_plot_from_3d_array(
336336
visu_keys (Tuple[str, ...]): Keys for visualizing data. such as ("u", "v").
337337
num_timestamps (int, optional): Number of timestamps coord/value contains. Defaults to 1.
338338
"""
339-
if os.path.isdir(os.path.dirname(filename)):
339+
if len(os.path.dirname(filename)):
340340
os.makedirs(os.path.dirname(filename), exist_ok=True)
341341

342342
fig = plt.figure(figsize=(10, 10))
@@ -482,7 +482,7 @@ def plot_weather(
482482
)
483483
plt.colorbar(mappable=map_, cax=None, ax=None, shrink=0.5, label=colorbar_label)
484484

485-
if os.path.isdir(os.path.dirname(filename)):
485+
if len(os.path.dirname(filename)):
486486
os.makedirs(os.path.dirname(filename), exist_ok=True)
487487
fig = plt.figure(facecolor="w", figsize=(7, 7))
488488
ax = fig.add_subplot(2, 1, 1)

ppsci/visualize/vtu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _save_vtu_from_array(filename, coord, value, value_keys, num_timestamps=1):
5353
if coord.shape[1] not in [2, 3]:
5454
raise ValueError(f"ndim of coord({coord.shape[1]}) should be 2 or 3.")
5555

56-
if os.path.isdir(os.path.dirname(filename)):
56+
if len(os.path.dirname(filename)):
5757
os.makedirs(os.path.dirname(filename), exist_ok=True)
5858

5959
# discard extension name
@@ -184,6 +184,6 @@ def save_vtu_to_mesh(
184184
points=points.T, cells=[("vertex", np.arange(npoint).reshape(npoint, 1))]
185185
)
186186
mesh.point_data = {key: data_dict[key] for key in value_keys}
187-
if os.path.isdir(os.path.dirname(filename)):
187+
if len(os.path.dirname(filename)):
188188
os.makedirs(os.path.dirname(filename), exist_ok=True)
189189
mesh.write(filename)

0 commit comments

Comments
 (0)