From 94482a1cf8264322b4dd6745ab5f8282e8752871 Mon Sep 17 00:00:00 2001 From: rabii-chaarani Date: Fri, 23 Aug 2024 06:30:47 +0000 Subject: [PATCH] style: style fixes by ruff and autoformatting by black --- LoopDataConverter/__init__.py | 2 +- .../converters/loop_converter.py | 4 +- .../converters/ntgs_converter.py | 59 ++++++++----------- .../file_readers/_file_readers.py | 9 ++- LoopDataConverter/input/input_data.py | 4 +- LoopDataConverter/utils/conversion.py | 1 - 6 files changed, 35 insertions(+), 44 deletions(-) diff --git a/LoopDataConverter/__init__.py b/LoopDataConverter/__init__.py index 6a7f1ee..cebb147 100644 --- a/LoopDataConverter/__init__.py +++ b/LoopDataConverter/__init__.py @@ -1,3 +1,3 @@ from .converters import LoopConverter from .input import InputData -from .datatypes import SurveyName, Datatype \ No newline at end of file +from .datatypes import SurveyName, Datatype diff --git a/LoopDataConverter/converters/loop_converter.py b/LoopDataConverter/converters/loop_converter.py index 52f0ba2..a77f675 100644 --- a/LoopDataConverter/converters/loop_converter.py +++ b/LoopDataConverter/converters/loop_converter.py @@ -25,8 +25,8 @@ def __init__(self, survey_name: SurveyName, data: InputData, layer: str = None): The `data` parameter in the `__init__` method is of type `InputData`. It seems to represent the data that will be used in the survey. layer : str - The `layer` parameter is a string that represents a specific layer within a .GPKG file. - It is an optional parameter with a default value of `None`, which means it can be omitted + The `layer` parameter is a string that represents a specific layer within a .GPKG file. + It is an optional parameter with a default value of `None`, which means it can be omitted when creating an instance of the class. If provided, it specifies the layer to ''' diff --git a/LoopDataConverter/converters/ntgs_converter.py b/LoopDataConverter/converters/ntgs_converter.py index 0e84cfb..f7bfd20 100644 --- a/LoopDataConverter/converters/ntgs_converter.py +++ b/LoopDataConverter/converters/ntgs_converter.py @@ -10,7 +10,6 @@ # external imports import pandas -import numpy class NTGSConverter(BaseConverter): @@ -31,7 +30,7 @@ def type(self): ''' return self._type_label - + def update_empty_rows(self): ''' The function `update_empty_rows` updates empty rows in the DataFrame with NaN values. @@ -39,23 +38,20 @@ def update_empty_rows(self): Parameters ---------- None - - This method operates on the DataFrames stored in the class and replaces all empty values + + This method operates on the DataFrames stored in the class and replaces all empty values (e.g., empty strings, None, NaN) with NaN across the specified tables. ''' - + # List of tables (DataFrames) to update - tables_to_update = [Datatype.FOLD, - Datatype.FAULT, - Datatype.STRUCTURE] - + tables_to_update = [Datatype.FOLD, Datatype.FAULT, Datatype.STRUCTURE] + for table in tables_to_update: # Replace empty strings, None, or NaN with np.nan in the entire table self.raw_data[table] = self.raw_data[table].map( lambda x: "NaN" if pandas.isna(x) or x == "" or x is None else x ) - def convert_fold_map(self): ''' The function `convert_fold_map` converts dip direction, dip, and tightness terms in the raw data @@ -63,17 +59,17 @@ def convert_fold_map(self): ''' # convert dip direction terms to degrees - self.raw_data[Datatype.FOLD]["AxPlaneDD"] = self.raw_data[Datatype.FOLD][ - "AxPlaneDD" - ].apply(lambda x: convert_dipdir_terms(x)) + self.raw_data[Datatype.FOLD]["AxPlaneDD"] = self.raw_data[Datatype.FOLD]["AxPlaneDD"].apply( + lambda x: convert_dipdir_terms(x) + ) # convert dip terms to degrees self.raw_data[Datatype.FOLD]["AxPlaneDip"] = self.raw_data[Datatype.FOLD][ "AxPlaneDip" ].apply(lambda x: convert_dip_terms(x, type="fold")) # convert tightness terms to degrees - self.raw_data[Datatype.FOLD]["Interlimb"] = self.raw_data[Datatype.FOLD][ - "Interlimb" - ].apply(lambda x: convert_tightness_terms(x)) + self.raw_data[Datatype.FOLD]["Interlimb"] = self.raw_data[Datatype.FOLD]["Interlimb"].apply( + lambda x: convert_tightness_terms(x) + ) def convert_fault_map(self): ''' @@ -83,18 +79,18 @@ def convert_fault_map(self): ''' # convert dip direction terms to degrees - + self.raw_data[Datatype.FAULT]["DipDirectn"] = self.raw_data[Datatype.FAULT][ - "DipDirectn" - ].apply(lambda x: convert_dipdir_terms(x)) + "DipDirectn" + ].apply(lambda x: convert_dipdir_terms(x)) # convert dip terms to degrees self.raw_data[Datatype.FAULT]["Dip"] = self.raw_data[Datatype.FAULT]["Dip"].apply( - lambda x: convert_dip_terms(x, type="fault") - ) + lambda x: convert_dip_terms(x, type="fault") + ) # convert displacement terms to meters - self.raw_data[Datatype.FAULT]["Displace"] = self.raw_data[Datatype.FAULT][ - "Displace" - ].apply(lambda x: convert_displacement_terms(x)) + self.raw_data[Datatype.FAULT]["Displace"] = self.raw_data[Datatype.FAULT]["Displace"].apply( + lambda x: convert_displacement_terms(x) + ) def convert_structure_map(self): ''' @@ -106,22 +102,19 @@ def convert_structure_map(self): condition = (self.raw_data[Datatype.STRUCTURE]["Dip"] == -99) & ( self.raw_data[Datatype.STRUCTURE]["DipEstimte"] != "NaN" ) - + # convert dip estimate to float (average of the range) self.raw_data[Datatype.STRUCTURE].loc[condition, "Dip"] = ( - self.raw_data[Datatype.STRUCTURE] - .loc[condition, "DipEstimte"] - .apply(lambda x: convert_dip_terms(x, type="structure")) + self.raw_data[Datatype.STRUCTURE] + .loc[condition, "DipEstimte"] + .apply(lambda x: convert_dip_terms(x, type="structure")) ) - + # discard any rows that has a dip value of -99 and does not have any estimated dip value condition = (self.raw_data[Datatype.STRUCTURE]["Dip"] == -99) & ( self.raw_data[Datatype.STRUCTURE]["DipEstimte"] == "NaN" ) - self.raw_data[Datatype.STRUCTURE] = self.raw_data[Datatype.STRUCTURE][ - ~condition - ] - + self.raw_data[Datatype.STRUCTURE] = self.raw_data[Datatype.STRUCTURE][~condition] def convert(self): ''' diff --git a/LoopDataConverter/file_readers/_file_readers.py b/LoopDataConverter/file_readers/_file_readers.py index fadfd98..225228c 100644 --- a/LoopDataConverter/file_readers/_file_readers.py +++ b/LoopDataConverter/file_readers/_file_readers.py @@ -6,7 +6,8 @@ import os import validators -#TODO: add a metaclass for methods that are repetitive +# TODO: add a metaclass for methods that are repetitive + class BaseFileReader(ABC): def __init__(self): @@ -22,7 +23,6 @@ def check_source_type(self, file_source: str): file_source ), "Invalid file source, must be a valid URL or file path" - @abstractmethod def get_file(self, file_source: str, layer: str = None): pass @@ -50,7 +50,6 @@ def type(self): def check_source_type(self, file_source: str): super().check_source_type(file_source) - def get_file(self, file_source: str, layer: str = None): return pandas.read_csv(file_source) @@ -76,7 +75,7 @@ def type(self): @beartype.beartype def check_source_type(self, file_source: str): super().check_source_type(file_source) - + # TODO: add general check for variable types def get_file(self, file_source: str, layer: str = None): file_extension = os.path.splitext(file_source)[1] @@ -86,7 +85,7 @@ def get_file(self, file_source: str, layer: str = None): elif file_extension == ".gpkg": assert layer is False, "Layer name must be provided for GeoPackage files" - + return geopandas.read_file(file_source, layer=layer) else: diff --git a/LoopDataConverter/input/input_data.py b/LoopDataConverter/input/input_data.py index e6db194..bfadf2f 100644 --- a/LoopDataConverter/input/input_data.py +++ b/LoopDataConverter/input/input_data.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from ..datatypes import Datatype + @dataclass class InputData: """Class to store input data for the loop data converter @@ -11,7 +12,7 @@ class InputData: fault: Optional[Datatype] = None fold: Optional[Datatype] = None """ - + GEOLOGY: Datatype.GEOLOGY = None STRUCTURE: Datatype.STRUCTURE = None FAULT: Datatype.FAULT = None @@ -35,7 +36,6 @@ def __getitem__(self, datatype: str): raise KeyError(f"Datatype {datatype.name} not found in InputData") - # @dataclass # class InputData: # """Class to store input data for the loop data converter diff --git a/LoopDataConverter/utils/conversion.py b/LoopDataConverter/utils/conversion.py index 64e18de..872cb9a 100644 --- a/LoopDataConverter/utils/conversion.py +++ b/LoopDataConverter/utils/conversion.py @@ -1,7 +1,6 @@ import numpy - def convert_dipdir_terms(cardinal: str): """ Convert cardinal directions to degrees.