def _getUsable(self, comp=None, dose=None):
     try:
         f=open(self.settings.ok_wells_asDICT, 'r')
         ok_dict=pickle.load(f);f.close()
     except:
         if comp is None:
             return quality_control.usable_XBSC(self.compound, self.dose,confDir=self.settings.plate_setups_folder)
         else:
             return quality_control.usable_XBSC(comp, dose,confDir=self.settings.plate_setups_folder)
     else:
         if comp is None:
             return ok_dict[self.compound][self.dose]
         else:
             return ok_dict[comp][dose]
def parameterStabilityEvaluation(well_num=50,n_list=list([0.4,0.5,0.6,0.7,0.8]), h_list=[10], deg_list=[2,3],
                                 pheno_list=['Anaphase_ch1', 'Apoptosis_ch1', 'Folded_ch1', 'Interphase_ch1', 'Metaphase_ch1',
                                           'Polylobbed_ch1', 'Prometaphase_ch1', 'WMicronuclei_ch1'],
                               loadingFolder='/media/lalil0u/New/projects/Xb_screen/dry_lab_results', passed=None):
    '''
    Here we're looking at how well results are robust to parameters change (result r1) and how well a subset of
    parameters is able to separate experiments (variance of r2).
    '''
    param_num=len(n_list)*len(h_list)*len(deg_list)
    param_list=[]
    if passed is None:
        passed,_,_,_=quality_control.computingToRedo(threshold_flou=0.4, threshold_init_cell_count=20)
    selected=np.array(passed)[np.random.permutation(len(passed))[:well_num]]
    processedDict={};
    result=defaultdict(list)
    for pl,w in selected:
        if pl not in processedDict:
            print "Opening processed dictionary file for pl ", pl
            f=open(os.path.join(loadingFolder, "processedDictResult_P{}.pkl".format(pl)))
            processedDict[pl]=pickle.load(f); f.close()
        try:
            currCtrl=quality_control.usable_XBSC(CONTROLS[processedDict[pl][int(w)]['Xenobiotic']], 0, pl)
        except KeyError:
            if processedDict[pl][int(w)]['Xenobiotic'] in CONTROLS.values():
                currCtrl=quality_control.usable_XBSC(processedDict[pl][int(w)]['Xenobiotic'], 0, pl)
            else:
                raise KeyError
        
        for pheno in pheno_list:
            y_exp=np.array(processedDict[pl][int(w)][pheno])
            for n,h,deg in product(n_list, h_list, deg_list):
                if (n,h,deg) not in param_list:
                    param_list.append((n,h,deg))
                    
                r=[]
                for pl, ctrl in filter(lambda x: x[1]!=w,currCtrl):
                    y_ctrl = np.array(processedDict[pl][int(ctrl)][pheno])
                    _, r_exp=localReg(y_exp, n, h, deg, plot=False)
                    _, r_ctrl = localReg(y_ctrl, n, h, deg, plot=False)
                    r.append(np.max(np.abs(r_exp-r_ctrl)))
                    
                result[pheno].append(np.median(r))
    print "Done with local regressions"
    for pheno in pheno_list:
        np.reshape(result[pheno], newshape=(well_num, param_num))
        
    r1=[np.ones(shape=(param_num, param_num), dtype=float) for pheno in pheno_list]
    r2=[np.zeros(shape=(param_num,), dtype=list) for pheno in pheno_list]
    for i, pheno in enumerate(pheno_list):
        result[pheno]=np.reshape(result[pheno], newshape=(well_num, param_num))
        for k in range(param_num):
            r2[i][k]=[0]
            iter_ = np.nditer(result[pheno][1:,k], flags=['c_index']) #one-dim array: the order is the same in C or F
            while not iter_.finished :
                r2[i][k]= np.hstack((r2[i][k],[a-iter_[0]  for a in np.nditer(result[pheno][:iter_.index+1,k])]))
                iter_.iternext()  
            r2[i][k]=np.abs(r2[i][k][1:])/np.mean(result[pheno][:,k])
            
            for j in range(k):
                r1[i][k,j]=spearmanr(result[pheno][:,k],result[pheno][:,j])[0]
                r1[i][j,k]=r1[i][k,j]
    return passed, param_list,r1, r2