Peter554 / StainTools

Tools for tissue image stain normalisation and augmentation in Python 3
MIT License
313 stars 106 forks source link

Error with division of zero in vahadane staining #4

Closed TuanDTr closed 6 years ago

TuanDTr commented 6 years ago

n=stainNorm_Vahadane.normalizer() n.fit(i1) normalized=utils.build_stack((i1,n.transform(i2),n.transform(i3),n.transform(i4),n.transform(i5),n.transform(i6))) stain_utils.py:143: RuntimeWarning: divide by zero encountered in log return -1 * np.log(I / 255) NotImplementedError Traceback (most recent call last)

in () 1 n=stainNorm_Vahadane.normalizer() ----> 2 n.fit(i1) 3 normalized=utils.build_stack((i1,n.transform(i2),n.transform(i3),n.transform(i4),n.transform(i5),n.transform(i6))) /home/tuantr/Documents/Thesis/Stain_Normalization/stainNorm_Vahadane.pyc in fit(self, target) 36 def fit(self, target): 37 target = ut.standardize_brightness(target) ---> 38 self.stain_matrix_target = get_stain_matrix(target) 39 40 def target_stains(self): /home/tuantr/Documents/Thesis/Stain_Normalization/stainNorm_Vahadane.pyc in get_stain_matrix(I, threshold, lamda) 17 OD = ut.RGB_to_OD(I).reshape((-1, 3)) 18 OD = OD[mask] ---> 19 dictionary = spams.trainDL(OD.T, K=2, lambda1=lamda, mode=2, modeD=0, posAlpha=True, posD=True, verbose=False).T 20 if dictionary[0, 0] < dictionary[1, 0]: 21 dictionary = dictionary[[1, 0], :] /home/tuantr/anaconda2/lib/python2.7/site-packages/spams.pyc in trainDL(X, return_model, model, D, numThreads, batchsize, K, lambda1, lambda2, iter, t0, mode, posAlpha, posD, expand, modeD, whiten, clean, verbose, gamma1, gamma2, rho, iter_updateD, stochastic_deprecated, modeParam, batch, log_deprecated, logName) 1978 lambda3 = 0. 1979 regul = None -> 1980 return __allTrainDL(X,return_model,model,False,D,None,None,numThreads,0.000001,True,False,batchsize,K,lambda1,lambda2,lambda3,iter,t0,mode,regul,posAlpha,posD,expand,modeD,whiten,clean,verbose,gamma1,gamma2,rho,iter_updateD,stochastic_deprecated,modeParam,batch,log_deprecated,logName) 1981 1982 def structTrainDL( /home/tuantr/anaconda2/lib/python2.7/site-packages/spams.pyc in __allTrainDL(X, return_model, model, in_memory, D, graph, tree, numThreads, tol, fixed_step, ista, batchsize, K, lambda1, lambda2, lambda3, iter, t0, mode, regul, posAlpha, posD, expand, modeD, whiten, clean, verbose, gamma1, gamma2, rho, iter_updateD, stochastic_deprecated, modeParam, batch, log_deprecated, logName) 1826 iter,t0,mode,regul,posAlpha,posD, 1827 expand,modeD,whiten,clean,verbose,gamma1,gamma2,rho,iter_updateD, -> 1828 stochastic_deprecated,modeParam,batch,log_deprecated,logName) 1829 1830 if return_model: /home/tuantr/anaconda2/lib/python2.7/site-packages/spams_wrap.pyc in alltrainDL(*args) 262 alltrainDL(Data< float > * X, bool in_memory, bool return_model, Matrix< float > * m_A, Matrix< float > * m_B, int m_iter, Matrix< float > * D1, Vector< float > * eta_g, SpMatrix< bool > * groups, SpMatrix< bool > * groups_var, Vector< int > * own_variables, Vector< int > * N_own_variables, int num_threads, float tol, bool fixed_step, bool ista, int batch_size, int K, double lambda1, double lambda2, double lambda3, int iter, double t0, constraint_type mode, char * name_regul, bool posAlpha, bool posD, bool expand, constraint_type_D modeD, bool whiten, bool clean, bool verbose, double gamma1, double gamma2, float rho, int iter_updateD, bool stochastic, int modeParam, bool batch, bool log, char * logName) -> Matrix< float > * 263 """ --> 264 return _spams_wrap.alltrainDL(*args) 265 266 def archetypalAnalysis(*args): NotImplementedError: Wrong number or type of arguments for overloaded function 'alltrainDL'. Possible C/C++ prototypes are: _alltrainDL< double >(Data< double > *,bool,Matrix< double > **,Matrix< double > **,Vector< int > **,bool,Matrix< double > *,Matrix< double > *,int,Matrix< double > *,Vector< double > *,SpMatrix< bool > *,SpMatrix< bool > *,Vector< int > *,Vector< int > *,int,double,bool,bool,int,int,double,double,double,int,double,constraint_type,char *,bool,bool,bool,constraint_type_D,bool,bool,bool,double,double,double,int,bool,int,bool,bool,char *) _alltrainDL< float >(Data< float > *,bool,Matrix< float > **,Matrix< float > **,Vector< int > **,bool,Matrix< float > *,Matrix< float > *,int,Matrix< float > *,Vector< float > *,SpMatrix< bool > *,SpMatrix< bool > *,Vector< int > *,Vector< int > *,int,float,bool,bool,int,int,double,double,double,int,double,constraint_type,char *,bool,bool,bool,constraint_type_D,bool,bool,bool,double,double,float,int,bool,int,bool,bool,char *)
Peter554 commented 6 years ago

I see you are using Python 2.7. I have only tested on python 3.5. Most likely you have a rounding error e.g. I/255=0 and log(0) is not defined (or = - infinity). So maybe import division from future?

Peter554 commented 6 years ago

Let me know if you're still having any issues. For now I assume it's sorted