Matrix-Profile icon indicating copy to clipboard operation
Matrix-Profile copied to clipboard

Issues when trying to update ND4J depenency to 1.0.0-M2.1

Open wolfig opened this issue 1 year ago • 1 comments

Hi Enzosos,

I am trying to use your library in a Project for tuning an particle ion source (similar to what is show here: Ion Source Optimization Using Bi-Objective Genetic and Matrix-Profile Algorithm). In the paper I used a Python implementation of matrix-profile, but not I want to move the logic to JAVA. One goal in this is to update the dependencies to np4j to version 1.0.0-M2.1, which unfortunately has breaking changes (introduced with nd4j 1.0.0-beta4).

One issue I am facing when running the unit tests with the updated dependency is that all calls of the type

INDArray.get(INDArrayIndex... indexes)

Need to be two dimensional now. This is not a general issue, but when changing the code in MatrixProfileCalculator::MPRunnable::run(), from

    @Override
    public void run() {
        INDArray distanceProfile      = distProfile.getDistanceProfile(timeSeriesA, timeSeriesB, index, window);
        INDArray distanceProfileIndex = distProfile.getDistanceProfileIndex(tsBLength, index, window);

        if (trivialMatch) {
            INDArrayIndex[] indices = new INDArrayIndex[] { NDArrayIndex.interval(
                            Math.max(0, index - window / 2),
                            Math.min(index + window / 2 + 1, tsBLength)) };
            distanceProfile.put(indices, Double.POSITIVE_INFINITY);
        }

        updateProfile(distanceProfile, distanceProfileIndex);
    }

to (NDArrayIndex.all() to be compatible to nd4j 1.0.0-M2.1)

    @Override
    public void run() {
        INDArray distanceProfile      = distProfile.getDistanceProfile(timeSeriesA, timeSeriesB, index, window);
        INDArray distanceProfileIndex = distProfile.getDistanceProfileIndex(tsBLength, index, window);

        if (trivialMatch) {
            INDArrayIndex[] indices = new INDArrayIndex[] { **NDArrayIndex.all(),** NDArrayIndex.interval(
                            Math.max(0, index - window / 2),
                            Math.min(index + window / 2 + 1, tsBLength)) };
            distanceProfile.put(indices, Double.POSITIVE_INFINITY);
        }

        updateProfile(distanceProfile, distanceProfileIndex);
    }

I get errors like

  java.lang.IllegalStateException: Indices are out of range: Cannot get interval index Interval(b=0,e=5,s=1) on array with size(1)=4. Array shape: [1, 4], indices: [all(), Interval(b=0,e=5,s=1)]

for all "testMatrixProfileSelfJoin*" test cases of Matrix profile test. The cause of this error is the fact that distanceProfile.put(...) (calling INDArray.get()) fails, because for the get(...) the IntervalIndex in ìndicesis larger than the distanceProfilearray. This again is caused, because the IntervalIndexis created using the size of tsB which is larger than distanceProfile.

One way to cure this is to change the code to in MatrixProfileCalculator to

    @Override
    public void run() {
        INDArray distanceProfile      = distProfile.getDistanceProfile(timeSeriesA, timeSeriesB, index, window);
        INDArray distanceProfileIndex = distProfile.getDistanceProfileIndex(tsBLength, index, window);

        if (trivialMatch) {
            INDArrayIndex[] indices = new INDArrayIndex[] { **NDArrayIndex.all(),** NDArrayIndex.interval(
                            Math.max(0, index - window / 2),
                            Math.min(index + window / 2 + 1, **distanceProfile.length()**)) };
            distanceProfile.put(indices, Double.POSITIVE_INFINITY);
        }

        updateProfile(distanceProfile, distanceProfileIndex);
    }

I am not sure if this is the right approach as it changes the logic of calculating Matrix-Profile. With this change, the tests do not throw errors any more, but I get assertion failures in tests Windows8, 2SawTeeth, 2Humps, ... ; Windows4, Windows5, StraightLine, Plateau become green.

Could you have a look into this?

wolfig avatar May 24 '23 07:05 wolfig