from ows_refactored.others.aster.abstract_aster_cfg import (
    abstract_aster_aloh_group_composition, abstract_aster_aloh_group_content,
    abstract_aster_false_colour, abstract_aster_feoh_group_content,
    abstract_aster_ferric_oxide_composition,
    abstract_aster_ferric_oxide_content,
    abstract_aster_ferrous_iron_content_in_mgoh,
    abstract_aster_ferrous_iron_index, abstract_aster_green_vegetation,
    abstract_aster_gypsum_index, abstract_aster_kaolin_group_index,
    abstract_aster_mgoh_group_composition, abstract_aster_mgoh_group_content,
    abstract_aster_opaque_index, abstract_aster_quartz_index,
    abstract_aster_regolith_ratios, abstract_aster_silica_index)
from ows_refactored.others.aster.band_aster_cfg import (
    bands_aster, bands_aster_single_band)
from ows_refactored.others.aster.style_aster_cfg import (
    style_aster_aloh_comp_ramp, style_aster_aloh_cont_ramp,
    style_aster_b2_gray, style_aster_false_colour, style_aster_feoh_cont_ramp,
    style_aster_ferrous_idx_ramp, style_aster_ferrous_mgoh_ramp,
    style_aster_ferrox_comp_ramp, style_aster_ferrox_cont_ramp,
    style_aster_green_veg_ramp, style_aster_gypsum_idx_ramp,
    style_aster_kaolin_idx_ramp, style_aster_mgoh_comp_ramp,
    style_aster_mgoh_cont_ramp, style_aster_opaque_idx_ramp,
    style_aster_quartz_idx_ramp, style_aster_silica_idx_ramp,
    style_aster_simple_rgb)
from ows_refactored.ows_reslim_cfg import reslim_standard

layers = {
    "title": "ASTER Geoscience Map of Australia",
    "abstract": """
The National ASTER Map of Australia is the parent datafile of a dataset that comprises a set of 14+ geoscience products made up of mosaiced ASTER (Advanced Spaceborne Thermal Emission and Reflection Radiometer) scenes across Australia.

The individual geoscience products are a combination of bands and band ratios to highlight different mineral groups and parameters including:
- False Colour Mosaic
- CSIRO Landsat TM
- Regolith Ratios
- AlOH Group Composition
- AlOH Group Content
- FeOH Group Content
- Ferric Oxide Composition
- Ferric Oxide Content
- Ferrous Iron Content in MgOH/Carbonate
- Ferrous Iron Index
- Green Vegetation Content
- Gypsum Index
- Kaolin Group Index
- MgOH Group Composition
- MgOH Group Content
- Opaque Index
- TIR Silica index
- TIR Quartz Index
- Surface mineral group distribution (relative abundance and composition)

For parent datafile information, see the dataset record: http://pid.geoscience.gov.au/dataset/ga/74347
""",
    "layers": [
        {
            "title": "False Colour Mosaic",
            "name": "aster_false_colour",
            "abstract": abstract_aster_false_colour,
            "product_name": "aster_false_colour",
            "bands": bands_aster,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "false_colour",
                "styles": [
                    style_aster_false_colour,
                    style_aster_b2_gray,
                ],
            },
        },
        {
            "title": "Regolith Ratios",
            "name": "aster_regolith_ratios",
            "abstract": abstract_aster_regolith_ratios,
            "product_name": "aster_regolith_ratios",
            "bands": bands_aster,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "simple_rgb",
                "styles": [
                    style_aster_simple_rgb,
                ],
            },
        },
        {
            "title": "AlOH Group Composition",
            "name": "aster_aloh_group_composition",
            "abstract": abstract_aster_aloh_group_composition,
            "product_name": "aster_aloh_group_composition",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_aloh_comp_ramp,
                ],
            },
        },
        {
            "title": "AlOH Group Content",
            "name": "aster_aloh_group_content",
            "abstract": abstract_aster_aloh_group_content,
            "product_name": "aster_aloh_group_content",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_aloh_cont_ramp,
                ],
            },
        },
        {
            "title": "FeOH Group Content",
            "name": "aster_feoh_group_content",
            "abstract": abstract_aster_feoh_group_content,
            "product_name": "aster_feoh_group_content",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_feoh_cont_ramp,
                ],
            },
        },
        {
            "title": "Ferric Oxide Composition",
            "name": "aster_ferric_oxide_composition",
            "abstract": abstract_aster_ferric_oxide_composition,
            "product_name": "aster_ferric_oxide_composition",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_ferrox_comp_ramp,
                ],
            },
        },
        {
            "title": "Ferric Oxide Content",
            "name": "aster_ferric_oxide_content",
            "abstract": abstract_aster_ferric_oxide_content,
            "product_name": "aster_ferric_oxide_content",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_ferrox_cont_ramp,
                ],
            },
        },
        {
            "title": "Ferrous Iron Content in MgOH/Carbonate",
            "name": "aster_ferrous_iron_content_in_mgoh",
            "abstract": abstract_aster_ferrous_iron_content_in_mgoh,
            "product_name": "aster_ferrous_iron_content_in_mgoh",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_ferrous_mgoh_ramp,
                ],
            },
        },
        {
            "title": "Ferrous Iron Index",
            "name": "aster_ferrous_iron_index",
            "abstract": abstract_aster_ferrous_iron_index,
            "product_name": "aster_ferrous_iron_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_ferrous_idx_ramp,
                ],
            },
        },
        {
            "title": "Green Vegetation Content",
            "name": "aster_green_vegetation",
            "abstract": abstract_aster_green_vegetation,
            "product_name": "aster_green_vegetation",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_green_veg_ramp,
                ],
            },
        },
        {
            "title": "Gypsum Index",
            "name": "aster_gypsum_index",
            "abstract": abstract_aster_gypsum_index,
            "product_name": "aster_gypsum_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_gypsum_idx_ramp,
                ],
            },
        },
        {
            "title": "Kaolin Group Index",
            "name": "aster_kaolin_group_index",
            "abstract": abstract_aster_kaolin_group_index,
            "product_name": "aster_kaolin_group_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_kaolin_idx_ramp,
                ],
            },
        },
        {
            "title": "MgOH Group Composition",
            "name": "aster_mgoh_group_composition",
            "abstract": abstract_aster_mgoh_group_composition,
            "product_name": "aster_mgoh_group_composition",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_mgoh_comp_ramp,
                ],
            },
        },
        {
            "title": "MgOH Group Content",
            "name": "aster_mgoh_group_content",
            "abstract": abstract_aster_mgoh_group_content,
            "product_name": "aster_mgoh_group_content",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_mgoh_cont_ramp,
                ],
            },
        },
        {
            "title": "Opaque Index",
            "name": "aster_opaque_index",
            "abstract": abstract_aster_opaque_index,
            "product_name": "aster_opaque_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_opaque_idx_ramp,
                ],
            },
        },
        {
            "title": "TIR Silica index",
            "name": "aster_silica_index",
            "abstract": abstract_aster_silica_index,
            "product_name": "aster_silica_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_silica_idx_ramp,
                ],
            },
        },
        {
            "title": "TIR Quartz Index",
            "name": "aster_quartz_index",
            "abstract": abstract_aster_quartz_index,
            "product_name": "aster_quartz_index",
            "bands": bands_aster_single_band,
            "resource_limits": reslim_standard,
            "native_crs": "EPSG:4326",
            "native_resolution": [0.00027777778, -0.00027777778],
            "image_processing": {
                "extent_mask_func": "datacube_ows.ogc_utils.mask_by_val",
                "always_fetch_bands": [],
                "manual_merge": False,
            },
            "styling": {
                "default_style": "ramp",
                "styles": [
                    style_aster_quartz_idx_ramp,
                ],
            },
        },
    ],
}


# -*- coding: utf-8 -*-

import joblib
import pandas as pd
from sklearn import metrics
from sklearn import tree
import config
import os
import argparse

def run(fold):
    df = pd.read_csv(config.TRAINING_FILE)
    # train dataset where fold != kfold
    # reset the window
    df_train = df[df.kfold != fold].reset_index(drop =True)
    # validation is oposite
    df_valid = df[df.kfold == fold].reset_index(drop =True)

    X_train = df_train.drop("label", axis =1).values
    y_train = df_train.label.values

    X_valid = df_valid.drop("label", axis =1).values
    y_valid = df_valid.label.values

    model = tree.DecisionTreeClassifier()

    model.fit(X_train, y_train)

    predict = model.predict(X_valid)

    accuracy = metrics.accuracy_score(y_valid, predict)
    print(f"Fold =  {fold}, Accuracy = {accuracy}")

    # save model
    print("saving file to " + str(os.path.join(config.MODEL_OUTPUT, f"dt_{fold}.bin")))
    joblib.dump(
        model,
        os.path.join(config.MODEL_OUTPUT, f"dt_{fold}.bin")
        )

if __name__ == "__main__":
    # first initialize argument parser class
    parser = argparse.ArgumentParser()

    # define a argumwnt
    parser.add_argument("--fold", type = int, help= "put k fold value")

    # read the argument from command line
    args = parser.parse_args()

    # run the specific line with args

    run(args.fold)


# RNN

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.metrics import mean_squared_error

## import the dataset
dataset_train = pd.read_csv('Google_Stock_Price_Train.csv')
training_set = dataset_train.iloc[:,1:2].values

## feature scaling -- Normalization
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)

## creating a structure with 60 timesteps and 1 output
X_train = []
y_train = []
for i in range(60,len(training_set)):
    X_train.append(training_set_scaled[i-60:i,0])
    y_train.append(training_set_scaled[i,0])

## since RNN needs an array as an input, we need to make those lists arrays
X_train, y_train = np.array(X_train), np.array(y_train)

## Reshape
X_train = np.reshape(X_train, newshape = (X_train.shape[0], X_train.shape[1], 1))

## Building RNN
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

## Initializing the RNN
regressor = Sequential()

## Adding the first LSTM layer and Dropout Regularization
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))

## Adding the second LSTM layer and Dropout Regularization
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

## Adding the third LSTM layer and Dropout Regularization
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

## Adding the fourth LSTM layer and Dropout Regularization
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

## Adding the output layer
regressor.add(Dense(units = 1))

## Compiling the RNN
regressor.compile(optimizer='adam',loss = 'mean_squared_error')

## Fitting the RNN to the training set
regressor.fit(X_train, y_train, epochs=5, batch_size=50)

## Making the predication and visualizing the results

## real stock price of 2017
dataset_test = pd.read_csv('Google_Stock_Price_Test.csv')
test_set = dataset_test.iloc[:,1:2].values

# Getting the real stock price of 2017
dataset_test = pd.read_csv('Google_Stock_Price_Test.csv')
real_stock_price = dataset_test.iloc[:, 1:2].values

# Getting the predicted stock price of 2017
dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 80):
    X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_stock_price = regressor.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)

rmse = math.sqrt(mean_squared_error(real_stock_price, predicted_stock_price))

# Visualising the results
plt.plot(real_stock_price, color = 'red', label = 'Real Google Stock Price')
plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted Google Stock Price')
plt.title('Google Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Google Stock Price')
plt.legend()
plt.show()