@@ -157,14 +157,17 @@ def _moebius_to_base_interaction(
157157 strict = False ,
158158 ):
159159 moebius_size = len (moebius_set )
160- # for higher-order Möbius sets (size > order) distribute the value on all interactions
161160 for interaction in powerset (moebius_set , min_size = 0 , max_size = order ):
162161 val_distributed = distribution_weights [moebius_size , len (interaction )]
163162 # Check if Möbius value is distributed onto this interaction
164- if interaction in base_interaction_dict :
165- base_interaction_dict [interaction ] += moebius_val * val_distributed
166- else :
167- base_interaction_dict [interaction ] = moebius_val * val_distributed
163+ moebius_val_calc = moebius_val * val_distributed
164+ if moebius_val_calc == 0 :
165+ continue
166+ base_interaction_dict [interaction ] = (
167+ base_interaction_dict .get (interaction , 0 ) + moebius_val_calc
168+ )
169+ if base_interaction_dict [interaction ] == 0 :
170+ base_interaction_dict .pop (interaction )
168171
169172 base_interaction_values = np .zeros (len (base_interaction_dict ))
170173 base_interaction_lookup = {}
@@ -227,19 +230,24 @@ def _stii_routine(
227230 if moebius_size < order :
228231 # For STII, interaction below size order are the Möbius coefficients
229232 val_distributed = distribution_weights [moebius_size , moebius_size ]
230- if moebius_set in stii_dict :
231- stii_dict [moebius_set ] += moebius_val * val_distributed
232- else :
233- stii_dict [moebius_set ] = moebius_val * val_distributed
233+ moebius_val_calc = moebius_val * val_distributed
234+ if moebius_val_calc == 0 :
235+ continue
236+ stii_dict [moebius_set ] = stii_dict .get (moebius_set , 0 ) + moebius_val_calc
237+ # if Möbius values sum up to zero, we pop it from the dict
238+ if stii_dict [moebius_set ] == 0 :
239+ stii_dict .pop (moebius_set )
234240 else :
235241 # higher-order Möbius sets (size > order) distribute to all top-order interactions
236242 for interaction in powerset (moebius_set , min_size = order , max_size = order ):
237243 val_distributed = distribution_weights [moebius_size , len (interaction )]
238244 # Check if Möbius value is distributed onto this interaction
239- if interaction in stii_dict :
240- stii_dict [interaction ] += moebius_val * val_distributed
241- else :
242- stii_dict [interaction ] = moebius_val * val_distributed
245+ moebius_val_calc = moebius_val * val_distributed
246+ if moebius_val_calc == 0 :
247+ continue
248+ stii_dict [interaction ] = stii_dict .get (interaction , 0 ) + moebius_val_calc
249+ if stii_dict [interaction ] == 0 :
250+ stii_dict .pop (interaction )
243251
244252 stii_values = np .zeros (len (stii_dict ))
245253 stii_lookup = {}
@@ -311,10 +319,13 @@ def _fii_routine(self, index: Literal["FSII", "FBII"], order: int) -> Interactio
311319 for interaction in powerset (moebius_set , min_size = 1 , max_size = order ):
312320 val_distributed = distribution_weights [moebius_size , len (interaction )]
313321 # Check if Möbius value is distributed onto this interaction
314- if interaction in fii_dict :
315- fii_dict [interaction ] += moebius_val * val_distributed
316- else :
317- fii_dict [interaction ] = moebius_val * val_distributed
322+ moebius_val_calc = moebius_val * val_distributed
323+ if moebius_val_calc == 0 :
324+ continue
325+ fii_dict [interaction ] = fii_dict .get (interaction , 0 ) + moebius_val_calc
326+ # if Möbius values sum up to zero, we pop it from the dict
327+ if fii_dict [interaction ] == 0 :
328+ fii_dict .pop (interaction )
318329
319330 fii_values = np .zeros (len (fii_dict ))
320331 fii_lookup = {}
0 commit comments