Skip to content

Instantly share code, notes, and snippets.

@crosstyan
Last active April 19, 2022 07:13
Show Gist options
  • Save crosstyan/9943c38bdb0a248830f32c828a65703e to your computer and use it in GitHub Desktop.
Save crosstyan/9943c38bdb0a248830f32c828a65703e to your computer and use it in GitHub Desktop.
;; in 1.0a4 no need of bracket
(import numpy :as np
pandas :as pd
random [random]
functools [reduce]
seaborn :as sns
matplotlib.pyplot :as plt
scipy.stats [norm]
os [listdir])
;; https://docs.hylang.org/en/alpha/tutorial.html?highlight=defn
;; 1.0a4 version remove &optional keyword. just pass a list
(defn gen-rnd-data [start [bin 1]] (+ start (* bin (random))))
;; maybe I shouldn't listify it. Keep it lazy.
(defn gen-n [start n [bin 1]] (map (fn [x] (gen-rnd-data start bin)) (range n)))
;; (x, times)
;; (setv data-tuple (zip data.x data.y))
(defn flat-map [f x] (reduce (fn [acc y] (+ acc (f y))) x []))
; TODO: https://peps.python.org/pep-0484/#function-method-overloading
(defn gen-n-by-tuple [tuple [bin 1]]
(let [x (get tuple 0)
y (get tuple 1)]
(gen-n x y bin)))
;; have to listify gen-fake-list because flat_map implimentation is not lazy
(defn gen-concat-fake-list [tuple [bin 1]] (flat-map (fn [x] (list (gen-n-by-tuple x bin))) tuple))
(defn get-norm-dist-fit [xs [pts 1000] [mul 5]]
(let [(, mu std) (norm.fit xs)
xmint (- mu (* mul std))
xmin (if (< xmint 0) 0 xmint)
xmax (+ mu (* mul std))
x-vals (np.linspace xmin xmax pts)
y-vals (norm.pdf x-vals mu std)]
(, x-vals y-vals)))
(defn get-data [data-from-csv [x-name "X"] [y-name "Y"]]
(let [xs (get data-from-csv x-name)
ys (get data-from-csv y-name)
bin (- (get xs 1) (get xs 0))]
(, xs ys bin)))
(setv csvfiles (list (filter (fn [x] (in ".csv" x)) (listdir))))
(setv many-data (list (map pd.read_csv csvfiles)))
;; (setv repack-data (map get-data many-data))
(setv xss (list (map (fn [data]
(let [(, xs ys bin) (get-data data)
tuple (, xs ys)]
(gen-concat-fake-list tuple bin))) many-data)))
(setv norm-dists (list (map (fn [x] (get-norm-dist-fit x)) xss)))
(sns.set_theme :style "white" :context "talk")
;; ;; https://stackoverflow.com/questions/64130332/seaborn-futurewarning-pass-the-following-variables-as-keyword-args-x-y
(for [xs norm-dists]
(let [(, x-vals y-vals) xs]
(sns.lineplot :x x-vals :y y-vals)))
(plt.xlabel "Numbers")
(plt.ylabel "Denisty")
(plt.show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment