Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ val WritersideStyle = DataFrameHtmlData(
function sendHeight() {
let totalHeight = 0;
document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));
Expand All @@ -46,6 +46,8 @@ function sendHeight() {
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});
Expand Down Expand Up @@ -99,11 +101,13 @@ function isElementVisible(el) {
function sendInitialHeight() {
let initialHeight = 0;
document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package org.jetbrains.kotlinx.dataframe.samples.api

import io.kotest.matchers.shouldBe
import org.jetbrains.kotlinx.dataframe.AnyFrame
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.DynamicDataFrameBuilder
import org.jetbrains.kotlinx.dataframe.api.Infer
Expand All @@ -25,13 +26,10 @@ import org.jetbrains.kotlinx.dataframe.api.toColumn
import org.jetbrains.kotlinx.dataframe.api.toColumnOf
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
import org.jetbrains.kotlinx.dataframe.api.value
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
import org.jetbrains.kotlinx.dataframe.explainer.TransformDataFrameExpressions
import org.jetbrains.kotlinx.dataframe.kind
import org.jetbrains.kotlinx.dataframe.type
import org.junit.Test
import java.io.File
import kotlin.reflect.typeOf
import kotlin.random.Random as KotlinRandom

class Create : TestBase() {

Expand Down Expand Up @@ -222,6 +220,70 @@ class Create : TestBase() {
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun createRandomDataFrame() {
// stable random + clean examples
@Suppress("LocalVariableName")
val Random = KotlinRandom(42)
fun <T> List<T>.random() = this.random(Random)
// SampleStart
val categories = listOf("Electronics", "Books", "Clothing")
// DataFrame with 4 columns and 7 rows
(0 until 7).toDataFrame {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe nice to show off ..< instead of until :) but up to you

"productId" from { "P${1000 + it}" }
"category" from { categories.random() }
"price" from { Random.nextDouble(10.0, 500.0) }
"inStock" from { Random.nextInt(0, 100) }
}
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun createNestedRandomDataFrame() {
// stable random + clean examples
@Suppress("LocalVariableName")
val Random = KotlinRandom(42)
fun <T> List<T>.random() = this.random(Random)
// SampleStart
val categories = listOf("Electronics", "Books", "Clothing")
// DataFrame with 5 columns and 7 rows
(0 until 7).toDataFrame {
"productId" from { "P${1000 + it}" }
"category" from { categories.random() }
"price" from { Random.nextDouble(10.0, 500.0) }

// Column Group
"manufacturer" {
"country" from { listOf("USA", "China", "Germany", "Japan").random() }
"yearEstablished" from { Random.nextInt(1950, 2020) }
}

// Frame Column
"reviews" from {
val reviewCount = Random.nextInt(0, 8)
(0 until reviewCount).toDataFrame {
val ratings: DataColumn<Int> = expr { Random.nextInt(1, 6) }
val comments = ratings.map {
when (it) {
5 -> listOf("Amazing quality!", "Best purchase ever!", "Highly recommend!", "Absolutely perfect!")
4 -> listOf("Great product!", "Very satisfied", "Good value for money", "Would buy again")
3 -> listOf("It's okay", "Does the job", "Average quality", "Neither good nor bad")
2 -> listOf("Could be better", "Disappointed", "Not what I expected", "Poor quality")
else -> listOf("Terrible!", "Not worth the price", "Complete waste of money", "Do not buy!")
}.random()
}

"author" from { "User${Random.nextInt(1000, 9999)}" }
ratings into "rating"
comments into "comment"
}
}
}
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun createDataFrameOfPairs() {
Expand Down Expand Up @@ -254,7 +316,11 @@ class Create : TestBase() {
fun createDataFrameWithFill() {
// SampleStart
// Multiplication table
dataFrameOf(1..10) { x -> (1..10).map { x * it } }
(1..10).toDataFrame {
(1..10).forEach { x ->
"$x" from { x * it }
}
}
// SampleEnd
}

Expand Down Expand Up @@ -330,10 +396,6 @@ class Create : TestBase() {

val df = persons.toDataFrame()
// SampleEnd
df.columnsCount() shouldBe 2
df.rowsCount() shouldBe 3
df["name"].type() shouldBe typeOf<String>()
df["age"].type() shouldBe typeOf<Int>()
}

@Test
Expand All @@ -353,11 +415,6 @@ class Create : TestBase() {

val df = students.toDataFrame(maxDepth = 1)
// SampleEnd
df.columnsCount() shouldBe 3
df.rowsCount() shouldBe 2
df["name"].kind shouldBe ColumnKind.Group
df["name"]["firstName"].type() shouldBe typeOf<String>()
df["scores"].kind shouldBe ColumnKind.Frame
}

@Test
Expand Down Expand Up @@ -392,12 +449,6 @@ class Create : TestBase() {
}
}
// SampleEnd
df.columnsCount() shouldBe 5
df.rowsCount() shouldBe 2
df["name"].kind shouldBe ColumnKind.Value
df["name"].type shouldBe typeOf<Name>()
df["scores"].kind shouldBe ColumnKind.Frame
df["summary"]["min score"].values() shouldBe listOf(3, 5)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -525,6 +525,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -578,11 +580,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -625,6 +625,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -678,11 +680,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -773,6 +773,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -826,11 +828,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -577,6 +577,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -630,11 +632,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -595,6 +595,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -648,11 +650,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@
function sendHeight() {
let totalHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
totalHeight += getElementHeight(element.querySelector(':scope > summary'));

Expand All @@ -513,6 +513,8 @@
}
} else if (element.tagName === 'BR') {
totalHeight += getElementHeight(element);
} else if (element.tagName === 'TABLE') {
totalHeight += getElementHeight(element);
}
});

Expand Down Expand Up @@ -566,11 +568,13 @@
function sendInitialHeight() {
let initialHeight = 0;

document.querySelectorAll('body > details, body > br').forEach(element => {
document.querySelectorAll('body > details, body > br, body > table').forEach(element => {
if (element.tagName === 'DETAILS') {
initialHeight += getElementHeight(element.querySelector(':scope > summary'));
} else if (element.tagName === 'BR') {
initialHeight += getElementHeight(element);
} else if (element.tagName === `TABLE`) {
initialHeight += getElementHeight(element);
}
});

Expand Down
Loading