spectralpython / spectral

Python module for hyperspectral image processing
MIT License
571 stars 139 forks source link

Error computing bdist #69

Closed asentis closed 7 years ago

asentis commented 7 years ago

Hello,

I receive the following attribute error when attempting to use the bdist function:

File "/anaconda/lib/python3.6/site-packages/spectral/algorithms/algorithms.py", line 1177, in bdist_terms
    m = a.stats.mean - b.stats.mean
AttributeError: 'numpy.ndarray' object has no attribute 'stats'

Is there a solution for this? Thanks very much.

tboggs commented 7 years ago

The function is expecting two TrainingClass objects as its arguments. It looks like you are passing an ndarray instead. You can't compute the Bhattacharyya distance with just two arrays because it requires a mean and covariance for each class.

Note that the function doesn't really need the arguments to be TrainingClass objects - just any two objects that themselves have a stats attribute of type GaussianStats. So you could cheat and do something like

c1 = spy.calc_stats(array1)
c1.stats = c1
c2 = spy.calc_stats(array2)
c2.stats = c2
m = spy.bdist(c1, c2)
asentis commented 7 years ago

Thanks for the prompt reply! I missed that requirement in the documentation.

Separately, my datasets are of size m x 2 and n x 2. Is it required that the TraningClass object inputs be of the same dimensions?

tboggs commented 7 years ago

No, you only need to have the same number of bands for each data set.

asentis commented 7 years ago

If I understand correctly, my datasets should both have number of bands = 1. However, I receive the following error when using your above code:

    m = a.stats.mean - b.stats.mean
ValueError: operands could not be broadcast together with shapes (36,) (32,)

I have confirmed that my numpy.ndarrays have shape (36,2) and (32,2).

tboggs commented 7 years ago

It doesn't make sense to compute the Bhattacharyya distance for single-band data because it is only applicable to multivariate distributions. The data for which you are computing statistics should have shape (nrows, ncols, nbands) where nrows or ncols could be 1 but nbands must be greater than 1.