1
+ import os
2
+
1
3
import numpy as np
2
4
import pandas as pd
3
5
11
13
from scipy import stats
12
14
13
15
from ...utils import functions as func
14
-
16
+ from ... utils import paths as paths
15
17
16
18
def plotExplorationResults (
17
19
dfResults ,
@@ -26,6 +28,7 @@ def plotExplorationResults(
26
28
one_figure = False ,
27
29
contour = None ,
28
30
alpha_mask = None ,
31
+ savename = None ,
29
32
** kwargs ,
30
33
):
31
34
"""
@@ -136,29 +139,37 @@ def plotExplorationResults(
136
139
contour_alpha = kwargs ["contour_alpha" ] if "contour_alpha" in kwargs else 1
137
140
contour_kwargs = kwargs ["contour_kwargs" ] if "contour_kwargs" in kwargs else dict ()
138
141
139
- # check if this is a dataframe
140
- if isinstance (contour , pd .DataFrame ):
141
- contourPlotDf (
142
- contour ,
143
- color = contour_color ,
144
- ax = ax ,
145
- levels = contour_levels ,
146
- alpha = contour_alpha ,
147
- contour_kwargs = contour_kwargs ,
148
- )
149
- # if it's a string, take that value as the contour plot value
150
- elif isinstance (contour , str ):
151
- df_contour = df .pivot_table (values = contour , index = par2 , columns = par1 , dropna = False )
152
- if nan_to_zero :
153
- df_contour = df_contour .fillna (0 )
154
- contourPlotDf (
155
- df_contour ,
156
- color = contour_color ,
157
- ax = ax ,
158
- levels = contour_levels ,
159
- alpha = contour_alpha ,
160
- contour_kwargs = contour_kwargs ,
161
- )
142
+ def plot_contour (contour , contour_color , contour_levels , contour_alpha , contour_kwargs ):
143
+ # check if this is a dataframe
144
+ if isinstance (contour , pd .DataFrame ):
145
+ contourPlotDf (
146
+ contour ,
147
+ color = contour_color ,
148
+ ax = ax ,
149
+ levels = contour_levels ,
150
+ alpha = contour_alpha ,
151
+ contour_kwargs = contour_kwargs ,
152
+ )
153
+ # if it's a string, take that value as the contour plot value
154
+ elif isinstance (contour , str ):
155
+ df_contour = df .pivot_table (values = contour , index = par2 , columns = par1 , dropna = False )
156
+ if nan_to_zero :
157
+ df_contour = df_contour .fillna (0 )
158
+ contourPlotDf (
159
+ df_contour ,
160
+ color = contour_color ,
161
+ ax = ax ,
162
+ levels = contour_levels ,
163
+ alpha = contour_alpha ,
164
+ contour_kwargs = contour_kwargs ,
165
+ )
166
+
167
+ # check if contour is alist of variables, e.g. ["max_output", "domfr"]
168
+ if isinstance (contour , list ):
169
+ for ci in range (len (contour )):
170
+ plot_contour (contour [ci ], contour_color [ci ], contour_levels [ci ], contour_alpha [ci ], contour_kwargs [ci ])
171
+ else :
172
+ plot_contour (contour , contour_color , contour_levels , contour_alpha , contour_kwargs )
162
173
163
174
# colorbar
164
175
if one_figure == False :
@@ -178,14 +189,21 @@ def plotExplorationResults(
178
189
if not isinstance (i , tuple ):
179
190
i = (i ,)
180
191
if by != ["_by" ]:
181
- ax .set_title (" " .join ([f"{ bb } ={ bi } " for bb , bi in zip (by_label , i )]))
192
+ title = " " .join ([f"{ bb } ={ bi } " for bb , bi in zip (by_label , i )])
193
+ ax .set_title (title )
182
194
if one_figure == False :
195
+ if savename :
196
+ save_fname = os .path .join (paths .FIGURES_DIR , f"{ title } _{ savename } " )
197
+ plt .savefig (save_fname )
183
198
plt .show ()
184
199
else :
185
200
axi += 1
186
201
187
202
if one_figure == True :
188
203
plt .tight_layout ()
204
+ if savename :
205
+ save_fname = os .path .join (paths .FIGURES_DIR , f"{ savename } " )
206
+ plt .savefig (save_fname )
189
207
plt .show ()
190
208
191
209
@@ -200,15 +218,18 @@ def contourPlotDf(
200
218
clabel = False ,
201
219
** contour_kwargs ,
202
220
):
203
- levels = levels or [0 , 1.0001 ]
221
+ levels = levels or [0 , 1 ]
204
222
Xi , Yi = np .meshgrid (dataframe .columns , dataframe .index )
205
223
ax = ax or plt
206
224
207
225
if contourf :
208
226
contours = plt .contourf (Xi , Yi , dataframe , 10 , levels = levels , alpha = alpha , cmap = "plasma" )
209
227
228
+ # unpack, why necessary??
229
+ contour_kwargs = contour_kwargs ["contour_kwargs" ]
230
+
210
231
contours = ax .contour (
211
- Xi , Yi , dataframe , colors = color , linestyles = "solid" , levels = levels , zorder = 1 , alpha = alpha , ** contour_kwargs ,
232
+ Xi , Yi , dataframe , colors = color , levels = levels , zorder = 1 , alpha = alpha , ** contour_kwargs ,
212
233
)
213
234
214
235
clabel = contour_kwargs ["clabel" ] if "clabel" in contour_kwargs else False
0 commit comments