Skip to content

Commit 9f9ce94

Browse files
Smooth simulation (#62)
* add possibility to plot simulation with more points than measurements * add ScatterPlot to Isensee example * add replicates test * skip one old test * Apply suggestions from code review Co-authored-by: ErikaDudkin <49193148+erikadudki@users.noreply.github.com> * returns in docstrings Co-authored-by: ErikaDudkin <49193148+erikadudki@users.noreply.github.com>
1 parent 99b2d21 commit 9f9ce94

File tree

6 files changed

+196
-153
lines changed

6 files changed

+196
-153
lines changed

doc/example/example_Isensee/Isensee_visualizationSpecification.tsv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ plot4 JI09_151102_Drg421_418_Age LinePlot MeanAndSD JI09_151102_Drg421_418_Age__
3030
plot5 JI09_160126_Drg449_444_CycNuc BarPlot MeanAndSD JI09_160126_Drg449_444_CycNuc__ctrl condition 0 Condition pRII_Western 0 pRII_Western Ctrl lin lin
3131
plot5 JI09_160126_Drg449_444_CycNuc BarPlot MeanAndSD JI09_160126_Drg449_444_CycNuc__Fsk10_and_IBMX100 condition 0 Condition pRII_Western 0 pRII_Western IBMX(100)/Fsk(10) lin lin
3232
plot5 JI09_160126_Drg449_444_CycNuc BarPlot MeanAndSD JI09_160126_Drg449_444_CycNuc__Sp8_Br_cAMPS_AM10 condition 0 Condition pRII_Western 0 pRII_Western Sp8-Br-cAMPS-AM(10) lin lin
33-
plot6 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__ctrl time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Ctrl lin lin
34-
plot6 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Fsk time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Fsk(10) lin lin
35-
plot6 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Sp8_Br_cAMPS_AM time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Sp8-Br-cAMPS-AM(10) lin lin
33+
plot6 JI09_160201_Drg453-452_CycNuc ScatterPlot MeanAndSD JI09_160201_Drg453-452_CycNuc__ctrl time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Ctrl lin lin
34+
plot6 JI09_160201_Drg453-452_CycNuc ScatterPlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Fsk time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Fsk(10) lin lin
35+
plot6 JI09_160201_Drg453-452_CycNuc ScatterPlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Sp8_Br_cAMPS_AM time 0 Time [min] Calpha_Microscopy 0 Calpha_Microscopy Sp8-Br-cAMPS-AM(10) lin lin
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plotId plotName plotTypeSimulation plotTypeData datasetId xValues xOffset yValues yOffset yLabel legendEntry xScale yScale
2+
plot1 JI09_150302_Drg345_343_CycNuc LinePlot MeanAndSD JI09_150302_Drg345_343_CycNuc__4_ABnOH_and_ctrl fourABnOH_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy 4-ABnOH/Ctrl lin lin
3+
plot1 JI09_150302_Drg345_343_CycNuc LinePlot MeanAndSD JI09_150302_Drg345_343_CycNuc__4_ABnOH_and_Fsk fourABnOH_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy 4-ABnOH/Fsk(10) lin lin
4+
plot1 JI09_150302_Drg345_343_CycNuc LinePlot MeanAndSD JI09_150302_Drg345_343_CycNuc__Rp_cAMPS_pAB_and_ctrl Rp_cAMPS_pAB_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy Rp-cAMPS-pAB/Ctrl lin lin
5+
plot1 JI09_150302_Drg345_343_CycNuc LinePlot MeanAndSD JI09_150302_Drg345_343_CycNuc__Rp_cAMPS_pAB_and_ctrl Rp_cAMPS_pAB_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy Rp-cAMPS-pAB/Fsk(10) lin lin
6+
plot2 JI09_150302_Drg345_343_CycNuc LinePlot replicate JI09_150302_Drg345_343_CycNuc__4_ABnOH_and_ctrl fourABnOH_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy 4-ABnOH/Ctrl lin lin
7+
plot2 JI09_150302_Drg345_343_CycNuc LinePlot replicate JI09_150302_Drg345_343_CycNuc__4_ABnOH_and_Fsk fourABnOH_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy 4-ABnOH/Fsk(10) lin lin
8+
plot2 JI09_150302_Drg345_343_CycNuc LinePlot replicate JI09_150302_Drg345_343_CycNuc__Rp_cAMPS_pAB_and_ctrl Rp_cAMPS_pAB_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy Rp-cAMPS-pAB/Ctrl lin lin
9+
plot2 JI09_150302_Drg345_343_CycNuc LinePlot replicate JI09_150302_Drg345_343_CycNuc__Rp_cAMPS_pAB_and_ctrl Rp_cAMPS_pAB_incubation_time 0 pRII_Microscopy 0 pRII_Microscopy Rp-cAMPS-pAB/Fsk(10) lin lin
10+
plot3 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__ctrl time 0 Calpha_Microscopy 0 Calpha_Microscopy Ctrl lin lin
11+
plot3 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Fsk time 0 Calpha_Microscopy 0 Calpha_Microscopy Fsk(10) lin lin
12+
plot3 JI09_160201_Drg453-452_CycNuc LinePlot MeanAndSD JI09_160201_Drg453-452_CycNuc__Sp8_Br_cAMPS_AM time 0 Calpha_Microscopy 0 Calpha_Microscopy Sp8-Br-cAMPS-AM(10) lin lin
13+
plot4 JI09_160201_Drg453-452_CycNuc LinePlot replicate JI09_160201_Drg453-452_CycNuc__ctrl time 0 Calpha_Microscopy 0 Calpha_Microscopy Ctrl lin lin
14+
plot4 JI09_160201_Drg453-452_CycNuc LinePlot replicate JI09_160201_Drg453-452_CycNuc__Fsk time 0 Calpha_Microscopy 0 Calpha_Microscopy Fsk(10) lin lin
15+
plot4 JI09_160201_Drg453-452_CycNuc LinePlot replicate JI09_160201_Drg453-452_CycNuc__Sp8_Br_cAMPS_AM time 0 Calpha_Microscopy 0 Calpha_Microscopy Sp8-Br-cAMPS-AM(10) lin lin

petab/visualize/plotter.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ def generate_lineplot(self, ax, dataplot: DataPlot, plotTypeData):
5252
plotTypeData
5353
"""
5454

55-
simu_colors = None
56-
data_to_plot = self.data_provider.get_data_to_plot(
57-
dataplot, plotTypeData == PROVIDED)
55+
simu_color = None
56+
measurements_to_plot, simulations_to_plot = \
57+
self.data_provider.get_data_to_plot(dataplot,
58+
plotTypeData == PROVIDED)
5859
noise_col = None
5960
# set type of noise
6061
if plotTypeData == MEAN_AND_SD:
@@ -66,66 +67,65 @@ def generate_lineplot(self, ax, dataplot: DataPlot, plotTypeData):
6667

6768
label_base = dataplot.legendEntry
6869

69-
if data_to_plot.measurements_to_plot is not None:
70+
if measurements_to_plot is not None:
7071
# plotting all measurement data
7172

7273
if plotTypeData == REPLICATE:
7374
replicates = np.stack(
74-
data_to_plot.measurements_to_plot.repl.values)
75+
measurements_to_plot.data_to_plot.repl.values)
7576

7677
# plot first replicate
7778
p = ax.plot(
78-
data_to_plot.conditions.values,
79+
measurements_to_plot.conditions,
7980
replicates[:, 0],
8081
linestyle='-.',
8182
marker='x', markersize=10, label=label_base
8283
)
8384

8485
# plot other replicates with the same color
8586
ax.plot(
86-
data_to_plot.conditions.values,
87+
measurements_to_plot.conditions,
8788
replicates[:, 1:],
8889
linestyle='-.',
8990
marker='x', markersize=10, color=p[0].get_color()
9091
)
9192

9293
# construct errorbar-plots: noise specified above
9394
else:
94-
# sort index for the case that indices of conditions and
95-
# measurements differ if indep_var='time', conditions is a
96-
# numpy array, for indep_var=observable its a Series
97-
if isinstance(data_to_plot.conditions, np.ndarray):
98-
data_to_plot.conditions.sort()
99-
elif isinstance(data_to_plot.conditions,
100-
pd.core.series.Series):
101-
data_to_plot.conditions.sort_index(inplace=True)
102-
else:
103-
raise ValueError('Strange: conditions object is '
104-
'neither numpy nor series...')
105-
data_to_plot.measurements_to_plot.sort_index(inplace=True)
10695
# sorts according to ascending order of conditions
10796
scond, smean, snoise = \
10897
zip(*sorted(zip(
109-
data_to_plot.conditions,
110-
data_to_plot.measurements_to_plot['mean'],
111-
data_to_plot.measurements_to_plot[noise_col])))
98+
measurements_to_plot.conditions,
99+
measurements_to_plot.data_to_plot['mean'],
100+
measurements_to_plot.data_to_plot[noise_col])))
112101
p = ax.errorbar(
113102
scond, smean, snoise,
114103
linestyle='-.', marker='.', label=label_base
115104
)
116105

117106
# simulations should have the same colors if both measurements
118107
# and simulations are plotted
119-
simu_colors = p[0].get_color()
108+
simu_color = p[0].get_color()
120109

121110
# construct simulation plot
122-
if data_to_plot.simulations_to_plot is not None:
123-
124-
xs, ys = zip(*sorted(zip(data_to_plot.conditions,
125-
data_to_plot.simulations_to_plot)))
111+
if simulations_to_plot is not None:
112+
# markers will be displayed only for points that have measurement
113+
# counterpart
114+
if measurements_to_plot is not None:
115+
meas_conditions = measurements_to_plot.conditions.to_numpy() \
116+
if isinstance(measurements_to_plot.conditions, pd.Series) \
117+
else measurements_to_plot.conditions
118+
every = [condition in meas_conditions
119+
for condition in simulations_to_plot.conditions]
120+
else:
121+
every = None
122+
# sorts according to ascending order of conditions
123+
xs, ys = zip(*sorted(zip(simulations_to_plot.conditions,
124+
simulations_to_plot.data_to_plot['mean'])
125+
))
126126
ax.plot(
127-
xs, ys, linestyle='-', marker='o',
128-
label=label_base + " simulation", color=simu_colors
127+
xs, ys, linestyle='-', marker='o', markevery=every,
128+
label=label_base + " simulation", color=simu_color
129129
)
130130

131131
def generate_barplot(self, ax, dataplot: DataPlot, plotTypeData: str):
@@ -149,12 +149,13 @@ def generate_barplot(self, ax, dataplot: DataPlot, plotTypeData: str):
149149
noise_col = 'noise_model'
150150

151151
simu_colors = None
152-
data_to_plot = self.data_provider.get_data_to_plot(
153-
dataplot, plotTypeData == PROVIDED)
152+
measurements_to_plot, simulations_to_plot = \
153+
self.data_provider.get_data_to_plot(dataplot,
154+
plotTypeData == PROVIDED)
154155

155156
x_name = dataplot.legendEntry
156157

157-
if data_to_plot.simulations_to_plot:
158+
if simulations_to_plot:
158159
bar_kwargs = {
159160
'align': 'edge',
160161
'width': -1/3,
@@ -167,16 +168,17 @@ def generate_barplot(self, ax, dataplot: DataPlot, plotTypeData: str):
167168

168169
color = plt.rcParams["axes.prop_cycle"].by_key()["color"][0]
169170

170-
if data_to_plot.measurements_to_plot is not None:
171-
p = ax.bar(x_name, data_to_plot.measurements_to_plot['mean'],
172-
yerr=data_to_plot.measurements_to_plot[noise_col],
171+
if measurements_to_plot is not None:
172+
p = ax.bar(x_name, measurements_to_plot.data_to_plot['mean'],
173+
yerr=measurements_to_plot.data_to_plot[noise_col],
173174
color=color, **bar_kwargs, label='measurement')
174175
simu_colors = p[0].get_facecolor()
175176

176-
if data_to_plot.simulations_to_plot is not None:
177+
if simulations_to_plot is not None:
177178
bar_kwargs['width'] = -bar_kwargs['width']
178-
ax.bar(x_name, data_to_plot.simulations_to_plot, color='white',
179-
edgecolor=simu_colors, **bar_kwargs, label='simulation')
179+
ax.bar(x_name, simulations_to_plot.data_to_plot['mean'],
180+
color='white', edgecolor=simu_colors, **bar_kwargs,
181+
label='simulation')
180182

181183
def generate_scatterplot(self, ax, dataplot: DataPlot, plotTypeData: str):
182184
"""
@@ -189,14 +191,15 @@ def generate_scatterplot(self, ax, dataplot: DataPlot, plotTypeData: str):
189191
plotTypeData
190192
"""
191193

192-
data_to_plot = self.data_provider.get_data_to_plot(
193-
dataplot, plotTypeData == PROVIDED)
194+
measurements_to_plot, simulations_to_plot = \
195+
self.data_provider.get_data_to_plot(dataplot,
196+
plotTypeData == PROVIDED)
194197

195-
if data_to_plot.simulations_to_plot is None:
198+
if simulations_to_plot is None:
196199
raise NotImplementedError('Scatter plots do not work without'
197200
' simulation data')
198-
ax.scatter(data_to_plot.measurements_to_plot['mean'],
199-
data_to_plot.simulations_to_plot,
201+
ax.scatter(measurements_to_plot.data_to_plot['mean'],
202+
simulations_to_plot.data_to_plot['mean'],
200203
label=getattr(dataplot, LEGEND_ENTRY))
201204
ax = self._square_plot_equal_ranges(ax)
202205

0 commit comments

Comments
 (0)