Hi, I just started using Kotlin Dataframes, notboo...
# datascience
s
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
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
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:
Copy code
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
You can use dataframe and kandy library for visualization. So you can make two columns of coordinates initially.
s
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
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:
Copy code
%useLatestDescriptors
%use kandy
s
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
Also note that usually LinkedHashMap is used in kotlin unless otherwise noted
s
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
Try updating the kernel to the latest version. If you have it installed via pip:
Copy code
pip install kotlin-jupyter-kernel==0.11.0.348
s
I installed the Intellij plugin
p
сс @Ilya Muradyan
s
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
How about
Copy code
srcMap.map { (k, v) -> k to v }.unzip()
i
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
Is the only reason for
kandy
to exist to have a multi-engine backend? Thinking it looks similar to
lets-plot
right?
i
Kandy's DSL is much easier to use from my perspective of the person who's not much into plotting
h
I see
s
@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_()
204 Views