Title
s

Sebastian Lehrbaum

04/20/2023, 5:40 PM
Hi, I just started using Kotlin Dataframes, notbooks and the lets plot plotting library. Mostly worked really well, but I did not find a good way to convert a map of data points to something the plotting library takes. I have Map<Int, Int>. Intuitively I thought maybe
mapOf("x" to map.keys, "y" to map.values)
however keys and values on maps are sets, they are not ordered so how would I know that the value pairs would stay in the correct order?
a

04/20/2023, 5:42 PM
can you share a simple example of the contents of the map before and after? it might help to look through the ‘associate’ function docs - they’re confusing but pretty powerful for transforming maps https://kotlinlang.org/docs/collection-transformations.html#associate
s

Sebastian Lehrbaum

04/20/2023, 5:46 PM
Say the initial map has these values: { 1: 10, 2: 15, 3: 20 }, then in order to plot this using lets-plot, by my understand I have to convert it to a map of this format: { "x": [1, 2, 3], "y": [10, 15, 20] } and this works, but the converting is complicated and does not feel natural at all. I assume there is some better way.
This is what I currently use for this conversion:
fun Map<Int, Int>.toPlotData(xTitle: String, yTitle: String): Map<String, List<Int>> {
val plotPoints = this.entries
.sortedBy { it.key }
.fold(Pair(mutableListOf<Int>(), mutableListOf<Int>())) { acc, (k, v) ->
acc.first += k
acc.second += v
acc
}
return mapOf(
xTitle to plotPoints.first,
yTitle to plotPoints.second
)
}
p

Pavel Gorgulov

04/20/2023, 5:50 PM
You can use dataframe and kandy library for visualization. So you can make two columns of coordinates initially.
s

Sebastian Lehrbaum

04/20/2023, 5:54 PM
I was thinking about dataframe, but there is no toDataFrame for Map<Int, Int> so I wasn't sure how to convert that. About Kandy, do I need to do something to use it?
p

Pavel Gorgulov

04/20/2023, 5:56 PM
yes, we just added it. so apparently it didn’t make it into the descriptors of your kernel version. Use this way to get the latest descriptors and versions:
%useLatestDescriptors
%use kandy
s

Sebastian Lehrbaum

04/20/2023, 6:01 PM
Thanks, I already like the dsl. Upon reading the documentation all the examples still expect me to have a Map<String, List<Int>> or DataFrame or separate lists. I don't think it can take a map of Datapoints but I will look further. If you have any fast approach to convert a map of Datapoints to a Dataframe I'd appreciate it. 🙂 Edit: Corrected list to map
p

Pavel Gorgulov

04/20/2023, 6:03 PM
Also note that usually LinkedHashMap is used in kotlin unless otherwise noted
s

Sebastian Lehrbaum

04/20/2023, 6:07 PM
Trying to follow the train of thought. If I remember correctly that one has a stable order.
map.keys
and
map.values
are Sets and since by default a set is backed by a hashmap, if that also uses a LinkedHashMap that means it should most likely be correctly ordered. Feels a bit weird to rely on the inner working like this, but I guess it will just work.
Sorry to bother you once more, is this a bug or do I need a newer version of something?
p

Pavel Gorgulov

04/20/2023, 6:22 PM
Try updating the kernel to the latest version. If you have it installed via pip:
pip install kotlin-jupyter-kernel==0.11.0.348
s

Sebastian Lehrbaum

04/20/2023, 6:23 PM
I installed the Intellij plugin
p

Pavel Gorgulov

04/20/2023, 6:23 PM
s

Sebastian Lehrbaum

04/20/2023, 6:25 PM
Thanks. I don't want to pressure anyone, I love that it is so easy to use via the plugin and I understand that it might take a few days to be updated 🙂 Looking forward to it!
i

Igor Alshannikov

04/20/2023, 6:37 PM
srcMap.map { (k, v) -> k to v }.unzip()
i

04/20/2023, 6:58 PM
2023.1.1-RC will be released in a few days, and plugin there will contain rather fresh version of kernel. You can obtain it in the Toolbox by turning EAP versions. Release of 2023.1.1 is planned to the end of the April
We will handle bug with type hints in the wrong place, thank you for sharing screenshot
h

Hampus Londögård

04/21/2023, 5:35 AM
Is the only reason for
kandy
to exist to have a multi-engine backend? Thinking it looks similar to
lets-plot
right?
i

04/21/2023, 9:19 AM
Kandy's DSL is much easier to use from my perspective of the person who's not much into plotting
h

Hampus Londögård

04/21/2023, 9:20 AM
I see
s

Sebastian Lehrbaum

04/21/2023, 11:31 AM
@Igor Alshannikov thanks that is indeed a nicer way of creating the two lists. Still it means I have to create a helper function from datapoints to plot data in my notebooks. Seems like a common function to have in one of the libraries. Maybe even in dataframes. Notably you could make
._map_ *{ it*._toPair_() *}*._unzip_()