<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import absolute_import <EOL> import numpy , copy <EOL> try : <EOL> import scipy . interpolate , scipy . optimize , scipy . odr . odrpack <EOL> except : <EOL> pass <EOL> numpy . seterr ( all = '<STR_LIT:ignore>' ) <EOL> import pyeq2 <EOL> from . import diffev <EOL> class custom_prng_for_diffev ( numpy . random . mtrand . RandomState ) : <EOL> def __init__ ( self ) : <EOL> numpy . random . mtrand . RandomState . __init__ ( self ) <EOL> numpy . random . seed ( <NUM_LIT:3> ) <EOL> self . max_index = <NUM_LIT> <EOL> self . array = numpy . random . rand ( self . max_index ) <EOL> self . index = <NUM_LIT:0> <EOL> def rand ( self , inSize ) : <EOL> if self . index + inSize > self . max_index : <EOL> self . index = <NUM_LIT:0> <EOL> return_val = self . array [ self . index : self . index + inSize ] <EOL> self . index += inSize <EOL> return return_val <EOL> class SolverService ( object ) : <EOL> ListOfNonLinearSolverAlgorithmNames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> self . fminIterationLimit = <NUM_LIT> <EOL> self . fmin_xtol = <NUM_LIT> <EOL> self . fmin_ftol = <NUM_LIT> <EOL> self . fmin_FunctionLimit = <NUM_LIT> <EOL> def SolveUsingLinear ( self , inModel ) : <EOL> if ( inModel . CanLinearSolverBeUsedForSSQABS ( ) ) == False and ( inModel . fittingTarget == "<STR_LIT>" ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> inModel . solvedCoefficients = numpy . linalg . lstsq ( inModel . dataCache . FindOrCreateAllDataCache ( inModel ) . T , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> return inModel . solvedCoefficients <EOL> def SolveUsingSimplex ( self , inModel ) : <EOL> inModel . dataCache . FindOrCreateAllDataCache ( inModel ) <EOL> inModel . solvedCoefficients = scipy . optimize . fmin ( inModel . CalculateAllDataFittingTarget , inModel . estimatedCoefficients , maxiter = len ( inModel . estimatedCoefficients ) * self . fminIterationLimit , maxfun = len ( inModel . estimatedCoefficients ) * self . fmin_FunctionLimit , disp = <NUM_LIT:0> , xtol = self . fmin_xtol , ftol = self . fmin_ftol ) <EOL> return inModel . solvedCoefficients <EOL> def SolveUsingDE ( self , inModel ) : <EOL> crossoverProbabilityForGA = <NUM_LIT> <EOL> diffScaleForGA = <NUM_LIT> <EOL> numberOfCoefficients = len ( inModel . GetCoefficientDesignators ( ) ) <EOL> oneThirdOfPopulationSizeForGA = <NUM_LIT> * numberOfCoefficients <EOL> if oneThirdOfPopulationSizeForGA > <NUM_LIT> : <EOL> oneThirdOfPopulationSizeForGA = <NUM_LIT> <EOL> maxGenerationsForGA = <NUM_LIT:5> * numberOfCoefficients <EOL> if maxGenerationsForGA > <NUM_LIT> : <EOL> maxGenerationsForGA = <NUM_LIT> <EOL> guessDivisorForGA = <NUM_LIT> * numberOfCoefficients <EOL> if guessDivisorForGA > <NUM_LIT> : <EOL> guessDivisorForGA = <NUM_LIT> <EOL> inModel . dataCache . FindOrCreateReducedDataCache ( inModel ) <EOL> numpy . random . seed ( <NUM_LIT:3> ) <EOL> largeValuesArray = numpy . random . random ( oneThirdOfPopulationSizeForGA * numberOfCoefficients ) * <NUM_LIT> - <NUM_LIT> <EOL> smallValuesArray = numpy . random . random ( oneThirdOfPopulationSizeForGA * numberOfCoefficients ) * <NUM_LIT> - <NUM_LIT:1.0> <EOL> tinyValuesArray = numpy . random . random ( oneThirdOfPopulationSizeForGA * numberOfCoefficients ) * <NUM_LIT> - <NUM_LIT> <EOL> pop0 = numpy . append ( largeValuesArray , numpy . append ( smallValuesArray , tinyValuesArray ) ) <EOL> numpy . random . shuffle ( pop0 ) <EOL> pop0 = pop0 . reshape ( oneThirdOfPopulationSizeForGA * <NUM_LIT:3> , numberOfCoefficients ) <EOL> if inModel . estimatedCoefficients != [ ] : <EOL> pop0 [ <NUM_LIT:0> ] = copy . deepcopy ( inModel . estimatedCoefficients ) <EOL> depData = inModel . dataCache . reducedDataCacheDictionary [ '<STR_LIT>' ] <EOL> sufficientSolution = ( max ( depData ) - min ( depData ) ) / guessDivisorForGA <EOL> de = diffev . DiffEvolver ( inModel . CalculateReducedDataFittingTarget , pop0 , crossover_rate = crossoverProbabilityForGA , scale = diffScaleForGA , prng = custom_prng_for_diffev ( ) ) <EOL> de . solve ( sufficientSolution , maxGenerationsForGA ) <EOL> inModel . solvedCoefficients = de . best_vector <EOL> return inModel . solvedCoefficients <EOL> def SolveUsingSelectedAlgorithm ( self , inModel , inAlgorithmName ) : <EOL> if inAlgorithmName not in self . ListOfNonLinearSolverAlgorithmNames : <EOL> raise Exception ( '<STR_LIT:">' + inAlgorithmName + '<STR_LIT>' ) <EOL> inModel . dataCache . FindOrCreateAllDataCache ( inModel ) <EOL> inModel . dataCache . FindOrCreateReducedDataCache ( inModel ) <EOL> results = [ ] <EOL> try : <EOL> if inAlgorithmName == '<STR_LIT>' : <EOL> coeffs , unused = scipy . optimize . curve_fit ( inModel . WrapperForScipyCurveFit , None , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , numpy . ones ( len ( inModel . GetCoefficientDesignators ( ) ) ) , maxfev = <NUM_LIT> ) <EOL> else : <EOL> coeffs = scipy . optimize . minimize ( inModel . CalculateAllDataFittingTarget , numpy . ones ( len ( inModel . GetCoefficientDesignators ( ) ) ) , method = inAlgorithmName , maxiter = len ( inModel . estimatedCoefficients ) * self . fminIterationLimit , disp = <NUM_LIT:0> ) <EOL> SSQ = inModel . CalculateAllDataFittingTarget ( coeffs ) <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> try : <EOL> if inAlgorithmName == '<STR_LIT>' : <EOL> coeffs , unused = scipy . optimize . curve_fit ( inModel . WrapperForScipyCurveFit , None , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , inModel . deEstimatedCoefficients , maxfev = <NUM_LIT> ) <EOL> else : <EOL> coeffs = scipy . optimize . minimize ( inModel . CalculateAllDataFittingTarget , inModel . deEstimatedCoefficients , method = inAlgorithmName , maxiter = len ( inModel . estimatedCoefficients ) * self . fminIterationLimit , disp = <NUM_LIT:0> ) <EOL> SSQ = inModel . CalculateAllDataFittingTarget ( coeffs ) <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> if len ( inModel . estimatedCoefficients ) > <NUM_LIT:0> : <EOL> try : <EOL> coeffs = inModel . estimatedCoefficients <EOL> SSQ = inModel . CalculateAllDataFittingTarget ( coeffs ) <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> try : <EOL> if inAlgorithmName == '<STR_LIT>' : <EOL> coeffs , unused = scipy . optimize . curve_fit ( inModel . WrapperForScipyCurveFit , None , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , inModel . estimatedCoefficients , maxfev = <NUM_LIT> ) <EOL> else : <EOL> coeffs = scipy . optimize . minimize ( inModel . CalculateAllDataFittingTarget , inModel . estimatedCoefficients , method = inAlgorithmName , maxiter = len ( inModel . estimatedCoefficients ) * self . fminIterationLimit , disp = <NUM_LIT:0> ) <EOL> SSQ = inModel . CalculateAllDataFittingTarget ( coeffs ) <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> if results == [ ] : <EOL> return numpy . ones ( len ( inModel . GetCoefficientDesignators ( ) ) ) <EOL> if len ( results ) > <NUM_LIT:1> : <EOL> results . sort ( key = lambda item : item [ <NUM_LIT:0> ] ) <EOL> inModel . solvedCoefficients = results [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> return inModel . solvedCoefficients <EOL> def SolveUsingODR ( self , inModel ) : <EOL> data = inModel . dataCache . FindOrCreateAllDataCache ( inModel ) <EOL> modelObject = scipy . odr . odrpack . Model ( inModel . WrapperForODR ) <EOL> if len ( inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] ) : <EOL> dataObject = scipy . odr . odrpack . Data ( inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] ) <EOL> else : <EOL> dataObject = scipy . odr . odrpack . Data ( inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] ) <EOL> results = [ ] <EOL> try : <EOL> myodr = scipy . odr . odrpack . ODR ( dataObject , modelObject , beta0 = numpy . ones ( len ( inModel . GetCoefficientDesignators ( ) ) ) , maxit = len ( inModel . GetCoefficientDesignators ( ) ) * self . fminIterationLimit ) <EOL> myodr . set_job ( fit_type = <NUM_LIT:0> , deriv = <NUM_LIT:0> ) <EOL> out = myodr . run ( ) <EOL> coeffs = out . beta <EOL> SSQ = out . sum_square <EOL> if not numpy . any ( numpy . isnan ( coeffs ) ) : <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> try : <EOL> myodr = scipy . odr . odrpack . ODR ( dataObject , modelObject , beta0 = inModel . deEstimatedCoefficients , maxit = len ( inModel . GetCoefficientDesignators ( ) ) * self . fminIterationLimit ) <EOL> myodr . set_job ( fit_type = <NUM_LIT:0> , deriv = <NUM_LIT:0> ) <EOL> out = myodr . run ( ) <EOL> coeffs = out . beta <EOL> SSQ = out . sum_square <EOL> if not numpy . any ( numpy . isnan ( coeffs ) ) : <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> if len ( inModel . estimatedCoefficients ) > <NUM_LIT:0> : <EOL> try : <EOL> coeffs = inModel . estimatedCoefficients <EOL> SSQ = inModel . CalculateAllDataFittingTarget ( coeffs ) <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> try : <EOL> myodr = scipy . odr . odrpack . ODR ( dataObject , modelObject , beta0 = inModel . estimatedCoefficients , maxit = len ( inModel . GetCoefficientDesignators ( ) ) * self . fminIterationLimit ) <EOL> myodr . set_job ( fit_type = <NUM_LIT:0> , deriv = <NUM_LIT:0> ) <EOL> out = myodr . run ( ) <EOL> coeffs = out . beta <EOL> SSQ = out . sum_square <EOL> if not numpy . any ( numpy . isnan ( coeffs ) ) : <EOL> results . append ( [ SSQ , coeffs ] ) <EOL> except : <EOL> pass <EOL> if results == [ ] : <EOL> return numpy . ones ( len ( inModel . GetCoefficientDesignators ( ) ) ) <EOL> if len ( results ) > <NUM_LIT:1> : <EOL> results . sort ( key = lambda item : item [ <NUM_LIT:0> ] ) <EOL> inModel . solvedCoefficients = results [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> return inModel . solvedCoefficients <EOL> def SolveUsingSpline ( self , inModel ) : <EOL> data = inModel . dataCache . FindOrCreateAllDataCache ( inModel ) <EOL> if inModel . GetDimensionality ( ) == <NUM_LIT:2> : <EOL> inModel . scipySpline = scipy . interpolate . fitpack2 . UnivariateSpline ( data [ <NUM_LIT:0> ] , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , s = inModel . smoothingFactor , k = inModel . xOrder ) <EOL> inModel . solvedCoefficients = inModel . scipySpline . _eval_args <EOL> return inModel . solvedCoefficients <EOL> else : <EOL> inModel . scipySpline = scipy . interpolate . fitpack2 . SmoothBivariateSpline ( data [ <NUM_LIT:0> ] , data [ <NUM_LIT:1> ] , inModel . dataCache . allDataCacheDictionary [ '<STR_LIT>' ] , s = inModel . smoothingFactor , kx = inModel . xOrder , ky = inModel . yOrder ) <EOL> inModel . solvedCoefficients = inModel . scipySpline . tck <EOL> return inModel . solvedCoefficients <EOL> def SolveStatisticalDistribution ( self , distributionName , data , inCriteriaForUseInListSorting ) : <EOL> criteriaList = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if inCriteriaForUseInListSorting not in criteriaList : <EOL> raise Exception ( '<STR_LIT>' , str ( criteriaList ) ) <EOL> try : <EOL> exec ( '<STR_LIT>' + distributionName ) <EOL> except : <EOL> return <NUM_LIT:0> <EOL> eps = numpy . finfo ( float ) . eps * <NUM_LIT> <EOL> data_min = data . min ( ) <EOL> data_max = data . max ( ) <EOL> data_mean = data . mean ( ) <EOL> data_range = data_max - data_min <EOL> data_std_dev = numpy . std ( data , dtype = numpy . float32 ) <EOL> best_nnlf = <NUM_LIT> <EOL> best_parameters = None <EOL> if distribution . name in [ '<STR_LIT>' ] : <EOL> try : <EOL> rangeData = ( data - data_min ) / data_range <EOL> data_mean = rangeData . mean ( ) <EOL> data_var = rangeData . var ( ) <EOL> par_a = data_mean * ( ( data_mean * ( <NUM_LIT:1.0> - data_mean ) / data_var ) - <NUM_LIT:1.0> ) <EOL> par_b = ( <NUM_LIT:1.0> - data_mean ) * ( ( data_mean * ( <NUM_LIT:1.0> - data_mean ) / data_var ) - <NUM_LIT:1.0> ) <EOL> par0 = ( par_a , par_b , data_min - <NUM_LIT> * data_min , data_range * <NUM_LIT> ) <EOL> par_est = tuple ( distribution . fit ( data , * ( par0 [ : - <NUM_LIT:2> ] ) , loc = data_min - <NUM_LIT> * data_min , scale = data_range * <NUM_LIT> ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> if distribution . name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> par0 = ( data_mean - <NUM_LIT> * data_std_dev , data_mean + <NUM_LIT> * data_std_dev ) <EOL> par_est = tuple ( distribution . fit ( data , loc = data_mean , scale = data_std_dev , * par0 ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = <NUM_LIT:0.0> , scale = <NUM_LIT:1.0> ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = data_mean , scale = data_std_dev ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = data_max + eps , scale = data_std_dev ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = data_min - eps , scale = data_std_dev ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = data_max + eps , scale = data_range ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> try : <EOL> par_est = tuple ( distribution . fit ( data , loc = data_min - eps , scale = data_range ) ) <EOL> nnlf = distribution . nnlf ( par_est , data ) <EOL> if numpy . isfinite ( nnlf ) and nnlf < best_nnlf : <EOL> best_parameters = par_est <EOL> best_nnlf = nnlf <EOL> except : <EOL> pass <EOL> if ( best_nnlf < <NUM_LIT> ) and ( best_parameters is not None ) : <EOL> try : <EOL> k = len ( best_parameters ) <EOL> AIC = <NUM_LIT> * k + <NUM_LIT> * best_nnlf <EOL> n = len ( data ) <EOL> AICc_BA = AIC + ( ( <NUM_LIT> * k * ( k + <NUM_LIT:1.0> ) ) / ( n - k - <NUM_LIT:1.0> ) ) <EOL> temp = { } <EOL> temp [ '<STR_LIT>' ] = distributionName <EOL> temp [ '<STR_LIT>' ] = best_parameters <EOL> temp [ '<STR_LIT>' ] = best_nnlf <EOL> temp [ '<STR_LIT>' ] = AIC <EOL> temp [ '<STR_LIT>' ] = AICc_BA <EOL> if inCriteriaForUseInListSorting == '<STR_LIT>' : <EOL> return [ best_nnlf , temp ] <EOL> elif inCriteriaForUseInListSorting == '<STR_LIT>' : <EOL> return [ AIC , temp ] <EOL> else : <EOL> return [ AICc_BA , temp ] <EOL> except : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:0> </s>
