Esempio n. 1
0
 def test_mac_os_unsupported_vers(self, platform):
     supported_vers = ["9.01.xx", "10.11.xx", "12.xx"]
     platform.system.side_effect = lambda: "Darwin"
     for supported_ver in supported_vers:
         platform.mac_ver.side_effect = [[supported_ver]]
         with self.assertRaises(RuntimeError):
             resources.get_libcbm_bin_path()
Esempio n. 2
0
def initialize_cbm(sit, dll_path=None, parameters_factory=None):
    """Create an initialized instance of
        :py:class:`libcbm.model.cbm.cbm_model.CBM` based on SIT input

    Args:
        sit (object): sit instance as returned by :py:func:`load_sit`
        dll_path (str, optional): path to the libcbm compiled library, if not
            specified a default value is used.
        parameters_factory (func, optional): a parameterless function that
            returns parameters for the cbm model.  If unspecified the sit
            default is used. Defaults to None.

    Returns:
        libcbm.model.cbm.cbm_model.CBM: an initialized CBM instance
    """

    if not dll_path:
        dll_path = resources.get_libcbm_bin_path()
    if parameters_factory is None:
        parameters_factory = sit.defaults.get_parameters_factory()
    with cbm_factory.create(
            dll_path=dll_path,
            dll_config_factory=sit.defaults.get_configuration_factory(),
            cbm_parameters_factory=parameters_factory,
            merch_volume_to_biomass_factory=lambda: cbm_config.
            merch_volume_to_biomass_config(
                db_path=sit.db_path,
                merch_volume_curves=get_merch_volumes(
                    sit.sit_data.yield_table, sit.sit_data.classifiers, sit.
                    sit_data.classifier_values, sit.sit_data.age_classes, sit.
                    sit_mapping)),
            classifiers_factory=lambda: get_classifiers(
                sit.sit_data.classifiers, sit.sit_data.classifier_values
            )) as cbm:
        yield cbm
Esempio n. 3
0
    def test_unsupported_linux_versions(self, warnings, platform,
                                        get_linux_os_release):

        unsupported_os_release = {"NAME": "UNKNOWN", "VERSION_ID": "40.00"}
        platform.system.side_effect = lambda: "Linux"
        get_linux_os_release.side_effect = lambda: unsupported_os_release
        self.assertTrue(os.path.exists(resources.get_libcbm_bin_path()))
        warnings.warn.assert_called_once()
Esempio n. 4
0
    def test_linux_versions(self, platform, get_linux_os_release):

        supported_os_releases = [{
            "NAME": "UBUNTU",
            "VERSION_ID": "18.04"
        }, {
            "NAME": "Ubuntu",
            "VERSION_ID": "20.04"
        }]
        for mock_os_release in supported_os_releases:
            platform.system.side_effect = lambda: "Linux"
            get_linux_os_release.side_effect = lambda: mock_os_release
            self.assertTrue(os.path.exists(resources.get_libcbm_bin_path()))
Esempio n. 5
0
def create_model(pools: list[dict], flux_indicators: list[dict]):

    libcbm_config = {
        "pools": [{
            'name': p,
            'id': p_idx,
            'index': p_idx
        } for p, p_idx in pools.items()],
        "flux_indicators": [{
            "id": f_idx + 1,
            "index": f_idx,
            "process_id": f["process_id"],
            "source_pools": [int(x) for x in f["source_pools"]],
            "sink_pools": [int(x) for x in f["sink_pools"]],
        } for f_idx, f in enumerate(flux_indicators)]
    }

    with LibCBMHandle(resources.get_libcbm_bin_path(),
                      json.dumps(libcbm_config)) as handle:
        yield ModelHandle(LibCBMWrapper(handle), pools, flux_indicators)
Esempio n. 6
0
 def _initialize_libcbm(self):
     libcbm_config = {
         "pools": [{
             'name': p.name,
             'id': int(p),
             'index': p_idx
         } for p_idx, p in enumerate(Pool)],
         "flux_indicators": [{
             "id":
             f_idx + 1,
             "index":
             f_idx,
             "process_id":
             f["process_id"],
             "source_pools": [int(x) for x in f["source_pools"]],
             "sink_pools": [int(x) for x in f["sink_pools"]],
         } for f_idx, f in enumerate(FLUX_INDICATORS)]
     }
     self.dll = LibCBMWrapper(
         LibCBMHandle(resources.get_libcbm_bin_path(),
                      json.dumps(libcbm_config)))
Esempio n. 7
0
    def __init__(self,
                 classifiers,
                 merch_volumes,
                 db_path=None,
                 locale="en-CA",
                 dll_path=None):
        """Initialize an instance of CBMStandFactory using classifiers and
        merch volumes.

        Example classifiers::

            {
                "c1": ["c1_v1", "c1_v2", ...],
                "c1": ["c1_v1", "c1_v2", ...],
                ...
                "cN": ["cN_v1", "cN_v2", ...],
            }

        Example merch_volumes::

                [
                    {
                        "classifier_set": ["c1_v1", "c2_v1", ..., "cN_vK"],
                        "merch_volumes": [
                            "species_name": "Spruce",
                            "age_volume_pairs": [
                                [0, 0],
                                [50, 100],
                                [100, 150],
                                [150, 200],
                            ]
                        ]
                    }
                ]


        Args:
            classifiers (dict): dictionary describing classifiers and
                classifier values
            merch_volumes (list): list of dictionaries describing merchantable
                volume components. See example.
            db_path (str, optional): path to a cbm_defaults database. If None,
                the default database is used. Defaults to None.
            locale_code (str, optional): a locale code used to fetch the
                corresponding translated version of default parameter strings
            dll_path (str, optional): path to the libcbm compiled library, if
                not specified a default value is used.
        """
        if not db_path:
            self._db_path = resources.get_cbm_defaults_path()
        else:
            self._db_path = db_path

        if not dll_path:
            self._dll_path = resources.get_libcbm_bin_path()
        else:
            self._dll_path = dll_path

        self._classifiers = classifiers
        self._merch_volumes = merch_volumes
        self._locale = locale
        self.defaults_ref = CBMDefaultsReference(self._db_path, self._locale)

        self._classifier_config = self._get_classifier_config()
        self._classifier_idx = cbm_config.get_classifier_indexes(
            self._classifier_config)
        self.merch_vol_factory = self.merch_volumes_factory()
Esempio n. 8
0
def load_dll(config):

    dll = LibCBMWrapper(
        LibCBMHandle(resources.get_libcbm_bin_path(), json.dumps(config)))
    return dll
Esempio n. 9
0
 def test_error_returned_by_library(self):
     handle = LibCBMHandle(resources.get_libcbm_bin_path(),
                           json.dumps(TEST_CONFIG))
     with self.assertRaises(RuntimeError):
         # try to free an unallocated op to trigger an error
         handle.call("LibCBM_Free_Op", 1)
Esempio n. 10
0
 def test_disposal(self):
     handle = LibCBMHandle(resources.get_libcbm_bin_path(),
                           json.dumps(TEST_CONFIG))
     with handle:
         self.assertTrue(handle.pointer > 0)
     self.assertTrue(handle.pointer == 0)
Esempio n. 11
0
 def test_initialization_error(self):
     with self.assertRaises(RuntimeError):
         LibCBMHandle(resources.get_libcbm_bin_path(), "")
Esempio n. 12
0
 def test_linux_no_os_release(self, platform, get_linux_os_release):
     platform.system.side_effect = lambda: "Linux"
     get_linux_os_release.side_effect = lambda: None
     with self.assertRaises(RuntimeError):
         resources.get_libcbm_bin_path()
Esempio n. 13
0
 def test_get_windows_path(self, platform):
     platform.system.side_effect = lambda: "Windows"
     self.assertTrue(os.path.exists(resources.get_libcbm_bin_path()))
Esempio n. 14
0
 def test_fail_on_32_bit_system(self, sys):
     sys.maxsize = 2 ^ 32 - 1
     with self.assertRaises(RuntimeError):
         resources.get_libcbm_bin_path()
Esempio n. 15
0
 def test_unsupported_platform(self, platform):
     platform.system.side_effect = lambda: "Java"
     with self.assertRaises(RuntimeError):
         resources.get_libcbm_bin_path()
Esempio n. 16
0
 def test_mac_os_supported_vers(self, platform):
     supported_vers = ["10.12.xx", "10.13.xx", "10.14.xx", "10.15.xx"]
     platform.system.side_effect = lambda: "Darwin"
     for supported_ver in supported_vers:
         platform.mac_ver.side_effect = [[supported_ver]]
         self.assertTrue(os.path.exists(resources.get_libcbm_bin_path()))