Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolanrensen committed May 29, 2024
1 parent d9c958b commit fe080c6
Show file tree
Hide file tree
Showing 76 changed files with 478 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public annotation class ColumnsSelectionDslMarker
*
*/
@ColumnsSelectionDslMarker
public interface ColumnsSelectionDsl<out T> : /* SingleColumn<DataRow<T>> */
public interface ColumnsSelectionDsl<out T> : // SingleColumn<DataRow<T>>
ColumnSelectionDsl<T>,

// first {}, firstCol()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,29 @@ internal interface DiffDocs
*/
internal interface DiffOrNullDocs

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
/**
* Calculates the difference between the results of a row expression computed on the current and previous DataRow.
*
* @return [firstRowValue] for the first row; difference between expression computed for current and previous row for the following rows
*/
public fun <T> DataRow<T>.diff(firstRowResult: Double, expression: RowExpression<T, Double>): Double = prev()?.let { p ->
expression(
this,
this,
) - expression(p, p)
} ?: firstRowResult

// required to resolve `diff(0) { intValue }`
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public fun <T> DataRow<T>.diff(firstRowResult: Double, expression: RowExpression<T, Double>): Double =
prev()?.let { p ->
expression(
this,
this,
) - expression(p, p)
} ?: firstRowResult

/**
* Calculates the difference between the results of a row expression computed on the current and previous DataRow.
*
* @return [firstRowValue] for the first row; difference between expression computed for current and previous row for the following rows
*/
// required to resolve `diff(0) { intValue }`
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public fun <T> DataRow<T>.diff(firstRowResult: Int, expression: RowExpression<T, Int>): Int = prev()?.let { p ->
expression(
this,
Expand All @@ -141,13 +142,13 @@ public fun <T> DataRow<T>.diff(firstRowResult: Long, expression: RowExpression<T
public fun <T> DataRow<T>.diff(firstRowResult: Float, expression: RowExpression<T, Float>): Float =
prev()?.let { p -> expression(this, this) - expression(p, p) } ?: firstRowResult

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
/**
* Calculates the difference between the results of a row expression computed on the current and previous DataRow.
*
* @return null for the first row; difference between expression computed for current and previous row for the following rows
*/
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public fun <T> DataRow<T>.diffOrNull(expression: RowExpression<T, Double>): Double? = prev()?.let { p ->
expression(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package org.jetbrains.kotlinx.dataframe.api

import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.AnyCol
import org.jetbrains.kotlinx.dataframe.AnyColumnReference
import org.jetbrains.kotlinx.dataframe.AnyFrame
import org.jetbrains.kotlinx.dataframe.AnyRow
import org.jetbrains.kotlinx.dataframe.ColumnsSelector
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.Update.UpdateOperationArg
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.documentation.*
import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
import org.jetbrains.kotlinx.dataframe.documentation.ExcludeFromSources
import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
import org.jetbrains.kotlinx.dataframe.documentation.NA
import org.jetbrains.kotlinx.dataframe.documentation.NaN
import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
import org.jetbrains.kotlinx.dataframe.get
import org.jetbrains.kotlinx.dataframe.typeClass
import kotlin.reflect.KProperty

// region fillNulls
Expand Down Expand Up @@ -299,7 +312,7 @@ internal inline val Any?.isNA: Boolean

internal inline val AnyCol.canHaveNaN: Boolean get() = typeClass.let { it == Double::class || it == Float::class }

internal inline val AnyCol.canHaveNA: Boolean get() = hasNulls() || canHaveNaN || kind != ColumnKind.Value
internal inline val AnyCol.canHaveNA: Boolean get() = hasNulls() || canHaveNaN || kind() != ColumnKind.Value

internal inline val Double?.isNA: Boolean get() = this == null || this.isNaN()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import org.jetbrains.kotlinx.dataframe.exceptions.DuplicateColumnNamesException
import org.jetbrains.kotlinx.dataframe.exceptions.UnequalColumnSizesException
import org.jetbrains.kotlinx.dataframe.impl.ColumnNameGenerator
import org.jetbrains.kotlinx.dataframe.impl.DataFrameImpl
import org.jetbrains.kotlinx.dataframe.impl.UNNAMED_COLUMN_PREFIX
import org.jetbrains.kotlinx.dataframe.impl.asList
import org.jetbrains.kotlinx.dataframe.impl.columnName
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnAccessorImpl
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumn
import org.jetbrains.kotlinx.dataframe.impl.columns.createComputedColumnReference
import org.jetbrains.kotlinx.dataframe.impl.columns.forceResolve
import org.jetbrains.kotlinx.dataframe.impl.columns.unbox
import org.jetbrains.kotlinx.dataframe.impl.unnamedColumnPrefix
import org.jetbrains.kotlinx.dataframe.size
import kotlin.random.Random
import kotlin.random.nextInt
Expand Down Expand Up @@ -401,7 +401,7 @@ public class DynamicDataFrameBuilder {

public fun add(col: AnyCol): String {
val uniqueName = if (col.name().isEmpty()) {
generator.addUnique(unnamedColumnPrefix)
generator.addUnique(UNNAMED_COLUMN_PREFIX)
} else {
generator.addUnique(col.name())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jetbrains.kotlinx.dataframe.api

import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.AnyRow
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.DataRow
import org.jetbrains.kotlinx.dataframe.impl.api.convertTo
import org.jetbrains.kotlinx.dataframe.impl.columnName
import org.jetbrains.kotlinx.dataframe.impl.owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import org.jetbrains.kotlinx.jupyter.api.VariableName
public data class CodeWithConverter(val declarations: Code, val converter: (VariableName) -> Code = EmptyConverter) {

public companion object {
public const val EmptyDeclarations: Code = ""
public const val EMPTY_DECLARATIONS: Code = ""
public val EmptyConverter: (VariableName) -> Code = { it }
public val Empty: CodeWithConverter = CodeWithConverter(EmptyDeclarations, EmptyConverter)
public val Empty: CodeWithConverter = CodeWithConverter(EMPTY_DECLARATIONS, EmptyConverter)
}

val hasDeclarations: Boolean get() = declarations.isNotBlank()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package org.jetbrains.kotlinx.dataframe.columns
import org.jetbrains.kotlinx.dataframe.ColumnsSelector
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.impl.columns.*
import org.jetbrains.kotlinx.dataframe.impl.columns.TransformableColumnSet
import org.jetbrains.kotlinx.dataframe.impl.columns.TransformableSingleColumn
import org.jetbrains.kotlinx.dataframe.impl.columns.transform

/**
* ## ColumnsResolver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.jetbrains.kotlinx.dataframe.documentation

import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.api.count
import org.jetbrains.kotlinx.dataframe.api.mean
import org.jetbrains.kotlinx.dataframe.documentation.ExpressionsGivenColumn.ColumnExpressionLink
import org.jetbrains.kotlinx.dataframe.ColumnExpression as DfColumnExpression

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetbrains.kotlinx.dataframe.documentation

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.api.select
import org.jetbrains.kotlinx.dataframe.documentation.ExpressionsGivenDataFrame.DataFrameExpressionLink
import org.jetbrains.kotlinx.dataframe.DataFrameExpression as DfDataFrameExpression

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetbrains.kotlinx.dataframe.documentation

import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.api.mean
import org.jetbrains.kotlinx.dataframe.documentation.ExpressionsGivenRowAndColumn.RowColumnExpressionLink
import org.jetbrains.kotlinx.dataframe.RowColumnExpression as DfRowColumnExpression

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package org.jetbrains.kotlinx.dataframe.documentation

import org.jetbrains.kotlinx.dataframe.RowFilter
import org.jetbrains.kotlinx.dataframe.RowValueFilter
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl
import org.jetbrains.kotlinx.dataframe.api.count
import org.jetbrains.kotlinx.dataframe.api.diff
import org.jetbrains.kotlinx.dataframe.api.drop
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.api.first
import org.jetbrains.kotlinx.dataframe.api.format
import org.jetbrains.kotlinx.dataframe.api.gather
import org.jetbrains.kotlinx.dataframe.api.update
import org.jetbrains.kotlinx.dataframe.documentation.SelectingRows.RowConditionLink
import org.jetbrains.kotlinx.dataframe.documentation.SelectingRows.RowValueConditionLink
import org.jetbrains.kotlinx.dataframe.index
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.kotlinx.dataframe.exceptions

import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
import kotlin.reflect.*
import kotlin.reflect.KType

public open class TypeConversionException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.kotlinx.dataframe.exceptions

import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
import kotlin.reflect.*
import kotlin.reflect.KType

public class TypeConverterNotFoundException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class UnequalColumnSizesException(
) : IllegalArgumentException() {

override val message: String
get() = "Unequal column sizes. Expected rows count: $expectedRowsCount. Actual column sizes:\n${columnSizes.joinToString(
"\n",
) { it.first + ": " + it.second }}"
get() = "Unequal column sizes. " +
"Expected rows count: $expectedRowsCount. " +
"Actual column sizes:\n${columnSizes.joinToString("\n") { it.first + ": " + it.second }}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ internal class TypedColumnDataCollector<T>(initCapacity: Int = 0, val type: KTyp
override fun add(value: T?) {
if (checkTypes && value != null && !value.javaClass.kotlin.isSubclassOf(kclass)) {
throw IllegalArgumentException(
"Can not add value of class ${value.javaClass.kotlin.qualifiedName} to column of type $type. Value = $value",
"Can not add value of class ${value.javaClass.kotlin.qualifiedName} to column of type $type. " +
"Value = $value",
)
}
super.add(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.resolveSingle
import org.jetbrains.kotlinx.dataframe.io.renderToString
import kotlin.reflect.KProperty

internal const val unnamedColumnPrefix = "untitled"
internal const val UNNAMED_COLUMN_PREFIX = "untitled"

internal open class DataFrameImpl<T>(cols: List<AnyCol>, val nrow: Int) :
DataFrame<T>,
Expand Down Expand Up @@ -65,7 +65,7 @@ internal open class DataFrameImpl<T>(cols: List<AnyCol>, val nrow: Int) :
columns = cols.mapIndexed { i, col ->
val name = col.name
if (name.isEmpty()) {
val uniqueName = nameGenerator.addUnique(unnamedColumnPrefix)
val uniqueName = nameGenerator.addUnique(UNNAMED_COLUMN_PREFIX)
val renamed = col.rename(uniqueName)
columnsMap[uniqueName] = i
renamed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetbrains.kotlinx.dataframe.impl

import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.io.defaultPrecision
import org.jetbrains.kotlinx.dataframe.io.DEFAULT_PRECISION
import org.jetbrains.kotlinx.dataframe.typeClass
import java.math.BigDecimal

Expand All @@ -13,7 +13,7 @@ internal fun <T : Number> DataColumn<T?>.scale(): Int {
BigDecimal::class -> values().maxOf { (it as? BigDecimal)?.scale() ?: 1 }
Number::class -> values().maxOf { (it as? Number)?.scale() ?: 0 }
else -> 0
}.coerceAtMost(defaultPrecision)
}.coerceAtMost(DEFAULT_PRECISION)
}

internal fun Double.scale() = if (isFinite()) toBigDecimal().scale() else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
import kotlin.reflect.KTypeProjection
import kotlin.reflect.KVariance
import kotlin.reflect.KVariance.*
import kotlin.reflect.KVariance.IN
import kotlin.reflect.KVariance.INVARIANT
import kotlin.reflect.KVariance.OUT
import kotlin.reflect.KVisibility
import kotlin.reflect.full.allSuperclasses
import kotlin.reflect.full.createType
Expand Down Expand Up @@ -214,7 +216,8 @@ internal fun commonParents(classes: Iterable<KClass<*>>): List<KClass<*>> = when
.filterNot { it == Nothing::class } // Nothing is a subtype of everything
.let {
when {
it.size == 1 && it[0].visibility == KVisibility.PUBLIC -> { // if there is only one class - return it
// if there is only one class - return it
it.size == 1 && it[0].visibility == KVisibility.PUBLIC -> {
listOf(it[0])
}

Expand Down Expand Up @@ -440,7 +443,8 @@ internal fun guessValueType(values: Sequence<Any?>, upperBound: KType? = null, l
if (listifyValues) {
val typeInLists = classesInCollection.commonType(
nullable = nullsInCollection || allListsAreEmpty,
upperBound = nothingType(nullable = false), // for when the list is empty, make it Nothing instead of Any?
// for when the list is empty, make it Nothing instead of Any?
upperBound = nothingType(nullable = false),
)
val typeOfOthers = classes.commonType(nullable = nullsInCollection, upperBound = upperBound)
val commonType = listOf(typeInLists, typeOfOthers).commonTypeListifyValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ internal fun convertToDataFrame(
} else {
val value = try {
val value = it.call(obj)
/**
/*
* here we do what compiler does
* @see org.jetbrains.kotlinx.dataframe.api.CreateDataFrameTests.testKPropertyGetLibrary
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import org.jetbrains.kotlinx.dataframe.impl.schema.DataFrameSchemaImpl
import org.jetbrains.kotlinx.dataframe.impl.schema.extractSchema
import org.jetbrains.kotlinx.dataframe.impl.schema.intersectSchemas
import org.jetbrains.kotlinx.dataframe.impl.splitByIndices
import org.jetbrains.kotlinx.dataframe.io.ARRAY_COLUMN_NAME
import org.jetbrains.kotlinx.dataframe.io.JSON.TypeClashTactic
import org.jetbrains.kotlinx.dataframe.io.JSON.TypeClashTactic.ANY_COLUMNS
import org.jetbrains.kotlinx.dataframe.io.JSON.TypeClashTactic.ARRAY_AND_VALUE_COLUMNS
import org.jetbrains.kotlinx.dataframe.io.arrayColumnName
import org.jetbrains.kotlinx.dataframe.io.valueColumnName
import org.jetbrains.kotlinx.dataframe.io.VALUE_COLUMN_NAME
import org.jetbrains.kotlinx.dataframe.ncol
import org.jetbrains.kotlinx.dataframe.nrow
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
Expand Down Expand Up @@ -207,7 +207,7 @@ internal fun fromJsonListAnyColumns(
else -> collector.add(v)
}
}
val column = collector.toColumn(valueColumnName)
val column = collector.toColumn(VALUE_COLUMN_NAME)
val res = if (nanIndices.isNotEmpty()) {
fun <C> DataColumn<C>.updateNaNs(nanValue: C): DataColumn<C> {
var j = 0
Expand Down Expand Up @@ -258,14 +258,14 @@ internal fun fromJsonListAnyColumns(
val elementType = col.type
val values = col.values.asList().splitByIndices(startIndices.asSequence()).toList()
DataColumn.createValueColumn(
name = arrayColumnName,
name = ARRAY_COLUMN_NAME,
values = values,
type = List::class.createType(listOf(KTypeProjection.invariant(elementType))),
)
}

else -> DataColumn.createFrameColumn(
name = arrayColumnName, // will be erased
name = ARRAY_COLUMN_NAME, // will be erased
df = parsed.unwrapUnnamedColumns(),
startIndices = startIndices,
)
Expand Down Expand Up @@ -324,7 +324,7 @@ internal fun fromJsonListAnyColumns(
listOf(
UnnamedColumn(
DataColumn.createFrameColumn(
name = valueColumnName, // will be erased unless at top-level
name = VALUE_COLUMN_NAME, // will be erased unless at top-level
groups = dataFrames,
schema = lazy {
DataFrameSchemaImpl(
Expand Down Expand Up @@ -437,14 +437,14 @@ internal fun fromJsonListArrayAndValueColumns(

// Add a value column to the collected names if needed
val valueColumn = if (hasPrimitive || records.isEmpty()) {
nameGenerator.addUnique(valueColumnName)
nameGenerator.addUnique(VALUE_COLUMN_NAME)
} else {
null
}

// Add an array column to the collected names if needed
val arrayColumn = if (hasArray) {
nameGenerator.addUnique(arrayColumnName)
nameGenerator.addUnique(ARRAY_COLUMN_NAME)
} else {
null
}
Expand Down Expand Up @@ -496,7 +496,7 @@ internal fun fromJsonListArrayAndValueColumns(
listOf(
UnnamedColumn(
DataColumn.createFrameColumn(
name = valueColumnName, // will be erased unless at top-level
name = VALUE_COLUMN_NAME, // will be erased unless at top-level
groups = dataFrames,
schema = lazy {
dataFrames.mapNotNull { it.takeIf { it.nrow > 0 }?.schema() }.intersectSchemas()
Expand Down
Loading

0 comments on commit fe080c6

Please sign in to comment.