import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
"""
# Part 0
**Data reading and cleaning**
"""
folder = "../input/crypto-mining-data/"
dataPackage = [ pd.read_csv(folder+file) for file in os.listdir(folder) ]
"""
Let's clean data from NaN values.
"""
def del_cols_with_many_nans(data : pd.DataFrame) -> pd.DataFrame:
    cols = list(data.columns)
    new_cols = []
    data_len = float(len(data))
    for col in cols:
        nans_count = float(data[col].isna().sum())
        if(nans_count/data_len <= 0.01):
            new_cols.append(col)
    return data[new_cols]
for i in range(len(dataPackage)):
    dataPackage[i] = del_cols_with_many_nans(dataPackage[i]).dropna()
    print( str(i+1) + "st file\ncols: " + str(len(dataPackage[i].columns)) + " number of object: " + str(len(dataPackage[i])), end="\n\n" )
"""
# Part 1
**Primary preprocessing**
"""
def select_month(data : pd.DataFrame) -> pd.DataFrame:
    new_data = data.copy()
    new_data["month"] = list(range(len(new_data)))
    for i in new_data.index:
        new_data["month"][i] = float( new_data["date"][i].split("-")[1])
    cols = list(new_data.columns)[1:]
    return new_data[cols].astype("float")

def select_year(data : pd.DataFrame) -> pd.DataFrame:
    new_data = data.copy()
    new_data["year"] = list(range(len(new_data)))
    for i in new_data.index:
        new_data["year"][i] = float( new_data["date"][i].split("-")[0])
    cols = list(new_data.columns)[1:]
    return new_data[cols].astype("float")
data_per_month = dataPackage.copy()
for i in range(len(data_per_month)):
    data_per_month[i] = select_month(data_per_month[i])

data_per_year = dataPackage.copy()
for i in range(len(data_per_year)):
    data_per_year[i] = select_year(data_per_year[i])
for i in range(len(data_per_month)):
    data_per_month[i] = data_per_month[i].groupby("month").mean()
for i in range(len(data_per_year)):
    data_per_year[i] = data_per_year[i].groupby("year").mean()
"""
# Part 2
**Visualization**
"""
cryptocurrencies = [
    "binance coin",
    "bitcoin",
    "bitcoin gold",
    "dash",
    "dogecoin",
    "ethereum",
    "ethereum classic",
    "litecoin",
    "tether"
]
def visualize_for_month(data : pd.DataFrame, currency : str):
    plt.figure(figsize=(13, 13))
    for col in data.columns:
        plt.plot(data.index, np.cbrt(np.cbrt(data[col])), label=col)
    
    plt.xlabel("month")
    plt.ylabel("root of the 9th degree of mean")
    plt.title(currency)
    plt.legend()
    plt.show()
def visualize_for_year(data : pd.DataFrame, currency : str):
    global cryptocurrencies
    
    plt.figure(figsize=(13, 13))
    for col in data.columns:
        plt.plot(data.index, np.cbrt(np.cbrt(data[col])), label=col)
    
    plt.xlabel("year")
    plt.ylabel("root of the 9th degree of mean")
    plt.title(currency)
    plt.legend()
    plt.show()
"""
Visualization for month.
"""
for i in range(9):
    visualize_for_month(data_per_month[i], cryptocurrencies[i])
for i in range(9):
    visualize_for_year(data_per_year[i], cryptocurrencies[i])