Quickstart¶

Open in Colab

If you run this notebook in google colab, you should not need to install anything else. You might, however, need to restart your session to have the required versions of matplotlib, numpy, Plotly ready.

In [1]:
#in case you are in colab
import sys
IN_COLAB = "google.colab" in sys.modules
if IN_COLAB:
    !pip3 install git+https://github.com/TheRedElement/LStein.git
In [2]:
#%%imports
import importlib
import numpy as np
import polars as pl

from lstein import (
    utils as lsu,
    makedata as md
)
from lstein import lstein
importlib.reload(lstein)
Out[2]:
<module 'lstein.lstein' from '/home/lukas/github/LStein/src/lstein/lstein.py'>
In [3]:
#generate some data
raw, pro = md.simulate(5, opt="sin")
df_raw = pl.from_dict(raw).sample(2000).sort("period", "time") #subsample to get random sequence lengths 
df_pro = pl.from_dict(pro).sample(1800).sort("period", "time") #subsample to get random sequence lengths
print(df_raw)
shape: (2_000, 5)
┌────────┬────────────┬───────────┬─────────────┬────────────┐
│ period ┆ time       ┆ amplitude ┆ amplitude_e ┆ processing │
│ ---    ┆ ---        ┆ ---       ┆ ---         ┆ ---        │
│ f64    ┆ f64        ┆ f64       ┆ f64         ┆ str        │
╞════════╪════════════╪═══════════╪═════════════╪════════════╡
│ 13.0   ┆ -49.699399 ┆ 1.535369  ┆ NaN         ┆ raw        │
│ 13.0   ┆ -49.398798 ┆ 1.078042  ┆ NaN         ┆ raw        │
│ 13.0   ┆ -48.496994 ┆ 1.029387  ┆ NaN         ┆ raw        │
│ 13.0   ┆ -48.196393 ┆ 1.057772  ┆ NaN         ┆ raw        │
│ 13.0   ┆ -48.196393 ┆ 0.716052  ┆ NaN         ┆ raw        │
│ …      ┆ …          ┆ …         ┆ …           ┆ …          │
│ 39.8   ┆ 98.196393  ┆ 0.465662  ┆ NaN         ┆ raw        │
│ 39.8   ┆ 98.196393  ┆ -0.632033 ┆ NaN         ┆ raw        │
│ 39.8   ┆ 98.496994  ┆ 0.340356  ┆ NaN         ┆ raw        │
│ 39.8   ┆ 98.797595  ┆ -0.2584   ┆ NaN         ┆ raw        │
│ 39.8   ┆ 99.398798  ┆ -0.095092 ┆ NaN         ┆ raw        │
└────────┴────────────┴───────────┴─────────────┴────────────┘
In [4]:
#get dimensions
##raw
df_raw_p = df_raw.partition_by("period")    #partition by unique values in `"period"`
theta_raw = [df["period"].to_numpy()[0] for df in df_raw_p]    #only one value per series
x_raw = [df["time"].to_numpy() for df in df_raw_p]             #K values per series
y_raw = [df["amplitude"].to_numpy() for df in df_raw_p]        #K values per series

print(f"{len(theta_raw)=}")
print(f"{len(x_raw)=}, Series Lengths: {list(map(len, x_raw))}")
print(f"{len(y_raw)=}, Series Lengths: {list(map(len, y_raw))}")

##processed
df_pro_p = df_pro.partition_by("period")    #partition by unique values in `"period"`
theta_pro = [df["period"].to_numpy()[0] for df in df_pro_p]    #only one value per series
x_pro = [df["time"].to_numpy() for df in df_pro_p]             #K values per series
y_pro = [df["amplitude"].to_numpy() for df in df_pro_p]        #K values per series

print(f"{len(theta_pro)=}")
print(f"{len(x_pro)=}, Series Lengths: {list(map(len, x_pro))}")
print(f"{len(y_pro)=}, Series Lengths: {list(map(len, y_pro))}")
len(theta_raw)=5
len(x_raw)=5, Series Lengths: [415, 398, 405, 370, 412]
len(y_raw)=5, Series Lengths: [415, 398, 405, 370, 412]
len(theta_pro)=5
len(x_pro)=5, Series Lengths: [380, 343, 354, 350, 373]
len(y_pro)=5, Series Lengths: [380, 343, 354, 350, 373]
In [5]:
#define ticks
thetaticks = np.linspace(5, 40, 5).astype(int)
xticks = np.linspace(-45, 80, 6).astype(int)
yticks = np.linspace(-2,2,5).astype(int)
In [6]:
#create a canvas
LSC = lstein.LSteinCanvas(
    thetaticks=thetaticks, xticks=xticks, yticks=yticks
)

Standard Usage¶

In [7]:
#get colors for colormapping `theta`
colors = lsu.get_colors(theta_raw, "viridis")

#add different panels
for i in range(len(theta_raw)):
    
    #add a panel (similar to `ax = fig.add_subplot()`)
    LSP = LSC.add_panel(
        theta=theta_raw[i], #add panel at `theta_raw[i]`
    )

    #add a series to the panel
    LSP.plot(x_raw[i], y_raw[i], seriestype="scatter", c=colors[i])
    LSP.plot(x_pro[i], y_pro[i], seriestype="line", c="w", lw=3)
    LSP.plot(x_pro[i], y_pro[i], seriestype="line", color=colors[i])
In [8]:
#display the plot
fig = lstein.draw(LSC, figsize=(9,9))
fig.show()
/tmp/ipykernel_38677/1636926589.py:3: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown
  fig.show()
No description has been provided for this image

Convenience Usage¶

In [9]:
#add data to plot (automatically generates panels for you)
LSC.plot(theta_raw, x_raw, y_raw, seriestype="scatter")
LSC.plot(theta_pro, x_pro, y_pro, seriestype="line", series_kwargs=dict(c="w", lw=3))   #white outline to make discernable from scatter
LSC.plot(theta_pro, x_pro, y_pro, seriestype="line")
In [10]:
#display the plot
fig = lstein.draw(LSC, figsize=(9,9))
fig.show()
/tmp/ipykernel_38677/1636926589.py:3: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown
  fig.show()
No description has been provided for this image