-
Notifications
You must be signed in to change notification settings - Fork 63
DTW method realization #517
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
Open
EjenY-Poltavchiny
wants to merge
18
commits into
SciProgCentre:dev
Choose a base branch
from
mrFendel:ejeny_branch_
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b6b8ac7
Hello world!
EjenY-Poltavchiny e7b56e4
New file in example dir
EjenY-Poltavchiny 74a550e
Merge branch 'dev' into ejeny_branch_
EjenY-Poltavchiny 134f265
Merge branch 'dev' into ejeny_branch_
EjenY-Poltavchiny b8809d1
first DTW method realization
EjenY-Poltavchiny 55bcd20
first DTW method realization
EjenY-Poltavchiny 13da33e
Merge branch 'master' into ejeny_branch_
EjenY-Poltavchiny 455c9d1
Changed according to the notes
EjenY-Poltavchiny 1773b49
Changed tests
EjenY-Poltavchiny 11cbf7c
Deleted waste file.
EjenY-Poltavchiny d5c3aa5
Enum class and some code corrections.
EjenY-Poltavchiny 31be2b5
Comments fixed.
EjenY-Poltavchiny 39244eb
Remove extra enum class and data class. Fix docstrings and comments r…
lounres 91f5436
Merge branch 'dev' into ejeny_branch_
EjenY-Poltavchiny d20e532
Merge branch 'dev' into ejeny_branch_
lounres 4ba1a16
Merge branch 'dev' into ejeny_branch_
EjenY-Poltavchiny c6e1eb8
Fixed algorithm mistakes. There was "index out of bounds" situation.
EjenY-Poltavchiny 051a389
Test added, examples moved to folder "examples".
EjenY-Poltavchiny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
examples/src/main/kotlin/space/kscience/kmath/series/DTWTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright 2018-2023 KMath contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package space.kscience.kmath.series | ||
|
|
||
| import space.kscience.kmath.nd.* | ||
| import space.kscience.kmath.operations.algebra | ||
| import space.kscience.kmath.operations.bufferAlgebra | ||
| import space.kscience.kmath.structures.asBuffer | ||
|
|
||
| fun main() = with(Double.algebra.bufferAlgebra.seriesAlgebra()) { | ||
| val firstSequence: DoubleArray = doubleArrayOf(0.0, 2.0, 3.0, 1.0, 3.0, 0.1, 0.0, 1.0) | ||
| val secondSequence: DoubleArray = doubleArrayOf(1.0, 0.0, 3.0, 0.0, 0.0, 3.0, 2.0, 0.0, 2.0) | ||
|
|
||
| val seriesOne = firstSequence.asBuffer() | ||
| val seriesTwo = secondSequence.asBuffer() | ||
|
|
||
| val result = DoubleFieldOpsND.dynamicTimeWarping(seriesOne, seriesTwo) | ||
| println("Total penalty coefficient: ${result.totalCost}") | ||
| print("Alignment: ") | ||
| println(result.alignMatrix) | ||
| for ((i , j) in result.alignMatrix.indices) { | ||
| if (result.alignMatrix[i, j] > 0.0) { | ||
| print("[$i, $j] ") | ||
| } | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/DynamicTimeWarping.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright 2018-2023 KMath contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package space.kscience.kmath.series | ||
|
|
||
| import space.kscience.kmath.PerformancePitfall | ||
| import space.kscience.kmath.nd.* | ||
| import space.kscience.kmath.nd.DoubleBufferND | ||
| import space.kscience.kmath.nd.ShapeND | ||
| import space.kscience.kmath.structures.DoubleBuffer | ||
| import kotlin.math.abs | ||
|
|
||
|
|
||
| /** | ||
| * Stores a result of [dynamicTimeWarping]. The class contains: | ||
| * 1. [Total penalty cost][totalCost] for series alignment. | ||
| * 2. [Align matrix][alignMatrix] that describes which point of the first series matches to point of the other series. | ||
| */ | ||
| public data class DynamicTimeWarpingData( | ||
| val totalCost : Double = 0.0, | ||
| val alignMatrix : DoubleBufferND = DoubleFieldOpsND.structureND(ShapeND(0, 0)) { (_, _) -> 0.0} | ||
| ) | ||
|
|
||
| /** | ||
| * DTW method implementation. Returns alignment matrix for two series comparing and penalty for this alignment. | ||
| */ | ||
| @OptIn(PerformancePitfall::class) | ||
| public fun DoubleFieldOpsND.dynamicTimeWarping(series1 : DoubleBuffer, series2 : DoubleBuffer) : DynamicTimeWarpingData { | ||
| // Create a special matrix of costs alignment for the two series. | ||
| val costMatrix = structureND(ShapeND(series1.size, series2.size)) { (row, col) -> | ||
| abs(series1[row] - series2[col]) | ||
| } | ||
|
|
||
| // Initialise the cost matrix by formulas | ||
| // costMatrix[i, j] = euclideanNorm(series1(i), series2(j)) + | ||
| // min(costMatrix[i - 1, j], costMatrix[i, j - 1], costMatrix[i - 1, j - 1]). | ||
| for ((row, col) in costMatrix.indices) { | ||
| costMatrix[row, col] += when { | ||
| row == 0 && col == 0 -> continue | ||
| row == 0 -> costMatrix[row, col - 1] | ||
| col == 0 -> costMatrix[row - 1, col] | ||
| else -> minOf( | ||
| costMatrix[row, col - 1], | ||
| costMatrix[row - 1, col], | ||
| costMatrix[row - 1, col - 1] | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // alignMatrix contains non-zero values at position where two points from series matches | ||
| // Values are penalty for concatenation of current points. | ||
| val alignMatrix = structureND(ShapeND(series1.size, series2.size)) { _ -> 0.0} | ||
| var index1 = series1.size - 1 | ||
| var index2 = series2.size - 1 | ||
| var cost = 0.0 | ||
| var pathLength = 0 | ||
|
|
||
| alignMatrix[index1, index2] = costMatrix[index1, index2] | ||
| cost += costMatrix[index1, index2] | ||
| pathLength++ | ||
| while (index1 != 0 || index2 != 0) { | ||
| when { | ||
| index1 == 0 -> { | ||
| index2-- | ||
| } | ||
| index2 == 0 -> { | ||
| index1-- | ||
| } | ||
| costMatrix[index1, index2] == costMatrix[index1, index2 - 1] + abs(series1[index1] - series2[index2]) -> { | ||
| index2-- | ||
| } | ||
| costMatrix[index1, index2] == costMatrix[index1 - 1, index2] + abs(series1[index1] - series2[index2]) -> { | ||
| index1-- | ||
| } | ||
| costMatrix[index1, index2] == costMatrix[index1 - 1, index2 - 1] + abs(series1[index1] - series2[index2]) -> { | ||
| index1-- | ||
| index2-- | ||
| } | ||
| } | ||
| alignMatrix[index1, index2] = costMatrix[index1, index2] | ||
| cost += costMatrix[index1, index2] | ||
| pathLength++ | ||
| } | ||
| cost /= pathLength | ||
|
|
||
| return DynamicTimeWarpingData(cost, alignMatrix) | ||
| } | ||
|
|
||
|
|
||
57 changes: 57 additions & 0 deletions
57
kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/DTWTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2018-2023 KMath contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package space.kscience.kmath.series | ||
|
|
||
| import space.kscience.kmath.nd.* | ||
| import space.kscience.kmath.operations.DoubleField | ||
| import space.kscience.kmath.operations.algebra | ||
| import space.kscience.kmath.operations.bufferAlgebra | ||
| import space.kscience.kmath.structures.asBuffer | ||
| import space.kscience.kmath.structures.toDoubleBuffer | ||
| import kotlin.math.PI | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
|
|
||
| class DTWTest { | ||
|
|
||
| @Test | ||
| fun someData() { | ||
| val firstSequence: DoubleArray = doubleArrayOf(0.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 1.0) | ||
| val secondSequence: DoubleArray = doubleArrayOf(0.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 1.0) | ||
|
|
||
| val seriesOne = firstSequence.asBuffer() | ||
| val seriesTwo = secondSequence.asBuffer() | ||
|
|
||
| val result = DoubleFieldOpsND.dynamicTimeWarping(seriesOne, seriesTwo) | ||
| assertEquals(result.totalCost, 0.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun pathTest() = with(Double.algebra.bufferAlgebra.seriesAlgebra()) { | ||
| val s1 = series(10) { DoubleField.sin(2 * PI * it / 100)}.toDoubleBuffer() | ||
| val s2 = series(20) {sin(PI * it / 100)}.toDoubleBuffer() | ||
| val s3 = series(20) {sin(PI * it / 100 + 2 * PI)}.toDoubleBuffer() | ||
|
|
||
| val resS1S2 = DoubleFieldOpsND.dynamicTimeWarping(s1, s2).alignMatrix | ||
| var pathLengthS1S2 = 0 | ||
| for ((i,j) in resS1S2.indices) { | ||
| if (resS1S2[i, j] > 0.0) { | ||
| ++pathLengthS1S2 | ||
| } | ||
| } | ||
|
|
||
| val resS1S3 = DoubleFieldOpsND.dynamicTimeWarping(s1, s3).alignMatrix | ||
| var pathLengthS1S3 = 0 | ||
| for ((i,j) in resS1S3.indices) { | ||
| if (resS1S2[i, j] > 0.0) { | ||
| ++pathLengthS1S3 | ||
| } | ||
| } | ||
| assertEquals(pathLengthS1S3, pathLengthS1S2) | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.