from features.base_feature import BaseFeature
from pathlib import Path
import numpy as np
import pandas as pd
import dask.dataframe as dd
import datetime
from exceptions import IrregularExtractArgumentException
from features.get_dtype import get_csv_dtype


class Kernel02(BaseFeature):
    """
    [ref]
    https://www.kaggle.com/cdeotte/malware-timestamps-2
    """

    def get_loadcols(self):
        cols = [
            "AvSigVersion",
            "Census_OSVersion"
        ]
        return cols

    def extract(self):
        return super().extract()

    def sort_by_version(self, df):
        """
        Census_OSVersion_sort_by_date
        """
        def my_key(item):
            return tuple(int(part) for part in item[0].split("."))

        datedict = np.load(Path(__file__).absolute().parents[1] / "data" / "external" / "OSVersionTimestamps.npy")[()]
        items = dict(sorted(datedict.items(), key=my_key))

        data_val = self.load_full()["Census_OSVersion"].unique()

        new_map, cnt = {}, 0
        for key in items.keys():
            new_map[key] = cnt
            if key in data_val:
                cnt += 1

        df["Census_OSVersion"] = df["Census_OSVersion"].map(new_map)
        return df
