Skip to content

Commit

Permalink
Create a function to check for criteria without data for the target v…
Browse files Browse the repository at this point in the history
…olcano

With the changes in this commit, the new function check_for_criteria_
without_data() raises a PyvolcansError if there is any volcanological
criteria without data for the specific target volcano chosen by the
user to run PyVOLCANS. The associated warning message suggests to set
the weights for those criteria to zero.
  • Loading branch information
PTierz committed May 24, 2021
1 parent d4dda82 commit 753fe80
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
15 changes: 12 additions & 3 deletions pyvolcans/pyvolcans.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
set_weights_from_args,
open_gvp_website,
output_result,
check_for_criteria_without_data,
check_for_perfect_analogues,
convert_to_idx,
plot_bar_apriori_analogues,
Expand Down Expand Up @@ -114,15 +115,23 @@ def cli():
# call PyVOLCANS
try:
# main PyVOLCANS result for all volcanoes (and weighting scheme used)
volcans_result = calculate_weighted_analogy_matrix(
volcano_input, weights=new_weights)
volcans_result, my_volcano_data = \
calculate_weighted_analogy_matrix(volcano_input,
weights=new_weights)

# final PyVOLCANS result (specific of the target volcano selected)
[top_analogues,
volcano_name] = get_analogies(volcano_input,
volcans_result,
count)

# check for volcanological criteria without data for target volcano
try:
check_for_criteria_without_data(my_volcano_data, volcano_name)
except PyvolcansError as exc:
# do not quit the program in this situation
logging.warning(exc.args[0])

# check for 'too many perfect analogues' (see Tierz et al., 2019)
try:
check_for_perfect_analogues(result=top_analogues)
Expand All @@ -141,7 +150,7 @@ def cli():
volcans_result['smithsonian_id'].iloc[convert_to_idx(volcano_input)]

# print main PyVOLCANS result to stdout
print(f"Top {count} analogue volcanoes for {volcano_name}, "
print(f"\nTop {count} analogue volcanoes for {volcano_name}, "
f"{my_volcano_country} ({my_volcano_vnum}):")
print(result)

Expand Down
48 changes: 46 additions & 2 deletions pyvolcans/pyvolcans_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def calculate_weighted_analogy_matrix(my_volcano, weights,
particular run of PyVOLCANS. A different weighting scheme can generate
an entirely different set of total analogy values.
my_volcano_data_dictionary: dict
my_volcano_data_dictionary : dict
Dictionary containing information on whether there is volcanological
data available (dict_value = 1), or there is not (dict_value = 0); for
each of the volcanological criteria used by PyVOLCANS, considering the
Expand Down Expand Up @@ -422,7 +422,7 @@ def calculate_weighted_analogy_matrix(my_volcano, weights,
volcans_result['ASt'] = \
weighted_eruption_style_analogy[volcano_idx, ]

return volcans_result
return volcans_result, my_volcano_data_dictionary


def get_analogies(my_volcano, volcans_result, count=10):
Expand Down Expand Up @@ -514,6 +514,50 @@ def check_for_perfect_analogues(result):
raise PyvolcansError(msg)


def check_for_criteria_without_data(my_volcano_data, my_volcano_name):
"""
Assesses whether some volcanological criteria do not have any data for the
specific target volcano chosen by the user, raising a PyvolcansError
exception if this is the case, informing the user which are these criteria.
Parameters
----------
my_volcano_name : str
Target volcano selected by the user, as volcano name.
my_volcano_data: dict
Dictionary containing information on whether there is volcanological
data available (dict_value = 1), or there is not (dict_value = 0); for
each of the volcanological criteria used by PyVOLCANS, considering the
specific target volcano chosen by the user to run the program.
Raises
-------
PyvolcansError
If one or more volcanological criteria have no data available for the
selected target volcano.
"""

# based on:
# https://thispointer.com/python-how-to-find-keys-by-value-in-dictionary
my_list_keys = list()
my_list_items = my_volcano_data.items()
for item in my_list_items:
if item[1] == 0:
my_list_keys.append(item[0])

# check whether the list is not empty (in other words, there are some
# volcanological criteria without data)
if my_list_keys:
nodata_criteria_text = ', '.join(my_list_keys)
msg = ("WARNING!!! "
"The following volcanological criteria do not have "
"any data available for the selected target volcano "
f"({my_volcano_name}): {nodata_criteria_text}. Please "
"consider excluding these criteria from your weighting scheme "
"(i.e. setting their weights to zero).")
raise PyvolcansError(msg)


def open_gvp_website(top_analogue_vnum):
"""
Opens the GVP website for the top analogue volcano to the target volcano
Expand Down

0 comments on commit 753fe80

Please sign in to comment.