1919#
2020# -------------------------------------------------------------
2121import itertools
22+ import random
2223from typing import List
2324
24- from aligner .task import Task
25- from modality .aligned_modality import AlignedModality
26- from modality .modality import Modality
27- from representations .representation import Representation
25+ from systemds .scuro .aligner .task import Task
26+ from systemds .scuro .modality .aligned_modality import AlignedModality
27+ from systemds .scuro .modality .modality import Modality
28+ from systemds .scuro .representations .representation import Representation
29+
30+ import warnings
31+
32+ warnings .filterwarnings ('ignore' )
2833
2934
3035def get_modalities_by_name (modalities , name ):
3136 for modality in modalities :
3237 if modality .name == name :
3338 return modality
34-
39+
3540 raise 'Modality ' + name + 'not in modalities'
3641
3742
@@ -51,9 +56,9 @@ def __init__(self, modalities: List[Modality], task: Task, representations: List
5156 self .best_modalities = None
5257 self .best_representation = None
5358 self .best_score = - 1
54-
59+
5560 def set_best_params (self , modality_name : str , representation : Representation ,
56- score : float , modality_names : List [str ]):
61+ scores : List [ float ] , modality_names : List [str ]):
5762 """
5863 Updates the best parameters for given modalities, representation, and score
5964 :param modality_name: The name of the aligned modality
@@ -62,43 +67,66 @@ def set_best_params(self, modality_name: str, representation: Representation,
6267 :param modality_names: List of modality names used in this setting
6368 :return:
6469 """
65-
70+
6671 # check if modality name is already in dictionary
6772 if modality_name not in self .scores .keys ():
6873 # if not add it to dictionary
6974 self .scores [modality_name ] = {}
70-
75+
7176 # set score for representation
72- self .scores [modality_name ][representation ] = score
73-
77+ self .scores [modality_name ][representation ] = scores
78+
7479 # compare current score with best score
75- if score > self .best_score :
76- self .best_score = score
80+ if scores [ 1 ] > self .best_score :
81+ self .best_score = scores [ 1 ]
7782 self .best_representation = representation
7883 self .best_modalities = modality_names
79-
80- def fit (self ):
84+
85+ def reset_best_params (self ):
86+ self .best_score = - 1
87+ self .best_modalities = None
88+ self .best_representation = None
89+ self .scores = {}
90+
91+ def fit_random (self , seed = - 1 ):
92+ """
93+ This method randomly selects a modality or combination of modalities and representation
94+ """
95+ if seed != - 1 :
96+ random .seed (seed )
97+
98+ modalities = []
99+ for M in range (1 , len (self .modalities ) + 1 ):
100+ for combination in itertools .combinations (self .modalities , M ):
101+ modalities .append (combination )
102+
103+ modality_combination = random .choice (modalities )
104+ representation = random .choice (self .representations )
105+
106+ modality = AlignedModality (representation , list (modality_combination )) # noqa
107+ modality .combine ()
108+
109+ scores = self .task .run (modality .data )
110+ self .set_best_params (modality .name , representation , scores , modality .get_modality_names ())
111+
112+ return self .best_representation , self .best_score , self .best_modalities
113+
114+ def fit_enumerate_all (self ):
81115 """
82116 This method finds the best representation out of a given List of uni-modal modalities and
83117 representations
84118 :return: The best parameters found in the search procedure
85119 """
86-
120+
87121 for M in range (1 , len (self .modalities ) + 1 ):
88122 for combination in itertools .combinations (self .modalities , M ):
89- if len (combination ) == 1 :
90- modality = combination [0 ]
91- score = self .task .run (modality .representation .scale_data (modality .data , self .task .train_indices ))
92- self .set_best_params (modality .name , modality .representation .name , score , [modality .name ])
93- self .scores [modality ] = score
94- else :
95- for representation in self .representations :
96- modality = AlignedModality (representation , list (combination )) # noqa
97- modality .combine (self .task .train_indices )
98-
99- score = self .task .run (modality .data )
100- self .set_best_params (modality .name , representation , score , modality .get_modality_names ())
101-
123+ for representation in self .representations :
124+ modality = AlignedModality (representation , list (combination )) # noqa
125+ modality .combine ()
126+
127+ scores = self .task .run (modality .data )
128+ self .set_best_params (modality .name , representation , scores , modality .get_modality_names ())
129+
102130 return self .best_representation , self .best_score , self .best_modalities
103131
104132 def transform (self , modalities : List [Modality ]):
@@ -108,17 +136,16 @@ def transform(self, modalities: List[Modality]):
108136 :param modalities: List of uni-modal modalities
109137 :return: aligned data
110138 """
111-
139+
112140 if self .best_score == - 1 :
113141 raise 'Please fit representations first!'
114-
142+
115143 used_modalities = []
116-
144+
117145 for modality_name in self .best_modalities :
118146 used_modalities .append (get_modalities_by_name (modalities , modality_name ))
119-
147+
120148 modality = AlignedModality (self .best_representation , used_modalities ) # noqa
121149 modality .combine (self .task .train_indices )
122-
150+
123151 return modality .data
124-
0 commit comments