<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import logging <EOL> import pandas <EOL> logger = logging . getLogger ( __name__ ) <EOL> from math import pi <EOL> import numpy as np <EOL> from skspec . core . anyspectra import AnyFrame <EOL> from skspec . plotting . correlation_plot import corr2d , corr3d , corr_multi <EOL> import skspec . config as pvconfig <EOL> import skspec . core . utilities as pvutils <EOL> from skspec . core . specindex import SpecIndex <EOL> from skspec . pandas_utils . metadframe import MetaDataFrame <EOL> from pca_lite import PCA <EOL> def noda_matrix ( length ) : <EOL> '''<STR_LIT>''' <EOL> Njk = np . empty ( ( length , length ) ) <EOL> for j in range ( length ) : <EOL> for k in range ( length ) : <EOL> if j == k : <EOL> Njk [ j , k ] = <NUM_LIT:0> <EOL> else : <EOL> Njk [ j , k ] = <NUM_LIT:1.0> / ( pi * ( k - j ) ) <EOL> return Njk <EOL> class CorrError ( Exception ) : <EOL> """<STR_LIT:U+0020>""" <EOL> class Spec2dError ( Exception ) : <EOL> """<STR_LIT:U+0020>""" <EOL> class Spec2d ( AnyFrame ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . spec = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . scaled = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . centered = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( Spec2d , self ) . __init__ ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def from_corr2d ( cls , arrayout , corr2d , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> specout = cls ( <EOL> arrayout , <EOL> scaled = corr2d . _scale_string , <EOL> centered = corr2d . center , <EOL> spec = corr2d . spec , <EOL> * args , ** kwargs <EOL> ) <EOL> specout . index = corr2d . index <EOL> specout . columns = corr2d . index <EOL> return specout <EOL> @ property <EOL> def _var_span ( self ) : <EOL> if self . spec is None : <EOL> return '<STR_LIT>' <EOL> cols = self . spec . columns <EOL> return pvutils . _compute_span ( cols , with_unit = True ) <EOL> @ property <EOL> def _header ( self ) : <EOL> """<STR_LIT>""" <EOL> delim = pvconfig . HEADERDELIM <EOL> name = pvutils . safe_lookup ( self , '<STR_LIT:name>' ) <EOL> ftunit = pvutils . safe_lookup ( self , '<STR_LIT>' ) <EOL> spunit = pvutils . safe_lookup ( self , '<STR_LIT>' ) <EOL> header = "<STR_LIT>" % ( name , delim , self . scaled ) <EOL> return header <EOL> @ property <EOL> def _header_html ( self ) : <EOL> """<STR_LIT>""" <EOL> delim = pvconfig . HEADERHTMLDELIM <EOL> ftunit = pvutils . safe_lookup ( self , '<STR_LIT>' ) <EOL> spunit = pvutils . safe_lookup ( self , '<STR_LIT>' ) <EOL> if self . ndim > <NUM_LIT:1> : <EOL> s1 , s2 = self . shape <EOL> colorshape = '<STR_LIT>' % ( <EOL> s1 , <EOL> spunit , <EOL> s2 , <EOL> ftunit ) <EOL> else : <EOL> colorshape = '<STR_LIT>' % ( self . shape ) <EOL> scale_string = '<STR_LIT>' % self . scaled <EOL> span_string = '<STR_LIT>' % self . _var_span <EOL> header = "<STR_LIT>" % ( self . name , <EOL> colorshape , <EOL> delim , <EOL> scale_string , <EOL> delim , <EOL> span_string <EOL> ) <EOL> return header <EOL> def plot ( self , kind = '<STR_LIT>' , ** pltkwargs ) : <EOL> """<STR_LIT:U+0020>""" <EOL> pltkwargs . setdefault ( '<STR_LIT:title>' , '<STR_LIT>' % ( self . name , self . _var_span ) ) <EOL> if kind not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return super ( Spec2d , self ) . plot ( kind = kind , ** pltkwargs ) <EOL> pltkwargs . setdefault ( '<STR_LIT>' , self . full_specunit ) <EOL> pltkwargs . setdefault ( '<STR_LIT>' , self . full_varunit ) <EOL> if kind == '<STR_LIT>' : <EOL> if '<STR_LIT>' not in pltkwargs and '<STR_LIT>' not in pltkwargs and '<STR_LIT>' not in pltkwargs : <EOL> pltkwargs [ '<STR_LIT>' ] = pvconfig . CMAP_CONTOUR <EOL> if self . index [ <NUM_LIT:0> ] > self . index [ - <NUM_LIT:1> ] : <EOL> pltkwargs [ '<STR_LIT>' ] = True <EOL> return corr2d ( self , ** pltkwargs ) <EOL> elif kind == '<STR_LIT>' : <EOL> return corr3d ( self , ** pltkwargs ) <EOL> class Corr2d ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , spec , refspec = None ) : <EOL> """<STR_LIT>""" <EOL> if spec . ndim != <NUM_LIT:2> : <EOL> raise CorrError ( '<STR_LIT>' ) <EOL> if not isinstance ( spec , MetaDataFrame ) : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % type ( data ) <EOL> self . spec = spec . deepcopy ( ) <EOL> self . index = spec . index <EOL> self . columns = spec . columns <EOL> self . specunit = spec . specunit <EOL> self . varunit = spec . varunit <EOL> self . _noda = noda_matrix ( self . M ) <EOL> self . _scaled = False <EOL> self . alpha = <NUM_LIT> <EOL> self . beta = <NUM_LIT:0.0> <EOL> self . _PCA = None <EOL> if refspec is not None : <EOL> refspec = np . array ( refspec ) <EOL> if refspec . shape != self . index . shape : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . index . shape , refspec . shape ) ) <EOL> self . ref_spectrum = np . array ( refspec ) <EOL> self . _center = '<STR_LIT>' <EOL> self . dyn_spec = self . spec . subtract ( self . ref_spectrum , axis = <NUM_LIT:0> ) <EOL> else : <EOL> self . set_center ( '<STR_LIT>' ) <EOL> @ property <EOL> def _scale_string ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _scaled : <EOL> return '<STR_LIT>' % ( self . alpha , self . beta ) <EOL> return '<STR_LIT:False>' <EOL> def scale ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if args [ <NUM_LIT:0> ] == True : <EOL> if self . _scaled == True : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> self . _scaled = True <EOL> elif args [ <NUM_LIT:0> ] == False : <EOL> if self . _scaled == False : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> self . _scaled = False <EOL> else : <EOL> raise CorrError ( '<STR_LIT>' ) <EOL> else : <EOL> self . _scaled = True <EOL> self . alpha = kwargs . pop ( '<STR_LIT>' , self . alpha ) <EOL> self . beta = kwargs . pop ( '<STR_LIT>' , self . beta ) <EOL> if self . alpha > <NUM_LIT:1> or self . beta > <NUM_LIT:1> : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> @ property <EOL> def center ( self ) : <EOL> return self . _center <EOL> def set_center ( self , style , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if not style : <EOL> self . _center = None <EOL> ref_spectrum = self . spec [ <NUM_LIT:0> ] * <NUM_LIT:0.0> <EOL> elif style == '<STR_LIT>' : <EOL> self . _center = '<STR_LIT>' <EOL> ref_spectrum = self . spec . mean ( axis = <NUM_LIT:1> ) <EOL> else : <EOL> self . _center = '<STR_LIT>' <EOL> ref_spectrum = style ( self . spec , * args , ** kwargs ) <EOL> except Exception : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' % style ) <EOL> self . ref_spectrum = np . array ( ref_spectrum ) <EOL> if len ( self . ref_spectrum ) != self . shape [ <NUM_LIT:0> ] : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . shape [ <NUM_LIT:0> ] , len ( self . ref_spectrum ) ) ) <EOL> self . dyn_spec = self . spec . subtract ( self . ref_spectrum , axis = <NUM_LIT:0> ) <EOL> @ property <EOL> def shape ( self ) : <EOL> return self . spec . shape <EOL> @ property <EOL> def M ( self ) : <EOL> return self . shape [ <NUM_LIT:1> ] <EOL> @ property <EOL> def sync_noscale ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( self . dyn_spec , self . _dynconjtranspose ) / ( self . M - <NUM_LIT:1.0> ) <EOL> @ property <EOL> def async_noscale ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return np . dot ( self . dyn_spec , np . dot ( self . _noda , self . _dynconjtranspose ) ) / ( self . M - <NUM_LIT:1.0> ) <EOL> @ property <EOL> def coeff_corr ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . divide ( self . sync_noscale , self . joint_var ) <EOL> @ property <EOL> def coeff_disr ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . divide ( self . async_noscale , self . joint_var ) <EOL> @ property <EOL> def joint_var ( self ) : <EOL> """<STR_LIT>""" <EOL> std = self . dyn_spec . std ( axis = <NUM_LIT:1> ) <EOL> return np . outer ( std , std ) <EOL> @ property <EOL> def _dynconjtranspose ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . conj ( self . dyn_spec ) . transpose ( ) <EOL> @ property <EOL> def sync ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if self . _scaled : <EOL> matrixout = self . sync_noscale * self . joint_var ** ( - <NUM_LIT:1.0> * self . alpha ) * abs ( self . coeff_corr ) ** ( self . beta ) <EOL> else : <EOL> matrixout = self . sync_noscale <EOL> return Spec2d . from_corr2d ( matrixout , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> @ property <EOL> def async ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if self . _scaled : <EOL> matrixout = self . async_noscale * self . joint_var ** ( - <NUM_LIT:1.0> * self . alpha ) * abs ( self . coeff_disr ) ** ( self . beta ) <EOL> else : <EOL> matrixout = self . async_noscale <EOL> return Spec2d . from_corr2d ( matrixout , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> @ property <EOL> def phase ( self ) : <EOL> """<STR_LIT>""" <EOL> phase = np . arctan ( self . async / self . sync ) <EOL> phase . name = '<STR_LIT>' <EOL> phase . iunit = '<STR_LIT>' <EOL> return phase <EOL> @ property <EOL> def modulous ( self ) : <EOL> """<STR_LIT>""" <EOL> modulous = np . sqrt ( self . sync ** <NUM_LIT:2> + self . async ** <NUM_LIT:2> ) <EOL> modulous . name = '<STR_LIT>' <EOL> modulous . iunit = '<STR_LIT>' <EOL> return modulous <EOL> @ property <EOL> def correlation ( self ) : <EOL> """<STR_LIT>""" <EOL> return Spec2d . from_corr2d ( self . coeff_corr , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> @ property <EOL> def disrelation ( self ) : <EOL> """<STR_LIT>""" <EOL> return Spec2d . from_corr2d ( self . coeff_disr , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> @ property <EOL> def char_index ( self ) : <EOL> """<STR_LIT>""" <EOL> m = self . M <EOL> if np . count_nonzero ( self . ref_spectrum ) == <NUM_LIT:0> : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> coeff = <NUM_LIT:1.0> / ( m * self . ref_spectrum ) <EOL> summation = <NUM_LIT:0> <EOL> k_matrix = np . empty ( m ) <EOL> for k in range ( <NUM_LIT:1> , m + <NUM_LIT:1> ) : <EOL> k_matrix . fill ( k ) <EOL> summation += self . dyn_spec . dot ( k_matrix ) + ( ( m + <NUM_LIT:1> ) / <NUM_LIT:2> ) <EOL> return coeff * summation <EOL> @ property <EOL> def char_perturb ( self ) : <EOL> """<STR_LIT>""" <EOL> tm , t1 = self . columns [ - <NUM_LIT:1> ] , self . columns [ <NUM_LIT:0> ] <EOL> Kj = self . char_index <EOL> return ( ( tm - t1 ) * ( ( Kj - <NUM_LIT:1> ) / ( self . M - <NUM_LIT:1> ) ) ) + t1 <EOL> @ property <EOL> def async_codist ( self ) : <EOL> """<STR_LIT>""" <EOL> numrows = self . shape [ <NUM_LIT:0> ] <EOL> async = np . empty ( ( numrows , numrows ) ) <EOL> tm , t1 = self . columns [ - <NUM_LIT:1> ] , self . columns [ <NUM_LIT:0> ] <EOL> tbar = self . char_perturb . values <EOL> var = self . joint_var <EOL> for i in range ( numrows ) : <EOL> for j in range ( numrows ) : <EOL> coeff = ( tbar [ j ] - tbar [ i ] ) / ( tm - t1 ) <EOL> async [ i ] [ j ] = coeff * var [ i , j ] <EOL> return Spec2d . from_corr2d ( async , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> @ property <EOL> def sync_codist ( self ) : <EOL> """<STR_LIT>""" <EOL> numrows = self . shape [ <NUM_LIT:0> ] <EOL> var = self . joint_var <EOL> async_cod = self . async_codist . values <EOL> sync = np . empty ( ( numrows , numrows ) ) <EOL> for i in range ( numrows ) : <EOL> for j in range ( numrows ) : <EOL> sync [ i ] [ j ] = np . sqrt ( var [ i , j ] ** <NUM_LIT:2> - async_cod [ i , j ] ** <NUM_LIT:2> ) <EOL> return Spec2d . from_corr2d ( sync , <EOL> corr2d = self , <EOL> name = '<STR_LIT>' , <EOL> iunit = '<STR_LIT>' ) <EOL> def plot ( self , ** pltkwargs ) : <EOL> """<STR_LIT>""" <EOL> return corr_multi ( self , ** pltkwargs ) <EOL> def _pcagate ( self , attr ) : <EOL> """<STR_LIT>""" <EOL> if not self . _PCA : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . __class__ . __name__ , attr ) <EOL> def pca_fit ( self , n_components = None , fit_transform = True ) : <EOL> """<STR_LIT>""" <EOL> if self . center : <EOL> logger . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _PCA = PCA ( n_components = n_components ) <EOL> if fit_transform : <EOL> return self . _PCA . fit_transform ( self . dyn_spec ) <EOL> else : <EOL> self . _PCA . fit ( self . dyn_spec ) <EOL> @ property <EOL> def PCA ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _pcagate ( '<STR_LIT>' ) <EOL> return self . _PCA <EOL> @ property <EOL> def pca_evals ( self ) : <EOL> self . _pcagate ( '<STR_LIT>' ) <EOL> return self . _PCA . eigen_values_ <EOL> @ property <EOL> def pca_evecs ( self ) : <EOL> self . _pcagate ( '<STR_LIT>' ) <EOL> return self . _PCA . eigen_vectors_ <EOL> def load_vec ( self , k ) : <EOL> """<STR_LIT>""" <EOL> self . _pcagate ( '<STR_LIT>' ) <EOL> if k > len ( self . shape [ <NUM_LIT:1> ] ) : <EOL> raise CorrError ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . shape [ <NUM_LIT:1> ] ) <EOL> if self . _PCA . _k : <EOL> if k > len ( self . pca_evals ) : <EOL> logger . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . _PCA . _k ) <EOL> self . pca_fit ( n_components = k , fit_transform = False ) <EOL> return self . _PCA . eigen_vectors_ [ : , k ] <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> pad = pvconfig . PAD <EOL> address = super ( Corr2d , self ) . __repr__ ( ) . split ( ) [ - <NUM_LIT:1> ] . strip ( "<STR_LIT:'>" ) . strip ( '<STR_LIT:>>' ) <EOL> outstring = '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . shape [ <NUM_LIT:0> ] , self . shape [ <NUM_LIT:1> ] , address ) <EOL> outstring += '<STR_LIT>' % ( pad , self . center ) <EOL> if self . _scaled : <EOL> outstring += '<STR_LIT>' % ( pad , self . _scale_string ) <EOL> else : <EOL> outstring += '<STR_LIT>' % ( pad , self . _scaled ) <EOL> outstring += '<STR_LIT>' % ( pad , <EOL> self . specunit . lower ( ) , <EOL> self . varunit . lower ( ) ) <EOL> return outstring <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from skspec . data import aunps_glass , solvent_evap , aunps_water <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> ts = solvent_evap ( ) <EOL> cd = Corr2d ( ts ) <EOL> cd . set_center ( '<STR_LIT>' ) <EOL> print cd . sync <EOL> cd . scale ( alpha = <NUM_LIT:0> , beta = <NUM_LIT:0> ) <EOL> cd . sync . plot ( contours = <NUM_LIT:20> , kind = '<STR_LIT>' ) <EOL> plt . show ( ) </s>
