Skip to content

Adding functionality to choose closest matching threshold.#2405

Open
mo-jbeaver wants to merge 3 commits into
metoppv:masterfrom
mo-jbeaver:mobt1176_threshold_selection_alternates
Open

Adding functionality to choose closest matching threshold.#2405
mo-jbeaver wants to merge 3 commits into
metoppv:masterfrom
mo-jbeaver:mobt1176_threshold_selection_alternates

Conversation

@mo-jbeaver

Copy link
Copy Markdown
Contributor

Addresses #1176

Adding changes to the plugin to choose the closest threshold, when there are multiple matching thresholds available .

Testing:

  • Ran tests and they passed OK
  • Added new tests for the new feature(s)

@brhooper brhooper left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mo-jbeaver I think that this PR makes a useful improvement to the robustness of the decision tree application plugin. I've left you a few minor comments.

Comment thread improver/categorical/decision_tree.py Outdated
Comment on lines +291 to +292
diff_index = diff.index(min(diff))
closest_point = threshold_points[diff_index]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
diff_index = diff.index(min(diff))
closest_point = threshold_points[diff_index]
# find closest available threshold to desired threshold
closest_point = threshold_points[diff.index(min(diff))]

diff_index is only used once, so I don't think we need to create a variable to store it.

Comment thread improver/categorical/decision_tree.py Outdated
Comment on lines +309 to +313
else:
missing_data.append(
f"name: {diagnostic}, threshold: {threshold}, "
f"(closest match extraction failed)\n"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually possible to enter this else statement? If it is possible, I don't think that we currently have a unit test to cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've re-checked this and I agree, so I have removed this section of the code.

)
num_thresholds = len(threshold_points)

if num_thresholds > 1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an inline code comment here to describe how we handle finding multiple thresholds could be useful here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline comment added


threshold_coord = find_threshold_coordinate(self.cubes[0])
additional_threshold = threshold_coord.points[0] * (
original_threshold_coord = threshold_coord.points[0]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think original_threshold is a better name for this variable. I don't think that it stores a coordinate, just a single point from a coordinate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name updated

for cube in result
if threshold_coord.name() in [coord.name() for coord in cube.coords()]
]
self.assertIn(original_threshold_coord, extracted_thresholds)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also assert that extracted_thresholds has length one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test adjusted to better fit the outputs, and a length check added.

@brhooper brhooper left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mo-jbeaver I've added another couple of minor comments.

expected_thresholds = {original_threshold, threshold_coord.points[2]}
modified_cube = [cube for cube in result if "lwe_snowfall_rate" in cube.name()]
for cube in modified_cube:
self.assertGreater(len(modified_cube), 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be outside the for loop. I also think that we expect exactly one cube in modified_cube.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the assertion to outside of the for loop.
For the modified_cube [now called snowfall_cubes], it pulls all snowfall related cubes from result. There are multiple cubes, as each cube refers to a node on the decision tree.

cube_thresholds = cube.coord(find_threshold_coordinate(cube).name()).points
self.assertEqual(len(cube_thresholds), 1)
cube_threshold_value = cube_thresholds[0]
self.assertIn(cube_threshold_value, expected_thresholds)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to know which threshold we expect to be in the output cube, and to check that the actual returned threshold is the expected one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, there are multiple cubes in snowfall_cubes each its own threshold value, depending on its position on the decision tree. Here, we are asserting that we expect that this threshold value is either the original_threshold or threshold_coord.points[2], as the plugin will have selected the original_threshold over the additional_threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants