haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
5.97k stars 1.13k forks source link

ICA example request #730

Closed LeXing1105 closed 1 year ago

LeXing1105 commented 1 year ago

Describe the bug I am creating a simulated multiple-channel EEG signal using random numbers, which has an array size of 'double[32][1250]' in Java.

I want to use the ICA algorithm for computing the Independent Components for a quick test on Android Studio for smartphones. When I use the 'ICA' class, the results were all zeros, nothing comes out.

Expected behavior I would expect the algorithm can output 32 independent components, the same data size as the input.

Actual behavior However, the results are wrong!

Could you please help me with debugging or show me an example of how to use the code correctly? Your Java library is very good, the programming styles are easy to understand. Thank you very much if you could reply or help! Attached is my code.

Capture
LeXing1105 commented 1 year ago

double[][] ICA_results = new double[32][250*5];

ICA_results is an empty array, What I understand is: the ICA will return and store the ICs to the 'ICA_results' array

haifengl commented 1 year ago

Your code is wrong. ICA.fit is a static method and it returns the model. You should do something like

ICA model = ICA.fit(data, 32, ...);
double[][] ica_results = ice.components;
haifengl commented 1 year ago

See the example in https://github.com/haifengl/smile/blob/master/base/src/test/java/smile/ica/ICATest.java

haifengl commented 1 year ago

Note that you should build the master branch to use latest ICA. If it works as you expect, I will make a new release so that you can get it from maven.

LeXing1105 commented 1 year ago

@haifengl Sir, thank you very much for your reply and example! Now I have changed the code a bit as you said, but something is still wrong here, making the program crash! I am using 'smile-base-3.0.0', by using Gradle: implementation 'com.github.haifengl:smile-core:3.0.0'

(The library imported is shown on the left of the screenshot)

Capture
  1. I simply simulated data, 32 rows, 1250 columns, (32*1250); so the expected number of components was set as 32 too;
  2. I am not sure if I use the "public static ICA fit(double[][] data, int p, DifferentiableFunction contrast, double tol, int maxIter)" correctly? The 5 parameters;
  3. Is my code still wrong? Or there are some differences between Java IDE and Android Studio, the platform issues?
  4. My aim is just to calculate the ICA's processing time on a phone.

Do you have any ideas for this issue? Your help will be highly appreciated!

Best wishes, Le

haifengl commented 1 year ago

Can you share the error message and stack trace?

LeXing1105 commented 1 year ago

Thank you for coming back to me!

In my Android App, I click the button to execute the ICA algorithm, and then the App crashes, the error message and stack trace is attached here.

error message
haifengl commented 1 year ago

You should add following libraries

libraryDependencies ++= Seq(
      "org.bytedeco" % "javacpp"   % "1.5.8"        classifier "android-arm64"
      "org.bytedeco" % "openblas"  % "0.3.21-1.5.8" classifier "android-arm64"
    )
LeXing1105 commented 1 year ago

Hi Sir, I added the implementations to Gradle, like the attached screenshot, and then ran the program, but it still crashed. In Android, I cannot add libraries as you showed me, need to use gradle / maven.

Error message: Caused by: java.lang.UnsatisfiedLinkError: Could not find jniopenblas_nolapack in class, module, and library paths. What's the problem this time? Thank you for your help!

gradle error2
saudet commented 1 year ago

If you're building Android App Bundles (AAB files), the Android plugin doesn't support native libraries in JAR files. We can use the Platform Plugin from Gradle JavaCPP to work around that though: https://github.com/bytedeco/gradle-javacpp#the-platform-plugin

LeXing1105 commented 1 year ago

@saudet Perfect! problem solved! The program is working very well now, after adding the JavaCPP libraries as you said. Thank you very much for your help!

Also, thank you again for your help! @haifengl

Merry Christmas to both of you!!! :)