コード例 #1
0
ファイル: test_panel.py プロジェクト: BobMcFry/pandas
    def test_panel_setitem(self):

        with catch_warnings(record=True):
            # GH 7763
            # loc and setitem have setting differences
            np.random.seed(0)
            index = range(3)
            columns = list('abc')

            panel = Panel({'A': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns),
                           'B': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns),
                           'C': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns)})

            replace = DataFrame(np.eye(3, 3), index=range(3), columns=columns)
            expected = Panel({'A': replace, 'B': replace, 'C': replace})

            p = panel.copy()
            for idx in list('ABC'):
                p[idx] = replace
            tm.assert_panel_equal(p, expected)

            p = panel.copy()
            for idx in list('ABC'):
                p.loc[idx, :, :] = replace
            tm.assert_panel_equal(p, expected)
コード例 #2
0
    def test_panel_setitem(self):

        with catch_warnings(record=True):
            # GH 7763
            # loc and setitem have setting differences
            np.random.seed(0)
            index = range(3)
            columns = list('abc')

            panel = Panel({
                'A':
                DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                'B':
                DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                'C':
                DataFrame(np.random.randn(3, 3), index=index, columns=columns)
            })

            replace = DataFrame(np.eye(3, 3), index=range(3), columns=columns)
            expected = Panel({'A': replace, 'B': replace, 'C': replace})

            p = panel.copy()
            for idx in list('ABC'):
                p[idx] = replace
            tm.assert_panel_equal(p, expected)

            p = panel.copy()
            for idx in list('ABC'):
                p.loc[idx, :, :] = replace
            tm.assert_panel_equal(p, expected)
コード例 #3
0
    def test_swaplevel_panel(self):
        panel = Panel({'ItemA': self.frame, 'ItemB': self.frame * 2})

        result = panel.swaplevel(0, 1, axis='major')
        expected = panel.copy()
        expected.major_axis = expected.major_axis.swaplevel(0, 1)
        tm.assert_panel_equal(result, expected)
コード例 #4
0
ファイル: test_multilevel.py プロジェクト: flyingV/pandas
    def test_swaplevel_panel(self):
        panel = Panel({"ItemA": self.frame, "ItemB": self.frame * 2})

        result = panel.swaplevel(0, 1, axis="major")
        expected = panel.copy()
        expected.major_axis = expected.major_axis.swaplevel(0, 1)
        tm.assert_panel_equal(result, expected)
コード例 #5
0
ファイル: test_multilevel.py プロジェクト: hammer/pandas
    def test_swaplevel_panel(self):
        panel = Panel({'ItemA' : self.frame,
                       'ItemB' : self.frame * 2})

        result = panel.swaplevel(0, 1, axis='major')
        expected = panel.copy()
        expected.major_axis = expected.major_axis.swaplevel(0, 1)
コード例 #6
0
ファイル: test_partial.py プロジェクト: MasonGallo/pandas
    def test_partial_setting(self):

        # GH2578, allow ix and friends to partially set

        # series
        s_orig = Series([1, 2, 3])

        s = s_orig.copy()
        s[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        # iloc/iat raise
        s = s_orig.copy()

        def f():
            s.iloc[3] = 5.

        pytest.raises(IndexError, f)

        def f():
            s.iat[3] = 5.

        pytest.raises(IndexError, f)

        # ## frame ##

        df_orig = DataFrame(
            np.arange(6).reshape(3, 2), columns=['A', 'B'], dtype='int64')

        # iloc/iat raise
        df = df_orig.copy()

        def f():
            df.iloc[4, 2] = 5.

        pytest.raises(IndexError, f)

        def f():
            df.iat[4, 2] = 5.

        pytest.raises(IndexError, f)

        # row setting where it exists
        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.iloc[1] = df.iloc[2]
        tm.assert_frame_equal(df, expected)

        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.loc[1] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # like 2578, partial setting with dtype preservation
        expected = DataFrame(dict({'A': [0, 2, 4, 4], 'B': [1, 3, 5, 5]}))
        df = df_orig.copy()
        df.loc[3] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # single dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': [0, 2, 4]}))
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': Series([0, 2, 4])}))
        df = df_orig.copy()
        df['B'] = df['B'].astype(np.float64)
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # single dtype frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        with catch_warnings(record=True):
            # ## panel ##
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'], dtype='float64')

            # panel setting via item
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'], dtype='float64')
            expected = p_orig.copy()
            expected['Item3'] = expected['Item1']
            p = p_orig.copy()
            p.loc['Item3'] = p['Item1']
            tm.assert_panel_equal(p, expected)

            # panel with aligned series
            expected = p_orig.copy()
            expected = expected.transpose(2, 1, 0)
            expected['C'] = DataFrame({'Item1': [30, 30, 30, 30],
                                       'Item2': [32, 32, 32, 32]},
                                      index=p_orig.major_axis)
            expected = expected.transpose(2, 1, 0)
            p = p_orig.copy()
            p.loc[:, :, 'C'] = Series([30, 32], index=p_orig.items)
            tm.assert_panel_equal(p, expected)

        # GH 8473
        dates = date_range('1/1/2000', periods=8)
        df_orig = DataFrame(np.random.randn(8, 4), index=dates,
                            columns=['A', 'B', 'C', 'D'])

        expected = pd.concat([df_orig, DataFrame(
            {'A': 7}, index=[dates[-1] + 1])])
        df = df_orig.copy()
        df.loc[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)

        exp_other = DataFrame({0: 7}, index=[dates[-1] + 1])
        expected = pd.concat([df_orig, exp_other], axis=1)

        df = df_orig.copy()
        df.loc[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)
コード例 #7
0
    def test_partial_setting(self):

        # GH2578, allow ix and friends to partially set

        # series
        s_orig = Series([1, 2, 3])

        s = s_orig.copy()
        s[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        # iloc/iat raise
        s = s_orig.copy()

        def f():
            s.iloc[3] = 5.

        pytest.raises(IndexError, f)

        def f():
            s.iat[3] = 5.

        pytest.raises(IndexError, f)

        # ## frame ##

        df_orig = DataFrame(np.arange(6).reshape(3, 2),
                            columns=['A', 'B'],
                            dtype='int64')

        # iloc/iat raise
        df = df_orig.copy()

        def f():
            df.iloc[4, 2] = 5.

        pytest.raises(IndexError, f)

        def f():
            df.iat[4, 2] = 5.

        pytest.raises(IndexError, f)

        # row setting where it exists
        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.iloc[1] = df.iloc[2]
        tm.assert_frame_equal(df, expected)

        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.loc[1] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # like 2578, partial setting with dtype preservation
        expected = DataFrame(dict({'A': [0, 2, 4, 4], 'B': [1, 3, 5, 5]}))
        df = df_orig.copy()
        df.loc[3] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # single dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': [0, 2, 4]}))
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': Series([0, 2, 4])}))
        df = df_orig.copy()
        df['B'] = df['B'].astype(np.float64)
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # single dtype frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        with catch_warnings(record=True):
            # ## panel ##
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'],
                           dtype='float64')

            # panel setting via item
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'],
                           dtype='float64')
            expected = p_orig.copy()
            expected['Item3'] = expected['Item1']
            p = p_orig.copy()
            p.loc['Item3'] = p['Item1']
            tm.assert_panel_equal(p, expected)

            # panel with aligned series
            expected = p_orig.copy()
            expected = expected.transpose(2, 1, 0)
            expected['C'] = DataFrame(
                {
                    'Item1': [30, 30, 30, 30],
                    'Item2': [32, 32, 32, 32]
                },
                index=p_orig.major_axis)
            expected = expected.transpose(2, 1, 0)
            p = p_orig.copy()
            p.loc[:, :, 'C'] = Series([30, 32], index=p_orig.items)
            tm.assert_panel_equal(p, expected)

        # GH 8473
        dates = date_range('1/1/2000', periods=8)
        df_orig = DataFrame(np.random.randn(8, 4),
                            index=dates,
                            columns=['A', 'B', 'C', 'D'])

        expected = pd.concat(
            [df_orig, DataFrame({'A': 7}, index=[dates[-1] + 1])])
        df = df_orig.copy()
        df.loc[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)

        exp_other = DataFrame({0: 7}, index=[dates[-1] + 1])
        expected = pd.concat([df_orig, exp_other], axis=1)

        df = df_orig.copy()
        df.loc[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)
コード例 #8
0
ファイル: opticalstack.py プロジェクト: Python3pkg/PAME
class DielectricSlab(HasTraits):
    '''Class used to store data in an interactive tabular environment'''
    
    # Need base_app to delegate fiberparms, so find that delegate the rest
    base_app = Any
    specparms = DelegatesTo('base_app')
    fiberparms = DelegatesTo('base_app')
    layereditor = DelegatesTo('base_app')

    x_unit = DelegatesTo('specparms') #<-- Required by optic_view for xaxis, wish easier to acess these globally
    lambdas = DelegatesTo('specparms')	
    Mode = DelegatesTo('fiberparms')
    angles = DelegatesTo('fiberparms')
    angles_radians=DelegatesTo('fiberparms')
#   betas=DelegatesTo('fiberparms')

    angle_avg = DelegatesTo('fiberparms')
    N = DelegatesTo('fiberparms')
    
#    layereditor=Instance(LayerEditor,())            #Need to initialize this because properties depend on this instance
    stack= DelegatesTo('layereditor')               #Variables are stored here just because they can be useful for future implementations

    # PRIMARY STORAGE OBJECT FROM TRANSFER MATRIX FORMALISM
    optical_stack = Instance(Panel)
    opticview = Instance(OpticalView)

    nsubstrate=Property(Array, depends_on='stack')
    ns=Property(Array, depends_on='stack')    #This is a list of arrays [n1, n2, n3] one for each layer
    ds=Property(Array, depends_on='stack')    #This is a lis

    #sim_designator=Str('New Simulation') #<--- WHY
    implements(IOptic)
    
    # Need to initialize base_app first or Delegation Will not work
    def __init__(self, *args, **kwargs):
        self.base_app = kwargs.pop('base_app')
        super(DielectricSlab, self).__init__(*args, **kwargs)

    def _opticview_default(self):
        return OpticalView(optic_model = self)

    def update_opticview(self): #pass
        """ Recomputes optical parameter. Updates the plot.  """
        # Updates Stack parameters (R, T, A, rt...)
        self.update_optical_stack()
        # Updates plot (opticview)
        self.opticview.update()

    #@cached_property
    def _get_ns(self): 
        rows=len(self.stack) 
        cols=self.lambdas.shape[0]
        ns=empty( (rows, cols), dtype=complex )
        for i in range(rows):	
            layer=self.stack[i]
            ns[i,:]=layer.material.narray	#LIST OF ARRAYS NOT AN ARRAY 		
        return ns	

    #@cached_property
    def _get_nsubstrate(self):
        for layer in self.stack:
            if layer.name == 'Substrate':
                return layer.material.narray

    #@cached_property
    def _get_ds(self): 
        """Returns inf, d1, d2, d3, inf for layers"""
        ds = [inf, inf]
        for layer in self.stack:
            if layer.d != globalparms.semiinf_layer:  #When does this happen?  Substrate/solvent?
                ds.insert(-1, layer.d)
        return array(ds)

    # RENAME
    def update_optical_stack(self):
        """ Calls the transfer method matrix vectorially (for each wavelength) and for each angle.
        Results are stored in a pandas Panel of Item Axis Angle.  For each angle, there's a 
        DataFrame which stores R(lam), T(lam), r(lam) etc... ie the vectorized reflectance, 
        transmittance, reflectance amplitude etc... anything returned by vector_com_tmm().
        
        If polarization is both, the average of these values at each polariziation is stored.  For example
        Rs + Rp / 2.0 gives the average, unpolarized reflection coefficient.  This should work for
        complex quantities like the reflection amplitude (ie rs + rp / 2.0); however, the average
        (possilby complex) reflection amplitude squared is not necessarily equal to Rs + Rp /2 unless
        rs and rp are real.  
        
        The takeaway is that for unpolarized light, the operation (results_s + results_p) / 2.0 is performed
        on the DataFrames, irregardless of a real or complex value in each columns.  We confirmed this works
        as expected, and when plotted, only the real part will be plotted anyway (default behavior of pandas plot).
        """
        print('recomputing optical stack')

        if self.Mode == 'S-polarized':
            pol = 's'
        elif self.Mode == 'P-polarized':
            pol = 'p'
        elif self.Mode == 'Unpolarized':
            pol = 'both'
        else:
            raise OpticalModelError('Mode must be "S-polarized", "P-polarized" or Unpolarized; crashing intentionally.')
            sys.exit()
            

        paneldict = {}        
        for ang in self.angles:
            # CALCULATION IN RADIAN MODE
            ang_rad = math.radians(ang)
            
            if pol == 'both':
                df_s = vector_com_tmm('s', self.ns, self.ds, ang_rad, self.lambdas) 
                df_p = vector_com_tmm('p', self.ns, self.ds, ang_rad, self.lambdas)                  
                result_dataframe = (df_s+df_p) / 2.0

                # Add ellipsometry parameter Psi
                tan_psi = df_p['r_amp'].abs() / df_s['r_amp'].abs()
                result_dataframe['r_psi'] = np.arctan(tan_psi)

                # j ln( tan_psi * [r_s / r_p ] ) reversed ratio in algebra, work it out...

                # Use mpmath http://mpmath.googlecode.com/svn/trunk/doc/build/functions/powers.html
                # But mpmath needs loops, doesn't work on array
                # Tried numpy.log, numpy.lib.scimath.log.... same thing
                result_dataframe['r_delta'] = 1.0j * \
                    (np.log( (tan_psi * (df_s['r_amp'] / df_p['r_amp']) ) ))
                
                
            else:
                result_dataframe = vector_com_tmm(
                    pol, self.ns, self.ds, ang_rad, self.lambdas
                                            )
                # FILL PSI/DELTA TO NANS IF UNPOLARIZED!
                result_dataframe['r_psi'] = np.nan * np.empty(len(self.lambdas))
                result_dataframe['r_delta'] = np.nan * np.empty(len(self.lambdas))
                

            paneldict[ang] = result_dataframe

        # UPDATE optical_stack!
        self.optical_stack = Panel(paneldict)
              

    def as_stack(self, attr):
        """ Return attribute from optical stack in a 2darray.  IE if have 5 angles and 
        for each angle have 100 reflectance coefficients, returns a 5x100 matrix.  Used
        for arrayplotdata compatibility with .
        """
        # Potentially going to mix complex and floats, so these will be objects.
        out_2d = np.vstack([self.optical_stack[item][attr] for item in self.optical_stack])
        #print out_2d, '\nattr', out_2d.dtype
        return out_2d
    
    def _angle_avg_default(self):
        return 'Equal'
    
    # Should this return Series instead??  For simulation, have to reconstruct it...
    def compute_average(self, attr):
        """ Returns the angle average of an optical parameter of self.optical_stack,
        eg "R" or "A".  Averaging style delegates to FiberParms (angle_avg)
        """
        # DOES IT MATTER THAT AVERAGE IS COMPLEX
        matrix = self.as_stack(attr)

        if self.angle_avg == 'Gupta': 
            return self.gupta_averaging(attr)

        elif self.angle_avg == 'Equal': 
            return np.average(matrix, axis=0)            
        
        else:
            raise OpticalModelError('Unknown averaging style: %s' % self.angle_avg)
            

    def gupta_averaging(self, matrix):
        """ CITE ME!!
        matrix is the return of as_stack(attr) where attr can be Reflectance, Transmittance 
        etc..."""
        P_num=empty((self.angles.shape[0], self.nsubstrate.shape[0]), dtype=float)  #<-- COMPLEX!? RN IS NOW COMPLEX
        P_den=empty((self.angles.shape[0], self.nsubstrate.shape[0]), dtype=float)
        
        sa = np.sin(self.angles_radians)
        ca = np.cos(self.angles_radians)
        
        for i in range(len(self.angles)):
            f1=self.nsubstrate**2 * sa[i] * ca[i]       #Technically nsubstrate is complex so this is complaining
            Rn=self.matrix[i,:]**self.N[i]
            logging.info('N, rn', self.N[i], Rn)

#			f2=1.0 - (self.nsubstrate**2 * self.ca[i]**2)     
#			f3=f1/(f2**2) 	

            P_num[i,:]=(self.matrix[i,:]**Rn) * f1  #NOTATION FROM GUPTA LED  I THINK ITS NUMERICALLY UNSTABLE BECAUSE R<1 and N is huge so equals 0
            P_den[i,:]=f1	

        tempnum =simps(P_num, axis=0, even='last')
        tempden =simps(P_den, axis=0, even='last')
        return tempnum/tempden
    
    def simulation_requested(self, update=False):
        """ Returns optical stack and any other useful traits of dielectric slab for 
        simulation. Update used to skip updating, since simulations often call this twice.
        """
        if update:
            self.update_optical_stack()
        return {'optical_stack':self.optical_stack.copy(deep=True)}
コード例 #9
0
ファイル: opticalstack.py プロジェクト: hugadams/PAME
class DielectricSlab(HasTraits):
    '''Class used to store data in an interactive tabular environment'''
    
    # Need base_app to delegate fiberparms, so find that delegate the rest
    base_app = Any
    specparms = DelegatesTo('base_app')
    fiberparms = DelegatesTo('base_app')
    layereditor = DelegatesTo('base_app')

    x_unit = DelegatesTo('specparms') #<-- Required by optic_view for xaxis, wish easier to acess these globally
    lambdas = DelegatesTo('specparms')	
    Mode = DelegatesTo('fiberparms')
    angles = DelegatesTo('fiberparms')
    angles_radians=DelegatesTo('fiberparms')
#   betas=DelegatesTo('fiberparms')

    angle_avg = DelegatesTo('fiberparms')
    N = DelegatesTo('fiberparms')
    
#    layereditor=Instance(LayerEditor,())            #Need to initialize this because properties depend on this instance
    stack= DelegatesTo('layereditor')               #Variables are stored here just because they can be useful for future implementations

    # PRIMARY STORAGE OBJECT FROM TRANSFER MATRIX FORMALISM
    optical_stack = Instance(Panel)
    opticview = Instance(OpticalView)

    nsubstrate=Property(Array, depends_on='stack')
    ns=Property(Array, depends_on='stack')    #This is a list of arrays [n1, n2, n3] one for each layer
    ds=Property(Array, depends_on='stack')    #This is a lis

    #sim_designator=Str('New Simulation') #<--- WHY
    implements(IOptic)
    
    # Need to initialize base_app first or Delegation Will not work
    def __init__(self, *args, **kwargs):
        self.base_app = kwargs.pop('base_app')
        super(DielectricSlab, self).__init__(*args, **kwargs)

    def _opticview_default(self):
        return OpticalView(optic_model = self)

    def update_opticview(self): #pass
        """ Recomputes optical parameter. Updates the plot.  """
        # Updates Stack parameters (R, T, A, rt...)
        self.update_optical_stack()
        # Updates plot (opticview)
        self.opticview.update()

    #@cached_property
    def _get_ns(self): 
        rows=len(self.stack) 
        cols=self.lambdas.shape[0]
        ns=empty( (rows, cols), dtype=complex )
        for i in range(rows):	
            layer=self.stack[i]
            ns[i,:]=layer.material.narray	#LIST OF ARRAYS NOT AN ARRAY 		
        return ns	

    #@cached_property
    def _get_nsubstrate(self):
        for layer in self.stack:
            if layer.name == 'Substrate':
                return layer.material.narray

    #@cached_property
    def _get_ds(self): 
        """Returns inf, d1, d2, d3, inf for layers"""
        ds = [inf, inf]
        for layer in self.stack:
            if layer.d != globalparms.semiinf_layer:  #When does this happen?  Substrate/solvent?
                ds.insert(-1, layer.d)
        return array(ds)

    # RENAME
    def update_optical_stack(self):
        """ Calls the transfer method matrix vectorially (for each wavelength) and for each angle.
        Results are stored in a pandas Panel of Item Axis Angle.  For each angle, there's a 
        DataFrame which stores R(lam), T(lam), r(lam) etc... ie the vectorized reflectance, 
        transmittance, reflectance amplitude etc... anything returned by vector_com_tmm().
        
        If polarization is both, the average of these values at each polariziation is stored.  For example
        Rs + Rp / 2.0 gives the average, unpolarized reflection coefficient.  This should work for
        complex quantities like the reflection amplitude (ie rs + rp / 2.0); however, the average
        (possilby complex) reflection amplitude squared is not necessarily equal to Rs + Rp /2 unless
        rs and rp are real.  
        
        The takeaway is that for unpolarized light, the operation (results_s + results_p) / 2.0 is performed
        on the DataFrames, irregardless of a real or complex value in each columns.  We confirmed this works
        as expected, and when plotted, only the real part will be plotted anyway (default behavior of pandas plot).
        """
        print 'recomputing optical stack'

        if self.Mode == 'S-polarized':
            pol = 's'
        elif self.Mode == 'P-polarized':
            pol = 'p'
        elif self.Mode == 'Unpolarized':
            pol = 'both'
        else:
            raise OpticalModelError('Mode must be "S-polarized", "P-polarized" or Unpolarized; crashing intentionally.')
            sys.exit()
            

        paneldict = {}        
        for ang in self.angles:
            # CALCULATION IN RADIAN MODE
            ang_rad = math.radians(ang)
            
            if pol == 'both':
                df_s = vector_com_tmm('s', self.ns, self.ds, ang_rad, self.lambdas) 
                df_p = vector_com_tmm('p', self.ns, self.ds, ang_rad, self.lambdas)                  
                result_dataframe = (df_s+df_p) / 2.0

                # Add ellipsometry parameter Psi
                tan_psi = df_p['r_amp'].abs() / df_s['r_amp'].abs()
                result_dataframe['r_psi'] = np.arctan(tan_psi)

                # j ln( tan_psi * [r_s / r_p ] ) reversed ratio in algebra, work it out...

                # Use mpmath http://mpmath.googlecode.com/svn/trunk/doc/build/functions/powers.html
                # But mpmath needs loops, doesn't work on array
                # Tried numpy.log, numpy.lib.scimath.log.... same thing
                result_dataframe['r_delta'] = 1.0j * \
                    (np.log( (tan_psi * (df_s['r_amp'] / df_p['r_amp']) ) ))
                
                
            else:
                result_dataframe = vector_com_tmm(
                    pol, self.ns, self.ds, ang_rad, self.lambdas
                                            )
                # FILL PSI/DELTA TO NANS IF UNPOLARIZED!
                result_dataframe['r_psi'] = np.nan * np.empty(len(self.lambdas))
                result_dataframe['r_delta'] = np.nan * np.empty(len(self.lambdas))
                

            paneldict[ang] = result_dataframe

        # UPDATE optical_stack!
        self.optical_stack = Panel(paneldict)
              

    def as_stack(self, attr):
        """ Return attribute from optical stack in a 2darray.  IE if have 5 angles and 
        for each angle have 100 reflectance coefficients, returns a 5x100 matrix.  Used
        for arrayplotdata compatibility with .
        """
        # Potentially going to mix complex and floats, so these will be objects.
        out_2d = np.vstack([self.optical_stack[item][attr] for item in self.optical_stack])
        #print out_2d, '\nattr', out_2d.dtype
        return out_2d
    
    def _angle_avg_default(self):
        return 'Equal'
    
    # Should this return Series instead??  For simulation, have to reconstruct it...
    def compute_average(self, attr):
        """ Returns the angle average of an optical parameter of self.optical_stack,
        eg "R" or "A".  Averaging style delegates to FiberParms (angle_avg)
        """
        # DOES IT MATTER THAT AVERAGE IS COMPLEX
        matrix = self.as_stack(attr)

        if self.angle_avg == 'Gupta': 
            return self.gupta_averaging(attr)

        elif self.angle_avg == 'Equal': 
            return np.average(matrix, axis=0)            
        
        else:
            raise OpticalModelError('Unknown averaging style: %s' % self.angle_avg)
            

    def gupta_averaging(self, matrix):
        """ CITE ME!!
        matrix is the return of as_stack(attr) where attr can be Reflectance, Transmittance 
        etc..."""
        P_num=empty((self.angles.shape[0], self.nsubstrate.shape[0]), dtype=float)  #<-- COMPLEX!? RN IS NOW COMPLEX
        P_den=empty((self.angles.shape[0], self.nsubstrate.shape[0]), dtype=float)
        
        sa = np.sin(self.angles_radians)
        ca = np.cos(self.angles_radians)
        
        for i in range(len(self.angles)):
            f1=self.nsubstrate**2 * sa[i] * ca[i]       #Technically nsubstrate is complex so this is complaining
            Rn=self.matrix[i,:]**self.N[i]
            logging.info('N, rn', self.N[i], Rn)

#			f2=1.0 - (self.nsubstrate**2 * self.ca[i]**2)     
#			f3=f1/(f2**2) 	

            P_num[i,:]=(self.matrix[i,:]**Rn) * f1  #NOTATION FROM GUPTA LED  I THINK ITS NUMERICALLY UNSTABLE BECAUSE R<1 and N is huge so equals 0
            P_den[i,:]=f1	

        tempnum =simps(P_num, axis=0, even='last')
        tempden =simps(P_den, axis=0, even='last')
        return tempnum/tempden
    
    def simulation_requested(self, update=False):
        """ Returns optical stack and any other useful traits of dielectric slab for 
        simulation. Update used to skip updating, since simulations often call this twice.
        """
        if update:
            self.update_optical_stack()
        return {'optical_stack':self.optical_stack.copy(deep=True)}
コード例 #10
0
ファイル: fin.py プロジェクト: rufinomendoza/threadneedle
tickers = ['AAPL', 'GS']
sd = '1/1/2000'
ed = '6/1/2012'
px = Panel({t: web.get_data_yahoo(t,sd,ed) for t in tickers})

#
close.save('pickled')
read = pd.load ('pickled')
read
#

close.to_csv('csv')
csv = pd.read_csv('csv', index_col=, parse_dates=True) #idx parse
csv.index[0]

px.ix[10:20, 'AAPL']
px.ix[10:20, 'GS']

pl = px.copy()
zero_neg_mask = pl <= 0
pl[zero_neg_mask] = np.nan
pl.ix[10:20, 'AAPL']

filled = p1.fillna(method='ffill')
filled.ix[10:20, 'AAPL']

limited = p1.fillna(method='ffill',limit=2)
filled.ix[10:20,'AAPL']


pl.AAPL.rank()