diff --git a/CHANGELOG.md b/CHANGELOG.md index d6ca2c46..8227485c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [3.1.8](https://github.com/Loop3D/map2loop/compare/v3.1.7...v3.1.8) (2024-06-20) + + +### Bug Fixes + +* pin LPF to 0.1.3 ([f3766a8](https://github.com/Loop3D/map2loop/commit/f3766a83d0f09f293429cc975933cbf79b01c5a2)) + ## [3.1.7](https://github.com/Loop3D/map2loop/compare/v3.1.6...v3.1.7) (2024-06-18) diff --git a/conda/meta.yaml b/conda/meta.yaml index e4d0b39d..26e160f3 100644 --- a/conda/meta.yaml +++ b/conda/meta.yaml @@ -20,7 +20,6 @@ requirements: - numpy - geopandas - map2model - - hjson - loopprojectfile - beartype - owslib @@ -30,7 +29,6 @@ requirements: - pandas - geopandas - map2model - - hjson - loopprojectfile - beartype - owslib @@ -40,7 +38,6 @@ test: - map2loop - geopandas - map2model - - hjson - loopprojectfile - beartype - owslib diff --git a/dependencies.txt b/dependencies.txt index 4c52daee..96ab72a4 100644 --- a/dependencies.txt +++ b/dependencies.txt @@ -4,5 +4,5 @@ networkx owslib map2model hjson -loopprojectfile>=0.1.3 +loopprojectfile==0.1.3 beartype \ No newline at end of file diff --git a/map2loop/config.py b/map2loop/config.py index 21d9e536..a40b2e88 100644 --- a/map2loop/config.py +++ b/map2loop/config.py @@ -1,5 +1,6 @@ +import urllib.error import beartype -import hjson +import json import urllib import time import pathlib @@ -188,8 +189,9 @@ def update_from_file( Update the config dictionary from the provided json filename or url Args: - filename (str): Filename or URL of the JSON config file + filename (Union[pathlib.Path, str]): Filename or URL of the JSON config file legacy_format (bool, optional): Whether the JSON is an old version. Defaults to False. + lower (bool, optional): convert keys to lowercase. Defaults to False. """ if legacy_format: func = self.update_from_legacy_file @@ -199,26 +201,49 @@ def update_from_file( try: filename = str(filename) + #if url, open the url if filename.startswith("http") or filename.startswith("ftp"): - try_count = 10 + try_count = 5 success = False - while try_count >= 0 and not success: + # try 5 times to access the URL + while try_count > 0 and not success: try: with urllib.request.urlopen(filename) as url_data: - data = hjson.load(url_data) + data = json.load(url_data) func(data, lower) success = True - except Exception as e: - # Catch a failed online access or file load, re-attempt - # a few times before throwing further + + #case 1. handle url error + except urllib.error.URLError as e: + # wait 0.25 seconds before trying again time.sleep(0.25) - try_count = try_count - 1 - if try_count < 0: - raise e + # decrease the number of tries by 1 + try_count -= 1 + # if no more tries left, raise the error + if try_count <= 0: + raise urllib.error.URLError(f"Failed to access URL after multiple attempts: {filename}") from e + + # case 2. handle json error + except json.JSONDecodeError as e: + raise json.JSONDecodeError( + f"Error decoding JSON data from URL: {filename}") from e else: - with open(filename) as url_data: - data = hjson.load(url_data) - func(data, lower) + try: + with open(filename) as file_data: + data = json.load(file_data) + func(data, lower) + except FileNotFoundError as e: + err_string = f"The specified config file does not exist ({filename}).\n" + err_string += "Please check the file exists and is accessible, then try again.\n" + raise FileNotFoundError(err_string) from e + except json.JSONDecodeError as e: + raise json.JSONDecodeError( + f"Error decoding JSON data from file: {filename}" + ) from e + + except FileNotFoundError: + raise + except Exception: err_string = f"There is a problem parsing the config file ({filename}).\n" if filename.startswith("http"): @@ -228,4 +253,4 @@ def update_from_file( if not legacy_format: err_string += "Also check if this is a legacy config file and add clut_file_legacy=True to the Project function\n" err_string += "Check the contents for mismatched quotes or brackets!" - raise Exception(err_string) + raise Exception(err_string) \ No newline at end of file diff --git a/map2loop/version.py b/map2loop/version.py index 02f8c630..e9c7bfb1 100644 --- a/map2loop/version.py +++ b/map2loop/version.py @@ -1 +1 @@ -__version__ = "3.1.7" +__version__ = "3.1.8" diff --git a/setup.py b/setup.py index f654acc5..00bec5a0 100644 --- a/setup.py +++ b/setup.py @@ -34,10 +34,8 @@ def get_description(): "pandas", "geopandas", "shapely", - "tqdm", "networkx", "owslib", - "hjson", "loopprojectfile", "map2model", "beartype", diff --git a/tests/thickness/InterpolatedStructure/test_interpolated_structure.py b/tests/thickness/InterpolatedStructure/test_interpolated_structure.py index 986b57df..56d7f992 100644 --- a/tests/thickness/InterpolatedStructure/test_interpolated_structure.py +++ b/tests/thickness/InterpolatedStructure/test_interpolated_structure.py @@ -18,6 +18,7 @@ from map2loop.sampler import SamplerSpacing, SamplerDecimator from map2loop.m2l_enums import Datatype import map2loop +import numpy def create_raster(output_path, bbox, epsg, pixel_size, value=100): @@ -272,6 +273,7 @@ def test_compute( result = interpolated_structure_thickness.compute( units, stratigraphic_order, basal_contacts, samples, map_data ) + assert ( interpolated_structure_thickness.thickness_calculator_label == "InterpolatedStructure" ), 'Thickness_calc interpolated structure not being set properly' @@ -284,11 +286,11 @@ def test_compute( assert ( 'ThicknessMean' in result.columns ), 'Thickness not being calculated in InterpolatedStructure calculator' - assert result['ThicknessMedian'].dtypes == float, 'ThicknessMedian column is not float' + assert result['ThicknessMedian'].dtypes == numpy.float64, 'ThicknessMedian column is not float' assert ( 'ThicknessStdDev' in result.columns ), 'Thickness std not being calculated in InterpolatedStructure calculator' - assert result['ThicknessStdDev'].dtypes == float, 'ThicknessStdDev column is not float' + assert result['ThicknessStdDev'].dtypes == numpy.float64, 'ThicknessStdDev column is not float' # check for nas in thickness assert result['ThicknessMedian'].isna().sum() == 0, 'ThicknessMedian column has NaNs'