示例#1
0
    def sample_source_model_logictree(self, random_seed, mfd_bin_width):
        """
        Perform a Monte-Carlo sampling of source model logic tree.

        :param random_seed:
            An integer random seed value to initialize random generator
            before doing random sampling.
        :param mfd_bin_width:
            Float, the width of sources' MFD histograms bins.
        :return:
            String, json-serialized source model sample. For serialization
            the java class ``org.gem.JsonSerializer`` is used.
        """
        rnd = random.Random(random_seed)
        sm_reader = jvm().JClass('org.gem.engine.hazard.'
                                 'parsers.SourceModelReader')
        branch = self.source_model_lt.root_branchset.sample(rnd)
        sources = sm_reader(branch.value, float(mfd_bin_width)).read()
        while True:
            branchset = branch.child_branchset
            if branchset is None:
                break
            branch = branchset.sample(rnd)
            for source in sources:
                branchset.apply_uncertainty(branch.value, source)

        serializer = jvm().JClass('org.gem.JsonSerializer')
        return serializer.getJsonSourceList(sources)
示例#2
0
    def __init__(self, src_model_path,
        mfd_bin_width=DEFAULT_MFD_BIN_WIDTH, owner_id=1, input_id=None):
        """
        :param src_model_path: path to a source model file
        :type src_model_path: str

        :param mfd_bin_width: Magnitude Frequency Distribution bin width
        :type mfd_bin_width: float


        :param owner_id: ID of an admin.organization entity in the database. By
            default, the default 'GEM Foundation' group will be used.
            Note(LB): This is kind of ugly and needs to be revisited later.

        :param int input_id: The database key of the uploaded input file from
            which this source was extracted. Please note that the `input_id`
            will only be supplied when uploading source model files via the
            GUI.
        """
        self.src_model_path = src_model_path
        self.mfd_bin_width = mfd_bin_width
        self.owner_id = owner_id
        self.input_id = input_id

        # Java SourceModelReader object
        java.jvm().java.lang.System.setProperty(
            "openquake.nrml.schema", xml.nrml_schema_file())
        self.src_reader = java.jclass('SourceModelReader')(
            self.src_model_path, self.mfd_bin_width)
示例#3
0
    def sample_source_model_logictree(self, random_seed, mfd_bin_width):
        """
        Perform a Monte-Carlo sampling of source model logic tree.

        :param random_seed:
            An integer random seed value to initialize random generator
            before doing random sampling.
        :param mfd_bin_width:
            Float, the width of sources' MFD histograms bins.
        :return:
            String, json-serialized source model sample. For serialization
            the java class ``org.gem.JsonSerializer`` is used.
        """
        rnd = random.Random(random_seed)
        sm_reader = jvm().JClass('org.gem.engine.hazard.'
                                 'parsers.SourceModelReader')
        branch = self.source_model_lt.root_branchset.sample(rnd)
        sources = sm_reader(branch.value, float(mfd_bin_width)).read()
        while True:
            branchset = branch.child_branchset
            if branchset is None:
                break
            branch = branchset.sample(rnd)
            for source in sources:
                branchset.apply_uncertainty(branch.value, source)

        serializer = jvm().JClass('org.gem.JsonSerializer')
        return serializer.getJsonSourceList(sources)
示例#4
0
 def preloader(self, *args, **kwargs):
     """Validate job"""
     self.cache = java.jclass("KVS")(
             settings.KVS_HOST,
             settings.KVS_PORT)
     self.calc = java.jclass("LogicTreeProcessor")(
             self.cache, self.key)
     java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                             xml.nrml_schema_file())
     return fn(self, *args, **kwargs)
示例#5
0
 def preloader(self, *args, **kwargs):
     """Validate job"""
     self.cache = java.jclass("KVS")(
             config.get("kvs", "host"),
             int(config.get("kvs", "port")))
     self.calc = java.jclass("LogicTreeProcessor")(
             self.cache, self.key)
     java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                             xml.nrml_schema_file())
     return fn(self, *args, **kwargs)
示例#6
0
 def test_jvm_memmax_setting_is_not_passed(self):
     """Do not pass -Xmx to the jvm."""
     with helpers.patch("jpype.startJVM") as startjvm_mock:
         with helpers.patch("jpype.isJVMStarted") as isjvmstarted_mock:
             # Make sure that startJVM() gets called.
             isjvmstarted_mock.side_effect = lambda: False
             with helpers.patch("openquake.java.init_logs"):
                 java.jvm()
                 args, _ = startjvm_mock.call_args
                 self.assertFalse(filter(lambda a: a.startswith("-Xmx"), args))
示例#7
0
    def setUpClass(cls):
        # This is safe to call even if the jvm was already running from a
        # previous test.
        # Even better would be to start with a fresh jvm but this is currently
        # not possible (see
        # http://jpype.sourceforge.net/doc/user-guide/userguide.html#limitation
        # )
        java.jvm()

        # save the java stdout and stderr that will be trashed during this test
        cls.old_java_out = jpype.java.lang.System.out
        cls.old_java_err = jpype.java.lang.System.err
    def __init__(self, *args, **kwargs):
        """
        One-time setup stuff for this entire test case class.
        """
        super(NrmlModelLoaderTestCase, self).__init__(*args, **kwargs)

        self.src_reader = java.jclass('SourceModelReader')(
            TEST_SRC_FILE, db_loader.SourceModelLoader.DEFAULT_MFD_BIN_WIDTH)
        java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                                xml.nrml_schema_file())
        self.sources = self.src_reader.read()
        self.simple, self.complex, self.area, self.point = self.sources
示例#9
0
    def test_jvm_memmax_setting_is_enforced(self):
        """The `-Xmx` property is passed to the JVM."""
        with helpers.patch("jpype.startJVM") as startjvm_mock:
            with helpers.patch("jpype.isJVMStarted") as isjvmstarted_mock:
                # Make sure that startJVM() gets called.

                def side_effect():
                    isjvmstarted_mock.side_effect = lambda: True
                    return False

                isjvmstarted_mock.side_effect = side_effect
                java.jvm()
                args, _ = startjvm_mock.call_args
                self.assertTrue(
                    filter(lambda a: a.startswith("-Xmx"), args))
示例#10
0
 def _is_complexfault(cls, source):
     """
     Return ``True`` if ``source`` is opensha source of complex fault type.
     """
     classname = 'org.opensha.sha.earthquake.rupForecastImpl.' \
                 'GEM1.SourceData.GEMSubductionFaultSourceData'
     return isinstance(source, jvm().JClass(classname))
示例#11
0
 def to_java(self):
     """Converts to a Java Site object"""
     jpype = java.jvm()
     loc_class = jpype.JClass("org.opensha.commons.geo.Location")
     site_class = jpype.JClass("org.opensha.commons.data.Site")
     # TODO(JMC): Support named sites?
     return site_class(loc_class(self.latitude, self.longitude))
示例#12
0
 def _is_complexfault(cls, source):
     """
     Return ``True`` if ``source`` is opensha source of complex fault type.
     """
     classname = 'org.opensha.sha.earthquake.rupForecastImpl.' \
                 'GEM1.SourceData.GEMSubductionFaultSourceData'
     return isinstance(source, jvm().JClass(classname))
示例#13
0
 def _is_gr_mfd(cls, mfd):
     """
     Return ``True`` if ``mfd`` is opensha GR MFD object
     (and in particular not evenly discretized function).
     """
     classname = 'org.opensha.sha.magdist.GutenbergRichterMagFreqDist'
     return isinstance(mfd, jvm().JClass(classname))
示例#14
0
 def to_java(self):
     """Converts to a Java Site object"""
     jpype = java.jvm()
     loc_class = jpype.JClass("org.opensha.commons.geo.Location")
     site_class = jpype.JClass("org.opensha.commons.data.Site")
     # TODO(JMC): Support named sites?
     return site_class(loc_class(self.latitude, self.longitude))
示例#15
0
 def _is_area(cls, source):
     """
     Return ``True`` if ``source`` is opensha source of area type.
     """
     classname = 'org.opensha.sha.earthquake.rupForecastImpl.' \
                 'GEM1.SourceData.GEMAreaSourceData'
     return isinstance(source, jvm().JClass(classname))
示例#16
0
    def parameterize_sites(self, site_list):
        """Convert python Sites to Java Sites, and add default parameters."""
        # TODO(JMC): There's Java code for this already, sets each site to have
        # the same default parameters

        jpype = java.jvm()
        jsite_list = java.jclass("ArrayList")()
        for x in site_list:
            site = x.to_java()

            vs30 = java.jclass("DoubleParameter")(jpype.JString("Vs30"))
            vs30.setValue(float(self.calc_proxy.params["REFERENCE_VS30_VALUE"]))
            depth25 = java.jclass("DoubleParameter")("Depth 2.5 km/sec")
            depth25.setValue(float(self.calc_proxy.params["REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM"]))
            sadigh = java.jclass("StringParameter")("Sadigh Site Type")
            sadigh.setValue(self.calc_proxy.params["SADIGH_SITE_TYPE"])

            depth1km = java.jclass("DoubleParameter")(jpype.JString("Depth 1.0 km/sec"))
            depth1km.setValue(float(self.calc_proxy.params["DEPTHTO1PT0KMPERSEC"]))
            vs30_type = java.jclass("StringParameter")("Vs30 Type")
            # Enum values must be capitalized in the Java domain!
            vs30_type.setValue(self.calc_proxy.params["VS30_TYPE"].capitalize())

            site.addParameter(vs30)
            site.addParameter(depth25)
            site.addParameter(sadigh)
            site.addParameter(depth1km)
            site.addParameter(vs30_type)
            jsite_list.add(site)
        return jsite_list
示例#17
0
 def _is_gr_mfd(cls, mfd):
     """
     Return ``True`` if ``mfd`` is opensha GR MFD object
     (and in particular not evenly discretized function).
     """
     classname = 'org.opensha.sha.magdist.GutenbergRichterMagFreqDist'
     return isinstance(mfd, jvm().JClass(classname))
示例#18
0
    def test_write_uhs_spectrum_data(self):
        # Test `write_uhs_spectrum_data`.

        # To start with, we need to write the 'container' records for the UHS
        # results:
        write_uh_spectra(self.job_ctxt)

        uhs_results = []  # The results we want to write to HDF5
        uhs_result = java.jvm().JClass('org.gem.calc.UHSResult')

        # Build up a result list that we can pass to the function under test:
        for poe, uhs in self.UHS_RESULTS:
            uhs_results.append(uhs_result(poe, list_to_jdouble_array(uhs)))

        realization = 0
        test_site = Site(0.0, 0.0)

        # Call the function under test
        write_uhs_spectrum_data(
            self.job_ctxt, realization, test_site, uhs_results)

        uhs_data = UhSpectrumData.objects.filter(
            uh_spectrum__uh_spectra__output__oq_job=(
            self.job.id))

        self.assertEqual(len(self.UHS_RESULTS), len(uhs_data))
        self.assertTrue(all([x.realization == 0 for x in uhs_data]))

        uhs_results_dict = dict(self.UHS_RESULTS)  # keyed by PoE
        for uhs_datum in uhs_data:
            self.assertTrue(
                numpy.allclose(uhs_results_dict[uhs_datum.uh_spectrum.poe],
                               uhs_datum.sa_values))
            self.assertEqual(test_site.point.to_wkt(), uhs_datum.location.wkt)
示例#19
0
 def _is_area(cls, source):
     """
     Return ``True`` if ``source`` is opensha source of area type.
     """
     classname = 'org.opensha.sha.earthquake.rupForecastImpl.' \
                 'GEM1.SourceData.GEMAreaSourceData'
     return isinstance(source, jvm().JClass(classname))
示例#20
0
    def test_write_uhs_spectrum_data(self):
        # Test `write_uhs_spectrum_data`.

        # To start with, we need to write the 'container' records for the UHS
        # results:
        write_uh_spectra(self.job_ctxt)

        uhs_results = []  # The results we want to write to HDF5
        uhs_result = java.jvm().JClass('org.gem.calc.UHSResult')

        # Build up a result list that we can pass to the function under test:
        for poe, uhs in self.UHS_RESULTS:
            uhs_results.append(uhs_result(poe, list_to_jdouble_array(uhs)))

        realization = 0
        test_site = Site(0.0, 0.0)

        # Call the function under test
        write_uhs_spectrum_data(
            self.job_ctxt, realization, test_site, uhs_results)

        uhs_data = UhSpectrumData.objects.filter(
            uh_spectrum__uh_spectra__output__oq_job=(
            self.job.id))

        self.assertEqual(len(self.UHS_RESULTS), len(uhs_data))
        self.assertTrue(all([x.realization == 0 for x in uhs_data]))

        uhs_results_dict = dict(self.UHS_RESULTS)  # keyed by PoE
        for uhs_datum in uhs_data:
            self.assertTrue(
                numpy.allclose(uhs_results_dict[uhs_datum.uh_spectrum.poe],
                               uhs_datum.sa_values))
            self.assertEqual(test_site.point.to_wkt(), uhs_datum.location.wkt)
示例#21
0
    def _get_exception(self):
        jpype = java.jvm()

        try:
            jpype.java.lang.Integer('foo')
        except jpype.JavaException, e:
            return e
示例#22
0
 def _parse_file(self, path):
     jpype = java.jvm()
     ltr = jpype.JClass('org.gem.engine.LogicTreeReader')(path)
     try:
         ltr.read()
     except jpype.JavaException, ex:
         opensha.unwrap_validation_error(
             jpype, ex, path)
    def setUp(self):
        self.jvm = java.jvm()
        self.handler = logging.handlers.BufferingHandler(capacity=float('inf'))
        self.python_logger = logging.getLogger('java')
        self.python_logger.addHandler(self.handler)
        self.python_logger.setLevel(logging.DEBUG)

        jlogger_class = self.jvm.JClass("org.apache.log4j.Logger")
        self.root_logger = jlogger_class.getRootLogger()
        self.other_logger = jlogger_class.getLogger('other_logger')
示例#24
0
    def setUp(self):
        self.jvm = java.jvm()
        self.handler = logging.handlers.BufferingHandler(capacity=float('inf'))
        self.python_logger = logging.getLogger('java')
        self.python_logger.addHandler(self.handler)
        self.python_logger.setLevel(logging.DEBUG)

        jlogger_class = self.jvm.JClass("org.apache.log4j.Logger")
        self.root_logger = jlogger_class.getRootLogger()
        self.other_logger = jlogger_class.getLogger('other_logger')
示例#25
0
    def validate_uncertainty_value(self, node, branchset, value):
        """
        See superclass' method for description and signature specification.

        Checks that the value is the name of the class inside java package
        :attr:`GMPE_PACKAGE` which implements interface :attr:`BASE_GMPE`.
        """
        base_gmpe = jvm().JClass(self.BASE_GMPE)
        try:
            gmpe = jvm().JClass('%s.%s' % (self.GMPE_PACKAGE, value))
        except jvm().JavaException:
            # Class not found
            pass
        else:
            if issubclass(gmpe, base_gmpe):
                # Class exists and implements the proper interface.
                return value
        raise ValidationError(node, self.filename, self.basepath,
                              'gmpe %r is not available' % value)
示例#26
0
 def store_gmpe_map(self, seed):
     """Generates a hash of tectonic regions and GMPEs, using the logic tree
     specified in the job config file."""
     key = kvs.generate_product_key(self.id, kvs.tokens.GMPE_TOKEN)
     print "GMPE map key is", key
     jpype = java.jvm()
     try:
         self.calc.sampleAndSaveGMPETree(self.cache, key, seed)
     except jpype.JException(jpype.java.lang.RuntimeException), ex:
         unwrap_validation_error(
             jpype, ex, self.params.get("GMPE_LOGIC_TREE_FILE_PATH"))
示例#27
0
    def tearDown(self):
        # reset Log4j config
        jvm = java.jvm()
        jvm.JClass("org.apache.log4j.BasicConfigurator").resetConfiguration()
        jvm.JClass("org.apache.log4j.BasicConfigurator").configure()

        # reset logging config
        cleanup_loggers()

        # restore process name
        multiprocessing.current_process().name = self.process_name
示例#28
0
    def _serialize_test_helper(self, test_file, expected_tables):
        engine = db_utils.get_db_session("hzrdi", "writer").connection().engine
        java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                                xml.nrml_schema_file())
        src_loader = db_loader.SourceModelLoader(test_file, engine)

        results = src_loader.serialize()

        # we should get a 3 item list of results
        self.assertEquals(3, len(results))

        # We expect there to have been 3 inserts.
        # The results are a list of dicts with a single key.
        # The key is the table name (including table space);
        # the value is the id (as an int) of the new record.

        # First, check that the results includes the 3 tables we expect:
        result_tables = [x.keys()[0] for x in results]

        self.assertEqual(expected_tables, result_tables)

        # Everything appears to be fine, but let's query the database to make
        # sure the expected records are there.
        # At this point, we're not going to check every single value; we just
        # want to make sure the records made it into the database.
        tables = src_loader.meta.tables

        # list of tuples of (table name, id)
        table_id_pairs = [x.items()[0] for x in results]

        for table_name, record_id in table_id_pairs:
            table = tables[table_name]

            # run a query against the table object to get a ResultProxy
            result_proxy = table.select(table.c.id == record_id).execute()

            # there should be 1 record here
            self.assertEqual(1, result_proxy.rowcount)

        # clean up db resources
        src_loader.close()
示例#29
0
    def validate_uncertainty_value(self, node, branchset, value):
        """
        See superclass' method for description and signature specification.

        Checks that the value is the name of the class inside java package
        :attr:`GMPE_PACKAGE` which implements interface :attr:`BASE_GMPE`.
        """
        base_gmpe = jvm().JClass(self.BASE_GMPE)
        try:
            gmpe = jvm().JClass('%s.%s' % (self.GMPE_PACKAGE, value))
        except jvm().JavaException:
            # Class not found
            pass
        else:
            if issubclass(gmpe, base_gmpe):
                # Class exists and implements the proper interface.
                return value
        raise ValidationError(
            node, self.filename, self.basepath,
            'gmpe %r is not available' % value
        )
示例#30
0
    def setUp(self):
        # starting the jvm...
        print "About to start the jvm..."
        jpype = java.jvm()
        java_class = jpype.JClass("org.gem.engine.hazard.redis.Cache")
        print ("Not dead yet, and found the class...")
        self.java_client = java_class(settings.KVS_HOST, settings.KVS_PORT)

        self.python_client = kvs.get_client(binary=False)

        self.reader = reader.Reader(self.python_client)
        self._delete_test_file()
示例#31
0
    def setUp(self):
        # starting the jvm...
        print "About to start the jvm..."
        jpype = java.jvm()
        java_class = jpype.JClass("org.gem.engine.hazard.redis.Cache")
        print("Not dead yet, and found the class...")
        self.java_client = java_class(settings.KVS_HOST, settings.KVS_PORT)

        self.python_client = kvs.get_client(binary=False)

        self.reader = reader.Reader(self.python_client)
        self._delete_test_file()
示例#32
0
    def setUp(self):
        # starting the jvm...
        print "About to start the jvm..."
        jpype = java.jvm()
        java_class = jpype.JClass("org.gem.engine.hazard.redis.Cache")
        print "Not dead yet, and found the class..."
        self.java_client = java_class(config.get("kvs", "host"),
                                      int(config.get("kvs", "port")))

        self.python_client = kvs.get_client()
        self.python_client.flushdb()

        self._delete_test_file()
示例#33
0
    def setUp(self):
        # starting the jvm...
        print "About to start the jvm..."
        jpype = java.jvm()
        java_class = jpype.JClass("org.gem.engine.hazard.redis.Cache")
        print "Not dead yet, and found the class..."
        self.java_client = java_class(
            config.get("kvs", "host"), int(config.get("kvs", "port")))

        self.python_client = kvs.get_client()
        self.python_client.flushdb()

        self._delete_test_file()
示例#34
0
    def compute_ground_motion_fields(self, site_list, stochastic_set_id, seed):
        """Ground motion field calculation, runs on the workers."""
        jpype = java.jvm()

        jsite_list = self.parameterize_sites(site_list)
        key = kvs.generate_product_key(self.id,
                                       kvs.tokens.STOCHASTIC_SET_TOKEN,
                                       stochastic_set_id)
        gmc = self.params['GROUND_MOTION_CORRELATION']
        correlate = (gmc == "true" and True or False)
        java.jclass("HazardCalculator").generateAndSaveGMFs(
            self.cache, key, stochastic_set_id, jsite_list,
            self.generate_erf(), self.generate_gmpe_map(),
            java.jclass("Random")(seed), jpype.JBoolean(correlate))
示例#35
0
 def compute_hazard_curve(self, sites, realization):
     """ Compute hazard curves, write them to KVS as JSON,
     and return a list of the KVS keys for each curve. """
     jpype = java.jvm()
     try:
         calc = java.jclass("HazardCalculator")
         poes_list = calc.getHazardCurvesAsJson(
             self.parameterize_sites(sites),
             self.generate_erf(),
             self.generate_gmpe_map(),
             self.get_iml_list(),
             float(self.params['MAXIMUM_DISTANCE']))
     except jpype.JavaException, ex:
         unwrap_validation_error(jpype, ex)
示例#36
0
    def compute_ground_motion_fields(self, site_list, history, realization,
                                     seed):
        """Ground motion field calculation, runs on the workers."""
        jpype = java.jvm()

        jsite_list = self.parameterize_sites(site_list)
        key = kvs.tokens.stochastic_set_key(self.job_ctxt.job_id, history,
                                            realization)
        correlate = self.job_ctxt['GROUND_MOTION_CORRELATION']
        stochastic_set_id = "%s!%s" % (history, realization)
        java.jclass("HazardCalculator").generateAndSaveGMFs(
            self.cache, key, stochastic_set_id, jsite_list,
            self.generate_erf(), self.generate_gmpe_map(),
            java.jclass("Random")(seed), jpype.JBoolean(correlate))
示例#37
0
    def compute_ground_motion_fields(self, site_list, stochastic_set_id, seed):
        """Ground motion field calculation, runs on the workers."""
        jpype = java.jvm()

        jsite_list = self.parameterize_sites(site_list)
        key = kvs.generate_product_key(
            self.id, kvs.tokens.STOCHASTIC_SET_TOKEN, stochastic_set_id)
        gmc = self.params['GROUND_MOTION_CORRELATION']
        correlate = (gmc == "true" and True or False)
        java.jclass("HazardCalculator").generateAndSaveGMFs(
                self.cache, key, stochastic_set_id, jsite_list,
                 self.generate_erf(),
                self.generate_gmpe_map(),
                java.jclass("Random")(seed),
                jpype.JBoolean(correlate))
示例#38
0
文件: core.py 项目: bwyss/oq-engine
 def compute_hazard_curve(self, sites, realization):
     """ Compute hazard curves, write them to KVS as JSON,
     and return a list of the KVS keys for each curve. """
     jpype = java.jvm()
     try:
         calc = java.jclass("HazardCalculator")
         poes_list = calc.getHazardCurvesAsJson(
             self.parameterize_sites(sites), self.generate_erf(),
             self.generate_gmpe_map(),
             general.get_iml_list(
                 self.job_ctxt.imls,
                 self.job_ctxt.params['INTENSITY_MEASURE_TYPE']),
             self.job_ctxt['MAXIMUM_DISTANCE'])
     except jpype.JavaException, ex:
         unwrap_validation_error(jpype, ex)
示例#39
0
    def compute_ground_motion_fields(self, site_list, history, realization,
                                     seed):
        """Ground motion field calculation, runs on the workers."""
        jpype = java.jvm()

        jsite_list = self.parameterize_sites(site_list)
        key = kvs.tokens.stochastic_set_key(self.job_ctxt.job_id, history,
                                            realization)
        correlate = self.job_ctxt['GROUND_MOTION_CORRELATION']
        stochastic_set_id = "%s!%s" % (history, realization)
        java.jclass("HazardCalculator").generateAndSaveGMFs(
                self.cache, key, stochastic_set_id, jsite_list,
                self.generate_erf(),
                self.generate_gmpe_map(),
                java.jclass("Random")(seed),
                jpype.JBoolean(correlate))
示例#40
0
 def set_gmpe_params(self, gmpe_map):
     """Push parameters from configuration file into the GMPE objects"""
     jpype = java.jvm()
     gmpe_lt_data = self.calc.createGmpeLogicTreeData()
     for tect_region in gmpe_map.keySet():
         gmpe = gmpe_map.get(tect_region)
         gmpe_lt_data.setGmpeParams(self.params['COMPONENT'],
             self.params['INTENSITY_MEASURE_TYPE'],
             jpype.JDouble(float(self.params['PERIOD'])),
             jpype.JDouble(float(self.params['DAMPING'])),
             self.params['GMPE_TRUNCATION_TYPE'],
             jpype.JDouble(float(self.params['TRUNCATION_LEVEL'])),
             self.params['STANDARD_DEVIATION_TYPE'],
             jpype.JDouble(float(self.params['REFERENCE_VS30_VALUE'])),
             jpype.JObject(gmpe, java.jclass("AttenuationRelationship")))
         gmpe_map.put(tect_region, gmpe)
示例#41
0
    def store_source_model(self, seed):
        """Generates an Earthquake Rupture Forecast, using the source zones and
        logic trees specified in the job config file. Note that this has to be
        done currently using the file itself, since it has nested references to
        other files."""

        LOG.info("Storing source model from job config")
        key = kvs.generate_product_key(self.id, kvs.tokens.SOURCE_MODEL_TOKEN)
        print "source model key is", key
        jpype = java.jvm()
        try:
            self.calc.sampleAndSaveERFTree(self.cache, key, seed)
        except jpype.JException(jpype.java.lang.RuntimeException), ex:
            unwrap_validation_error(
                jpype, ex,
                self.params.get("SOURCE_MODEL_LOGIC_TREE_FILE_PATH"))
示例#42
0
    def setUp(self):
        # save and override process name
        self.process_name = multiprocessing.current_process().name
        multiprocessing.current_process().name = '->UnitTestProcess<-'

        # reset Log4j config
        jvm = java.jvm()
        jvm.JClass("org.apache.log4j.BasicConfigurator").resetConfiguration()

        # reset logging config
        cleanup_loggers()

        # setup AMQP logging
        logs.init_logs(log_type='amqp', level='debug')
        java.init_logs(log_type='amqp', level='debug')
        job.setup_job_logging('123')
示例#43
0
 def set_gmpe_params(self, gmpe_map):
     """Push parameters from configuration file into the GMPE objects"""
     jpype = java.jvm()
     gmpe_lt_data = self.calc.createGmpeLogicTreeData()
     for tect_region in gmpe_map.keySet():
         gmpe = gmpe_map.get(tect_region)
         gmpe_lt_data.setGmpeParams(self.params['COMPONENT'],
             self.params['INTENSITY_MEASURE_TYPE'],
             jpype.JDouble(float(self.params['PERIOD'])),
             jpype.JDouble(float(self.params['DAMPING'])),
             self.params['GMPE_TRUNCATION_TYPE'],
             jpype.JDouble(float(self.params['TRUNCATION_LEVEL'])),
             self.params['STANDARD_DEVIATION_TYPE'],
             jpype.JDouble(float(self.params['REFERENCE_VS30_VALUE'])),
             jpype.JObject(gmpe, java.jclass("AttenuationRelationship")))
         gmpe_map.put(tect_region, gmpe)
示例#44
0
def set_gmpe_params(gmpe_map, params):
    """Push parameters from the config file into the GMPE objects.

    :param gmpe_map: jpype instance of
        `HashMap<TectonicRegionType, ScalarIntensityMeasureRelationshipAPI>`
    :param dict params: job config params
    """
    jpype = java.jvm()

    jd_float = lambda x: jpype.JDouble(float(x))

    component = params.get("COMPONENT")
    imt = params.get("INTENSITY_MEASURE_TYPE")
    # PERIOD is not used in UHS calculations.
    period = jd_float(params.get("PERIOD")) if params.get("PERIOD") is not None else None
    damping = jd_float(params.get("DAMPING"))
    gmpe_trunc_type = params.get("GMPE_TRUNCATION_TYPE")
    trunc_level = jd_float(params.get("TRUNCATION_LEVEL"))
    stddev_type = params.get("STANDARD_DEVIATION_TYPE")

    j_set_gmpe_params = java.jclass("GmpeLogicTreeData").setGmpeParams
    for tect_region in gmpe_map.keySet():
        gmpe = gmpe_map.get(tect_region)
        # There are two overloads for this method; one with 'period'...
        if period is not None:
            j_set_gmpe_params(
                component,
                imt,
                period,
                damping,
                gmpe_trunc_type,
                trunc_level,
                stddev_type,
                jpype.JObject(gmpe, java.jclass("AttenuationRelationship")),
            )
        # ... and one without.
        else:
            j_set_gmpe_params(
                component,
                imt,
                damping,
                gmpe_trunc_type,
                trunc_level,
                stddev_type,
                jpype.JObject(gmpe, java.jclass("AttenuationRelationship")),
            )
        gmpe_map.put(tect_region, gmpe)
示例#45
0
    def parameterize_sites(self, site_list):
        """Convert python Sites to Java Sites, and add default parameters."""
        # TODO(JMC): There's Java code for this already, sets each site to have
        # the same default parameters

        jpype = java.jvm()
        jsite_list = java.jclass("ArrayList")()
        for x in site_list:
            site = x.to_java()

            vs30 = java.jclass("DoubleParameter")(jpype.JString("Vs30"))
            vs30.setValue(float(self.params['REFERENCE_VS30_VALUE']))
            depth25 = java.jclass("DoubleParameter")("Depth 2.5 km/sec")
            depth25.setValue(
                float(self.params['REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM']))
            sadigh = java.jclass("StringParameter")("Sadigh Site Type")
            sadigh.setValue(self.params['SADIGH_SITE_TYPE'])
            site.addParameter(vs30)
            site.addParameter(depth25)
            site.addParameter(sadigh)
            jsite_list.add(site)
        return jsite_list
示例#46
0
def set_gmpe_params(gmpe_map, params):
    """Push parameters from the config file into the GMPE objects.

    :param gmpe_map: jpype instance of
        `HashMap<TectonicRegionType, ScalarIntensityMeasureRelationshipAPI>`
    :param dict params: job config params
    """
    jpype = java.jvm()

    jd_float = lambda x: jpype.JDouble(float(x))

    component = params.get('COMPONENT')
    imt = params.get('INTENSITY_MEASURE_TYPE')
    # PERIOD is not used in UHS calculations.
    period = (jd_float(params.get('PERIOD'))
              if params.get('PERIOD') is not None else None)
    damping = jd_float(params.get('DAMPING'))
    gmpe_trunc_type = params.get('GMPE_TRUNCATION_TYPE')
    trunc_level = jd_float(params.get('TRUNCATION_LEVEL'))
    stddev_type = params.get('STANDARD_DEVIATION_TYPE')

    j_set_gmpe_params = java.jclass("GmpeLogicTreeData").setGmpeParams
    for tect_region in gmpe_map.keySet():
        gmpe = gmpe_map.get(tect_region)
        # There are two overloads for this method; one with 'period'...
        if period is not None:
            j_set_gmpe_params(
                component, imt, period, damping,
                gmpe_trunc_type, trunc_level, stddev_type,
                jpype.JObject(gmpe, java.jclass("AttenuationRelationship")))
        # ... and one without.
        else:
            j_set_gmpe_params(
                component, imt, damping,
                gmpe_trunc_type, trunc_level, stddev_type,
                jpype.JObject(gmpe, java.jclass("AttenuationRelationship")))
        gmpe_map.put(tect_region, gmpe)
示例#47
0
    def parameterize_sites(self, site_list):
        """Set vs30, vs30 type, z1pt0, z2pt5, and sadigh site type parameters
        on all input sites, returning a jpype `ArrayList` of OpenSHA `Site`
        objects.

        For vs30, vs30 type, z1pt0, and z2pt5:
        These params can be defined in general for the entire calculation.
        Alternatively, the calculation can define a `SITE_MODEL`, which supply
        site-specific parameters. This method handles both cases.

        NOTE: If a `SITE_MODEL` is used, it needs to be properly stored first.
        See :function:`~openquake.calculators.hazard.general.store_site_model`.

        :param site_list:
            `list` of :class:`~openquake.shapes.Site` objects.
        :returns:
            jpype `ArrayList` of `org.opensha.commons.data.Site` objects (with
            the above parameters set).
        """
        # make sure the JVM is started
        java.jvm()

        # the return value
        jsite_list = java.jclass("ArrayList")()

        job_profile = self.job_ctxt.oq_job_profile

        # The `sadigh site type` is the same in any case
        sadigh_param = java.jclass("StringParameter")("Sadigh Site Type")
        sadigh_param.setValue(
            job_params.REVERSE_ENUM_MAP[job_profile.sadigh_site_type]
        )

        site_model = get_site_model(self.job_ctxt.oq_job.id)

        if site_model is not None:
            # set site-specific parameters:
            for site in site_list:
                jsite = site.to_java()

                sm_data = get_closest_site_model_data(site_model, site)
                set_java_site_parameters(jsite, sm_data)
                # The sadigh site type param is not site specific, but we need
                # to set it anyway.
                jsite.addParameter(sadigh_param)

                jsite_list.add(jsite)
        else:
            # use the same parameters for all sites
            vs30_param = java.jclass("DoubleParameter")("Vs30")
            vs30_param.setValue(job_profile.reference_vs30_value)

            vs30_type_param = java.jclass("StringParameter")("Vs30 Type")
            vs30_type_param.setValue(job_profile.vs30_type.capitalize())

            z1pt0_param = java.jclass("DoubleParameter")("Depth 1.0 km/sec")
            z1pt0_param.setValue(job_profile.depth_to_1pt_0km_per_sec)

            z2pt5_param = java.jclass("DoubleParameter")("Depth 2.5 km/sec")
            z2pt5_param.setValue(
                job_profile.reference_depth_to_2pt5km_per_sec_param
            )

            for site in site_list:
                jsite = site.to_java()

                jsite.addParameter(vs30_param)
                jsite.addParameter(vs30_type_param)
                jsite.addParameter(z1pt0_param)
                jsite.addParameter(z2pt5_param)
                jsite.addParameter(sadigh_param)

                jsite_list.add(jsite)

        return jsite_list
示例#48
0
        def test():
            jpype = java.jvm()

            jpype.java.lang.Integer('foo')
示例#49
0
def jtask_task(data):
    """
    Takes a single argument called `data` and might raise a Java exception.
    """
    return str(java.jvm().java.lang.Integer(data))