Skip to content

Conversation

@sroet
Copy link
Member

@sroet sroet commented Jun 16, 2021

Issue found by a master student:
If cv singletons are not unpacked the SparseHistogram might return broken output bins, which then fail in following code that assumes it is 1D (Like in this line).

This PR forces a reshape to prevent mangled output

master:

>>> from openpathsampling.numerics.histogram import SparseHistogram
>>> histo = SparseHistogram(bin_widths=(0.5, 0.3), left_bin_edges=(0.0, -0.1)) 
>>> data = ([0.0],[1.0])
>>> histo.map_to_bins(data)
(array([0., 0.]), array([2., 3.]))
>>> max(histo.map_to_bins(data))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

This PR:

>>> from openpathsampling.numerics.histogram import SparseHistogram
>>> histo = SparseHistogram(bin_widths=(0.5, 0.3), left_bin_edges=(0.0, -0.1)) 
>>> data = ([0.0],[1.0])
>>> histo.map_to_bins(data)
(0.0, 3.0)
>>> max(histo.map_to_bins(data))
3.0

It also fixes some pep8 issues in histogram.py

@codecov
Copy link

codecov bot commented Jun 16, 2021

Codecov Report

Merging #1029 (cd74c1d) into master (aaff2b8) will decrease coverage by 0.00%.
The diff coverage is 90.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1029      +/-   ##
==========================================
- Coverage   81.30%   81.29%   -0.01%     
==========================================
  Files         139      139              
  Lines       15150    15149       -1     
==========================================
- Hits        12318    12316       -2     
- Misses       2832     2833       +1     
Impacted Files Coverage Δ
openpathsampling/numerics/histogram.py 83.98% <90.00%> (-0.06%) ⬇️
openpathsampling/netcdfplus/cache.py 63.72% <0.00%> (-0.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aaff2b8...cd74c1d. Read the comment docs.

@sroet
Copy link
Member Author

sroet commented Jun 16, 2021

Codecov complaint is about a line that only has whitespace changes

@dwhswenson dwhswenson added the bugfix PRs fixing bugs label Jun 17, 2021
@dwhswenson
Copy link
Member

This is fine, but users creating CVs that return len-1 arrays seems to be a recurring issue that causes problems in multiple aspects of analysis. I'll accept this PR, but in general I'd be more interested in finding ways to discourage these CVs in the first place. I'll open a PR that tries to address this by raising a warning if using the CVDefinedVolume with a len-1 array.

The fundamental problem is that Python (and NumPy) allow ordering of len-1 iterables, which often lets the problem slip through sampling without causing problem. However, later analyses fail when they require that these be scalars (e.g., combining them into arrays).

@dwhswenson dwhswenson merged commit 0819cff into openpathsampling:master Jun 17, 2021
@sroet sroet deleted the fix_yol_issue branch June 17, 2021 11:04
@dwhswenson dwhswenson mentioned this pull request Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix PRs fixing bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants