Skip to content

Commit baa8dfa

Browse files
authored
Vis: code cleanup (#100)
1 parent 1e166ec commit baa8dfa

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

petab/visualize/plotter.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ class MPLPlotter(Plotter):
4545
def __init__(self, figure: Figure, data_provider: DataProvider):
4646
super().__init__(figure, data_provider)
4747

48+
@staticmethod
49+
def _error_column_for_plot_type_data(plot_type_data: str) -> Optional[str]:
50+
"""Translate PEtab plotTypeData value to column name of internal
51+
data representation
52+
53+
Parameters
54+
----------
55+
plot_type_data: PEtab plotTypeData value
56+
Returns
57+
-------
58+
Name of corresponding column
59+
"""
60+
if plot_type_data == MEAN_AND_SD:
61+
return 'sd'
62+
if plot_type_data == MEAN_AND_SEM:
63+
return 'sem'
64+
if plot_type_data == PROVIDED:
65+
return 'noise_model'
66+
return None
67+
4868
def generate_lineplot(self, ax: 'matplotlib.pyplot.Axes',
4969
dataplot: DataPlot,
5070
plotTypeData: str) -> None:
@@ -67,14 +87,7 @@ def generate_lineplot(self, ax: 'matplotlib.pyplot.Axes',
6787
measurements_to_plot, simulations_to_plot = \
6888
self.data_provider.get_data_to_plot(dataplot,
6989
plotTypeData == PROVIDED)
70-
noise_col = None
71-
# set type of noise
72-
if plotTypeData == MEAN_AND_SD:
73-
noise_col = 'sd'
74-
elif plotTypeData == MEAN_AND_SEM:
75-
noise_col = 'sem'
76-
elif plotTypeData == PROVIDED:
77-
noise_col = 'noise_model'
90+
noise_col = self._error_column_for_plot_type_data(plotTypeData)
7891

7992
label_base = dataplot.legendEntry
8093

@@ -155,14 +168,7 @@ def generate_barplot(self, ax: 'matplotlib.pyplot.Axes',
155168
Specifies how replicates should be handled.
156169
"""
157170
# TODO: plotTypeData == REPLICATE?
158-
# set type of noise
159-
noise_col = None
160-
if plotTypeData == MEAN_AND_SD:
161-
noise_col = 'sd'
162-
elif plotTypeData == MEAN_AND_SEM:
163-
noise_col = 'sem'
164-
elif plotTypeData == PROVIDED:
165-
noise_col = 'noise_model'
171+
noise_col = self._error_column_for_plot_type_data(plotTypeData)
166172

167173
simu_colors = None
168174
measurements_to_plot, simulations_to_plot = \
@@ -222,7 +228,7 @@ def generate_scatterplot(self, ax: 'matplotlib.pyplot.Axes',
222228
ax.scatter(measurements_to_plot.data_to_plot['mean'],
223229
simulations_to_plot.data_to_plot['mean'],
224230
label=getattr(dataplot, LEGEND_ENTRY))
225-
ax = self._square_plot_equal_ranges(ax)
231+
self._square_plot_equal_ranges(ax)
226232

227233
def generate_subplot(self,
228234
ax,
@@ -278,11 +284,10 @@ def generate_subplot(self,
278284
elif subplot.xScale == 'order':
279285
ax.set_xscale("linear")
280286
# check if conditions are monotone decreasing or increasing
281-
if np.all(
282-
np.diff(subplot.conditions) < 0): # monot. decreasing
283-
xlabel = subplot.conditions[::-1] # reversing
284-
conditions = range(len(subplot.conditions))[
285-
::-1] # reversing
287+
if np.all(np.diff(subplot.conditions) < 0):
288+
# monot. decreasing -> reverse
289+
xlabel = subplot.conditions[::-1]
290+
conditions = range(len(subplot.conditions))[::-1]
286291
ax.set_xticks(range(len(conditions)), xlabel)
287292
elif np.all(np.diff(subplot.conditions) > 0):
288293
xlabel = subplot.conditions
@@ -305,7 +310,7 @@ def ticks(y, _):
305310
if subplot.yScale == LOG:
306311
ax.yaxis.set_major_formatter(mtick.FuncFormatter(ticks))
307312

308-
if not subplot.plotTypeSimulation == BAR_PLOT:
313+
if subplot.plotTypeSimulation != BAR_PLOT:
309314
ax.legend()
310315
ax.set_title(subplot.plotName)
311316
if subplot.xlim:
@@ -315,10 +320,8 @@ def ticks(y, _):
315320
ax.autoscale_view()
316321

317322
# Beautify plots
318-
ax.set_xlabel(
319-
subplot.xLabel)
320-
ax.set_ylabel(
321-
subplot.yLabel)
323+
ax.set_xlabel(subplot.xLabel)
324+
ax.set_ylabel(subplot.yLabel)
322325

323326
def generate_figure(
324327
self,

petab/visualize/plotting.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ def add_x_offset(self, offset) -> None:
7272
self.conditions += offset
7373

7474
def add_y_offset(self, offset):
75-
self.data_to_plot['mean'] = \
76-
self.data_to_plot['mean'] + offset
77-
self.data_to_plot['repl'] = \
78-
self.data_to_plot['repl'] + offset
75+
self.data_to_plot['mean'] += offset
76+
self.data_to_plot['repl'] += offset
7977

8078
def add_offsets(self, x_offset=0, y_offset=0) -> None:
8179
"""
@@ -348,9 +346,9 @@ def save_to_tsv(self, output_file_path: str = 'visuSpec.tsv') -> None:
348346
dataplot.__dict__ if key in
349347
VISUALIZATION_DF_SINGLE_PLOT_LEVEL_COLS}
350348
row = {**subplot_level, **dataset_level}
351-
for key in row:
349+
for key, value in row.items():
352350
if key in visu_dict:
353-
visu_dict[key].append(row[key])
351+
visu_dict[key].append(value)
354352
else:
355353
visu_dict[key] = [row[key]]
356354
visu_df = pd.DataFrame.from_dict(visu_dict)
@@ -596,7 +594,6 @@ def get_data_to_plot(self, dataplot: DataPlot, provided_noise: bool
596594
SIMULATION,
597595
dataplot,
598596
provided_noise)
599-
600597
return measurements_to_plot, simulations_to_plot
601598

602599

@@ -874,11 +871,12 @@ def _get_vis_spec_dependent_columns_dict(
874871
plot_id_column = ['plot%s' % str(ind + 1) for ind, inner_list in
875872
enumerate(dataset_id_list) for _ in inner_list]
876873

877-
columns_dict = {PLOT_ID: plot_id_column,
878-
DATASET_ID: dataset_id_column,
879-
LEGEND_ENTRY: dataset_label_column,
880-
Y_VALUES: yvalues_column}
881-
return columns_dict
874+
return {
875+
PLOT_ID: plot_id_column,
876+
DATASET_ID: dataset_id_column,
877+
LEGEND_ENTRY: dataset_label_column,
878+
Y_VALUES: yvalues_column
879+
}
882880

883881
def _create_legend(self, dataset_id: str) -> str:
884882
"""

0 commit comments

Comments
 (0)