Skip to content

Commit ebb64d7

Browse files
committed
need some additional structure for Sequential ML scheme
1 parent 6fd6eb8 commit ebb64d7

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

ensemble/ensemble.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def calc_ml_prediction(self, input_state=None):
486486
"""
487487

488488
no_tot_run = int(self.sim.input_dict['parallel'])
489-
ml_pred_data = []
489+
ml_pred_data = ml_pred_data = [[] for _ in range(max(self.multilevel['levels'])+1)] # Ensure that the list is long enough
490490

491491
for level in tqdm(self.multilevel['levels'], desc='Fidelity level', position=1):
492492
# Setup forward simulator and redundant simulator at the correct fidelity
@@ -611,13 +611,34 @@ def calc_ml_prediction(self, input_state=None):
611611
en_pred[list_crash[indx]] = deepcopy(en_pred[el])
612612

613613
# Convert ensemble specific result into pred_data, and filter for NONE data
614-
ml_pred_data.append([{typ: np.concatenate(tuple((np.atleast_2d(el[ind][typ]).T) for el in en_pred), axis=1)
615-
if any(elem is not None for elem in tuple((el[ind][typ]) for el in en_pred))
616-
else None for typ in en_pred[0][0].keys()} for ind in range(len(en_pred[0]))])
617-
614+
ml_pred_data[level].extend([{typ: np.concatenate(tuple((np.atleast_2d(el[ind][typ]).T) for el in en_pred), axis=1)
615+
if any(elem is not None for elem in tuple((el[ind][typ]) for el in en_pred))
616+
else None for typ in en_pred[0][0].keys()} for ind in range(len(en_pred[0]))])
617+
618+
empty_indices = [i for i, entry in enumerate(ml_pred_data) if not isinstance(entry, list) or not entry]
619+
filled_idx = next(
620+
(i for i, entry in enumerate(ml_pred_data) if isinstance(entry, list) and entry),
621+
None
622+
)
623+
624+
if len(empty_indices):
625+
# fill the emply list with correct structure
626+
template = deepcopy(ml_pred_data[filled_idx])
627+
empty_struct = [#{
628+
#key: None
629+
#for key, val in template_elem.items()
630+
#}
631+
None for template_elem in template]
632+
for idx in empty_indices:
633+
ml_pred_data[idx] = empty_struct
618634
# loop over time instance first, and the level instance.
619635
self.pred_data = np.array(ml_pred_data).T.tolist()
620636

637+
# Treat scaling
638+
if 'scaling' in self.multilevel:
639+
self.pred_data = self.treat_scaling(self.multilevel['scaling'],self.pred_data,self.multilevel['scaling_map'])
640+
# this is for fidelity specific scaling of output
641+
621642
if hasattr(self,'treat_modeling_error'):
622643
self.treat_modeling_error()
623644

0 commit comments

Comments
 (0)