Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing Issue: Multiple Maximum Values in Posterior Distribution Leads to Unexpected Array Size of Index #73

Open
kompeki0 opened this issue Aug 22, 2024 · 0 comments

Comments

@kompeki0
Copy link

Bug: An error occurs when fitting a specific array under certain conditions.

Environment:

  • Python version: Python 3.7.3
  • psignifit version: 0.1
  • OS: macOS 14.6

Description:
An error occurs when running the following program:

import psignifit as ps
import numpy as np

arr = np.asarray([[0, 1, 10], [4, 0, 10], [8, 6, 10], [12, 8, 10], [16, 10, 10]])
options = {}
options['fixedPars'] = np.ones(5)*np.nan
options['fixedPars'][2] = 0.0
options['fixedPars'][3] = 0.0
options['sigmoidName'] = 'logistic'

result = ps.psignifit(arr, options)

Error Code:

File "psignifit/psignifit.py", line 393, in psignifitCore
    Fit[idx] = result['X1D'][idx][index[idx]] 
ValueError: could not broadcast input array from shape (2,) into shape (1,)

Remarks:
It seems that the issue arises in the following part of psignifit.py:

index = np.where(result['Posterior'] == np.max(result['Posterior'].ravel()))

The problem occurs when result['Posterior'].ravel() contains multiple maximum values, causing index to contain unexpected values (in this case, two values).

To work around this issue, I handled the case where index has multiple values by averaging them if they are consecutive.

Proposed Fix:
I modified the following part:

Fit[idx] = result['X1D'][idx][index[idx]]

to:

if len(index[idx]) > 1:
    unique_sorted_arr = sorted(set(index[idx]))
    expected_sequence = list(range(min(unique_sorted_arr), max(unique_sorted_arr) + 1))
    if unique_sorted_arr == expected_sequence:
        warnings.warn(f"index has multiple consecutive values: {index[idx]}. The mean value of results is used instead", UserWarning)
        Fit[idx] = result['X1D'][idx][index[idx]].mean() 
    else:
        raise ValueError(f"index has multiple non-consecutive values: {index[idx]}.")
else:
    Fit[idx] = result['X1D'][idx][index[idx]]

However, I am not fully confident that this fix is universally applicable.

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

No branches or pull requests

1 participant