Skip to content

Instantly share code, notes, and snippets.

@mariorz
Last active March 18, 2021 18:26
Show Gist options
  • Save mariorz/8f7641081d258bed97870e9e399ee58d to your computer and use it in GitHub Desktop.
Save mariorz/8f7641081d258bed97870e9e399ee58d to your computer and use it in GitHub Desktop.
(ns folio-deceso.muni
(:require [clj-time.format :as f]
[clj-time.core :as t]
[clojure.data.csv :as csv]
[clojure.edn :as edn]
[clojure.java.io :as io]
[oz.core :as oz]
[semantic-csv.core :as sc]))
(def muni-count2020
(with-open [in-file (io/reader "resources/1202xsomSE53.csv")]
(->> (csv/read-csv in-file)
(sc/remove-comments)
(sc/mappify)
(group-by (fn [r] [(:ENTIDAD_REG r) (:MUNICIPIO_REG r)]))
(map (fn [[k v]] {k (count v)}))
(apply merge)
(sort-by (fn [[k v]] v))
(reverse)
(doall))))
(def muni-populations
(with-open [in-file (io/reader "resources/conjunto_de_datos_iter_00CSV20.csv")]
(->> (csv/read-csv in-file)
(sc/mappify)
(group-by (fn [r] [(:ENTIDAD r) (:MUN r)]))
(map (fn [[k v]] {k {:pop (parse-int (:POBTOT (first v)))
:mun (:NOM_MUN (first v))
:ent (:NOM_ENT (first v))}}))
(apply merge)
(doall))))
(def muni-count2019
(with-open [in-file (io/reader "resources/defunciones2019-inegi.csv")]
(->> (csv/read-csv in-file)
(sc/mappify)
(group-by (fn [r] [(subs (:ent_regis r) 0 (- (count (:ent_regis r)) 1))
(subs (:mun_regis r) 0 (- (count (:mun_regis r)) 1))]))
(map (fn [[k v]] {k (count v)}))
(apply merge)
(doall))))
(def muni-xss
(->> muni-count2020
(map (fn [[k v]]
(try (let [count2020 v
count2019 (get muni-count2019 k)
xss (- count2020 count2019)
muni-info (get muni-populations k)
population (:pop muni-info)]
{k (into muni-info {:2020 v
:2019 count2019
:xss xss
:pop population
:xss-pop (* 1000000.0 (/ xss population))})})
(catch Exception e (println (str "error:" k))))))
(apply merge)
(filter (fn [[k v]] (> (:pop v) 100000)))
(sort-by (fn [[k v]] (:xss-pop v)))
(reverse)
(doall)))
#_(clojure.pprint/print-table
[:ent :mun :pop :2019 :2020 :xss :xss-pop]
(take 50 (vals muni-xss)))
(comment
;; => (take 10 muni-xss)
([["09" "014"]
{:pop 434153,
:mun "Benito Juárez",
:ent "Ciudad de México",
:2020 28168,
:2019 10779,
:xss 17389,
:xss-pop 40052.70031532662}]
[["09" "015"]
{:pop 545884,
:mun "Cuauhtémoc",
:ent "Ciudad de México",
:2020 48689,
:2019 34831,
:xss 13858,
:xss-pop 25386.345817060028}]
[["09" "010"]
{:pop 759137,
:mun "Álvaro Obregón",
:ent "Ciudad de México",
:2020 26729,
:2019 16173,
:xss 10556,
:xss-pop 13905.263476816439}]
[["09" "005"]
{:pop 1173351,
:mun "Gustavo A. Madero",
:ent "Ciudad de México",
:2020 18896,
:2019 3977,
:xss 14919,
:xss-pop 12714.865372765691}]
[["15" "054"]
{:pop 242307,
:mun "Metepec",
:ent "México",
:2020 5144,
:2019 2530,
:xss 2614,
:xss-pop 10787.96733070031}]
[["19" "039"]
{:pop 1142994,
:mun "Monterrey",
:ent "Nuevo León",
:2020 36538,
:2019 26739,
:xss 9799,
:xss-pop 8573.098371470016}]
[["20" "067"]
{:pop 270955,
:mun "Oaxaca de Juárez",
:ent "Oaxaca",
:2020 5237,
:2019 3291,
:xss 1946,
:xss-pop 7182.004391873189}]
[["15" "099"]
{:pop 277562,
:mun "Texcoco",
:ent "México",
:2020 4389,
:2019 2571,
:xss 1818,
:xss-pop 6549.887952961862}]
[["15" "104"]
{:pop 672202,
:mun "Tlalnepantla de Baz",
:ent "México",
:2020 8849,
:2019 4676,
:xss 4173,
:xss-pop 6207.955346755886}]
[["09" "016"]
{:pop 414470,
:mun "Miguel Hidalgo",
:ent "Ciudad de México",
:2020 3200,
:2019 843,
:xss 2357,
:xss-pop 5686.780707892007}]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment