Skip to content

Commit

Permalink
Merge pull request #119 from EwoutH/static-analysis
Browse files Browse the repository at this point in the history
Improve code quality with static analysis
  • Loading branch information
quaquel committed May 3, 2022
2 parents ec838cd + b4ce855 commit 6f4a4ae
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 74 deletions.
6 changes: 3 additions & 3 deletions ema_workbench/analysis/dimensional_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def discretize(data, nbins=3, with_labels=False):
n_unique = column_data.unique().shape[0]
n = n_unique
column_data = column_data.cat.rename_categories(
[x for x in range(1, n + 1)]
list(range(1, n + 1))
)
indices = column_data

Expand Down Expand Up @@ -414,8 +414,8 @@ def create_pivot_plot(
n = nr_levels * 2

scores = scores.index.tolist()
rows = [entry for entry in scores[0:n:2]]
columns = [entry for entry in scores[1:n:2]]
rows = list(scores[0:n:2])
columns = list(scores[1:n:2])

discretized_x = discretize(x, nbins=nbins, with_labels=bin_labels)

Expand Down
10 changes: 4 additions & 6 deletions ema_workbench/analysis/plotting_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def plot_violinplot(ax, values, log, group_labels=None):
if not group_labels:
group_labels = [""]

data = pd.DataFrame.from_records({k: v for k, v in zip(group_labels, values)})
data = pd.DataFrame.from_records(dict(zip(group_labels, values)))
data = pd.melt(data)

sns.violinplot(x="variable", y="value", data=data, order=group_labels, ax=ax)
Expand All @@ -246,7 +246,7 @@ def plot_boxenplot(ax, values, log, group_labels=None):
if not group_labels:
group_labels = [""]

data = pd.DataFrame.from_records({k: v for k, v in zip(group_labels, values)})
data = pd.DataFrame.from_records(dict(zip(group_labels, values)))
data = pd.melt(data)

sns.boxenplot(x="variable", y="value", data=data, order=group_labels, ax=ax)
Expand Down Expand Up @@ -771,7 +771,7 @@ def prepare_data(
if filter_scalar:
outcomes = filter_scalar_outcomes(outcomes)
if not outcomes_to_show:
outcomes_to_show = [o for o in outcomes.keys()]
outcomes_to_show = list(outcomes.keys())

# group the data if desired
if group_by:
Expand All @@ -783,9 +783,7 @@ def prepare_data(
)
else:
column_to_group_by = experiments[group_by]
if (column_to_group_by.dtype == object) or (
column_to_group_by.dtype == "category"
):
if column_to_group_by.dtype in (object, "category"):
grouping_specifiers = set(column_to_group_by)
else:
grouping_specifiers = make_continuous_grouping_specifiers(
Expand Down
2 changes: 1 addition & 1 deletion ema_workbench/analysis/prim_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def __call__(self, box):

def is_significant(box, i, alpha=0.05):
qp = box.qp[i]
return not any([value > alpha for values in qp.values() for value in values])
return not any(value > alpha for values in qp.values() for value in values)


def is_pareto_efficient(data):
Expand Down
4 changes: 2 additions & 2 deletions ema_workbench/analysis/scenario_discovery_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_sorted_box_lims(boxes, box_init):
# sort the uncertainties based on the normalized size of the
# restricted dimensions
uncs = uncs[np.argsort(box_size)]
box_lims = [box for box in boxes]
box_lims = list(boxes)

return box_lims, uncs.tolist()

Expand Down Expand Up @@ -454,7 +454,7 @@ def _setup_figure(uncs):
ax.add_patch(rect)
ax.set_xlim(left=-0.2, right=1.2)
ax.set_ylim(top=-0.5, bottom=nr_unc - 0.5)
ax.yaxis.set_ticks([y for y in range(nr_unc)])
ax.yaxis.set_ticks(list(range(nr_unc)))
ax.xaxis.set_ticks([0, 0.25, 0.5, 0.75, 1])
ax.set_yticklabels(uncs[::-1])
return fig, ax
Expand Down
13 changes: 1 addition & 12 deletions ema_workbench/connectors/vensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,6 @@ def check_data(result):

return results

def cleanup(self):
super().cleanup()

def reset_model(self):
"""
Method for reseting the model to its initial state before runModel
was called
"""

super().reset_model()

def _delete_lookup_uncertainties(self):
"""
deleting lookup uncertainties from the uncertainty list
Expand Down Expand Up @@ -552,7 +541,7 @@ def _get_initial_lookup(self, name):
list2 = []
number = []
for c in b:
if (c != "(") and (c != ")"):
if c not in ("(", ")"):
list1.append(c)

list1.append(",")
Expand Down
2 changes: 0 additions & 2 deletions ema_workbench/em_framework/evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ def robust_optimize(


class SequentialEvaluator(BaseEvaluator):
def __init__(self, models, **kwargs):
super().__init__(models, **kwargs)

def initialize(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion ema_workbench/em_framework/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def _optimize(
"number of epsilon values does not match number " "of outcomes"
)

if all([isinstance(t, klass) for t in problem.types]):
if all(isinstance(t, klass) for t in problem.types):
variator = None
else:
variator = CombinedVariator()
Expand Down
2 changes: 1 addition & 1 deletion ema_workbench/em_framework/outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def __eq__(self, other):
all(
hasattr(self, key) == hasattr(other, key)
and getattr(self, key) == getattr(other, key)
for key in self.__dict__.keys()
for key in self.__dict__
),
self.__class__ == other.__class__,
]
Expand Down
7 changes: 3 additions & 4 deletions ema_workbench/em_framework/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def __repr__(self, *args, **kwargs):


class Category(Constant):
def __init__(self, name, value):
super().__init__(name, value)
pass


def create_category(cat):
Expand Down Expand Up @@ -384,7 +383,7 @@ def __init__(
self._categories = NamedObjectMap(Category)

self.categories = cats
self.resolution = [i for i in range(len(self.categories))]
self.resolution = list(range(len(self.categories)))
self.multivalue = multivalue

def index_for_cat(self, category):
Expand Down Expand Up @@ -488,7 +487,7 @@ def parameters_to_csv(parameters, file_name):
else:
values = param.lower_bound, param.upper_bound

dict_repr = {j: value for j, value in enumerate(values)}
dict_repr = dict(enumerate(values))
dict_repr["name"] = param.name

params[i] = dict_repr
Expand Down
2 changes: 1 addition & 1 deletion ema_workbench/em_framework/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def combine_cases_sampling(*point_collection):

# figure out the longest
def exhaust_cases(cases):
return [case for case in cases]
return list(cases)

point_collection = [exhaust_cases(case) for case in point_collection]
longest_cases = max(point_collection, key=len)
Expand Down
12 changes: 0 additions & 12 deletions ema_workbench/em_framework/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class AbstractSampler(metaclass=abc.ABCMeta):
"""

def __init__(self):
super().__init__()

def sample(self, distribution, size):
"""
method for sampling a number of samples from a particular distribution.
Expand Down Expand Up @@ -132,9 +129,6 @@ class LHSSampler(AbstractSampler):
generates a Latin Hypercube sample for each of the parameters
"""

def __init__(self):
super().__init__()

def sample(self, distribution, size):
"""
generate a Latin Hypercube Sample.
Expand Down Expand Up @@ -253,9 +247,6 @@ class MonteCarloSampler(AbstractSampler):
"""

def __init__(self):
super().__init__()

def sample(self, distribution, size):
"""
generate a Monte Carlo Sample.
Expand Down Expand Up @@ -286,9 +277,6 @@ class FullFactorialSampler(AbstractSampler):
"""

def __init__(self):
super().__init__()

def generate_samples(self, parameters, size):
"""
The main method of :class: `~sampler.Sampler` and its
Expand Down
2 changes: 1 addition & 1 deletion ema_workbench/examples/eijgenraam_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
52: (49.2200, 1.6075, 0.0047, 0.036173, 0.304, 0.001716, 4025.6, 0.00171, 1 / 1250),
53: (69.4565, 1.1625, 0.0028, 0.031651, 0.336, 0.002700, 9819.5, 0.00171, 1 / 1250),
}
data = {i: {k: v for k, v in zip(params, raw_data[i])} for i in raw_data.keys()}
data = {i: dict(zip(params, raw_data[i])) for i in raw_data}

# Set the ring we are analyzing
ring = 15
Expand Down
10 changes: 5 additions & 5 deletions test/models/Sales_Agent_Market_Building_Dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def income():
tier_1_income()
+ tier_2_income()
+ if_then_else(
time() < startup_subsidy_length(), lambda: startup_subsidy(), lambda: 0
time() < startup_subsidy_length(), startup_subsidy, lambda: 0
)
)

Expand Down Expand Up @@ -1051,16 +1051,16 @@ def time_step():
return 0.0625


_integ_total_cumulative_sales = Integ(lambda: accumulating_sales(), lambda: 0)
_integ_total_cumulative_sales = Integ(accumulating_sales, lambda: 0)


_integ_tenure = Integ(lambda: accumulating_tenure(), lambda: 0)
_integ_tenure = Integ(accumulating_tenure, lambda: 0)


_integ_total_cumulative_income = Integ(lambda: accumulating_income(), lambda: 0)
_integ_total_cumulative_income = Integ(accumulating_income, lambda: 0)


_integ_months_of_buffer = Integ(lambda: income() - expenses(), lambda: initial_buffer())
_integ_months_of_buffer = Integ(lambda: income() - expenses(), initial_buffer)


_integ_tier_2_clients = Integ(
Expand Down
10 changes: 5 additions & 5 deletions test/models/Sales_Agent_Motivation_Dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def income():
Technically in units of months of expenses earned per month
"""
return months_of_expenses_per_sale() * sales() + if_then_else(
time() < startup_subsidy_length(), lambda: startup_subsidy(), lambda: 0
time() < startup_subsidy_length(), startup_subsidy, lambda: 0
)


Expand Down Expand Up @@ -481,13 +481,13 @@ def time_step():
return 0.0625


_integ_total_cumulative_income = Integ(lambda: accumulating_income(), lambda: 0)
_integ_total_cumulative_income = Integ(accumulating_income, lambda: 0)


_integ_total_cumulative_sales = Integ(lambda: accumulating_sales(), lambda: 0)
_integ_total_cumulative_sales = Integ(accumulating_sales, lambda: 0)


_integ_tenure = Integ(lambda: accumulating_tenure(), lambda: 0)
_integ_tenure = Integ(accumulating_tenure, lambda: 0)


_integ_motivation = Integ(lambda: motivation_adjustment(), lambda: 1)
_integ_motivation = Integ(motivation_adjustment, lambda: 1)
2 changes: 1 addition & 1 deletion test/test_em_framework/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_store_results(self):

_, out = callback.get_results()

self.assertIn(outcomes[0].name, {entry for entry in out.keys()})
self.assertIn(outcomes[0].name, set(out.keys()))
self.assertEqual(out[outcomes[0].name].shape, (3,))

# case 2 time series shape = (1, nr_time_steps)
Expand Down
2 changes: 1 addition & 1 deletion test/test_em_framework/test_ema_ipyparallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def add_engines(n=1, profile="iptest", total=False):
eps.append(ep)
tic = time.time()
while len(rc) < base + n:
if any([ep.poll() is not None for ep in eps]):
if any(ep.poll() is not None for ep in eps):
raise RuntimeError("A test engine failed to start.")
elif time.time() - tic > 15:
raise RuntimeError("Timeout waiting for engines to connect.")
Expand Down
3 changes: 1 addition & 2 deletions test/test_em_framework/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@


class FileModelTest(FileModel):
def run_model(self, scenario, policy):
super().run_model(scenario, policy)
pass


class TestFileModel(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions test/test_em_framework/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def test_experiment_gemerator(self):
experiments = points.experiment_generator(
scenarios, model_structures, policies, combine="factorial"
)
experiments = [e for e in experiments]
experiments = list(experiments)
self.assertEqual(
len(experiments), 6, ("wrong number of experiments " "for factorial")
)

experiments = points.experiment_generator(
scenarios, model_structures, policies, combine="sample"
)
experiments = [e for e in experiments]
experiments = list(experiments)
self.assertEqual(
len(experiments), 3, ("wrong number of experiments " "for zipover")
)
Expand All @@ -30,7 +30,7 @@ def test_experiment_gemerator(self):
experiments = points.experiment_generator(
scenarios, model_structures, policies, combine="adf"
)
_ = [e for e in experiments]
_ = list(experiments)

# def test_experiment_generator(self):
# sampler = LHSSampler()
Expand Down
2 changes: 1 addition & 1 deletion test/test_em_framework/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_namedict(self):
self.assertEqual(2, len(nd), "length not correct")

# test in
for entry in kwargs.keys():
for entry in kwargs:
self.assertIn(entry, nd, f"{entry} not in NamedDict")

# test addition
Expand Down
15 changes: 5 additions & 10 deletions test/test_util/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def test_save_results(self):
)
outcome_q = np.random.rand(nr_experiments, 1)

outcomes = {}
outcomes[ScalarOutcome("q").name] = outcome_q
outcomes = {ScalarOutcome("q").name: outcome_q}
results = (experiments, outcomes)

# test for 2d
Expand All @@ -61,8 +60,7 @@ def test_save_results(self):
)
outcome_r = np.zeros((nr_experiments, nr_timesteps))

outcomes = {}
outcomes[ArrayOutcome("r").name] = outcome_r
outcomes = {ArrayOutcome("r").name: outcome_r}
results = (experiments, outcomes)

save_results(results, fn)
Expand All @@ -78,8 +76,7 @@ def test_save_results(self):
)
outcome_s = np.zeros((nr_experiments, nr_timesteps, nr_replications))

outcomes = {}
outcomes[ArrayOutcome("s").name] = outcome_s
outcomes = {ArrayOutcome("s").name: outcome_s}
results = (experiments, outcomes)

save_results(results, fn)
Expand All @@ -104,8 +101,7 @@ def test_load_results(self):

outcome_a = np.zeros((nr_experiments, 1))

outcomes = {}
outcomes[ArrayOutcome("a").name] = outcome_a
outcomes = {ArrayOutcome("a").name: outcome_a}
results = (experiments, outcomes)

save_results(results, "../data/test.tar.gz")
Expand All @@ -129,8 +125,7 @@ def test_load_results(self):

outcome_b = np.zeros((nr_experiments, nr_timesteps, nr_replications))

outcomes = {}
outcomes[ArrayOutcome("b").name] = outcome_b
outcomes = {ArrayOutcome("b").name: outcome_b}
results = (experiments, outcomes)

save_results(results, "../data/test.tar.gz")
Expand Down

0 comments on commit 6f4a4ae

Please sign in to comment.