<commit_before>import unittest

from nhe.const import TRT
from nhe.msr import Peer
from nhe.mfd import TruncatedGR
from nhe.common.geo import Point, Polygon
from nhe.common.pmf import PMF
from nhe.common.nodalplane import NodalPlane
from nhe.common.tom import PoissonTOM
from nhe.source.area import AreaSource


class AreaSourceIterRupturesTestCase(unittest.TestCase):
    def make_point_source(self, polygon, discretization, **kwargs):
        default_arguments = {
            'source_id': 'source_id', 'name': 'area source name',
            'tectonic_region_type': TRT.VOLCANIC,
            'mfd': TruncatedGR(a_val=3, b_val=1, min_mag=5,
                               max_mag=7, bin_width=1),
            'nodal_plane_distribution': PMF([(1, NodalPlane(1, 2, 3))]),
            'hypocenter_distribution': PMF([(1, 4)]),
            'upper_seismogenic_depth': 1.3,
            'lower_seismogenic_depth': 4.9,
            'magnitude_scaling_relationship': Peer(),
            'rupture_aspect_ratio': 1.333,
            'polygon': polygon,
            'area_discretization': discretization
        }
        default_arguments.update(kwargs)
        kwargs = default_arguments
        source = AreaSource(**kwargs)
        for key in kwargs:
            self.assertIs(getattr(source, key), kwargs[key])
        return source

    def test_1(self):
        source = self.make_point_source(
            Polygon([Point(-2, -2), Point(0, -2), Point(0, 0), Point(-2, 0)]),
            discretization=70
        )
        ruptures = list(source.iter_ruptures(PoissonTOM(50)))
        self.assertEqual(len(ruptures), 8 * 2)<commit_msg>tests/source/area: fixed area iter ruptures test<commit_after>import unittest

from nhe.const import TRT
from nhe.msr import Peer
from nhe.mfd import TruncatedGR
from nhe.common.geo import Point, Polygon
from nhe.common.pmf import PMF
from nhe.common.nodalplane import NodalPlane
from nhe.common.tom import PoissonTOM
from nhe.source.area import AreaSource


class AreaSourceIterRupturesTestCase(unittest.TestCase):
    def make_point_source(self, polygon, discretization, **kwargs):
        default_arguments = {
            'source_id': 'source_id', 'name': 'area source name',
            'tectonic_region_type': TRT.VOLCANIC,
            'mfd': TruncatedGR(a_val=3, b_val=1, min_mag=5,
                               max_mag=7, bin_width=1),
            'nodal_plane_distribution': PMF([(1, NodalPlane(1, 2, 3))]),
            'hypocenter_distribution': PMF([(1, 4)]),
            'upper_seismogenic_depth': 1.3,
            'lower_seismogenic_depth': 4.9,
            'magnitude_scaling_relationship': Peer(),
            'rupture_aspect_ratio': 1.333,
            'polygon': polygon,
            'area_discretization': discretization
        }
        default_arguments.update(kwargs)
        kwargs = default_arguments
        source = AreaSource(**kwargs)
        for key in kwargs:
            self.assertIs(getattr(source, key), kwargs[key])
        return source

    def test_1(self):
        source = self.make_point_source(Polygon([Point(-2, -2), Point(0, -2),
                                                 Point(0, 0), Point(-2, 0)]),
                                        discretization=66.7)
        ruptures = list(source.iter_ruptures(PoissonTOM(50)))
        self.assertEqual(len(ruptures), 9 * 2)
        # resulting 3x3 mesh has points in these coordinates:
        lons = [-1.4, -0.8, -0.2]
        lats = [-0.6, -1.2, -1.8]
        ruptures_iter = iter(ruptures)
        for lat in lats:
            for lon in lons:
                r1 = next(ruptures_iter)
                r2 = next(ruptures_iter)
                for rupture in [r1, r2]:
                    self.assertAlmostEqual(rupture.hypocenter.longitude,
                                           lon, delta=1e-3)
                    self.assertAlmostEqual(rupture.hypocenter.latitude,
                                           lat, delta=1e-3)
                self.assertEqual(r1.mag, 5.5)
                self.assertEqual(r2.mag, 6.5)
        self.assertEqual(len(ruptures), 9 * 2)
{'fraction_non_alphanumeric': 0.10921501706484642, 'fraction_numerical': 0.024134568503169185, 'mean_word_length': 2.1610169491525424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '782920', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1242, 'n_words': 269}