Skip to content

Commit 2d62cd4

Browse files
authored
Merge pull request #48 from IDEALLab/new_metrics
Metrics/Algo Bugfixes
2 parents 92a08be + 19a2f61 commit 2d62cd4

15 files changed

Lines changed: 28 additions & 28 deletions

engiopt/cgan_1d/cgan_1d.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ def prepare_data(problem: Problem, device: th.device) -> tuple[th.utils.data.Ten
212212
else:
213213
dummy_design, _ = problem.random_design()
214214
design_shape = spaces.flatten(problem.design_space, dummy_design).shape
215-
conditions = problem.conditions
216-
n_conds = len(conditions)
215+
n_conds = len(problem.conditions_keys)
217216

218217
# Logging
219218
run_name = f"{args.problem_id}__{args.algo}__{args.seed}__{int(time.time())}"
@@ -360,7 +359,7 @@ def sample_designs(n_designs: int) -> tuple[th.Tensor, th.Tensor]:
360359
ax.figure.canvas.draw()
361360
img = np.array(fig.canvas.renderer.buffer_rgba())
362361
axes[j].imshow(img)
363-
title = [(conditions[i][0], f"{dc[i]:.2f}") for i in range(n_conds)]
362+
title = [(problem.conditions_keys[i], f"{dc[i]:.2f}") for i in range(n_conds)]
364363
title_string = "\n ".join(f"{condition}: {value}" for condition, value in title)
365364
axes[j].title.set_text(title_string) # Set title
366365
axes[j].set_xticks([]) # Hide x ticks

engiopt/cgan_1d/evaluate_cgan_1d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def __init__(self):
9898

9999
model = Generator(
100100
latent_dim=run.config["latent_dim"],
101-
n_conds=len(problem.conditions),
101+
n_conds=len(problem.conditions_keys),
102102
design_shape=design_shape,
103103
design_normalizer=design_normalizer,
104104
conds_normalizer=conds_normalizer,

engiopt/cgan_2d/cgan_2d.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ def forward(self, design: th.Tensor, conds: th.Tensor) -> th.Tensor:
139139
problem.reset(seed=args.seed)
140140

141141
design_shape = problem.design_space.shape
142-
conditions = problem.conditions
143-
n_conds = len(conditions)
142+
n_conds = len(problem.conditions_keys)
144143

145144
# Logging
146145
run_name = f"{args.problem_id}__{args.algo}__{args.seed}__{int(time.time())}"
@@ -280,7 +279,7 @@ def sample_designs(n_designs: int) -> tuple[th.Tensor, th.Tensor]:
280279
img = tensor.cpu().numpy().reshape(design_shape[0], design_shape[1]) # Extract x and y coordinates
281280
dc = desired_conds[j].cpu()
282281
axes[j].imshow(img) # Scatter plot
283-
title = [(conditions[i][0], f"{dc[i]:.2f}") for i in range(n_conds)]
282+
title = [(problem.conditions_keys[i], f"{dc[i]:.2f}") for i in range(n_conds)]
284283
title_string = "\n ".join(f"{condition}: {value}" for condition, value in title)
285284
axes[j].title.set_text(title_string) # Set title
286285
axes[j].set_xticks([]) # Hide x ticks

engiopt/cgan_2d/evaluate_cgan_2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(self):
8888

8989
model = Generator(
9090
latent_dim=run.config["latent_dim"],
91-
n_conds=len(problem.conditions),
91+
n_conds=len(problem.conditions_keys),
9292
design_shape=problem.design_space.shape,
9393
).to(device)
9494
model.load_state_dict(ckpt["generator"])

engiopt/cgan_bezier/cgan_bezier.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def denormalize(self, x: th.Tensor) -> th.Tensor:
460460
training_ds = th.utils.data.TensorDataset(
461461
th.stack(coords_set),
462462
th.stack(design_scalars).unsqueeze(1),
463-
*[problem_dataset[key][:] for key, _ in problem.conditions],
463+
*[problem_dataset[key][:] for key in problem.conditions_keys],
464464
)
465465

466466
cond_tensors = th.stack(training_ds.tensors[2:])
@@ -481,7 +481,7 @@ def denormalize(self, x: th.Tensor) -> th.Tensor:
481481
discriminator = Discriminator(
482482
latent_dim=args.latent_dim,
483483
design_scalars=len(design_scalar_keys),
484-
num_conds=len(problem.conditions),
484+
num_conds=len(problem.conditions_keys),
485485
design_shape=problem.design_space["coords"].shape,
486486
conds_normalizer=conds_normalizer,
487487
design_scalars_normalizer=design_scalars_normalizer,
@@ -490,7 +490,7 @@ def denormalize(self, x: th.Tensor) -> th.Tensor:
490490
generator = Generator(
491491
latent_dim=args.latent_dim,
492492
noise_dim=args.noise_dim,
493-
num_conds=len(problem.conditions),
493+
num_conds=len(problem.conditions_keys),
494494
n_control_points=bezier_control_pts,
495495
n_data_points=n_data_points,
496496
conds_normalizer=conds_normalizer,
@@ -602,7 +602,7 @@ def sample_designs(n_designs: int) -> tuple[th.Tensor, th.Tensor, th.Tensor]:
602602
ax.figure.canvas.draw()
603603
img = np.array(fig.canvas.renderer.buffer_rgba())
604604
axes[j].imshow(img)
605-
title = [(problem.conditions[i - 1][0], f"{do1[i]:.2f}") for i in range(1, len(do1))]
605+
title = [(problem.conditions_keys[i - 1], f"{do1[i]:.2f}") for i in range(1, len(do1))]
606606
title_string = "\n ".join(f"{condition}: {value}" for condition, value in title)
607607
axes[j].title.set_text(title_string) # Set title
608608
axes[j].set_xticks([])

engiopt/cgan_cnn_2d/cgan_cnn_2d.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ def forward(self, x: th.Tensor, c: th.Tensor) -> th.Tensor:
235235
problem.reset(seed=args.seed)
236236

237237
design_shape = problem.design_space.shape
238-
conditions = problem.conditions
239-
n_conds = len(conditions)
238+
n_conds = len(problem.conditions_keys)
240239

241240
# Logging
242241
run_name = f"{args.problem_id}__{args.algo}__{args.seed}__{int(time.time())}"
@@ -377,7 +376,7 @@ def sample_designs(n_designs: int) -> tuple[th.Tensor, th.Tensor]:
377376
img = tensor.cpu().numpy().reshape(design_shape[0], design_shape[1]) # Extract x and y coordinates
378377
dc = desired_conds[j].cpu()
379378
axes[j].imshow(img) # Scatter plot
380-
title = [(conditions[i][0], f"{dc[i]:.2f}") for i in range(n_conds)]
379+
title = [(problem.conditions_keys[i], f"{dc[i]:.2f}") for i in range(n_conds)]
381380
title_string = "\n ".join(f"{condition}: {value}" for condition, value in title)
382381
axes[j].title.set_text(title_string) # Set title
383382
axes[j].set_xticks([]) # Hide x ticks

engiopt/cgan_cnn_2d/evaluate_cgan_cnn_2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self):
8787
ckpt_path = os.path.join(artifact_dir, "generator.pth")
8888
ckpt = th.load(ckpt_path, map_location=th.device(device))
8989
model = Generator(
90-
latent_dim=run.config["latent_dim"], n_conds=len(problem.conditions), design_shape=problem.design_space.shape
90+
latent_dim=run.config["latent_dim"], n_conds=len(problem.conditions_keys), design_shape=problem.design_space.shape
9191
)
9292
model.load_state_dict(ckpt["generator"])
9393
model.eval() # Set to evaluation mode

engiopt/cgan_cnn_3d/cgan_cnn_3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def compute_gradient_penalty(discriminator, real_samples, fake_samples, conds, d
358358
if len(design_shape) != DESIGN_SHAPE_LEN:
359359
raise ValueError(f"Expected 3D design shape, got {design_shape}")
360360

361-
conditions = problem.conditions
361+
conditions = problem.conditions_keys
362362
n_conds = len(conditions)
363363
condition_names = [cond[0] for cond in conditions]
364364

engiopt/cgan_cnn_3d/evaluate_cgan_cnn_3d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import pandas as pd
1111
import torch as th
1212
import tyro
13-
import wandb
1413

1514
from engiopt import metrics
1615
from engiopt.cgan_cnn_3d.cgan_cnn_3d import Generator3D
1716
from engiopt.dataset_sample_conditions import sample_conditions
17+
import wandb
1818

1919

2020
@dataclasses.dataclass
@@ -63,7 +63,7 @@ class Args:
6363

6464
# Reshape to match the expected input shape for the model
6565
conditions_tensor = conditions_tensor.unsqueeze(-1).unsqueeze(-1)
66-
conditions_tensor = conditions_tensor.view(args.n_samples, len(problem.conditions), 1, 1, 1)
66+
conditions_tensor = conditions_tensor.view(args.n_samples, len(problem.conditions_keys), 1, 1, 1)
6767

6868
### Set Up Generator ###
6969

engiopt/cgan_vae/cgan_vae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def compute_gradient_penalty(discriminator, real_samples, fake_samples, conds, d
451451
if len(design_shape) != DESIGN_SHAPE_LEN:
452452
raise ValueError(f"Expected 3D design shape, got {design_shape}")
453453

454-
conditions = problem.conditions
454+
conditions = problem.conditions_keys
455455
n_conds = len(conditions)
456456
condition_names = [cond[0] for cond in conditions]
457457

0 commit comments

Comments
 (0)