Skip to content

Commit

Permalink
added missing source file
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Oct 16, 2024
1 parent 9ff7f03 commit e0ddcd2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/main/kotlin/kravis/util/DeparseBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kravis.util

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.Infer
import org.jetbrains.kotlinx.dataframe.api.toColumn
import org.jetbrains.kotlinx.dataframe.api.toDataFrame

// todo javadoc example needed
/** Create a data-frame from a list of objects */
fun <T> Iterable<T>.deparseRecords(mapping: (T) -> DataFrameRow) = DataFrame.fromRecords(this, mapping)

internal typealias DeparseFormula<T> = T.(T) -> Any?

inline fun <reified T> Iterable<T>.deparseRecords(vararg mapping: Pair<String, DeparseFormula<T>>): DataFrame<*> {
// val revMapping = mapping.toMap().entries.associateBy({ it.value }) { it.key }
val mappings = mapOf<String, Any?>().toMutableMap().apply { putAll(mapping) }

val function = { record: T ->
mapping.toMap().map { (name, deparse) -> name to deparse(record, record) }.toMap() as DataFrameRow
}
return DataFrame.fromRecords(this, function)
}


infix fun <T> String.with(that: DeparseFormula<T>) = Pair(this, that)

typealias DataFrameRow = Map<String, Any?>

/** Create a data-frame from a list of objects */
fun <T> DataFrame.Companion.fromRecords(records: Iterable<T>, mapping: (T) -> DataFrameRow): DataFrame<*> {
val rowData = records.map { mapping(it) }
val columnNames = mapping(records.first()).keys

val columnData = columnNames.map { it to emptyList<Any?>().toMutableList() }.toMap()

for(record in rowData) {
columnData.forEach { colName, colData -> colData.add(record[colName]) }
}

val map = columnData.map { (name, data) -> data.toColumn("data", Infer.Type) }
return map.toDataFrame()
}


0 comments on commit e0ddcd2

Please sign in to comment.