|
1 | 1 | package org.jetbrains.kotlinx.dataframe.api
|
2 | 2 |
|
| 3 | +import kotlinx.datetime.Instant |
| 4 | +import kotlinx.datetime.LocalDate |
| 5 | +import kotlinx.datetime.LocalDateTime |
| 6 | +import kotlinx.datetime.LocalTime |
3 | 7 | import org.jetbrains.kotlinx.dataframe.AnyCol
|
4 | 8 | import org.jetbrains.kotlinx.dataframe.ColumnsSelector
|
5 | 9 | import org.jetbrains.kotlinx.dataframe.DataFrame
|
| 10 | +import org.jetbrains.kotlinx.dataframe.RowColumnExpression |
| 11 | +import org.jetbrains.kotlinx.dataframe.RowValueExpression |
6 | 12 | import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
|
7 | 13 | import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
|
8 | 14 | import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
|
| 15 | +import org.jetbrains.kotlinx.dataframe.dataTypes.IFRAME |
| 16 | +import org.jetbrains.kotlinx.dataframe.dataTypes.IMG |
| 17 | +import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls |
| 18 | +import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls.Convert |
| 19 | +import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarLink |
| 20 | +import org.jetbrains.kotlinx.dataframe.documentation.ExcludeFromSources |
| 21 | +import org.jetbrains.kotlinx.dataframe.documentation.Indent |
| 22 | +import org.jetbrains.kotlinx.dataframe.documentation.LineBreak |
| 23 | +import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns |
| 24 | +import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns.ColumnGroupsAndNestedColumnsMention |
9 | 25 | import org.jetbrains.kotlinx.dataframe.impl.api.corrImpl
|
| 26 | +import java.math.BigDecimal |
| 27 | +import java.math.BigInteger |
| 28 | +import java.net.URL |
10 | 29 | import kotlin.reflect.KProperty
|
| 30 | +import kotlin.reflect.KType |
11 | 31 | import kotlin.reflect.typeOf
|
12 | 32 |
|
| 33 | +/** |
| 34 | + * Calculates the correlation between values in the specified [columns\]. |
| 35 | + * |
| 36 | + * This function does not perform the calculation immediately. Instead, it selects a primary set of columns |
| 37 | + * and returns a [Corr] object, which serves as an intermediate step in the correlation analysis. |
| 38 | + * |
| 39 | + * The [Corr] object provides two methods to perform correlation computations: |
| 40 | + * - [with][Corr.with] — allows you to specify a second set of columns and computes correlations between |
| 41 | + * the initially selected columns and this second set. |
| 42 | + * - [withItself][Corr.withItself] — computes correlations within the initially selected columns. |
| 43 | + * |
| 44 | + * Each of these methods returns a [DataFrame] where rows correspond to one set of columns, columns to the other set, |
| 45 | + * and each cell contains the correlation coefficient between the corresponding pair of columns. |
| 46 | + * |
| 47 | + * If you need to compute correlations between all columns in a DataFrame, use [DataFrame.corr()][DataFrame.corr]. |
| 48 | + * |
| 49 | + * Check out [Grammar]. |
| 50 | + * |
| 51 | + * @include [SelectingColumns.ColumnGroupsAndNestedColumnsMention] |
| 52 | + * |
| 53 | + * See [Selecting Columns][ConvertSelectingOptions]. |
| 54 | + * |
| 55 | + * For more information: {@include [DocumentationUrls.Corr]} |
| 56 | + */ |
| 57 | +internal interface CorrDocs { |
| 58 | + |
| 59 | + /** |
| 60 | + * {@comment Version of [SelectingColumns] with correctly filled in examples} |
| 61 | + * @include [SelectingColumns] {@include [SetCorrOperationArg]} |
| 62 | + */ |
| 63 | + interface ConvertSelectingOptions |
| 64 | + |
| 65 | + /** |
| 66 | + * ## Corr Operation Grammar |
| 67 | + * {@include [LineBreak]} |
| 68 | + * {@include [DslGrammarLink]} |
| 69 | + * {@include [LineBreak]} |
| 70 | + * |
| 71 | + * **[`corr`][convert]**` { columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` }` |
| 72 | + * |
| 73 | + * {@include [Indent]} |
| 74 | + * __`.`__[**`with`**][Corr.with]` { columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` }` |
| 75 | + * |
| 76 | + * {@include [Indent]} |
| 77 | + *`| `__`.`__[**`withItself`**][Corr.withItself]`()` |
| 78 | + */ |
| 79 | + interface Grammar |
| 80 | +} |
| 81 | + |
| 82 | +/** {@set [SelectingColumns.OPERATION] [corr][corr]} */ |
| 83 | +@ExcludeFromSources |
| 84 | +private interface SetCorrOperationArg |
| 85 | + |
| 86 | +/** |
| 87 | + * {@include [CorrDocs]} |
| 88 | + * ### This Corr Overload |
| 89 | + */ |
| 90 | +@ExcludeFromSources |
| 91 | +private interface CommonCorrDocs |
| 92 | + |
13 | 93 | internal fun AnyCol.isSuitableForCorr() = isSubtypeOf<Number>() || type() == typeOf<Boolean>()
|
14 | 94 |
|
15 | 95 | // region DataFrame
|
16 | 96 |
|
| 97 | +/** |
| 98 | + * An intermediate class used in the [corr] operation. |
| 99 | + * |
| 100 | + * This class itself does not perform any computations — it is a transitional step |
| 101 | + * before specifying how to compute correlation. |
| 102 | + * It must be followed by one of the methods specifying correlation |
| 103 | + * computation to produce a new correlation [DataFrame]. |
| 104 | + * |
| 105 | + * Each of these methods returns a [DataFrame] where rows correspond to one set of columns, columns to the other set, |
| 106 | + * and each cell contains the correlation coefficient between the corresponding pair of columns. |
| 107 | + * |
| 108 | + * Use the following methods to perform the computation: |
| 109 | + * - [with { columnsSelector }][with] – selects a second set of columns and computes correlations between |
| 110 | + * the initially selected columns and this second set. |
| 111 | + * - [withItself()][withItself] - computes correlations within the initially selected columns. |
| 112 | + * |
| 113 | + * See [Grammar][CorrDocs.Grammar] for more details. |
| 114 | + */ |
17 | 115 | public data class Corr<T, C>(internal val df: DataFrame<T>, internal val columns: ColumnsSelector<T, C>)
|
18 | 116 |
|
19 | 117 | public fun <T> DataFrame<T>.corr(): DataFrame<T> = corr { colsAtAnyDepth { it.isSuitableForCorr() } }.withItself()
|
|
0 commit comments