Skip to content
Snippets Groups Projects
analysis.py 3.03 KiB
Newer Older
import numpy as np
import pandas as pd
import matplotlib as plt


class Analysis:
lorenzennio's avatar
lorenzennio committed
    """
    Analysis class:
        This class computes mean and error on the detector output angles.
        The results can then be visualized with two plots.
    """

    def __init__(self, angles):
lorenzennio's avatar
lorenzennio committed
        """
        The class is initialized with the angles from the detector results
        and then automatically deletes 'None' entries and computes the minimum
        range of angles and the error.
        """

        self.xAngles = pd.Series({'High' : angles[0][0],
                                    'Low'  : angles[0][1],})
        self.yAngles = pd.Series({'High' : angles[1][0],
                                    'Low'  : angles[1][1]})

        self.results = pd.DataFrame({'Mean' : np.zeros(2),
                                     'Error': np.zeros(2)})
lorenzennio's avatar
lorenzennio committed
        #Determine mean and std
        self.FillAngles()
        self.Angles(self.xAngles['High'], self.xAngles['Low'], call=0)
        self.Angles(self.yAngles['High'], self.yAngles['Low'], call=1)

        #TODO: Plot

lorenzennio's avatar
lorenzennio committed
    def FillAngles(self):
        """
        Fill the angle data into the class vaiebles.
        """
        self.xAngles['High'] = np.array([x for x in self.xAngles['High'] if x is not None])
        self.xAngles['Low'] = np.array([x for x in self.xAngles['Low'] if x is not None])
        self.yAngles['High'] = np.array([x for x in self.yAngles['High'] if x is not None])
        self.yAngles['Low'] = np.array([x for x in self.yAngles['Low'] if x is not None])

    def Angles(self, high, low, call=0):
lorenzennio's avatar
lorenzennio committed
        """
        Algorithm to compute the minimum possible range of angles.
        The upper and lower angles must be given as a argument and also
        the 'call' variable secifies the plane you are working in
        (x-z or y-z plane).
        The algorithim loops over the angles from the last detector layer
        to the first, and replaces the upper and lower angle bounds, if
        a certain layer restricts the range further.
        The mean is calculated as the arithmetic mean of the resulting
        angles and the error is the difference from the mean to the angle
        bounds.
        """
        highRev = high[::-1]
        lowRev = low[::-1]
        highBound = highRev[0]
        lowBound = lowRev[0]
        print(highBound,lowBound)
        for a,b in zip(highRev[1:], lowRev[1:]):
            if a < highBound and a > lowBound:
                highBound = a
            if b < highBound and b > lowBound:
                lowBound = b
lorenzennio's avatar
lorenzennio committed
            print(highBound,lowBound)
        error = (highBound - lowBound)/2
        mean = highBound - error
        self.results['Mean'][call] = mean
        self.results['Error'][call] = error

    def Plot(self):
        ...


if __name__ == "__main__":
    high = np.array([6,8,7,10,None])
    low = np.array([5,4,1,3,None])
    '''
    a = pd.DataFrame({'H' : high})
    a['H'] = np.array([x for x in a['H'] if x is not None])
    print(a['H'])
    '''
    angles = [[high, low], [high, low]]
    A = Analysis(angles)
    print(A.results)