Skip to content

Commit

Permalink
Corr plot module (#220)
Browse files Browse the repository at this point in the history
* fillDiagonal in correlation utils

* corr.py module

* Removed format parameter

* Removed _get_format method

* _tooltip_spec without format parameter

* flip = True by default

* flip = True by default

* flip = True by default

* _set_diverging_palette renamed to _set_brewer_palette

* Using API in build

* Removed div arg

* layers creation deferred till build

* Legend change: Correlation -> Corr

* Add fill_scale

* tiles implemented via geom_tile

* Fixed tile implementation.

* Fix height calculation

* coord_cartesian when tiles layer is active

* tiles width / height = 0.99

* Additive expand 0.5

* Set fill_diagonal in StatProto

* Change parameter passing way and some defaults

* correlation matrix examples

* Fix typo in methods names

* Fix typo in demo

* Demo layout fix

* brewer palette legend fix

* brewer palette legend fix in demo

* brewer palette example update

* add scale_fill_identity for tiles na values
  • Loading branch information
IKrukov-HORIS authored Nov 23, 2020
1 parent e942dcf commit b5c93db
Show file tree
Hide file tree
Showing 8 changed files with 6,020 additions and 19 deletions.
5,505 changes: 5,505 additions & 0 deletions docs/examples/jupyter-notebooks-dev/correlation_matrix.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@

package jetbrains.datalore.plot.base.stat

import jetbrains.datalore.base.numberFormat.NumberFormat
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.StatContext
import jetbrains.datalore.plot.base.stat.CorrelationUtil.correlationMatrix
import jetbrains.datalore.plot.base.stat.math3.correlationPearson
import kotlin.math.abs


class CorrelationStat : BaseStat(DEF_MAPPING) {
var correlationMethod = DEF_CORRELATION_METHOD
var type = DEF_TYPE
var fillDiagonal = DEF_FILL_DIAGONAL

override fun apply(data: DataFrame, statCtx: StatContext, messageConsumer: (s: String) -> Unit): DataFrame {
if (correlationMethod != Method.PEARSON)
throw IllegalArgumentException(
"Unsupported correlation method: $correlationMethod (only pearson is currently available)"
)
require(correlationMethod == Method.PEARSON) {
"Unsupported correlation method: $correlationMethod (only Pearson is currently available)"
}


val cm = correlationMatrix(data, type, ::correlationPearson)
val vals = cm.getNumeric(Stats.CORR)
val abs: List<Double> = vals.map { abs(it!!) }
val cm = correlationMatrix(data, type, fillDiagonal, ::correlationPearson)
val values = cm.getNumeric(Stats.CORR)
val abs: List<Double?> = values.map { it?.let(::abs) }

return cm.builder().putNumeric(Stats.CORR_ABS, abs).build()
}
Expand Down Expand Up @@ -59,6 +58,7 @@ class CorrelationStat : BaseStat(DEF_MAPPING) {

private val DEF_CORRELATION_METHOD = Method.PEARSON
private val DEF_TYPE = Type.FULL

const val FILL_DIAGONAL = "fill_diagonal"
const val DEF_FILL_DIAGONAL = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ object CorrelationUtil {
fun correlationMatrix(
data: DataFrame,
type: CorrelationStat.Type,
fillDiagonal: Boolean,
corrfn: (DoubleArray, DoubleArray) -> Double
): DataFrame {
val numerics = data.variables().filter { isNumeric(data, it.name) }

val var1: ArrayList<String> = arrayListOf()
val var2: ArrayList<String> = arrayListOf()
val corr: ArrayList<Double> = arrayListOf()
val corr: ArrayList<Double?> = arrayListOf()

for ((i, vx) in numerics.withIndex()) {

var1.add(vx.label)
var2.add(vx.label)
corr.add(1.0)

if (fillDiagonal) {
corr.add(1.0)
} else {
corr.add(null)
}

val xs = data.getNumeric(vx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ class CorrelationTest {

val labels2series = mapOf("A" to a, "B" to b, "C" to c)

val cm = correlationMatrix(data, CorrelationStat.Type.FULL, ::correlationPearson)
val cm = correlationMatrix(data, CorrelationStat.Type.FULL, fillDiagonal = true, corrfn = ::correlationPearson)

assertEquals(cm.rowCount(), a.size.ipow(2).toInt())

val v1 : List<String> = cm[Stats.X] as List<String>
val v2 : List<String> = cm[Stats.Y] as List<String>
val cr : List<Double> = cm.getNumeric(Stats.CORR) as List<Double>
val v1: List<String> = cm[Stats.X] as List<String>
val v2: List<String> = cm[Stats.Y] as List<String>
val cr: List<Double> = cm.getNumeric(Stats.CORR) as List<Double>

for ((v12, corr) in v1.zip(v2).zip(cr)) {
val s1 = labels2series[v12.first]
val s2 = labels2series[v12.second]
assertNotNull(s1)
assertNotNull(s2)
assertEquals(corr, correlation( s1, s2, ::correlationPearson))
assertEquals(corr, correlation(s1, s2, ::correlationPearson))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ class StatProto {
}
}

stat.fillDiagonal = OptionsAccessor.over(options)
.getBoolean(CorrelationStat.FILL_DIAGONAL, def = CorrelationStat.DEF_FILL_DIAGONAL)

return stat
}

Expand Down
1 change: 1 addition & 0 deletions python-package/lets_plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .export import *
from .frontend_context import *
from .settings_utils import *
from .bistro import *

__all__ = (plot.__all__ +
bistro.__all__ +
Expand Down
5 changes: 3 additions & 2 deletions python-package/lets_plot/bistro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Use of this source code is governed by the MIT license that can be found in the LICENSE file.

from .im import *
from .corr import *

__all__ = im.__all__

__all__ = (im.__all__ +
corr.__all__)
Loading

0 comments on commit b5c93db

Please sign in to comment.