<commit_before># -*- coding: utf-8 -*-
"""
===========================
Clustering
This script clusters typical days for a building
using SAX method for timeseries.

===========================
J. Fonseca  script development          27.10.16


"""
from __future__ import division

import time

import matplotlib.pyplot as plt
import pandas as pd

from cea.demand.calibration.clustering.sax import SAX
from cea.demand.calibration.clustering.sax_optimization import SAX_opt, print_pareto

__author__ = "<NAME>"
__copyright__ = "Copyright 2016, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


def clustering(locator, gv, word_size, alphabet_size, building_name, building_load, optimize, plot_clusters):
    """
    Function to cluster different days of the year following the SAX method (see class for more info)
    :param locator: locator class
    :param gv: global variables class
    :param word_size: estimated wordsize, after optimization
    :param alphabet_size: estimated alphabet size. after optimization
    :param building_name: building name to make the cluster of its time series
    :param building_load: name of time_series to use form the building demand i.e., Qhsf_kWh, Qcsf_kWh, Qwwf_kWh etc.
    :param optimize: Boolan: true to run the optimization. you will first run the optimization,
    choose the wordsize and alphabetsize. from there, run the program with the optimize = flase
    :param plot_clusters: when optimize if false, decide weather you would like to see the data in each defined cluster
    :return: timeseries per cluster  in locator.get_clustering_calibration_file(cluster_name). .csv file
        dataframe with ID of every cluster per day of the year in locator.get_clustering_calibration_sax_names_file()

    """
    t0 = time.clock()

    # import data
    data = pd.read_csv(locator.get_demand_results_file(building_name),
                       usecols=['DATE', building_load], index_col='DATE')
    data.set_index(pd.to_datetime(data.index), inplace=True)
    data['day'] = data.index.dayofyear

    # transform into dicts where key = day and value = 24 h array
    groups = data.groupby(data.day)
    arrays = [group[1][building_load].values for group in groups]

    # set optimization problem for wordzise and alpha number
    if optimize:
        pop, halloffame, paretofrontier, stats = SAX_opt(locator, arrays, time_series_len=24, BOUND_LOW = 4,
                                                         BOUND_UP = 24, NGEN = 50, MU = 8, CXPB = 0.9,
                                                         start_gen = None)
        if plot_clusters:
            print_pareto(pop, paretofrontier)
    else:
        s = SAX(word_size, alphabet_size)
        sax = [s.to_letter_rep(array)[0] for array in arrays]

        # calculate dict with data per hour for the whole year and create groups per pattern
        array_hours = range(24)
        array_days = range(365)
        arrays_T = [list(x) for x in zip(*arrays)]
        dict_data = dict((group, x) for group, x in zip(array_hours, arrays_T))
        dict_data.update({'sax': sax, 'day': array_days})

        # save all names and days of occurrence of each profile
        df = pd.DataFrame(dict_data)
        df[['sax', 'day']].to_csv(locator.get_calibration_clusters_names())<commit_msg>renaming arrays<commit_after># -*- coding: utf-8 -*-
"""
===========================
Clustering
This script clusters typical days for a building
using SAX method for timeseries.

===========================
<NAME>  script development          27.10.16


"""
from __future__ import division

import time

import matplotlib.pyplot as plt
import pandas as pd

from cea.demand.calibration.clustering.sax import SAX
from cea.demand.calibration.clustering.sax_optimization import SAX_opt, print_pareto

__author__ = "<NAME>"
__copyright__ = "Copyright 2016, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


def clustering(locator, gv, word_size, alphabet_size, building_name, building_load, optimize, plot_clusters):
    """
    Function to cluster different days of the year following the SAX method (see class for more info)
    :param locator: locator class
    :param gv: global variables class
    :param word_size: estimated wordsize, after optimization
    :param alphabet_size: estimated alphabet size. after optimization
    :param building_name: building name to make the cluster of its time series
    :param building_load: name of time_series to use form the building demand i.e., Qhsf_kWh, Qcsf_kWh, Qwwf_kWh etc.
    :param optimize: Boolan: true to run the optimization. you will first run the optimization,
    choose the wordsize and alphabetsize. from there, run the program with the optimize = flase
    :param plot_clusters: when optimize if false, decide weather you would like to see the data in each defined cluster
    :return: timeseries per cluster  in locator.get_clustering_calibration_file(cluster_name). .csv file
        dataframe with ID of every cluster per day of the year in locator.get_clustering_calibration_sax_names_file()

    """
    t0 = time.clock()

    # import data
    data = pd.read_csv(locator.get_demand_results_file(building_name),
                       usecols=['DATE', building_load], index_col='DATE')
    data.set_index(pd.to_datetime(data.index), inplace=True)
    data['day'] = data.index.dayofyear

    # transform into dicts where key = day and value = 24 h array
    groups = data.groupby(data.day)
    list_of_timeseries = [group[1][building_load].values for group in groups]


    if optimize:
        # set optimization problem for wordzise and alpha number
        pop, halloffame, paretofrontier, stats = SAX_opt(locator, list_of_timeseries, time_series_len=24, BOUND_LOW = 4,
                                                         BOUND_UP = 24, NGEN = 50, MU = 8, CXPB = 0.9,
                                                         start_gen = None)
        if plot_clusters:
            print_pareto(pop, paretofrontier)
    else:
        # calculate sax for timesieries data
        s = SAX(word_size, alphabet_size)
        sax = [s.to_letter_rep(x)[0] for x in list_of_timeseries]

        # calculate dict with data per hour for the whole year and create groups per pattern
        hours_of_day = range(24)
        days_of_year = range(365)
        list_of_timeseries_transposed = [list(x) for x in zip(*list_of_timeseries)]
        dict_data = dict((group, x) for group, x in zip(hours_of_day, list_of_timeseries_transposed))
        dict_data.update({'sax': sax, 'day': days_of_year})

        # save all names and days of occurrence of each profile
        df = pd.DataFrame(dict_data)
        df[['sax', 'day']].to_csv(locator.get_calibration_clusters_names())
{'fraction_non_alphanumeric': 0.09583571021106674, 'fraction_numerical': 0.009412435824301199, 'mean_word_length': 3.2684114424832624, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5445980', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2025, 'n_words': 682}