Skip to content

[MRG] FIX #13056 - FastICA whitening problem - #13096

Closed
fferrin wants to merge 22 commits into
scikit-learn:mainfrom
fferrin:bug-13056
Closed

fferrin wants to merge 22 commits into
scikit-learn:mainfrom
fferrin:bug-13056

Conversation

@fferrin

@fferrin fferrin commented Feb 6, 2019

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #13056

What does this implement/fix? Explain your changes.

There was an error in the implementation of the FastICA algorithm since, when the whiten parameter was set to True, the variance of the result was not 1.0. In this PR, this is corrected and also the default value True of the whiten parameter is changed to 'unit-variance'.

Any other comments?

Comment thread sklearn/decomposition/fastica_.py Outdated
if (n_components > min(n, p)):
n_components = min(n, p)
warnings.warn('n_components is too large: it will be set to %s' % n_components)
warnings.warn(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please revert formatting changes.

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 run pycodestyle to autoformat the code. Apparently, that line was changed by it. I couldn't found in the documentation of the stable version how to format the code. Is there an automatic way to do the formatting?

Got it. I'll revert the changes in the next PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We follow PEP8. Some editors/IDEs give suggestions and warnings as you code, but the configurations depends on the editor you use, and not included in the contributing guides.

You can always run flake8 on your code to see if there are any issues.

Comment thread sklearn/decomposition/fastica_.py Outdated
whiten : boolean, optional
If whiten is false, the data is already considered to be
whitened, and no whitening is performed.
whiten : string or boolean, optional. Default: 'unit-variance'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
whiten : string or boolean, optional. Default: 'unit-variance'
whiten : string or boolean, optional (default='unit-variance')

ch_num = signal.shape[0]
signal = signal.transpose()
ica = FastICA(n_components=ch_num, whiten=True)
_ = ica.fit_transform(signal)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use a with pytest.warns(...) construct please.

Comment thread sklearn/decomposition/fastica_.py Outdated
"and whiten=True will behave like whiten='unit-variance'.",
category=DeprecationWarning)
elif self.whiten == 'unit-variance':
self.whiten = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use a local variable instead of changing the value of self.whiten

@fferrin

fferrin commented Feb 7, 2019 •

Copy link
Copy Markdown
Contributor Author

I'll take a look at the paper and see if there is something else wrong with the implementation, as @jnothman said about the rescaling of W.

If you have any suggestions about the tests or the implementation, are really welcome.

@adrinjalali in the next PR I'll also fix the format errors that you mentioned above.

@adrinjalali

Copy link
Copy Markdown
Member

@fferrin you should just push more commits to the same branch, you don't need to open a new PR.

Comment thread sklearn/decomposition/fastica_.py Outdated
if whiten:
if compute_sources:
S = np.dot(np.dot(W, K), X).T
S /= np.sqrt(np.var(S))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

S should have shape (n_samples, n_components) here. np.var(S) will compute the variance over all sources (components) and samples rather than for each source across samples.

Suggested change
S /= np.sqrt(np.var(S))
S_std = np.std(S, axis=0, keepdims=True)
S /= S_std

Then, to make this equation consistent again

S = np.dot(np.dot(W, K), X).T

you'll need to modify the right side by modifying W

W /= S_std.T

@jnothman

Copy link
Copy Markdown
Member

This is looking pretty good? Any reason to continue marking it as WIP?

Please add an entry to the change log at doc/whats_new/v0.21.rst. Like the other entries there, please reference this pull request with :issue: and credit yourself (and other contributors if applicable) with :user:

Comment thread sklearn/decomposition/fastica_.py Outdated
# else:
# S = None

S = np.dot(np.dot(W, K), X).T

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there's no need to compute S if compute_sources is False, this would be unnecessary computation.

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 know, but if I don't do that, the value of W is wrong (by a factor of 10 in the test). I couldn't find a way to update W without computing S. Any ideas?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair point! I guess in that case it's also fair to update the description of compute_sources parameter, maybe.

@fferrin fferrin Feb 12, 2019 •

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.

Great! If that's okay, I'll delete the commented lines (from 355 to 361).

Regarding the parameter compute_sources would it be something like this?

compute_sources: bool, optional
    Sources are computed in any case because they are needed to compute the 
    estimated un-mixing matrix. If False, sources are not returned (default=True).

def __init__(self, n_components=None, algorithm='parallel', whiten=True,
fun='logcosh', fun_args=None, max_iter=200, tol=1e-4,
w_init=None, random_state=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please revert this formatting change.

@fferrin fferrin Feb 12, 2019 •

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.

Yes, sorry about the formatting. I'll revert all the formatting changes made by pycodestyle

Comment thread sklearn/decomposition/fastica_.py Outdated
fun_args = {} if self.fun_args is None else self.fun_args

whiten = self.whiten
if whiten is True:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this validation could be done in fastica() and not here. That function also needs to understand unit-variance if we're changing the default here. And any module using that function should get the same warning I suppose.

@fferrin fferrin Feb 12, 2019 •

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 followed the guide from here (the fourth paragraph), and makes sense to me because unit-variance and True are they behave in the same way for fastica(). Of course, I only know about one or two modules of the project, so if you say so I'll change it, but I wanted to point that out.

Also, fastica() is only used by the tests and by FastICA class. Would it then make sense then to change fastica() to a private function?

@fferrin

fferrin commented Feb 12, 2019 •

Copy link
Copy Markdown
Contributor Author

I have a few questions / doubts:

  1. I don't know why Travis is failing. In my computer all the tests pass (Python 3.6.7)
    screenshot from 2019-02-12 12-37-17
coverage==4.5.2
Cython==0.29.4
numpy==1.16.1
pycodestyle==2.5.0
pyflakes==2.1.0
pytest==4.2.0
scipy==1.2.0
  1. I used DeprecationWarning for the default value of whiten but I think it's not right because the True value will still be used in later versions. Is there any other warning that you recommend in this case?

  2. I updated some of the tests including 'unit-variance' for the whiten, along with True and False. Is it necessary that I also add the warning check in these tests or do I only perform it in a separate test?

@jnothman

Copy link
Copy Markdown
Member

This doesn't look like it is maintaining backwards compatible behaviour for whiten=True. I don't really get why we now need to compute sources when all we are changing is a scaling factor being the shape of X

@fferrin

fferrin commented Feb 12, 2019 •

Copy link
Copy Markdown
Contributor Author

I think you will get the same results using either whiten=True or whiten='unit-variance. Also, if you set compute_sources=False it will return S = None but internally it will do the calculation.

At first, I changed the scaling factor for S, as you said, but later in the code S is used to compute W.

@jnothman

jnothman commented Feb 12, 2019 via email

Copy link
Copy Markdown
Member

@jnothman

jnothman commented Feb 12, 2019 via email

Copy link
Copy Markdown
Member

@fferrin

fferrin commented Feb 12, 2019

Copy link
Copy Markdown
Contributor Author

I think the backward compatibility is maintained because when whiten=True you get the same results as before but with the warning.

Regarding S, again you obtain the same results as before if you set compute_sources=False but internally S is computed anyway. I'll take a look at the paper and the code to see if I can avoid computing S.

@fferrin fferrin changed the title [WIP] FIX #13056 - FastICA whitening problem [MRG] FIX #13056 - FastICA whitening problem Feb 14, 2019
@jnothman

jnothman commented May 1, 2019

Copy link
Copy Markdown
Member

Sorry the reviews here have been slow.

This PR modifies doc/whats_new/v0.21.rst, but this patch won't be released in version 0.21. This changelog entry should be moved to doc/whats_new/v0.22.rst after merging master. You can use something like:

git fetch https://github.com/scikit-learn/scikit-learn
git merge FETCH_HEAD  # you may need to resolve conflicts and commit
git checkout FETCH_HEAD doc/whats_new/v0.21.rst
git commit -m "DOC Remove additions to v0.21 change log"
# show the additions to the changelog that need transferring to v0.22
git diff HEAD HEAD^ | grep '^+[^+]' | sed s/^.//

Sorry that this didn't make it into 0.21. We will work towards another release before the end of the year.

@amueller

amueller commented Aug 6, 2019

Copy link
Copy Markdown
Member

Can you please resolve the conflicts and update this changelog as @jnothman suggested?

@fferrin

fferrin commented Aug 6, 2019

Copy link
Copy Markdown
Contributor Author

Can you please resolve the conflicts and update this changelog as @jnothman suggested?

Yes! Sorry about the delay! I'll fix the conflicts before tomorrow.

@fferrin

fferrin commented Mar 18, 2020

Copy link
Copy Markdown
Contributor Author

@jnothman I have another question. It seems that I'm having problems with the doctests. Should I modify every place where FastICA without parameters is called in order to avoid the warnings? What do you think is the best approach in this case?

@jnothman

Copy link
Copy Markdown
Member

Should I modify every place where FastICA without parameters is called in order to avoid the warnings?

Perhaps.

Or perhaps we shouldn't raise the warning if, for that dataset, the result with unit variance will be the same as the result with whiten=True. We've done similar for scaling in SVMs I think.

@fferrin

fferrin commented Mar 20, 2020

Copy link
Copy Markdown
Contributor Author

Maybe is a good idea to warn the user anyway because he/she could think that FastICA with whiten=True produces an output with variance 1.0 when that's not the case and he/she was just lucky (particular input) to obtain that result.

I'll check SVM to see what it's doing anyway but I'd like to know if what I said makes sense to you.

@jnothman

Copy link
Copy Markdown
Member

Maybe is a good idea to warn the user anyway because he/she could think that FastICA with whiten=True produces an output with variance 1.0 when that's not the case and he/she was just lucky (particular input) to obtain that result.

The point is to warn them that the behaviour for that particular invocation (without specifying whiten explicitly, and with that dataset) will change in the future, so it's best to hide the warning if the behaviour is not changing.

@jnothman

Copy link
Copy Markdown
Member

Do you mind implementing that change, @fferrin, which will only raise the warning where the behaviour would change in the future?

@fferrin

fferrin commented Apr 22, 2020

Copy link
Copy Markdown
Contributor Author

Sorry @jnothman for making this fix longer than expected but I make one last consultation to make sure that I did not misunderstand you.

As I see there are 3 different situations, and I have doubt what to return and what warnings to raise in each one.

  1. FastICA():
    • Variance = 1?
    • Warning?
  2. FastICA(whitten=True):
    • Variance = 1?
    • Warning?
  3. FastICA(whitten='unit-variance'):
    • Variance = 1?
    • Warning?
  4. FastICA(whitten=False): I think this one is the same

@jnothman

Copy link
Copy Markdown
Member

How about:

  1. FastICA():
    • Warn "From version 0.25 whiten='unit-variance' by default" if variance != 1
    • Compute as in master
  2. FastICA(whiten=True):
    • Warn "From version 0.25 whiten=True should be specified as whiten='arbitrary-variance' (its current behaviour) or whiten='unit-variance'."
    • compute as in master
  3. FastICA(whiten='unit-variance'):
    • no warning
    • compute with unit variance
  4. FastICA(whiten='arbitrary-variance'):
    • no warning
    • compute as in master

@fferrin

fferrin commented Apr 23, 2020 •

Copy link
Copy Markdown
Contributor Author

Two more questions:

  1. I could find the option 'arbitrary-variance' in FastICA. Maybe it's a feature in development?

  2. Currently, default value for FastICA is True. In that case, is there a way to distinguish between FastICA() and FastICA(whitten=True)?

@jnothman

Copy link
Copy Markdown
Member
I could [not] find the option 'arbitrary-variance' in FastICA. Maybe it's a feature in development?

I'm suggesting that we create it in this PR.

Currently, default value for FastICA is True. In that case, is there a way to distinguish between FastICA() and FastICA(whitten=True)?

Indeed, we'd have whiten default to 'legacy' or 'warn' or some similar sentinel. We shouldn't document this as a valid value: we don't want anyone to enter it manually.

@cmarmo

cmarmo commented Aug 23, 2020

Copy link
Copy Markdown
Contributor

Hi @fferrin, thanks for your work! Are you still interested in working on this pull request? If so, do you mind fixing conflicts?

@fferrin

fferrin commented Sep 1, 2020

Copy link
Copy Markdown
Contributor Author

Hi @cmarmo ! Sorry for the delay. I'm a little bit busy now, so I'm not sure I'll be able to finish this right now, but I'd be glad to do this before the next release

@jnothman

jnothman commented Sep 1, 2020

Copy link
Copy Markdown
Member

We'd also be able to find someone to help complete the work, @fferrin. I suppose the concern is that if we try to close this release milestone over the next 6 weeks, we need the contribution to get two approvals (perhaps from volunteer developers) in that time.

@fferrin

fferrin commented Sep 13, 2020

Copy link
Copy Markdown
Contributor Author

@jnothman I'll be a bit busy this week but I'll try to continue with this work. Sorry about the delay. In case someone continue with this work, I can help, no problem with that 🙂

Base automatically changed from master to main January 22, 2021 10:50
@jjerphan

Copy link
Copy Markdown
Member

Hi @fferrin,

Are you interested in pursuing your PR? 🙂

@fferrin

fferrin commented Feb 18, 2021 •

Copy link
Copy Markdown
Contributor Author

I'd love to but not sure if I'll be able to do it. I'd invite someone else to continue with this, but I'll try to continue working on this during weekends

@cmarmo

cmarmo commented Jan 24, 2022

Copy link
Copy Markdown
Contributor

@jjerphan could you confirm that you took over and this PR can be closed? #13056 has already been closed. Thanks!

@cmarmo cmarmo added the Needs Decision - Close Requires decision for closing label Jan 24, 2022
@jjerphan

Copy link
Copy Markdown
Member

Thanks for the follow-up, @cmarmo.

This was superseeded by #19490.

@jjerphan jjerphan closed this Jan 24, 2022
@jjerphan jjerphan removed the Needs Decision - Close Requires decision for closing label Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug High Priority High priority issues and pull requests module:decomposition Stalled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastICA whitening problem (Bug)

8 participants