Commit c1f1e207 authored by Maximilian Dolling's avatar Maximilian Dolling

added pipenv

parent b04778b5
stages:
- test
test:pylint:
stage: test
allow_failure: true
image: python:3
before_script:
- pip install pylint
script:
- find . -iname '*.py' -exec pylint {} +
only:
changes:
- "**/*.py"
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pandas = "1.0.5"
matplotlib = "3.2.2"
[requires]
python_version = "3.8"
{
"_meta": {
"hash": {
"sha256": "245d0d843fd006dc922fae6c955d2e18f895e19ca6d2ff7c13d35f2d70da84a6"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.8"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"cycler": {
"hashes": [
"sha256:1d8a5ae1ff6c5cf9b93e8811e581232ad8920aeec647c37316ceac982b08cb2d",
"sha256:cd7b2d1018258d7247a71425e9f26463dfb444d411c39569972f4ce586b0c9d8"
],
"version": "==0.10.0"
},
"kiwisolver": {
"hashes": [
"sha256:03662cbd3e6729f341a97dd2690b271e51a67a68322affab12a5b011344b973c",
"sha256:18d749f3e56c0480dccd1714230da0f328e6e4accf188dd4e6884bdd06bf02dd",
"sha256:247800260cd38160c362d211dcaf4ed0f7816afb5efe56544748b21d6ad6d17f",
"sha256:38d05c9ecb24eee1246391820ed7137ac42a50209c203c908154782fced90e44",
"sha256:443c2320520eda0a5b930b2725b26f6175ca4453c61f739fef7a5847bd262f74",
"sha256:4eadb361baf3069f278b055e3bb53fa189cea2fd02cb2c353b7a99ebb4477ef1",
"sha256:556da0a5f60f6486ec4969abbc1dd83cf9b5c2deadc8288508e55c0f5f87d29c",
"sha256:603162139684ee56bcd57acc74035fceed7dd8d732f38c0959c8bd157f913fec",
"sha256:60a78858580761fe611d22127868f3dc9f98871e6fdf0a15cc4203ed9ba6179b",
"sha256:63f55f490b958b6299e4e5bdac66ac988c3d11b7fafa522800359075d4fa56d1",
"sha256:7cc095a4661bdd8a5742aaf7c10ea9fac142d76ff1770a0f84394038126d8fc7",
"sha256:be046da49fbc3aa9491cc7296db7e8d27bcf0c3d5d1a40259c10471b014e4e0c",
"sha256:c31bc3c8e903d60a1ea31a754c72559398d91b5929fcb329b1c3a3d3f6e72113",
"sha256:c955791d80e464da3b471ab41eb65cf5a40c15ce9b001fdc5bbc241170de58ec",
"sha256:d069ef4b20b1e6b19f790d00097a5d5d2c50871b66d10075dab78938dc2ee2cf",
"sha256:d52b989dc23cdaa92582ceb4af8d5bcc94d74b2c3e64cd6785558ec6a879793e",
"sha256:e586b28354d7b6584d8973656a7954b1c69c93f708c0c07b77884f91640b7657",
"sha256:efcf3397ae1e3c3a4a0a0636542bcad5adad3b1dd3e8e629d0b6e201347176c8",
"sha256:fccefc0d36a38c57b7bd233a9b485e2f1eb71903ca7ad7adacad6c28a56d62d2"
],
"markers": "python_version >= '3.6'",
"version": "==1.2.0"
},
"matplotlib": {
"hashes": [
"sha256:006413f08ba5db1f5b1e0d6fbdc2ac9058b062ccf552f57182563a78579c34b4",
"sha256:1ab264770e7cf2cf4feb99f22c737066aef21ddf1ec402dc255450ac15eacb7b",
"sha256:20bcd11efe194cd302bd0653cb025b8d16bcd80442359bfca8d49dc805f35ec8",
"sha256:2a6d64336b547e25730b6221e7aadfb01a391a065d43b5f51f0b9d7f673d2dd2",
"sha256:31d32c83bb2b617377c6156f75e88b9ec2ded289e47ad4ff0f263dc1019d88b1",
"sha256:3d77a6630d093d74cbbfebaa0571d00790966be1ed204e4a8239f5cbd6835c5d",
"sha256:4416825ebc9c1f135027a30e8d8aea0edcf45078ce767c7f7386737413cfb98f",
"sha256:465c752278d27895e23f1379d6fcfa3a2990643b803c25e3bc16a10641d2346a",
"sha256:647cf232ccf6265d2ba1ac4103e8c8b6ac7b03a40da3421234ffb03dda217f59",
"sha256:67065d938df34478451af62fbd0670d2b51c4d859fb66673064eb5de8660dd7c",
"sha256:81de040403a33bf3c68e9d4a40e26c8d24da00f7e3fadd845003b7e106785da7",
"sha256:894dd47c0a6ce38dc19bc87d1f7e2b0608310b2a18d1572291157450b05ce874",
"sha256:91c153f4318e3c67c035fd1185f5ea2613f15008b73b66985033033f6fe54bbd",
"sha256:a47abc48c7b81fe6e636dde8a58e49b13d87d140e0f448213a4879f4a3f73345",
"sha256:a68e42e22f7fd190a532e4215e142276970c2d54040a0c46842fcb3db8b6ec5b",
"sha256:da06fa530591a141ffbe1712bbeec784734c3436b40c942d21652f305199b5d9"
],
"index": "pypi",
"version": "==3.2.2"
},
"numpy": {
"hashes": [
"sha256:13af0184177469192d80db9bd02619f6fa8b922f9f327e077d6f2a6acb1ce1c0",
"sha256:26a45798ca2a4e168d00de75d4a524abf5907949231512f372b217ede3429e98",
"sha256:26f509450db547e4dfa3ec739419b31edad646d21fb8d0ed0734188b35ff6b27",
"sha256:30a59fb41bb6b8c465ab50d60a1b298d1cd7b85274e71f38af5a75d6c475d2d2",
"sha256:33c623ef9ca5e19e05991f127c1be5aeb1ab5cdf30cb1c5cf3960752e58b599b",
"sha256:356f96c9fbec59974a592452ab6a036cd6f180822a60b529a975c9467fcd5f23",
"sha256:3c40c827d36c6d1c3cf413694d7dc843d50997ebffbc7c87d888a203ed6403a7",
"sha256:4d054f013a1983551254e2379385e359884e5af105e3efe00418977d02f634a7",
"sha256:63d971bb211ad3ca37b2adecdd5365f40f3b741a455beecba70fd0dde8b2a4cb",
"sha256:658624a11f6e1c252b2cd170d94bf28c8f9410acab9f2fd4369e11e1cd4e1aaf",
"sha256:76766cc80d6128750075378d3bb7812cf146415bd29b588616f72c943c00d598",
"sha256:7b57f26e5e6ee2f14f960db46bd58ffdca25ca06dd997729b1b179fddd35f5a3",
"sha256:7b852817800eb02e109ae4a9cef2beda8dd50d98b76b6cfb7b5c0099d27b52d4",
"sha256:8cde829f14bd38f6da7b2954be0f2837043e8b8d7a9110ec5e318ae6bf706610",
"sha256:a2e3a39f43f0ce95204beb8fe0831199542ccab1e0c6e486a0b4947256215632",
"sha256:a86c962e211f37edd61d6e11bb4df7eddc4a519a38a856e20a6498c319efa6b0",
"sha256:a8705c5073fe3fcc297fb8e0b31aa794e05af6a329e81b7ca4ffecab7f2b95ef",
"sha256:b6aaeadf1e4866ca0fdf7bb4eed25e521ae21a7947c59f78154b24fc7abbe1dd",
"sha256:be62aeff8f2f054eff7725f502f6228298891fd648dc2630e03e44bf63e8cee0",
"sha256:c2edbb783c841e36ca0fa159f0ae97a88ce8137fb3a6cd82eae77349ba4b607b",
"sha256:cbe326f6d364375a8e5a8ccb7e9cd73f4b2f6dc3b2ed205633a0db8243e2a96a",
"sha256:d34fbb98ad0d6b563b95de852a284074514331e6b9da0a9fc894fb1cdae7a79e",
"sha256:d97a86937cf9970453c3b62abb55a6475f173347b4cde7f8dcdb48c8e1b9952d",
"sha256:dd53d7c4a69e766e4900f29db5872f5824a06827d594427cf1a4aa542818b796",
"sha256:df1889701e2dfd8ba4dc9b1a010f0a60950077fb5242bb92c8b5c7f1a6f2668a",
"sha256:fa1fe75b4a9e18b66ae7f0b122543c42debcf800aaafa0212aaff3ad273c2596"
],
"markers": "python_version >= '3.6'",
"version": "==1.19.0"
},
"pandas": {
"hashes": [
"sha256:02f1e8f71cd994ed7fcb9a35b6ddddeb4314822a0e09a9c5b2d278f8cb5d4096",
"sha256:13f75fb18486759da3ff40f5345d9dd20e7d78f2a39c5884d013456cec9876f0",
"sha256:35b670b0abcfed7cad76f2834041dcf7ae47fd9b22b63622d67cdc933d79f453",
"sha256:4c73f373b0800eb3062ffd13d4a7a2a6d522792fa6eb204d67a4fad0a40f03dc",
"sha256:5759edf0b686b6f25a5d4a447ea588983a33afc8a0081a0954184a4a87fd0dd7",
"sha256:5a7cf6044467c1356b2b49ef69e50bf4d231e773c3ca0558807cdba56b76820b",
"sha256:69c5d920a0b2a9838e677f78f4dde506b95ea8e4d30da25859db6469ded84fa8",
"sha256:8778a5cc5a8437a561e3276b85367412e10ae9fff07db1eed986e427d9a674f8",
"sha256:9871ef5ee17f388f1cb35f76dc6106d40cb8165c562d573470672f4cdefa59ef",
"sha256:9c31d52f1a7dd2bb4681d9f62646c7aa554f19e8e9addc17e8b1b20011d7522d",
"sha256:ab8173a8efe5418bbe50e43f321994ac6673afc5c7c4839014cf6401bbdd0705",
"sha256:ae961f1f0e270f1e4e2273f6a539b2ea33248e0e3a11ffb479d757918a5e03a9",
"sha256:b3c4f93fcb6e97d993bf87cdd917883b7dab7d20c627699f360a8fb49e9e0b91",
"sha256:c9410ce8a3dee77653bc0684cfa1535a7f9c291663bd7ad79e39f5ab58f67ab3",
"sha256:f69e0f7b7c09f1f612b1f8f59e2df72faa8a6b41c5a436dde5b615aaf948f107",
"sha256:faa42a78d1350b02a7d2f0dbe3c80791cf785663d6997891549d0f86dc49125e"
],
"index": "pypi",
"version": "==1.0.5"
},
"pyparsing": {
"hashes": [
"sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1",
"sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"
],
"markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==2.4.7"
},
"python-dateutil": {
"hashes": [
"sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c",
"sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==2.8.1"
},
"pytz": {
"hashes": [
"sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed",
"sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048"
],
"version": "==2020.1"
},
"six": {
"hashes": [
"sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259",
"sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==1.15.0"
}
},
"develop": {}
}
"""
This script analysis a data set about astronauts and creates different
plots as result.
"""
from datetime import date
from os import makedirs
from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use("ggplot")
_ASTRONAUT_DATA = "data/astronauts.json"
_OUTPUT_PATH = "results"
##
# Data preparation functions
##
def prepare_data_set(df):
df = rename_columns(df)
df = df.set_index("astronaut_id")
# Set pandas dtypes for columns with date or time
df = df.dropna(subset=["time_in_space"])
df["time_in_space"] = df["time_in_space"].astype(int)
df["time_in_space"] = pd.to_timedelta(df["time_in_space"], unit="m")
df["birthdate"] = pd.to_datetime(df["birthdate"])
df["date_of_death"] = pd.to_datetime(df["date_of_death"])
df.sort_values("birthdate", inplace=True)
# Calculate extra columns from the original data
df["time_in_space_D"] = df["time_in_space"].astype("timedelta64[D]")
df["alive"] = df["date_of_death"].apply(is_alive)
df["age"] = df["birthdate"].apply(calculate_age)
df["died_with_age"] = df.apply(died_with_age, axis=1)
return df
def rename_columns(df):
"""
The original column naming in the data set is not useful
for programming with pandas. So we rename it.
"""
name_mapping = {
"astronaut": "astronaut_id",
"astronautLabel": "name",
"birthplaceLabel": "birthplace",
"sex_or_genderLabel": "sex_or_gender",
}
df = df.rename(index=str, columns=name_mapping)
return df
def is_alive(date_of_death):
if pd.isnull(date_of_death):
return True
return False
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def died_with_age(row):
if pd.isnull(row["date_of_death"]):
return None
born = row["birthdate"]
today = row["date_of_death"]
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
##
# Plot functions
##
def create_time_of_x_in_space(df, filename, title):
"""
This function generated a plot with the summed up time of 'living beings'
in space over the years by their birthday's.
"""
reduced_df = df[["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df["accumulated_time_in_minutes"] = reduced_df["time_in_space"].cumsum()
reduced_df["accumulated_time_in_days"] = reduced_df["time_in_space_D"].cumsum()
axs = reduced_df.plot(x="birthdate", y="accumulated_time_in_days")
axs.set_title(title)
axs.set_xlabel("Years ")
axs.set_ylabel("t in days")
save(axs.get_figure(), filename)
def create_age_histogram(age_df, died_df):
"""
The function generates a combined histogram of astronauts
in the categories 'age at dead' and 'age alive'.
"""
fig, axs = plt.subplots(1, 1)
axs.hist(
[died_df["died_with_age"], age_df["age"]],
bins=70,
range=(31, 100),
stacked=True,
)
axs.set_xlabel("Age")
axs.set_ylabel("Number of astronauts")
axs.set_title("Dead vs. Alive astronauts")
save(fig, "combined_histogram.png")
def create_age_boxplot(age_df, died_df):
"""
The function generates a boxplot of astronauts age distribution
in the categories dead and alive.
"""
fig, axs = plt.subplots(1, 1)
axs.boxplot([died_df["died_with_age"], age_df["age"]])
axs.set_title("Age distribution; Dead vs. Alive astronauts")
axs.set_xlabel("Category")
plt.setp(axs, xticks=[1, 2], xticklabels=["Dead", "Alive"])
axs.set_ylabel("Age")
save(fig, "boxplot.png")
def save(fig, filename):
fig.savefig(Path(_OUTPUT_PATH).resolve() / Path(filename))
def perform_analysis():
""" Glues data preparation and plotting. """
# Set up directory structure and preprocess data
makedirs(_OUTPUT_PATH, exist_ok=True)
df = pd.read_json(Path(_ASTRONAUT_DATA).resolve())
df = prepare_data_set(df)
# Male humans in space
df_male = df.loc[
df["sex_or_gender"] == "male", ["birthdate", "time_in_space", "time_in_space_D"]
].copy()
create_time_of_x_in_space(
df_male,
"male_humans_in_space.png",
"Total time male humans have spend in space",
)
# Female humans in space
df_female = df.loc[
df["sex_or_gender"] == "female",
["birthdate", "time_in_space", "time_in_space_D"],
].copy()
create_time_of_x_in_space(
df_female,
"female_humans_in_space.png",
"Total time female humans have spend in space",
)
# Humans in space
create_time_of_x_in_space(
df, "humans_in_space.png", "Total time humans have spend in space"
)
# Dead and alive astronauts analysis
died_df = df.loc[df["alive"] == 0, ["died_with_age"]].copy()
age_df = df.loc[df["alive"] == 1, ["age"]].copy()
# Combined histogram of dead and alive astronauts
create_age_histogram(age_df, died_df)
# Box plots of dead vs alive astronauts
create_age_boxplot(age_df, died_df)
# Main entry point
if __name__ == "__main__":
perform_analysis()
from os import path, makedirs
import pandas as pd
import matplotlib.pyplot as plt
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def is_alive(date_of_death):
if pd.isnull(date_of_death):
return True
return False
def died_with_age(row):
if pd.isnull(row["date_of_death"]):
return None
born = row["birthdate"]
today = row["date_of_death"]
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
plt.style.use("ggplot")
df = pd.read_json("data/astronauts.json")
df = df.rename(index=str, columns={"astronaut": "astronaut_id", "astronautLabel": "name","birthplaceLabel": "birthplace","sex_or_genderLabel": "sex_or_gender"})
df = df.set_index("astronaut_id")
df = df.dropna(subset=["time_in_space"])
df["time_in_space"] = df["time_in_space"].astype(int)
df["time_in_space"] = pd.to_timedelta(df["time_in_space"], unit="m")
df["time_in_space_D"] = df["time_in_space"].astype("timedelta64[D]")
df["birthdate"] = pd.to_datetime(df["birthdate"])
df["date_of_death"] = pd.to_datetime(df["date_of_death"])
df.sort_values("birthdate", inplace=True)
df["alive"] = df["date_of_death"].apply(is_alive)
df["age"] = df["birthdate"].apply(calculate_age)
df["died_with_age"] = df.apply(died_with_age, axis=1)
# Male humans in space
df_male = df.loc[df["sex_or_gender"] == "male", ["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df = df_male[["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df["accumulated_time_in_minutes"] = reduced_df["time_in_space"].cumsum()
reduced_df["accumulated_time_in_days"] = reduced_df["time_in_space_D"].cumsum()
reduced_df.plot(x="birthdate", y="accumulated_time_in_days")
plt.title("Total time male humans have spend in space")
plt.xlabel("Years")
plt.ylabel("t in days")
fig = plt.gcf()
fig.savefig("male_humans_in_space.png")
# Female humans in space
df_female = df.loc[df["sex_or_gender"] == "female", ["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df = df_female[["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df["accumulated_time_in_minutes"] = reduced_df["time_in_space"].cumsum()
reduced_df["accumulated_time_in_days"] = reduced_df["time_in_space_D"].cumsum()
reduced_df.plot(x="birthdate", y="accumulated_time_in_days")
plt.title("Total time female humans have spend in space")
plt.xlabel("Years")
plt.ylabel("t in days")
fig = plt.gcf()
fig.savefig("female_humans_in_space.png")
# Humans in space
reduced_df = df[["birthdate", "time_in_space", "time_in_space_D"]].copy()
reduced_df["accumulated_time_in_minutes"] = reduced_df["time_in_space"].cumsum()
reduced_df["accumulated_time_in_days"] = reduced_df["time_in_space_D"].cumsum()
reduced_df.plot(x="birthdate", y="accumulated_time_in_days")
plt.title("Total time humans have spend in space")
plt.xlabel("Years")
plt.ylabel("t in days")
fig = plt.gcf()
fig.savefig("humans_in_space.png")
died_df = df.loc[df["alive"] == 0, ["died_with_age"]].copy()
age_df = df.loc[df["alive"] == 1, ["age"]].copy()
# Combined Histogram of dead and alive astronauts
fig, axs = plt.subplots(1, 1)
axs.hist([died_df["died_with_age"], age_df["age"]], bins=70, range=(31, 100), stacked=True)
axs.set_xlabel("Age")
axs.set_ylabel("Number of astronauts")
axs.set_title("Dead vs. Alive astronauts")
fig.savefig("combined_histogram.png")
# Box plots of dead vs alive astronauts
fig, axs = plt.subplots(1, 1)
axs.boxplot([died_df["died_with_age"], age_df["age"]])
axs.set_title("Age distribution; Dead vs. Alive astronauts")
axs.set_xlabel("Category")
plt.setp(axs, xticks=[1, 2], xticklabels=["Dead", "Alive"])
axs.set_ylabel("Age")
fig.savefig("boxplot.png")
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment