Example #1
0
    def testLocationObjects(self):
        class Dummy(object):
            def __init__(self, lat, lon, depth):
                self.lat = lat
                self.lon = lon
                self.depth = depth

        a0 = Location(lat=10., lon=12., depth=1100.)
        a1 = guts.clone(a0)
        a1.set_origin(lat=9., lon=11)

        b0 = Location(lat=11., lon=13., depth=2100.)
        b1 = guts.clone(b0)
        b1.set_origin(lat=9., lon=11)
        b2 = Dummy(b0.lat, b0.lon, b0.depth)

        dist_ab = orthodrome.distance_accurate50m(a0.lat, a0.lon, b0.lat,
                                                  b0.lon)

        azi_ab, bazi_ab = orthodrome.azibazi(a0.lat, a0.lon, b0.lat, b0.lon)

        def g_to_e(*args):
            return num.array(orthodrome.geodetic_to_ecef(*args))

        a_vec = g_to_e(a0.lat, a0.lon, -a0.depth)
        b_vec = g_to_e(b0.lat, b0.lon, -b0.depth)

        dist_3d_compare = math.sqrt(num.sum((a_vec - b_vec)**2))

        north_shift_compare, east_shift_compare = orthodrome.latlon_to_ne(
            a0.lat, a0.lon, b0.lat, b0.lon)

        for a in [a0, a1]:
            for b in [b0, b1, b2]:
                dist = a.distance_to(b)
                assert_allclose(dist, dist_ab)

                dist_3d = a.distance_3d_to(b)
                assert_allclose(dist_3d, dist_3d_compare, rtol=0.001)

                azi, bazi = a.azibazi_to(b)
                assert_allclose(azi % 360., azi_ab % 360., rtol=1e-2)
                assert_allclose(bazi % 360., bazi_ab % 360., rtol=1e-2)

                north_shift, east_shift = a.offset_to(b)
                assert_allclose((north_shift, east_shift),
                                (north_shift_compare, east_shift_compare),
                                rtol=5e-3)

        for x, y in [(a0, a1), (b0, b1), (b0, b2), (b1, b2)]:
            dist = x.distance_to(y)
            assert_allclose(dist, 0.0)
Example #2
0
    def add_scenario(self, scenario_id, scenario_generator):

        if scenario_generator.seed is None:
            scenario_generator = guts.clone(scenario_generator)
            scenario_generator.seed = num.random.randint(1, 2**32 - 1)

        path = self.get_path(scenario_id)
        try:
            os.mkdir(path)
        except OSError as e:
            if e.errno == errno.EEXIST:
                raise ScenarioError('scenario id is already in use: %s' %
                                    scenario_id)
            else:
                raise

        scenario = ScenarioCollectionItem(scenario_id=scenario_id,
                                          time_created=time.time())

        scenario_path = self.get_path(scenario_id, 'scenario.yaml')
        guts.dump(scenario, filename=scenario_path)

        generator_path = self.get_path(scenario_id, 'generator.yaml')
        guts.dump(scenario_generator, filename=generator_path)

        scenario.set_base_path(self.get_path(scenario_id))
        scenario.init_modelling(self._engine)

        self._scenarios.append(scenario)
Example #3
0
    def create_group_automap(self, config, iter_item_figure, **kwargs):
        group = PlotGroup(
            formats=guts.clone(config.formats),
            size_cm=config.size_cm,
            name=config.name,
            variant=config.variant,
            **kwargs)

        path_group = self.path_group(group=group)
        if os.path.exists(path_group):
            self.remove_group_files(path_group)

        group_ref = (group.name, group.variant)
        if group_ref in self._collection.group_refs:
            self._collection.group_refs.remove(group_ref)

        self.dump_collection()

        for item, automap in iter_item_figure:
            group.items.append(item)
            for format in group.formats:
                path = self.path_image(group, item, format)
                util.ensuredirs(path)
                format.render_automap(
                    automap,
                    path=path,
                    resolution=format.get_dpi(group.size_cm))

                logger.info('Figure saved: %s' % path)

        util.ensuredirs(path_group)
        group.dump(filename=path_group)
        self._collection.group_refs.append(group_ref)
        self.dump_collection()
Example #4
0
    def testClone(self):

        class A(Object):
            a = Int.T(optional=True)

        class B(Object):
            a_list = List.T(A.T())
            a_tuple = Tuple.T(3, A.T())
            a_dict = Dict.T(Int.T(), A.T())
            b = Float.T()

        a1 = A()
        a2 = A(a=1)
        b = B(
            a_list=[a1, a2],
            a_tuple=(a1, a2, a2),
            a_dict={1: a1, 2: a2},
            b=1.0)
        b_clone = clone(b)
        b.validate()
        b_clone.validate()
        self.assertEqual(b.dump(), b_clone.dump())
        assert b is not b_clone
        assert b.a_list is not b_clone.a_list
        assert b.a_tuple is not b_clone.a_tuple
        assert b.a_list[0] is not b_clone.a_list[0]
        assert b.a_tuple[0] is not b_clone.a_tuple[0]
    def add_scenario(self, scenario_id, scenario_generator):

        if scenario_generator.seed is None:
            scenario_generator = guts.clone(scenario_generator)
            scenario_generator.seed = num.random.randint(1, 2**32-1)

        path = self.get_path(scenario_id)
        try:
            os.mkdir(path)
        except OSError as e:
            if e.errno == errno.EEXIST:
                raise ScenarioError(
                    'scenario id is already in use: %s' % scenario_id)
            else:
                raise

        scenario = ScenarioCollectionItem(
            scenario_id=scenario_id,
            time_created=time.time())

        scenario_path = self.get_path(scenario_id, 'scenario.yaml')
        guts.dump(scenario, filename=scenario_path)

        generator_path = self.get_path(scenario_id, 'generator.yaml')
        guts.dump(scenario_generator, filename=generator_path)

        scenario.set_base_path(self.get_path(scenario_id))
        scenario.init_modelling(self._engine)

        self._scenarios.append(scenario)
Example #6
0
    def testClone(self):

        class A(Object):
            a = Int.T(optional=True)

        class B(Object):
            a_list = List.T(A.T())
            a_tuple = Tuple.T(3, A.T())
            a_dict = Dict.T(Int.T(), A.T())
            b = Float.T()

        a1 = A()
        a2 = A(a=1)
        b = B(
            a_list=[a1, a2],
            a_tuple=(a1, a2, a2),
            a_dict={1: a1, 2: a2},
            b=1.0)
        b_clone = clone(b)
        b.validate()
        b_clone.validate()
        self.assertEqual(b.dump(), b_clone.dump())
        assert b is not b_clone
        assert b.a_list is not b_clone.a_list
        assert b.a_tuple is not b_clone.a_tuple
        assert b.a_list[0] is not b_clone.a_list[0]
        assert b.a_tuple[0] is not b_clone.a_tuple[0]
Example #7
0
    def get_weeded(self, env):
        '''Get subset of plot configs supported by the current environment.'''

        plot_classes = env.get_plot_classes()
        plot_configs_avail = [
            clone(pc) for pc in self.plot_configs
            if pc.__class__ in plot_classes
        ]

        return PlotConfigCollection(plot_configs=plot_configs_avail)
Example #8
0
    def create_group_mpl(self, config, iter_item_figure, **kwargs):
        from matplotlib import pyplot as plt
        group = PlotGroup(
            formats=guts.clone(config.formats),
            size_cm=config.size_cm,
            name=config.name,
            variant=config.variant,
            **kwargs)

        path_group = self.path_group(group=group)
        if os.path.exists(path_group):
            self.remove_group_files(path_group)

        group_ref = (group.name, group.variant)
        if group_ref in self._collection.group_refs:
            self._collection.group_refs.remove(group_ref)

        self.dump_collection()

        figs_to_close = []
        for item, fig in iter_item_figure:
            group.items.append(item)
            for format in group.formats:
                path = self.path_image(group, item, format)
                util.ensuredirs(path)
                format.render_mpl(
                    fig,
                    path=path,
                    dpi=format.get_dpi(group.size_cm))

                logger.info('Figure saved: %s' % path)

            if not self._show:
                plt.close(fig)
            else:
                figs_to_close.append(fig)

        util.ensuredirs(path_group)
        group.validate()
        group.dump(filename=path_group)
        self._collection.group_refs.append(group_ref)
        self.dump_collection()

        if self._show:
            plt.show()

        for fig in figs_to_close:
            plt.close(fig)
Example #9
0
 def clone(self):
     return guts.clone(self)