From a19c0aa02c279dd6f23ce3ce3477ab04a66c7f87 Mon Sep 17 00:00:00 2001 From: Michael Rossol Date: Mon, 8 Mar 2021 09:13:33 -0700 Subject: [PATCH 1/3] add contains to exclusions handler simplify techmap args and kwargs --- reV/handlers/exclusions.py | 3 +++ reV/supply_curve/cli_sc_aggregation.py | 2 +- reV/supply_curve/tech_mapping.py | 16 ++++++---------- tests/test_supply_curve_tech_mapping.py | 3 +-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/reV/handlers/exclusions.py b/reV/handlers/exclusions.py index dcca0b22b..17c4d0c64 100644 --- a/reV/handlers/exclusions.py +++ b/reV/handlers/exclusions.py @@ -62,6 +62,9 @@ def __getitem__(self, keys): return out + def __contains__(self, layer): + return layer in self.layers + def close(self): """ Close h5 instance diff --git a/reV/supply_curve/cli_sc_aggregation.py b/reV/supply_curve/cli_sc_aggregation.py index a8cfc7eb5..86d161ff6 100644 --- a/reV/supply_curve/cli_sc_aggregation.py +++ b/reV/supply_curve/cli_sc_aggregation.py @@ -319,7 +319,7 @@ def direct(ctx, excl_fpath, gen_fpath, tm_dset, econ_fpath, res_fpath, dsets = list(f) if tm_dset not in dsets: try: - TechMapping.run(excl_fpath, res_fpath, tm_dset) + TechMapping.run(excl_fpath, res_fpath, dset=tm_dset) except Exception as e: logger.exception('TechMapping process failed. Received the ' 'following error:\n{}'.format(e)) diff --git a/reV/supply_curve/tech_mapping.py b/reV/supply_curve/tech_mapping.py index af9e5bf8f..2941d4817 100644 --- a/reV/supply_curve/tech_mapping.py +++ b/reV/supply_curve/tech_mapping.py @@ -28,7 +28,7 @@ class TechMapping: """Framework to create map between tech layer (exclusions), res, and gen""" - def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03, + def __init__(self, excl_fpath, res_fpath, distance_upper_bound=0.03, map_chunk=2560, max_workers=None): """ Parameters @@ -38,8 +38,6 @@ def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03, created in excl_fpath. res_fpath : str Filepath to .h5 resource file that we're mapping to. - dset : str - Dataset name in excl_fpath to save mapping results to. distance_upper_bound : float | None Upper boundary distance for KNN lookup between exclusion points and resource points. None will calculate a good distance based on the @@ -54,7 +52,6 @@ def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03, self._distance_upper_bound = distance_upper_bound self._excl_fpath = excl_fpath self._res_fpath = res_fpath - self._dset = dset self._check_fout() self._map_chunk = map_chunk @@ -408,7 +405,7 @@ def save_tech_map(lats, lons, ind, fpath_out, res_fpath, dset, .format(dset, fpath_out)) @classmethod - def run(cls, excl_fpath, res_fpath, dset, save_flag=True, + def run(cls, excl_fpath, res_fpath, dset=None, distance_upper_bound=0.03, map_chunk=2560, max_workers=None): """Run parallel mapping and save to h5 file. @@ -419,10 +416,9 @@ def run(cls, excl_fpath, res_fpath, dset, save_flag=True, created in excl_fpath. res_fpath : str Filepath to .h5 resource file that we're mapping to. - dset : str - Dataset name in excl_fpath to save mapping results to. - save_flag : bool - Flag to write techmap to excl_fpath. + dset : str, optional + Dataset name in excl_fpath to save mapping results to, if None + do not save tech_map to excl_fpath, by default None kwargs : dict Keyword args to initialize the TechMapping object. @@ -444,7 +440,7 @@ def run(cls, excl_fpath, res_fpath, dset, save_flag=True, lats, lons, ind = mapper._parallel_resource_map() distance_upper_bound = mapper._distance_upper_bound - if save_flag: + if dset: mapper.save_tech_map(lats, lons, ind, excl_fpath, res_fpath, dset, distance_upper_bound) diff --git a/tests/test_supply_curve_tech_mapping.py b/tests/test_supply_curve_tech_mapping.py index 1ef651768..3f41bebe8 100644 --- a/tests/test_supply_curve_tech_mapping.py +++ b/tests/test_supply_curve_tech_mapping.py @@ -24,8 +24,7 @@ def test_resource_tech_mapping(): """Run the supply curve technology mapping and compare to baseline file""" - lats, lons, ind = TechMapping.run(EXCL, RES, TM_DSET, max_workers=2, - save_flag=False) + lats, lons, ind = TechMapping.run(EXCL, RES, dset=None, max_workers=2) with ExclusionLayers(EXCL) as ex: lat_truth = ex.latitude From fd04f8e6d954bd3abe81fbcdfdcfdbbc005796fd Mon Sep 17 00:00:00 2001 From: Michael Rossol Date: Mon, 8 Mar 2021 09:35:18 -0700 Subject: [PATCH 2/3] fix run class method arg handling --- reV/supply_curve/tech_mapping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reV/supply_curve/tech_mapping.py b/reV/supply_curve/tech_mapping.py index 2941d4817..822a3be42 100644 --- a/reV/supply_curve/tech_mapping.py +++ b/reV/supply_curve/tech_mapping.py @@ -436,7 +436,7 @@ def run(cls, excl_fpath, res_fpath, dset=None, """ kwargs = {"distance_upper_bound": distance_upper_bound, "map_chunk": map_chunk, "max_workers": max_workers} - with cls(excl_fpath, res_fpath, dset, **kwargs) as mapper: + with cls(excl_fpath, res_fpath, **kwargs) as mapper: lats, lons, ind = mapper._parallel_resource_map() distance_upper_bound = mapper._distance_upper_bound From df945f1e096363a222b00b555d6d9c2d6c0a210a Mon Sep 17 00:00:00 2001 From: Michael Rossol Date: Mon, 8 Mar 2021 10:14:04 -0700 Subject: [PATCH 3/3] cleanup docstrings --- reV/supply_curve/tech_mapping.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/reV/supply_curve/tech_mapping.py b/reV/supply_curve/tech_mapping.py index 822a3be42..67ad78840 100644 --- a/reV/supply_curve/tech_mapping.py +++ b/reV/supply_curve/tech_mapping.py @@ -419,8 +419,15 @@ def run(cls, excl_fpath, res_fpath, dset=None, dset : str, optional Dataset name in excl_fpath to save mapping results to, if None do not save tech_map to excl_fpath, by default None - kwargs : dict - Keyword args to initialize the TechMapping object. + distance_upper_bound : float | None + Upper boundary distance for KNN lookup between exclusion points and + resource points. None will calculate a good distance based on the + resource meta data coordinates. 0.03 is a good value for a 4km + resource grid and finer. + map_chunk : int | None + Calculation chunk used for the tech mapping calc. + max_workers : int | None + Number of cores to run mapping on. None uses all available cpus. Returns -------