Skip to content

Commit 85d674b

Browse files
author
margovskiy_merck
committed
fix linting issues
1 parent 577e4fe commit 85d674b

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

obsidian/experiment/advanced_design.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AdvExpDesigner:
2222
"""
2323

2424
def __init__(
25-
self, continuous_params=None, conditional_subparameters=None, subparam_mapping=None, design_df = None
25+
self, continuous_params=None, conditional_subparameters=None, subparam_mapping=None, design_df=None
2626
):
2727
"""
2828
Initializes the AdvExpDesigner with experimental parameters and optional subparameter mappings.
@@ -37,17 +37,18 @@ def __init__(
3737

3838
if design_df is not None and not design_df.empty:
3939
self.design = design_df
40-
self.continuous_keys = list(self.continuous_params.keys()) if continuous_params else design_df.select_dtypes(include=['number']).columns.tolist()
4140
self.categorical_keys = design_df.select_dtypes(exclude=['number']).columns.tolist()
42-
else:
41+
if continuous_params:
42+
self.continuous_keys = list(self.continuous_params.keys())
43+
else:
44+
self.continuous_keys = design_df.select_dtypes(include=['number']).columns.tolist()
45+
else:
4346
self.continuous_keys = list(self.continuous_params.keys()) if continuous_params else []
4447
self.categorical_keys = list(self.conditional_subparameters.keys()) if conditional_subparameters else []
4548

4649
self.subparam_mapping = subparam_mapping or infer_subparam_mapping(self.conditional_subparameters)
4750
self.subparam_key = (list(self.subparam_mapping.values())[0] if self.subparam_mapping else None)
4851

49-
#consider adding a constructor function with DataFrame of design preloaded
50-
#include subparameter mapping schema, buffer_type -> pH, catalyst -> loading_range
5152

5253
def generate_design(self, seed, n_samples, optimize_categories=True):
5354
"""
@@ -433,7 +434,8 @@ def assign_conditional_subparameter(
433434

434435
def infer_subparam_mapping(conditional_subparameters):
435436
mapping = {}
436-
if len(conditional_subparameters) == 0: return mapping
437+
if len(conditional_subparameters) == 0:
438+
return mapping
437439
else:
438440
for cat_param, levels in conditional_subparameters.items():
439441
subparam_candidates = set()
@@ -923,7 +925,6 @@ def find_best_design_parallel(
923925
def plot_design_quality_evolution(metrics_df):
924926
metrics_df = metrics_df.sort_values("seed")
925927

926-
927928
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
928929
metrics = metrics_df.columns
929930

obsidian/experiment/sampling.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import numpy as np
1+
import numpy as np
22
import pandas as pd
33
import matplotlib.pyplot as plt
44

5+
56
def generate_weights(df, n, bias, plot_weights=False, enforce=False):
67
"""
7-
Generates a Pandas series of weights for each datum given a particular bias.
8+
Generates a Pandas series of weights for each datum given a particular bias.
89
910
df: DataFrame of candidates
1011
n: size of the design to pick
@@ -15,7 +16,7 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
1516
plot_weights: boolean, whether to plot distribution of weights, default False
1617
enforce: boolean, whether to force biases, default False
1718
18-
Returns: Pandas Series of normalized row weights.
19+
Returns: Pandas Series of normalized row weights.
1920
"""
2021
weights = pd.Series(1.0, index=df.index)
2122
for col, params in bias.items():
@@ -29,7 +30,7 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
2930
if enforce:
3031
if (weights > 0).sum() < n:
3132
raise ValueError(f"Not enough rows ({(weights > 0).sum()}) satisfy all enforce conditions for n={n}.")
32-
33+
3334
weights = weights / weights.sum()
3435

3536
print("Weights min:", weights.min(), "max:", weights.max())
@@ -43,10 +44,11 @@ def generate_weights(df, n, bias, plot_weights=False, enforce=False):
4344
plt.show()
4445

4546
return weights
46-
47+
48+
4749
def sample_with_bias(df, n, replace=False, seed=None, bias=None, enforce=False, plot_weights=False):
4850
"""
49-
Returns a random Pandas DataFrame sample of data points from a population with or without bias.
51+
Returns a random Pandas DataFrame sample of data points from a population with or without bias.
5052
5153
df: DataFrame of candidates
5254
n: int, size of the design to pick
@@ -58,14 +60,15 @@ def sample_with_bias(df, n, replace=False, seed=None, bias=None, enforce=False,
5860
enforce: boolean, whether to force biases, default False
5961
plot_weights: boolean, whether to plot distribution of weights, default False
6062
61-
Returns: Pandas DataFrame of sampled data points.
63+
Returns: Pandas DataFrame of sampled data points.
6264
"""
6365
if bias:
6466
w = generate_weights(df, n, bias, plot_weights, enforce)
6567
return df.sample(n=n, replace=replace, random_state=seed, weights=w)
6668
else:
6769
return df.sample(n=n, replace=replace, random_state=seed)
6870

71+
6972
def _space_filling_score(Z, metric="hybrid"):
7073
"""
7174
Z: (k, d) standardized features of the candidate sample
@@ -87,6 +90,7 @@ def _space_filling_score(Z, metric="hybrid"):
8790
return 0.6 * d_min + 0.4 * d_mnn
8891
raise ValueError("Unknown metric")
8992

93+
9094
def best_sample(df, k, feature_cols, *, n_trials=500, bias=None, plot_weights=False, enforce=False,
9195
random_state=None, standardize=True, dropna=True, metric="hybrid"):
9296
"""
@@ -103,7 +107,7 @@ def best_sample(df, k, feature_cols, *, n_trials=500, bias=None, plot_weights=Fa
103107
dfv = df.loc[idx]
104108
Xfull = base.loc[idx].to_numpy(dtype=float)
105109

106-
if bias:
110+
if bias:
107111
weights = generate_weights(df, k, bias, plot_weights, enforce)
108112
else:
109113
weights = None
@@ -137,4 +141,4 @@ def toZ(X): return X
137141
best_score = s
138142
best_df = cand
139143

140-
return best_df, {"score": best_score, "metric": metric, "n_trials": n_trials}
144+
return best_df, {"score": best_score, "metric": metric, "n_trials": n_trials}

0 commit comments

Comments
 (0)