ISISNeutronMuon / ISISNeutronMuon/MDMC
Corner plotting gives unhelpful error if refinement is run with too few steps
- Dominant language
- Python
- Stars
- 4
- Forks
- 0
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 5
Description
EDIT: the solution to this problem is by replacing the `corner` code with skopt's own plotting:
**jfkcooper (from comment below):**
>I found out (after writing all these methods) that there are actually some nicer ways of plotting the outputs already built into sk-opt. I would suggest using one/multiple of these and replacing some of my plotting code.
https://scikit-optimize.github.io/stable/auto_examples/plots/visualizing-results.html
using `from skopt.plots import` [`plot_objective`](https://scikit-optimize.github.io/stable/modules/generated/skopt.plots.plot_objective.html#skopt.plots.plot_objective)
---
If a refinement is run with fewer than 10 steps, the following error message appears upon running `control.plot_results()`:
```
Your data file apears not to have any points in, please check you have run the refinement and it saved correctly.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[9], line 3
1 # Run the refinement, i.e. refine the FF parameters against the data
2 control.refine(n_steps=8)
----> 3 control.plot_results();
File /usr/local/lib/python3.11/site-packages/MDMC/control/control.py:556, in Control.plot_results(self, filename, points, MH_norm)
553 filename = self.results_filename
554 plotter = PlotResults(filename, MH_norm=MH_norm, points=points,
555 quantiles=[0.34, 0.5, 0.68])
--> 556 cornerplot, means, stds = plotter.create_cornerplot()
558 if self.verbose != -1:
559 print(f'Parameter means = {means}, Parameter errors = {stds}')
TypeError: cannot unpack non-iterable NoneType object
```
**To reproduce:** this can be done with the Argon a-to-z tutorial; try it with fewer than 10 steps, then greater than 10 steps.
What is actually happening is if the method `_expected_minimum_random_sampling` produces an `IndexError`, the `create_cornerplot` function returns None and this makes `Control.plot_results` fall over. This *can* happen if the results file is not found, but can also happen if it isn't long enough!
The error handling should be improved here:
- if the corner plot actually fails to generate (i.e. there's too few datapoints to create valid contours), then this should be the error message.
- if the data file *doesn't* have any points in, then this should be an error itself: currently the exception handling is
```python
except IndexError:
msg = ("\n \n Your data file apears not to have any points in, please check you have "
"run the refinement and it saved correctly. \n")
print(msg)
return None
```
and then the None trips up a python error elsewhere, when it really should be
```python
except IndexError as error:
raise IndexError("Failed to read any points from the results file. Please check you have "
"run the refinement and it saved correctly.") from error
```
unless there's a really good case where this should be returning None to a user expecting a 3-tuple, rather than crashing out immediately.
- Alternatively, control.plot_results() could be rewritten to use the minimizer history directly when plotting results, rather than the results file, which would sidestep this exception needing to exist at all.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.