@@ -107,7 +107,7 @@ def _build_model(self) -> pd.DataFrame:
107
107
108
108
logger .debug (f"Time Index Monotonic: { data_i .index .is_monotonic } " )
109
109
110
- if self .loaded_models is not None :
110
+ if self .loaded_models is not None and s_id in self . loaded_models :
111
111
model = self .loaded_models [s_id ]
112
112
else :
113
113
model = automl .Pipeline (
@@ -208,82 +208,85 @@ def _generate_report(self):
208
208
)
209
209
selected_models = dict ()
210
210
models = self .models
211
- for i , (s_id , df ) in enumerate (self .full_data_dict .items ()):
212
- selected_models [s_id ] = {
213
- "series_id" : s_id ,
214
- "selected_model" : models [s_id ].selected_model_ ,
215
- "model_params" : models [s_id ].selected_model_params_ ,
216
- }
217
- selected_models_df = pd .DataFrame (
218
- selected_models .items (), columns = ["series_id" , "best_selected_model" ]
219
- )
220
- selected_df = selected_models_df ["best_selected_model" ].apply (pd .Series )
221
- selected_models_section = dp .Blocks (
222
- "### Best Selected Model" , dp .DataTable (selected_df )
223
- )
211
+ all_sections = []
212
+
213
+ if len (self .models ) > 0 :
214
+ for i , (s_id , m ) in enumerate (models .items ()):
215
+ selected_models [s_id ] = {
216
+ "series_id" : s_id ,
217
+ "selected_model" : m .selected_model_ ,
218
+ "model_params" : m .selected_model_params_ ,
219
+ }
220
+ selected_models_df = pd .DataFrame (
221
+ selected_models .items (), columns = ["series_id" , "best_selected_model" ]
222
+ )
223
+ selected_df = selected_models_df ["best_selected_model" ].apply (pd .Series )
224
+ selected_models_section = dp .Blocks (
225
+ "### Best Selected Model" , dp .DataTable (selected_df )
226
+ )
224
227
225
- all_sections = [selected_models_text , selected_models_section ]
228
+ all_sections = [selected_models_text , selected_models_section ]
226
229
227
230
if self .spec .generate_explanations :
228
- # try:
229
- # If the key is present, call the "explain_model" method
230
- self .explain_model ()
231
-
232
- # Create a markdown text block for the global explanation section
233
- global_explanation_text = dp .Text (
234
- f"## Global Explanation of Models \n "
235
- "The following tables provide the feature attribution for the global explainability."
236
- )
237
-
238
- # Convert the global explanation data to a DataFrame
239
- global_explanation_df = pd .DataFrame (self .global_explanation )
231
+ try :
232
+ # If the key is present, call the "explain_model" method
233
+ self .explain_model ()
240
234
241
- self .formatted_global_explanation = (
242
- global_explanation_df / global_explanation_df .sum (axis = 0 ) * 100
243
- )
244
- self .formatted_global_explanation = (
245
- self .formatted_global_explanation .rename (
246
- {self .spec .datetime_column .name : ForecastOutputColumns .DATE }, axis = 1
235
+ # Create a markdown text block for the global explanation section
236
+ global_explanation_text = dp .Text (
237
+ f"## Global Explanation of Models \n "
238
+ "The following tables provide the feature attribution for the global explainability."
247
239
)
248
- )
249
240
250
- # Create a markdown section for the global explainability
251
- global_explanation_section = dp .Blocks (
252
- "### Global Explainability " ,
253
- dp .DataTable (self .formatted_global_explanation ),
254
- )
241
+ # Convert the global explanation data to a DataFrame
242
+ global_explanation_df = pd .DataFrame (self .global_explanation )
255
243
256
- aggregate_local_explanations = pd .DataFrame ()
257
- for s_id , local_ex_df in self .local_explanation .items ():
258
- local_ex_df_copy = local_ex_df .copy ()
259
- local_ex_df_copy ["Series" ] = s_id
260
- aggregate_local_explanations = pd .concat (
261
- [aggregate_local_explanations , local_ex_df_copy ], axis = 0
244
+ self .formatted_global_explanation = (
245
+ global_explanation_df / global_explanation_df .sum (axis = 0 ) * 100
246
+ )
247
+ self .formatted_global_explanation = (
248
+ self .formatted_global_explanation .rename (
249
+ {self .spec .datetime_column .name : ForecastOutputColumns .DATE }, axis = 1
250
+ )
262
251
)
263
- self .formatted_local_explanation = aggregate_local_explanations
264
252
265
- local_explanation_text = dp .Text (f"## Local Explanation of Models \n " )
266
- blocks = [
267
- dp .DataTable (
268
- local_ex_df .div (local_ex_df .abs ().sum (axis = 1 ), axis = 0 ) * 100 ,
269
- label = s_id ,
253
+ # Create a markdown section for the global explainability
254
+ global_explanation_section = dp .Blocks (
255
+ "### Global Explainability " ,
256
+ dp .DataTable (self .formatted_global_explanation ),
270
257
)
271
- for s_id , local_ex_df in self .local_explanation .items ()
272
- ]
273
- local_explanation_section = (
274
- dp .Select (blocks = blocks ) if len (blocks ) > 1 else blocks [0 ]
275
- )
276
258
277
- # Append the global explanation text and section to the "all_sections" list
278
- all_sections = all_sections + [
279
- global_explanation_text ,
280
- global_explanation_section ,
281
- local_explanation_text ,
282
- local_explanation_section ,
283
- ]
284
- # except Exception as e:
285
- # logger.warn(f"Failed to generate Explanations with error: {e}.")
286
- # logger.debug(f"Full Traceback: {traceback.format_exc()}")
259
+ aggregate_local_explanations = pd .DataFrame ()
260
+ for s_id , local_ex_df in self .local_explanation .items ():
261
+ local_ex_df_copy = local_ex_df .copy ()
262
+ local_ex_df_copy ["Series" ] = s_id
263
+ aggregate_local_explanations = pd .concat (
264
+ [aggregate_local_explanations , local_ex_df_copy ], axis = 0
265
+ )
266
+ self .formatted_local_explanation = aggregate_local_explanations
267
+
268
+ local_explanation_text = dp .Text (f"## Local Explanation of Models \n " )
269
+ blocks = [
270
+ dp .DataTable (
271
+ local_ex_df .div (local_ex_df .abs ().sum (axis = 1 ), axis = 0 ) * 100 ,
272
+ label = s_id ,
273
+ )
274
+ for s_id , local_ex_df in self .local_explanation .items ()
275
+ ]
276
+ local_explanation_section = (
277
+ dp .Select (blocks = blocks ) if len (blocks ) > 1 else blocks [0 ]
278
+ )
279
+
280
+ # Append the global explanation text and section to the "all_sections" list
281
+ all_sections = all_sections + [
282
+ global_explanation_text ,
283
+ global_explanation_section ,
284
+ local_explanation_text ,
285
+ local_explanation_section ,
286
+ ]
287
+ except Exception as e :
288
+ logger .warn (f"Failed to generate Explanations with error: { e } ." )
289
+ logger .debug (f"Full Traceback: { traceback .format_exc ()} " )
287
290
288
291
model_description = dp .Text (
289
292
"The AutoMLx model automatically preprocesses, selects and engineers "
0 commit comments