Skip to content

Commit

Permalink
fix: Add special case for SA config file to lowercase the column name…
Browse files Browse the repository at this point in the history
…s. Also check for empty map2model relationship files and deal appropriately with them
  • Loading branch information
RoyThomsonMonash committed Jun 1, 2023
1 parent 4eb720a commit 82054df
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 43 deletions.
12 changes: 7 additions & 5 deletions map2loop/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self):
}

@beartype.beartype
def update_from_dictionary(self, dictionary: dict):
def update_from_dictionary(self, dictionary: dict, lower:bool = False):
"""
Update the config dictionary from a provided dict
Expand Down Expand Up @@ -102,7 +102,7 @@ def update_from_dictionary(self, dictionary: dict):
print(f"Unused keys from config format {list(dictionary.keys())}")

@beartype.beartype
def update_from_legacy_file(self, file_map: dict):
def update_from_legacy_file(self, file_map: dict, lower:bool=False):
"""
Update the config dictionary from the provided old version dictionary
Expand Down Expand Up @@ -147,6 +147,8 @@ def update_from_legacy_file(self, file_map: dict):
}
for code in code_mapping:
if code in file_map:
if lower is True:
file_map[code] = str(file_map[code]).lower()
code_mapping[code][0][code_mapping[code][1]] = file_map[code]
file_map.pop(code)

Expand All @@ -160,7 +162,7 @@ def update_from_legacy_file(self, file_map: dict):
print(f"Unused keys from legacy format {list(file_map.keys())}")

@beartype.beartype
def update_from_file(self, filename: str, legacy_format: bool = False):
def update_from_file(self, filename: str, legacy_format: bool = False, lower:bool = False):
"""
Update the config dictionary from the provided json filename or url
Expand All @@ -176,8 +178,8 @@ def update_from_file(self, filename: str, legacy_format: bool = False):
if filename.startswith("http") or filename.startswith("ftp"):
with urllib.request.urlopen(filename) as url_data:
data = hjson.load(url_data)
func(data)
func(data, lower)
else:
with open(filename) as url_data:
data = hjson.load(url_data)
func(data)
func(data, lower)
3 changes: 2 additions & 1 deletion map2loop/deformation_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ def __init__(self):
("minAge", float),
("maxAge", float),
("group", str),
("supergroup", str),
("avgDisplacement", float),
("avgDownthrowDir", float),
("influenceDirection", float),
("influenceDistance", float),
("verticalRadius", float),
("horizontalRadius", float),
("colour", str),
Expand Down
72 changes: 40 additions & 32 deletions map2loop/map2model_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,50 +155,58 @@ def run(self, verbose_level: VerboseLevel = None):
self.sorted_units = list(units_sorted[5])

# Parse fault intersections
df = pandas.read_csv(
os.path.join(self.map_data.tmp_path, "map2model_data", "fault-fault-intersection.txt"),
delimiter="{",
header=None,
)
df[1] = list(df[1].str.replace("}", "", regex=False))
df[1] = [re.findall("\(.*?\)", i) for i in df[1]] # noqa: W605 Valid escape for regex
df[0] = list(df[0].str.replace("^[0-9]*, ", "", regex=True))
df[0] = list(df[0].str.replace(", ", "", regex=False))
df[0] = "Fault_" + df[0]
for j in range(len(df)):
df[1][j] = [i.strip("()").replace(" ", "").split(",") for i in df[1][j]]

out = []
for _, row in df.iterrows():
for i in numpy.arange(len(row[1])):
out += [[row[0], "Fault_"+row[1][i][0], row[1][i][1], float(row[1][i][2])]]
fault_fault_intersection_filename = os.path.join(self.map_data.tmp_path, "map2model_data","fault-fault-intersection.txt")
if os.path.isfile(fault_fault_intersection_filename) and os.path.getsize(fault_fault_intersection_filename) > 0:
df = pandas.read_csv(
fault_fault_intersection_filename,
delimiter="{",
header=None,
)
df[1] = list(df[1].str.replace("}", "", regex=False))
df[1] = [re.findall("\(.*?\)", i) for i in df[1]] # noqa: W605 Valid escape for regex
df[0] = list(df[0].str.replace("^[0-9]*, ", "", regex=True))
df[0] = list(df[0].str.replace(", ", "", regex=False))
df[0] = "Fault_" + df[0]
for j in range(len(df)):
df[1][j] = [i.strip("()").replace(" ", "").split(",") for i in df[1][j]]

for _, row in df.iterrows():
for i in numpy.arange(len(row[1])):
out += [[row[0], "Fault_"+row[1][i][0], row[1][i][1], float(row[1][i][2])]]

df_out = pandas.DataFrame(columns=["Fault1", "Fault2", "Type", "Angle"], data=out)
self.fault_fault_relationships = df_out

# Parse unit fault relationships
df = pandas.read_csv(os.path.join(self.map_data.tmp_path, "map2model_data", "unit-fault-intersection.txt"), header=None, sep='{')
df[1] = list(df[1].str.replace("}", "", regex=False))
df[1] = df[1].astype(str).str.split(", ")
df[0] = list(df[0].str.replace("^[0-9]*, ", "", regex=True))
df[0] = list(df[0].str.replace(", ", "", regex=False))

out = []
for _, row in df.iterrows():
for i in numpy.arange(len(row[1])):
out += [[row[0], "Fault_"+row[1][i]]]
unit_fault_intersection_filename = os.path.join(self.map_data.tmp_path, "map2model_data", "unit-fault-intersection.txt")
if os.path.isfile(unit_fault_intersection_filename) and os.path.getsize(unit_fault_intersection_filename) > 0:
df = pandas.read_csv(unit_fault_intersection_filename, header=None, sep='{')
df[1] = list(df[1].str.replace("}", "", regex=False))
df[1] = df[1].astype(str).str.split(", ")
df[0] = list(df[0].str.replace("^[0-9]*, ", "", regex=True))
df[0] = list(df[0].str.replace(", ", "", regex=False))

for _, row in df.iterrows():
for i in numpy.arange(len(row[1])):
out += [[row[0], "Fault_"+row[1][i]]]

df_out = pandas.DataFrame(columns=["Unit", "Fault"], data=out)
self.unit_fault_relationships = df_out

# Parse unit unit relationships
units = []
links = []
with open(os.path.join(self.map_data.tmp_path, "map2model_data", "graph_all_None.gml.txt")) as file:
contents = file.read()
segments = contents.split("\n\n")
for line in segments[0].split("\n"):
units += [line.split(" ")]
for line in segments[1].split("\n")[:-1]:
links += [line.split(" ")]
graph_filename = os.path.join(self.map_data.tmp_path, "map2model_data", "graph_all_None.gml.txt")
if os.path.isfile(graph_filename) and os.path.getsize(graph_filename) > 0:
with open(os.path.join(self.map_data.tmp_path, "map2model_data", "graph_all_None.gml.txt")) as file:
contents = file.read()
segments = contents.split("\n\n")
for line in segments[0].split("\n"):
units += [line.split(" ")]
for line in segments[1].split("\n")[:-1]:
links += [line.split(" ")]

df = pandas.DataFrame(columns=["index", "unit"], data=units)
df.set_index("index", inplace=True)
Expand Down
15 changes: 10 additions & 5 deletions map2loop/mapdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_filename(self, datatype: Datatype):
return None

@beartype.beartype
def set_config_filename(self, filename: str, legacy_format: bool = False):
def set_config_filename(self, filename: str, legacy_format: bool = False, lower:bool = False):
"""
Set the config filename and update the config structure
Expand All @@ -235,7 +235,7 @@ def set_config_filename(self, filename: str, legacy_format: bool = False):
Whether the file is in m2lv2 form. Defaults to False.
"""
self.config_filename = filename
self.config.update_from_file(filename, legacy_format=legacy_format)
self.config.update_from_file(filename, legacy_format=legacy_format, lower=lower)

def get_config_filename(self):
"""
Expand Down Expand Up @@ -307,7 +307,8 @@ def set_filenames_from_australian_state(self, state: str):
self.set_filename(Datatype.FAULT, AustraliaStateUrls.aus_fault_urls[state])
self.set_filename(Datatype.FOLD, AustraliaStateUrls.aus_fold_urls[state])
self.set_filename(Datatype.DTM, "au")
self.set_config_filename(AustraliaStateUrls.aus_config_urls[state], legacy_format=True)
lower = state == "SA"
self.set_config_filename(AustraliaStateUrls.aus_config_urls[state], legacy_format=True, lower=lower)
self.set_colour_filename(AustraliaStateUrls.aus_clut_urls[state])
else:
raise ValueError(f"Australian state {state} not in state url database")
Expand Down Expand Up @@ -818,8 +819,9 @@ def parse_fault_map(self) -> tuple:
else:
faults["ID"] = faults.index

faults["NAME"] = faults.apply(lambda fault: "Fault_" + str(fault["ID"]) if fault["NAME"] == "nan" else fault["NAME"], axis=1)
faults["NAME"] = faults["NAME"].str.replace(" -/?", "_", regex=True)
if len(faults):
faults["NAME"] = faults.apply(lambda fault: "Fault_" + str(fault["ID"]) if fault["NAME"] == "nan" else fault["NAME"], axis=1)
faults["NAME"] = faults["NAME"].str.replace(" -/?", "_", regex=True)

self.data[Datatype.FAULT] = faults
return (False, "")
Expand Down Expand Up @@ -1154,6 +1156,9 @@ def get_value_from_raster_df(self, datatype: Datatype, df: pandas.DataFrame):
Returns:
pandas.DataFrame: The modified dataframe
"""
if len(df) <= 0:
df['Z'] = []
return df
data = self.get_map_data(datatype)
if data is None:
print("Cannot get value from data as data is not loaded")
Expand Down

0 comments on commit 82054df

Please sign in to comment.