(ns geni.cookbook-02
  (:require
   [clojure.java.io]
   [clojure.java.shell]
   [zero-one.geni.core :as g]
   [zero-one.geni.ml :as ml]))

(load-file "docs/cookbook/cookbook-util.clj")

;; Part 2: Selecting Rows and Columns

(def complaints-data-url
  "-cookbook/master/data/311-service-requests.csv")

(def complaints-data-path "data/cookbook/complaints.csv")

(download-data! complaints-data-url complaints-data-path)

(def raw-complaints
  (g/read-csv! complaints-data-path))

  2.1 What 's Even In It ?

(g/show raw-complaints)

(count (g/columns raw-complaints))

(g/count raw-complaints)

(g/print-schema raw-complaints)

(def complaints
  (g/read-csv! complaints-data-path {:kebab-columns true}))

(g/print-schema complaints)

   2.2 Selecting Columns and Rows

(-> complaints
    (g/select :complaint-type)
    g/show)

(-> complaints
    (g/limit 5)
    g/show)

(-> complaints
    (g/select :complaint-type)
    (g/limit 5)
    g/show)

(-> complaints
    (g/limit 5)
    (g/select :complaint-type)
    g/show)

   2.3 Selecting Multiple Columns

(-> complaints
    (g/select :complaint-type :borough)
    g/show)

(-> complaints
    (g/select :complaint-type :borough)
    (g/limit 10)
    g/show)

   2.4 What 's The Most Common Complaint Types ?

(-> complaints
    (g/group-by :complaint-type)
    g/count
    g/show)

(-> complaints
    (g/group-by :complaint-type)
    g/count
    (g/order-by (g/desc :count))
    (g/limit 10)
    g/show)

(-> complaints
    (g/select :complaint-type)
    g/value-counts
    (g/limit 10)
    g/show)

  2.5 Selecting Only Noise Complaints

(-> complaints
    (g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
    (g/select :complaint-type :borough :created-date :descriptor)
    (g/limit 3)
    g/show)

(-> complaints
    (g/filter (g/&&
               (g/= :complaint-type (g/lit "Noise - Street/Sidewalk"))
               (g/= :borough (g/lit "BROOKLYN"))))
    (g/select :complaint-type :borough :created-date :descriptor)
    (g/limit 3)
    g/show)

  2.6 Which Borough Has The Most Noise Complaints ?

(-> complaints
    (g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
    (g/select :borough)
    g/value-counts
    g/show)

 Part 2: Selecting Rows and Columns
(ns geni.cookbook-02
  (:require
   [clojure.java.io]
   [clojure.java.shell]
   [zero-one.geni.core :as g]
   [zero-one.geni.ml :as ml]))

(load-file "docs/cookbook/cookbook-util.clj")


(def complaints-data-url
  "-cookbook/master/data/311-service-requests.csv")

(def complaints-data-path "data/cookbook/complaints.csv")

(download-data! complaints-data-url complaints-data-path)

(def raw-complaints
  (g/read-csv! complaints-data-path))

  2.1 What 's Even In It ?

(g/show raw-complaints)

(count (g/columns raw-complaints))

(g/count raw-complaints)

(g/print-schema raw-complaints)

(def complaints
  (g/read-csv! complaints-data-path {:kebab-columns true}))

(g/print-schema complaints)

   2.2 Selecting Columns and Rows

(-> complaints
    (g/select :complaint-type)
    g/show)

(-> complaints
    (g/limit 5)
    g/show)

(-> complaints
    (g/select :complaint-type)
    (g/limit 5)
    g/show)

(-> complaints
    (g/limit 5)
    (g/select :complaint-type)
    g/show)

   2.3 Selecting Multiple Columns

(-> complaints
    (g/select :complaint-type :borough)
    g/show)

(-> complaints
    (g/select :complaint-type :borough)
    (g/limit 10)
    g/show)

   2.4 What 's The Most Common Complaint Types ?

(-> complaints
    (g/group-by :complaint-type)
    g/count
    g/show)

(-> complaints
    (g/group-by :complaint-type)
    g/count
    (g/order-by (g/desc :count))
    (g/limit 10)
    g/show)

(-> complaints
    (g/select :complaint-type)
    g/value-counts
    (g/limit 10)
    g/show)

  2.5 Selecting Only Noise Complaints

(-> complaints
    (g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
    (g/select :complaint-type :borough :created-date :descriptor)
    (g/limit 3)
    g/show)

(-> complaints
    (g/filter (g/&&
               (g/= :complaint-type (g/lit "Noise - Street/Sidewalk"))
               (g/= :borough (g/lit "BROOKLYN"))))
    (g/select :complaint-type :borough :created-date :descriptor)
    (g/limit 3)
    g/show)

  2.6 Which Borough Has The Most Noise Complaints ?

(-> complaints
    (g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
    (g/select :borough)
    g/value-counts
    g/show)
