2323import numpy .linalg as npl
2424from scipy .optimize import minimize
2525import random
26- import sys
27- import scipy
2826
2927class RidgeBICRegressor ():
30- def __init__ (self , alpha_range = (0.1 , 10.0 ), n_alphas = 10 , fit_intercept = True , normalize = False ):
31- self .alpha_range = alpha_range
32- self .n_alphas = n_alphas
28+ def __init__ (self , fit_intercept = True , normalize = False ):
29+ # self.alpha_range = alpha_range
30+ self .alphas = np .logspace (3 , 6 , 20 ).round ().astype (int )
31+ # self.n_alphas = n_alphas
3332 self .fit_intercept = fit_intercept
3433 self .normalize = normalize
3534 self .alpha_ = None
3635 self .model_ = None
3736
3837 def fit (self , X , y ):
3938 n , d = X .shape
40-
41- alpha_min , alpha_max = self .alpha_range
42- alphas = np .logspace (np .log10 (alpha_min ), np .log10 (alpha_max ), self .n_alphas )
39+ # alpha_min, alpha_max = self.alpha_range
40+ # alphas = np.logspace(np.log10(alpha_min), np.log10(alpha_max), self.n_alphas)
4341
4442 bic_scores = []
4543 models = []
4644
4745 ols = LinearRegression ()
4846 denom = np .std (y - ols .fit (X , y ).predict (X )) / (n - d )
4947
50- for alpha in alphas :
48+ for alpha in tqdm ( self . alphas ) :
5149 model = Ridge (alpha = alpha , fit_intercept = self .fit_intercept , normalize = self .normalize )
5250 model .fit (X , y )
5351 models .append (model )
@@ -59,7 +57,7 @@ def fit(self, X, y):
5957 bic_scores .append (bic )
6058
6159 best_model_index = np .argmin (bic_scores )
62- self .alpha_ = alphas [best_model_index ]
60+ self .alpha_ = self . alphas [best_model_index ]
6361 self .model_ = models [best_model_index ]
6462
6563 def predict (self , X ):
@@ -110,7 +108,7 @@ def get_roi_and_idx(run, out_dir, sigmas):
110108 if len (sys .argv ) > 1 :
111109 runs = [int (sys .argv [- 1 ])]
112110 else :
113- runs = list (range (300 )) # this number determines which neuron we will pick
111+ runs = list (range (100 )) # this number determines which neuron we will pick
114112 print ('\n runs' , runs )
115113
116114 # fit linear models
@@ -171,6 +169,12 @@ def get_roi_and_idx(run, out_dir, sigmas):
171169 os .makedirs (save_dir , exist_ok = True )
172170 print ('fitting' , roi , 'idx' , i )
173171
172+
173+ # check for cached file
174+ cached_fname = oj (save_dir , f'ridge_{ i } .pkl' )
175+ if os .path .exists (cached_fname ):
176+ print ('skipping' , i )
177+
174178 # select response for neuron i
175179 y_train = Y_train [i ]
176180 y_test = Y_test [i ]
0 commit comments