Ticket #11733: compare_test.py

File compare_test.py, 4.8 KB (added by Anton Piccardo-Selg, 5 years ago)
Line 
1from mantid import *
2import sys
3def run_comparison(input_ws_name, output_ws_name, relative_time_shift =0.0, absolute_time_shift="", log = False):
4    # Create a duplicate
5    duplicate = CloneWorkspace(InputWorkspace = input_ws_name)
6   
7    # Run ChangeTimeZero
8    ChangeTimeZero(InputWorkspace = input_ws_name, OutputWorkspace = output_ws_name, 
9                           RelativeTimeOffset= relative_time_shift , AbsoluteTimeOffset = absolute_time_shift)
10   
11    # Compare the sample logs
12    time_shift = 0.0
13    if (absolute_time_shift != ""):
14        proton_charge_times = duplicate.getRun().getProperty("proton_charge").times
15        if len(proton_charge_times) == 0:
16            sys.exit("When using absolute times you need a proton charge log")
17        time_shift = time_duration.total_nanoseconds(proton_charge_times[0]- DateAndTime(absolute_time_shift))
18    else:
19        time_shift = relative_time_shift
20
21    compare_logs(output_ws_name, duplicate, time_shift, log)
22   
23    # Compare the neutrons for event workspaces
24    if isinstance(duplicate, IEventWorkspace):
25        compare_neutrons(output_ws_name, duplicate, time_shift)
26
27def compare_logs(output_ws_name, duplicate, time_shift, log):
28    # Iterate over all TimeSeries properties
29    ws = mtd[output_ws_name]
30    run = ws.getRun()
31    props = run.getProperties()
32    props_duplicate = duplicate.getRun().getProperties()
33   
34    size = len(props)
35    size_duplicate = len(props_duplicate)
36    assert(size == size_duplicate)
37   
38    for prop in props:
39        prop_2 = []
40        for prop_duplicate in props_duplicate:
41            prop_2 = []
42            if prop_duplicate.name == prop.name:
43                prop_2 = prop_duplicate
44                break
45               
46        if isinstance(prop, FloatTimeSeriesProperty) or isinstance(prop, BoolTimeSeriesProperty) or isinstance(prop, StringTimeSeriesProperty):
47            # Need to find the property in duplicate ws
48            times_1 = prop.times
49            times_2 = prop_2.times
50            assert(len(times_1) == len(times_2))
51            compare_time_series_log(times_1, times_2, time_shift, log)
52        elif isinstance(prop, StringPropertyWithValue) and isinstance(prop_2, StringPropertyWithValue):
53            val_1 = 0.0
54            val_2 = 0.0
55            try:
56                val_1 = DateAndTime(prop.value)
57                val_2 = DateAndTime(prop_2.value)
58                compare_string_value(val_1, val_2, time_shift, log)
59            except ValueError:
60                pass
61
62def compare_time_series_log(times_1, times_2, expected_time_shift, log):
63    for i in range(0,len(times_1)):
64        if time_duration.total_nanoseconds(times_1[i] - times_2[i])/1e9 != expected_time_shift:
65            sys.exit("The time difference for a time series property does not seem correct.")
66           
67        if log:
68            print "| Orig: " + str(times_1[i]) + "  |  Shift: " + str(times_2[i]) + "  |  Diff[s]: "  + str(time_duration.total_nanoseconds(times_1[i] - times_2[i])/1e9) + "  |" 
69
70
71def compare_string_value(val1, val2, expected_time_shift, log):
72    if time_duration.total_nanoseconds(val1 - val2)/1e9 != expected_time_shift:
73        sys.exit("The time difference for a string property does not seem correct.")
74    if log:
75        print "| ValOrig: " + str(val1) + "  |  ValShift: " + str(val2) + "  |  Diff[s]: "  + str(time_duration.total_nanoseconds(val1 - val2)/1e9) + "  |" 
76
77def compare_neutrons(output_ws_name, duplicate, time_shift):
78    ws = mtd[output_ws_name]
79    for index in range(0,ws.getNumberHistograms()):
80        pulse_times = ws.getEventList(index).getPulseTimes()
81        pulse_times_2 = duplicate.getEventList(index).getPulseTimes()
82        for i in range(0, len(pulse_times)):
83            time1 = pulse_times[i]
84            time2 = pulse_times_2[i]
85            if time_duration.total_nanoseconds(pulse_times[i] - pulse_times_2[i])/1e9 != time_shift:
86                sys.exit("The time difference for neutrons does not seem correct. Original was: " + str(pulse_times[i]) + " and shifted was: pulse_times_2[i]" )
87
88#-------------------------------------------------------------------------
89# DEFINE SHIFTS HERE - You need to load a your workspace into Mantid and use their name as the input
90#-------------------------------------------------------------------------
91#SET THIS FLAG TO PRINT LOG COMPARISON
92PRINT_LOG = True
93
94#-- RELATIVE TIME SHIFT
95input_ws_name_REL = "SANS2D00022023"
96output_ws_name_REL = "REL_SAMPLE"
97time_shift_REL = 1000.0
98run_comparison(input_ws_name= input_ws_name_REL, output_ws_name= output_ws_name_REL,  relative_time_shift= time_shift_REL, log = PRINT_LOG)
99
100#-- ABSOLUTE TIME SHIFT
101input_ws_name_ABS = "SANS2D00022023"
102output_ws_name_ABS = "ABS_SAMPLE"
103absolute_time_shift_ABS ="2013-10-25T13:58:03"
104run_comparison(input_ws_name= input_ws_name_ABS, output_ws_name= output_ws_name_ABS,  absolute_time_shift= absolute_time_shift_ABS)
105