def test_calculatefirstderivative_xaxis(self): """Step4 - derivative: testing the first derivative calculation - axis x""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) [xdata_first, ydata_first] = peakfinder.getFirstDerivative() xdata10 = xdata_first[0:10] self.assertEqual(xdata10, [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5])
def test_calculatefirstderivative_yaxis(self): """Step4 - derivative: testing the first derivative calculation - axis y""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) [xdata_first, ydata_first] = peakfinder.getFirstDerivative() ydata10 = ydata_first[0:10] self.assertEqual(ydata10, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 1.0, -1.0])
def test_get5HighestPoints_xdata(self): """Step2 - 5highest points: using run 125682 to check calculation of 5 highest points - xdata """ [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) [high_x, high_y] = peakfinder1.get5HighestPoints() high_x = high_x.tolist() self.assertEqual(high_x, [155.0, 156.0, 154.0, 157.0, 153.0])
def test_get5HighestPoints_ydata(self): """Step2 - 5highest points: using run 125682 to check calculation of 5 highest points - ydata """ [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) [high_x, high_y] = peakfinder1.get5HighestPoints() high_y = high_y.tolist() self.assertEqual(high_y, [32351.0, 28999.0, 19351.0, 9503.0, 2796.0])
def __init__(self, workspace, lconfig=None, is_data=True, parent=None): #TODO: there appears to be duplicate instances of this class being # created when loading a file. self.parent = parent self._tof_axis = [] self.Ixyt = [] self.Exyt = [] self.data = [] self.xydata = [] self.ytofdata = [] self.countstofdata = [] self.countxdata = [] self.ycountsdata = [] self.workspace = mtd[workspace] self.workspace_name = workspace mt_run = self.workspace.getRun() self.ipts = mt_run.getProperty('experiment_identifier').value self.run_number = mt_run.getProperty('run_number').value if float(self.run_number) < RUN_NUMBER_0_BETTER_CHOPPER_COVERAGE: self.is_better_chopper_coverage = False self.lambda_requested = float( mt_run.getProperty('LambdaRequest').value[0]) self.lambda_requested_units = mt_run.getProperty('LambdaRequest').units self.thi = mt_run.getProperty('thi').value[0] self.thi_units = mt_run.getProperty('thi').units self.ths = mt_run.getProperty('ths').value[0] self.ths_units = mt_run.getProperty('ths').units self.tthd = mt_run.getProperty('tthd').value[0] self.tthd_units = mt_run.getProperty('tthd').units self.S1W = mt_run.getProperty('S1HWidth').value[0] self.S1H = mt_run.getProperty('S1VHeight').value[0] self.parent.current_ipts = mt_run.getProperty( 'experiment_identifier').value self.total_counts = self.workspace.getNumberEvents() try: self.SiW = mt_run.getProperty('SiHWidth').value[0] self.SiH = mt_run.getProperty('SiVHeight').value[0] self.isSiThere = True except: self.S2W = mt_run.getProperty('S2HWidth').value[0] self.S2H = mt_run.getProperty('S2VHeight').value[0] self.isSiThere = False self.attenuatorNbr = mt_run.getProperty('vATT').value[0] - 1 self.date = mt_run.getProperty('run_start').value sample = self.workspace.getInstrument().getSample() source = self.workspace.getInstrument().getSource() self.dMS = sample.getDistance(source) # create array of distances pixel->sample self.number_x_pixels = int( self.workspace.getInstrument().getNumberParameter( "number-of-x-pixels")[0]) # 256 self.number_y_pixels = int( self.workspace.getInstrument().getNumberParameter( "number-of-y-pixels")[0]) dPS_array = np.zeros((self.number_x_pixels, self.number_y_pixels)) for x in range(self.number_y_pixels): for y in range(self.number_x_pixels): _index = self.number_x_pixels * x + y detector = self.workspace.getDetector(_index) dPS_array[y, x] = sample.getDistance(detector) # distance sample->center of detector self.dSD = dPS_array[self.number_x_pixels / 2, self.number_y_pixels / 2] # distance source->center of detector self.dMD = self.dSD + self.dMS # calculate theta self.theta = self.calculate_theta() self.frequency = float(mt_run.getProperty('Speed1').value[0]) tof_coeff_narrow = 1.7 * 60 / self.frequency tof_coeff_large = 2.5 * 60 / self.frequency tof_coeff = 0.5 * 60 / self.frequency if lconfig is not None: autotmin = np.float(lconfig.tof_range[0]) autotmax = np.float(lconfig.tof_range[1]) else: if self.is_better_chopper_coverage: autotmin = self.dMD / H_OVER_M_NEUTRON * ( self.lambda_requested - tof_coeff_narrow) * 1e-4 autotmax = self.dMD / H_OVER_M_NEUTRON * ( self.lambda_requested + tof_coeff_narrow) * 1e-4 else: autotmin = self.dMD / H_OVER_M_NEUTRON * ( self.lambda_requested + tof_coeff - tof_coeff_narrow) * 1e-4 autotmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + tof_coeff + \ tof_coeff_narrow) * 1e-4 # automatically calcualte the TOF range for display if self.is_better_chopper_coverage: tmin = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested - tof_coeff_large) * 1e-4 tmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + tof_coeff_large) * 1e-4 else: tmin = self.dMD / H_OVER_M_NEUTRON * ( self.lambda_requested + tof_coeff - tof_coeff_large) * 1e-4 tmax = self.dMD / H_OVER_M_NEUTRON * ( self.lambda_requested + tof_coeff + tof_coeff_large) * 1e-4 if tmin < 0: tmin = 0 self.tof_range_auto = [autotmin, autotmax] # microS self.tof_range_auto_with_margin = [tmin, tmax] # microS # manual tof range (if user wants to use a manual time range) self.tof_range = [ autotmin, autotmax ] # for the first time, initialize tof_range like auto (microS) self.tof_range_manual = [autotmin, autotmax] self.binning = [tmin, self.read_options['bins'], tmax] self.calculate_lambda_range() self.incident_angle = 2. * self.calculate_theta( with_offset=False) # 2.theta self.calculate_q_range() # self.lambda_range = self.calculate_lambda_range() # Proton charge _proton_charge = float(mt_run.getProperty('gd_prtn_chrg').value) _proton_charge_units = mt_run.getProperty('gd_prtn_chrg').units new_proton_charge_units = 'mC' self.proton_charge = _proton_charge * 3.6 # to go from microA/h to mC self.proton_charge_units = new_proton_charge_units self.peak = [0, 0] self.back = [0, 0] self.clocking = [0, 0] self.back_flag = True self.all_plot_axis = AllPlotAxis() self.tof_auto_flag = True self.new_detector_geometry_flag = self.is_nexus_taken_after_refDate() self.data_loaded = False self.read_data() if lconfig is None: pf = PeakFinderDerivation(range(len(self.ycountsdata)), self.ycountsdata) [peak1, peak2] = pf.getPeaks() self.peak = [str(peak1), str(peak2)] backOffsetFromPeak = self.read_options['back_offset_from_peak'] back1 = int(peak1 - backOffsetFromPeak) back2 = int(peak2 + backOffsetFromPeak) self.back = [str(back1), str(back2)] lw_pf = LowResFinder(range(len(self.countsxdata)), self.countsxdata) [lowres1, lowres2] = lw_pf.get_low_res() self.low_res = [str(lowres1), str(lowres2)] clocking_pf = ClockingFinder(parent=self.parent, xdata=range(len(self.ycountsdata)), ydata=self.ycountsdata) [clocking1, clocking2] = clocking_pf.clocking clock_array = [clocking1, clocking2] clock_array.sort() self.clocking = [str(clocking1), str(clocking2)] else: # if we loaded a config that does not have the clocking info, we will have to retrieve once # everything has been loaded (before display) if lconfig.data_clocking[0] != '': self.clocking = [ np.float(lconfig.data_clocking[0]), np.float(lconfig.data_clocking[1]) ] else: clocking_pf = LowResFinder(range(len(self.ycountsdata)), self.ycountsdata) [clocking1, clocking2] = clocking_pf.get_low_res() self.clocking = [str(clocking1), str(clocking2)] self.tof_auto_flag = np.bool(lconfig.tof_auto_flag) if is_data: self.peak = [ np.int(lconfig.data_peak[0]), np.int(lconfig.data_peak[1]) ] self.back = [ np.int(lconfig.data_back[0]), np.int(lconfig.data_back[1]) ] self.low_res = [ np.int(lconfig.data_low_res[0]), np.int(lconfig.data_low_res[1]) ] self.low_res_flag = np.bool(lconfig.data_low_res_flag) self.back_flag = np.bool(lconfig.data_back_flag) else: self.peak = [ np.int(lconfig.norm_peak[0]), np.int(lconfig.norm_peak[1]) ] self.back = [ np.int(lconfig.norm_back[0]), np.int(lconfig.norm_back[1]) ] self.low_res = [ np.int(lconfig.norm_low_res[0]), np.int(lconfig.norm_low_res[1]) ] self.low_res_flag = np.bool(lconfig.norm_low_res_flag) self.back_flag = np.bool(lconfig.norm_back_flag)
def __init__(self, workspace, read_options): self.workspace = workspace self.read_options = read_options mt_run = self.workspace.getRun() self.run_number = mt_run.getProperty('run_number').value self.lambda_requested = float(mt_run.getProperty('LambdaRequest').value[0]) self.lambda_requested_units = mt_run.getProperty('LambdaRequest').units self.thi = mt_run.getProperty('thi').value[0] self.thi_units = mt_run.getProperty('thi').units self.tthd = mt_run.getProperty('tthd').value[0] self.tthd_units = mt_run.getProperty('tthd').units self.S1W = mt_run.getProperty('S1HWidth').value[0] self.S1H = mt_run.getProperty('S1VHeight').value[0] try: self.SiW = mt_run.getProperty('SiHWidth').value[0] self.SiH = mt_run.getProperty('SiVHeight').value[0] self.isSiThere = True except: self.S2W = mt_run.getProperty('S2HWidth').value[0] self.S2H = mt_run.getProperty('S2VHeight').value[0] self.attenuatorNbr = mt_run.getProperty('vATT').value[0] - 1 self.date = mt_run.getProperty('run_start').value self.full_file_name = mt_run.getProperty('Filename').value[0] self.filename = os.path.basename(self.full_file_name) sample = self.workspace.getInstrument().getSample() source = self.workspace.getInstrument().getSource() self.dMS = sample.getDistance(source) # create array of distances pixel->sample self.number_x_pixels = int(self.workspace.getInstrument().getNumberParameter("number-of-x-pixels")[0]) # 256 self.number_y_pixels = int(self.workspace.getInstrument().getNumberParameter("number-of-y-pixels")[0]) dPS_array = np.zeros((self.number_x_pixels, self.number_y_pixels)) for x in range(self.number_y_pixels): for y in range(self.number_x_pixels): _index = self.number_x_pixels * x + y detector = self.workspace.getDetector(_index) dPS_array[y, x] = sample.getDistance(detector) # distance sample->center of detector self.dSD = dPS_array[self.number_x_pixels / 2, self.number_y_pixels / 2] # distance source->center of detector self.dMD = self.dSD + self.dMS # calculate theta self.theta = self.calculate_theta() self.frequency = float(mt_run.getProperty('Speed1').value[0]) tof_coeff_large = 1.7 * 60 / self.frequency tmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + tof_coeff_large) * 1e-4 tmin = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested - tof_coeff_large) * 1e-4 if self.read_options['is_auto_tof_finder'] or self.tof_range == None: autotmin = tmin autotmax = tmax else: autotmin = np.float(self.tof_range[0]) autotmax = np.float(self.tof_range[1]) self.tof_range_auto = [autotmin, autotmax] # microS self.tof_range_auto_with_margin = [tmin, tmax] # microS self.tof_range = [autotmin, autotmax] # for the first time, initialize tof_range like auto (microS) self.binning = [tmin, self.read_options['bins'], tmax] self.calculate_lambda_range() self.q_range = self.calculate_q_range() self.incident_angle = self.calculate_theta(False) # Proton charge _proton_charge = float(mt_run.getProperty('gd_prtn_chrg').value) _proton_charge_units = mt_run.getProperty('gd_prtn_chrg').units new_proton_charge_units = 'mC' self.proton_charge = _proton_charge * 3.6 # to go from microA/h to mC self.proton_charge_units = new_proton_charge_units self.peak = [0, 0] self.back = [0, 0] self.back_flag = True self.all_plot_axis = AllPlotAxis self.tof_auto_flag = True self.new_detector_geometry_flag = True self.data_loaded = False self.read_data() if self.read_options['is_auto_peak_finder']: pf = PeakFinderDerivation(range(len(self.ycountsdata)), self.ycountsdata) [peak1, peak2] = pf.getPeaks() self.peak = [str(peak1), str(peak2)] backOffsetFromPeak = self.read_options['back_offset_from_peak'] back1 = int(peak1 - backOffsetFromPeak) back2 = int(peak2 + backOffsetFromPeak) self.back = [str(back1), str(back2)]
def test_calculateMinMaxDervativePixels_minPixelValue(self): """Step5 - calculate pixel of min derivative counts value""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) min_pixel_derivative_value = peakfinder.getMinDerivationPixelValue() self.assertEqual(min_pixel_derivative_value, 153.5)
def test_calculateMinMaxDervativePixels_maxValue(self): """Step5 - calculate max derivative counts value""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) max_derivative_value = peakfinder.getMaxDerivativeValue() self.assertEqual(max_derivative_value, 16555.0)
def test_calculatePeakPixel_peakPixelValue(self): """Step3 - calculate peak pixel value using run 125682""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) peak_pixel = peakfinder1.getPeakPixel() self.assertEqual(peak_pixel, 155.0)
def test_calcuatePeakPixel_sumPeakCountTimePixel(self): """Step3 - calculate peak pixel using run 125682 to check calculation of 5 highest points - sum_ydata """ [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) sum_five_highest_ydata = peakfinder1.getSumPeakCountsTimePixel() self.assertEqual(sum_five_highest_ydata, 14438061.0)
def test_case_hard_data_set(self): """Step7 - calculate final peak range using run 124135 (hard data set)""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/hard_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) peaks = peakfinder1.getPeaks() self.assertEqual(peaks, [145, 164])
def test_case_medium_data_set(self): """Step7 - calculate final peak range using run 124211 (medium data set)""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/medium_data_set.csv") peakfinder1 = PeakFinderDerivation(xdata, ydata, edata) peaks = peakfinder1.getPeaks() self.assertEqual(peaks, [151, 159])
def test_calculateAvgAndStdDerivation_std_deviation_counts_firstderi(self): """Step6 - calculate standard deviation of first derivation counts""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) std_deviation_counts_firstderi = peakfinder.getStdDeviationOfFirstDerivationCounts() self.assertAlmostEqual(std_deviation_counts_firstderi, 1741.838, delta=0.001)
def test_calculateAvgAndStdDerivation_mean_counts_firstderi(self): """Step6 - calculate average of first derivation counts""" [xdata, ydata, edata] = loadCsvFile("peak_finder_algorithms/easy_data_set.csv") peakfinder = PeakFinderDerivation(xdata, ydata, edata) mean_counts_firstderi = peakfinder.getAverageOfFirstDerivationCounts() self.assertEqual(mean_counts_firstderi, 0)
def __init__(self, workspace): self._tof_axis = [] self.Ixyt = [] self.Exyt= [] self.data = [] self.xydata = [] self.ytofdata = [] self.countstofdata = [] self.countxdata = [] self.ycountsdata = [] self.workspace = mtd[workspace] self.workspace_name = workspace mt_run = self.workspace.getRun() self.ipts = mt_run.getProperty('experiment_identifier').value self.run_number = mt_run.getProperty('run_number').value self.lambda_requested = float(mt_run.getProperty('LambdaRequest').value[0]) self.lambda_requested_units = mt_run.getProperty('LambdaRequest').units self.thi = mt_run.getProperty('thi').value[0] self.thi_units = mt_run.getProperty('thi').units self.tthd = mt_run.getProperty('tthd').value[0] self.tthd_units = mt_run.getProperty('tthd').units self.S1W = mt_run.getProperty('S1HWidth').value[0] self.S1H = mt_run.getProperty('S1VHeight').value[0] try: self.SiW = mt_run.getProperty('SiHWidth').value[0] self.SiH = mt_run.getProperty('SiVHeight').value[0] self.isSiThere = True except: self.S2W = mt_run.getProperty('S2HWidth').value[0] self.S2H = mt_run.getProperty('S2VHeight').value[0] self.isSiThere = False self.attenuatorNbr = mt_run.getProperty('vATT').value[0] - 1 self.date = mt_run.getProperty('run_start').value #self.full_file_name = mt_run.getProperty('Filename').value[0] #self.filename = ','.join([os.path.basename(_file) for _file in self.full_file_name]) sample = self.workspace.getInstrument().getSample() source = self.workspace.getInstrument().getSource() self.dMS = sample.getDistance(source) # create array of distances pixel->sample self.number_x_pixels = int(self.workspace.getInstrument().getNumberParameter("number-of-x-pixels")[0]) # 256 self.number_y_pixels = int(self.workspace.getInstrument().getNumberParameter("number-of-y-pixels")[0]) dPS_array = np.zeros((self.number_x_pixels, self.number_y_pixels)) for x in range(self.number_y_pixels): for y in range(self.number_x_pixels): _index = self.number_x_pixels * x + y detector = self.workspace.getDetector(_index) dPS_array[y, x] = sample.getDistance(detector) # distance sample->center of detector self.dSD = dPS_array[self.number_x_pixels / 2, self.number_y_pixels / 2] # distance source->center of detector self.dMD = self.dSD + self.dMS # calculate theta self.theta = self.calculate_theta() #if self.read_options['is_auto_tof_finder'] or self.tof_range == None: # automatically calculate the TOF range autotmin = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + 0.5 - 1.7) * 1e-4 autotmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + 0.5 + 1.7) * 1e-4 #else: #autotmin = np.float(self.tof_range[0]) #autotmax = np.float(self.tof_range[1]) # automatically calcualte the TOF range for display if mt_run.getProperty('Speed1').value[0] == 60: tmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + 0.5 + 2.5) * 1e-4 tmin = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + 0.5 - 2.5) * 1e-4 else: tmax = self.dMD / H_OVER_M_NEUTRON * (self.lambda_requested + 0.5 + 4.5) * 1e-4 tmin = 0 self.tof_range_auto = [autotmin, autotmax] # microS self.tof_range_auto_with_margin = [tmin, tmax] # microS # manual tof range (if user wants to use a manual time range) self.tof_range = [autotmin, autotmax] # for the first time, initialize tof_range like auto (microS) self.tof_range_manual = [autotmin, autotmax] self.binning = [tmin, self.read_options['bins'], tmax] self.calculate_lambda_range() self.incident_angle = 2.*self.calculate_theta(with_offset = False) # 2.theta self.calculate_q_range() # self.lambda_range = self.calculate_lambda_range() # Proton charge _proton_charge = float(mt_run.getProperty('gd_prtn_chrg').value) _proton_charge_units = mt_run.getProperty('gd_prtn_chrg').units new_proton_charge_units = 'mC' self.proton_charge = _proton_charge * 3.6 # to go from microA/h to mC self.proton_charge_units = new_proton_charge_units self.peak = [0, 0] self.back = [0, 0] self.clocking = [0, 0] self.back_flag = True self.all_plot_axis = AllPlotAxis() self.tof_auto_flag = True self.new_detector_geometry_flag = self.is_nexus_taken_after_refDate() self.data_loaded = False self.read_data() if self.read_options['is_auto_peak_finder']: pf = PeakFinderDerivation(range(len(self.ycountsdata)), self.ycountsdata) [peak1, peak2] = pf.getPeaks() self.peak = [str(peak1), str(peak2)] backOffsetFromPeak = self.read_options['back_offset_from_peak'] back1 = int(peak1 - backOffsetFromPeak) back2 = int(peak2 + backOffsetFromPeak) self.back = [str(back1), str(back2)] lw_pf = LowResFinder(range(len(self.countsxdata)), self.countsxdata) [lowres1, lowres2] = lw_pf.get_low_res() self.low_res = [str(lowres1), str(lowres2)] clocking_pf = LowResFinder(range(len(self.ycountsdata)), self.ycountsdata) [clocking1, clocking2] = clocking_pf.get_low_res() self.clocking = [str(clocking1), str(clocking2)]