示例#1
0
    def test_name_search_read_error(self):
        """
        Check an NameError is returned when no match is found
        """

        data_list = setup_McStasData_array()

        with self.assertRaises(NameError):
            name_search("Hero8", data_list)
示例#2
0
    def test_name_search_type_error_not_list(self):
        """
        Check error is given even when data list is just single object
        """

        data_list = set_dummy_McStasData_2d("Last_object_2d")

        with self.assertRaises(RuntimeError):
            name_search("Hero", data_list)
示例#3
0
    def test_name_search_type_error_not_McStasData(self):
        """
        Checks that an error is returned if the given dataset contains
        non McStasData objects
        """

        data_list = [1, 2, 3]

        with self.assertRaises(RuntimeError):
            name_search(1, data_list)
def add_data(initial, new_data):
    """
    Method for adding new data to a data set

    Updates Intensity, Error and Ncount

    Updates all data except metadata info
    """

    for monitor in initial:
        ref_ncount = float(monitor.metadata.info["Ncount"])

        new_monitor = name_search(monitor.name, new_data)
        new_ncount = float(new_monitor.metadata.info["Ncount"])

        total_ncount = ref_ncount + new_ncount

        scale_old = ref_ncount / total_ncount
        scale_new = new_ncount / total_ncount

        monitor.Intensity = scale_old * monitor.Intensity + scale_new * new_monitor.Intensity
        monitor.Error = np.sqrt(scale_old**2 * monitor.Error**2 +
                                scale_new**2 * new_monitor.Error**2)
        monitor.Ncount = monitor.Ncount + new_monitor.Ncount

        monitor.metadata.info["Ncount"] = total_ncount
示例#5
0
    def test_name_plot_options_simple(self):
        """
        Check set_plot_options can modify given attribute
        """

        data_list = setup_McStasData_array()
        name_plot_options("Hero", data_list, colormap="Oranges")
        hero_object = name_search("Hero", data_list)
        self.assertEqual(hero_object.plot_options.colormap, "Oranges")
示例#6
0
    def test_name_plot_options_simple(self):
        """
        Test simple case
        """

        data_list = setup_McStasData_array()
        name_plot_options("Hero", data_list, colormap="very hot")
        hero_object = name_search("Hero", data_list)
        self.assertEqual(hero_object.plot_options.colormap, "very hot")
示例#7
0
    def test_name_search_type_error_not_McStasData(self):
        """
        Test simple case
        """

        data_list = [1, 2, 3]

        with self.assertRaises(NameError):
            hero_object = name_search(1, data_list)
示例#8
0
    def test_name_search_type_error_not_list(self):
        """
        Test simple case
        """

        data_list = set_dummy_McStasData_2d("Last_object_2d")

        with self.assertRaises(NameError):
            hero_object = name_search("Hero", data_list)
示例#9
0
    def test_name_search_read_error(self):
        """
        Test simple case
        """

        data_list = setup_McStasData_array()

        with self.assertRaises(NameError):
            hero_object = name_search("Hero8", data_list)
示例#10
0
    def test_complex_instrument_interface(self, mock_stdout):
        """
        Test that a simulation can be performed through the simulation
        interface, or as close as I can through scripting.
        Need to join the simulation thread to the main thread in order
        to wait for the completion as it is performed in a new thread.
        """
        CURRENT_DIR = os.getcwd()
        THIS_DIR = os.path.dirname(os.path.abspath(__file__))
        os.chdir(THIS_DIR)

        Instr = setup_complex_instrument()

        interface = SimInterface(Instr)
        interface.show_interface()

        change = FakeChange()

        interface.run_simulation_thread(change)

        for thread in threading.enumerate():
            if thread.name != "MainThread":
                thread.join()

        data = interface.plot_interface.data

        os.chdir(CURRENT_DIR)

        intensity_data_pos = functions.name_search("PSD_1D_1", data).Intensity
        sum_outside_beam = sum(intensity_data_pos[0:50])
        sum_inside_beam = sum(intensity_data_pos[51:99])
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)

        intensity_data_neg = functions.name_search("PSD_1D_2", data).Intensity
        sum_outside_beam = sum(intensity_data_neg[51:99])
        sum_inside_beam = sum(intensity_data_neg[0:50])
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)

        intensity_data_all = functions.name_search("PSD_1D", data).Intensity
        sum_outside_beam = sum(intensity_data_all[49:51])
        sum_inside_beam = (sum(intensity_data_all[0:45]) +
                           sum(intensity_data_all[56:99]))
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)
示例#11
0
    def test_name_search_read_repeat(self):
        """
        Test simple case with repeat name
        """

        data_list = setup_McStasData_array_repeat()

        hero_object = name_search("Big_Hero", data_list)

        self.assertEqual(hero_object.metadata.dimension, 123)
示例#12
0
    def test_name_search_filename_read(self):
        """
        Test simple case
        """

        data_list = setup_McStasData_array()

        hero_object = name_search("Hero.dat", data_list)

        self.assertEqual(hero_object.metadata.dimension, 123)
示例#13
0
    def test_name_search_filename_read(self):
        """
        Test that Hero object can be found and check the unique dimension

        Here the name of the datafile is used
        """

        data_list = setup_McStasData_array()

        hero_object = name_search("Hero.dat", data_list)

        self.assertEqual(hero_object.metadata.dimension, 123)
示例#14
0
    def test_name_search_read_repeat(self):
        """
        Test that Hero object can be found and check the unique dimension
        Here the used data set has two monitors with Hero in the name

        Here the name of the monitor is used
        """

        data_list = setup_McStasData_array_repeat()

        hero_object = name_search("Big_Hero", data_list)

        self.assertEqual(hero_object.metadata.dimension, 123)
示例#15
0
    def test_complex_instrument(self, mock_stdout):
        """
        Test parameters can be controlled through McStasScript.  Here
        a slit is moved to one side and the result is verified.
        """
        CURRENT_DIR = os.getcwd()
        THIS_DIR = os.path.dirname(os.path.abspath(__file__))
        os.chdir(THIS_DIR)

        Instr = setup_complex_instrument()

        data = Instr.run_full_instrument(foldername="integration_test_complex",
                                         ncount=2E6,
                                         mpi=2,
                                         increment_folder_name=True,
                                         parameters={
                                             "guide_width": 0.03,
                                             "guide_length": 8.0
                                         })

        os.chdir(CURRENT_DIR)

        intensity_data_pos = functions.name_search("PSD_1D_1", data).Intensity
        sum_outside_beam = sum(intensity_data_pos[0:50])
        sum_inside_beam = sum(intensity_data_pos[51:99])
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)

        intensity_data_neg = functions.name_search("PSD_1D_2", data).Intensity
        sum_outside_beam = sum(intensity_data_neg[51:99])
        sum_inside_beam = sum(intensity_data_neg[0:50])
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)

        intensity_data_all = functions.name_search("PSD_1D", data).Intensity
        sum_outside_beam = sum(intensity_data_all[49:51])
        sum_inside_beam = (sum(intensity_data_all[0:45]) +
                           sum(intensity_data_all[56:99]))
        self.assertTrue(1000 * sum_outside_beam < sum_inside_beam)
示例#16
0
    def test_name_search_read_dubplicate(self):
        """
        Test simple case with duplicated name, should return list
        """

        data_list = setup_McStasData_array_repeat()

        hero_object = set_dummy_McStasData_2d("Big_Hero")
        hero_object.metadata.dimension = 321
        hero_object.plot_options.colormap = "very hot"

        data_list.append(hero_object)

        results = name_search("Big_Hero", data_list)

        self.assertEqual(len(results), 2)

        self.assertEqual(results[0].metadata.dimension, 123)
        self.assertEqual(results[1].metadata.dimension, 321)
示例#17
0
    def test_name_plot_options_duplicate(self):
        """
        Test case where several datasets are modified
        """

        data_list = setup_McStasData_array()

        hero_object = set_dummy_McStasData_2d("Hero")
        hero_object.metadata.dimension = 321
        hero_object.plot_options.colormap = "absurdly hot"

        data_list.append(hero_object)

        name_plot_options("Hero", data_list, colormap="cold")

        results = name_search("Hero", data_list)

        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].plot_options.colormap, "cold")
        self.assertEqual(results[1].plot_options.colormap, "cold")
示例#18
0
    def test_name_plot_options_duplicate(self):
        """
        Test case where several McStasData objects are modified since
        the internal name_search finds multiple matches
        """

        data_list = setup_McStasData_array()

        hero_object = set_dummy_McStasDataBinned_2d("Hero")
        hero_object.metadata.dimension = 321
        hero_object.plot_options.colormap = "absurdly hot"

        data_list.append(hero_object)

        name_plot_options("Hero", data_list, colormap="Blues")

        results = name_search("Hero", data_list)

        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].plot_options.colormap, "Blues")
        self.assertEqual(results[1].plot_options.colormap, "Blues")
示例#19
0
    def test_name_search_read_duplicate(self):
        """
        Test simple case with duplicated name, search should return list
        """

        data_list = setup_McStasData_array_repeat()

        # Adds another dataset with a name already in the data_list
        hero_object = set_dummy_McStasData_2d("Big_Hero")
        hero_object.metadata.dimension = 321
        hero_object.plot_options.colormap = "very hot"

        data_list.append(hero_object)

        # Now two McStasData objects match the Big_Hero name
        results = name_search("Big_Hero", data_list)

        self.assertEqual(type(results), list)
        # Check two results are returned
        self.assertEqual(len(results), 2)

        # Check they have the correct dimensions
        self.assertEqual(results[0].metadata.dimension, 123)
        self.assertEqual(results[1].metadata.dimension, 321)
示例#20
0
    def update_plot(self):
        """
        Updates the plot with current data, monitor and plot options

        Threading lock is used as this method is used in a threading context
        and can easily fail if new data is written while plotting. The lock
        prevents this from happening.
        """
        lock = threading.Lock()

        with lock:
            # Clear plot first
            self.ax.cla()
            #self.ax.xaxis.set_ticks([])
            #self.ax.yaxis.set_ticks([])
            self.colorbar_ax.cla()
            #self.colorbar_ax.xaxis.set_ticks([])
            #self.colorbar_ax.yaxis.set_ticks([])

            # Display message if not data can be plotted
            if self.data is None:
                self.ax.text(0.3, 0.5, "No data available yet")
                self.colorbar_ax.set_axis_off()
                self.ax.xaxis.set_ticks([])
                self.ax.yaxis.set_ticks([])
                return

            if len(self.data) == 0:
                self.ax.text(0.25, 0.5, "Simulation returned no data")
                self.colorbar_ax.set_axis_off()
                self.ax.xaxis.set_ticks([])
                self.ax.yaxis.set_ticks([])
                return

            if self.current_monitor is None:
                self.ax.text(0.3, 0.5, "Select a monitor to plot")
                self.colorbar_ax.set_axis_off()
                self.ax.xaxis.set_ticks([])
                self.ax.yaxis.set_ticks([])
                return

            # Get monitor and establish plot options
            monitor = name_search(self.current_monitor, self.data)
            plot_options = {
                "show_colorbar": True,
                "log": self.log_mode,
                "colormap": self.colormap
            }
            if self.orders_of_mag != "disabled":
                plot_options["orders_of_mag"] = self.orders_of_mag
            else:
                plot_options[
                    "orders_of_mag"] = 300  # Default value in McStasPlotOptions

            #print("Plotting with: ", plot_options)
            monitor.set_plot_options(**plot_options)
            with HiddenPrints():
                _plot_fig_ax(monitor,
                             self.fig,
                             self.ax,
                             colorbar_axes=self.colorbar_ax)

            self.colorbar_ax.set_aspect(20)

            # Show colorbar if something is present, otherwise hide it
            if self.colorbar_ax.lines or self.colorbar_ax.collections:
                self.colorbar_ax.set_axis_on()
            else:
                self.colorbar_ax.set_axis_off()

            #self.ax.set_axis_on()

            plt.tight_layout()
            self.fig.canvas.draw()