<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Version: Fri 28 Mar 2014
#   Initial build.
#

import os
import time

import numpy as np
import pandas as pd
import yaml

# ... : ted
# from ... import msg
from ... import env
# from ... import TEDError
from ... import Namespace

# . : ted.sdss.cutouts
from . import load_cutout_sequences


class CVHelper(object):
    """Loads parameters and makes various parameters and methods available"""

    def __init__(self):
        self._load_parameters()
        self._load_data()

    # Initialise
    # ----------

    def _load_parameters(self):
        self._params = load_parameters()

    def _load_data(self):
        self._css, self._targets = load_cutout_sequences()

    @property
    def _cv(self): return self._params.get('cv')

    @property
    def _cs(self): return self._params.get('cs')

    @property
    def _xp(self): return self._params.get('xp')

    # Experiment properties
    # ---------------------

    def set_exp(self, key=None):
        if self._xp.get(key) is None: return None
        getattr(self, '_set_exp_' + key)()

    def _set_exp_any(self):
        xpp = dict2numpy(self._xp.get('any'))
        xp = Namespace()
        xp.name = 'any'
        xp.sigmas = xpp.get('sigmas')
        xp.taus = xpp.get('taus')
        xp.N_sigmas = xp.sigmas.size
        xp.N_taus = xp.taus.size
        xp.gskw = dict(exp=xp.name, **xpp)
        self.xp = xp

    @property
    def quality(self):
        if not hasattr(self, '_quality'):
            return self._cs.get('quality')
        else:
            return self._quality

    def set_quality(self, quality):
        if isinstance(quality, list):
            self._quality = quality

    @staticmethod
    def _qstr(quality):
        return ''.join([str(q) for q in sorted(quality)])

    # Fold management
    # ---------------

    @property
    def _folds(self):
        return get_folds(self._css, self._targets, self._cv.get('N_folds'))

    # Cross validation
    # ----------------

    # def cv(self):
    #     for n, data in enumerate(self._folds):
    #         self._set_fold_parameters(fold=n + 1, data=data)
    #         # Training
    #         self._cube_of_predictions()
    #         self._matrix_of_accuracies()
    #         self._max_entries(moa)
    #         # Testing
    #         self._vector_of_predictions()
    #         self._scalar_of_accuracy()
    #         # Save to disk
    #         self._save_fold_results()

    def cv(self):
        self._cv_time = time.time()
        getattr(self, '_cv_' + self.xp.name)()

    def _cv_any(self):
        for n, data in enumerate(self._folds):
            # Fold number
            fnum = n + 1
            # Unpack training and test fold (f: features; l: labels)
            (train_f, train_l), (test_f, test_l) = data
            try:
                # raise Exception

                # Training set
                cop = self._cop_any(train_f)
                moa = self._moa_any(cop, train_l)
                self._save_fold_results_any(moa=moa, ftype='train', fnum=fnum)

            except Exception as e:
                print '_cv_any:', fnum, 'train'
                print '_cv_any:', e.message
                print '_cv_any:', '-' * 50, '\n'

            try:
                # raise Exception

                # Test set
                cop = self._cop_any(test_f)
                moa = self._moa_any(cop, test_l)
                self._save_fold_results_any(moa=moa, ftype='test', fnum=fnum)

            except Exception as e:
                print '_cv_any:', fnum, 'test'
                print '_cv_any:', e.message
                print '_cv_any:', '-' * 50, '\n'

    # Grid search
    # -----------

    def _cube_of_predictions(self, features):
        """Store result of training with chosen experiment"""
        # self._cop = getattr(self, '_cop_' + self.xp.name)()
        return getattr(self, '_cop_' + self.xp.name)(features)

    def _cop_any(self, features):
        """Return result of training with experiment ANY"""
        N = features.shape[0]
        cop = np.zeros((self.xp.N_sigmas, self.xp.N_taus, N)).astype(bool)
        for k, cs in enumerate(features):
            cs.set_fname_gsp('prediction_Q-{}.csv'.format(self._qstr))
            if cs.has_gs_prediction and cs.gs_prediction_time > self._cv_time:
            # if cs.has_gs_prediction and cs.gs_prediction_time > self._cv_time - 2 * 3600.:
                cop[:, :, k] = cs.gs_prediction
            else:
                cs.load(**self._cs)
                # print 'cs.quality:', cs.quality<commit_msg>BUGFIX: CVHelper.qstr returns qstr of CVHelper.quality. Before, all prediction_Q-{}.csv-files were still the same...<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Version: Fri 28 Mar 2014
#   Initial build.
#

import os
import time

import numpy as np
import pandas as pd
import yaml

# ... : ted
# from ... import msg
from ... import env
# from ... import TEDError
from ... import Namespace

# . : ted.sdss.cutouts
from . import load_cutout_sequences


class CVHelper(object):
    """Loads parameters and makes various parameters and methods available"""

    def __init__(self):
        self._load_parameters()
        self._load_data()

    # Initialise
    # ----------

    def _load_parameters(self):
        self._params = load_parameters()

    def _load_data(self):
        self._css, self._targets = load_cutout_sequences()

    @property
    def _cv(self): return self._params.get('cv')

    @property
    def _cs(self): return self._params.get('cs')

    @property
    def _xp(self): return self._params.get('xp')

    # Experiment properties
    # ---------------------

    def set_exp(self, key=None):
        if self._xp.get(key) is None: return None
        getattr(self, '_set_exp_' + key)()

    def _set_exp_any(self):
        xpp = dict2numpy(self._xp.get('any'))
        xp = Namespace()
        xp.name = 'any'
        xp.sigmas = xpp.get('sigmas')
        xp.taus = xpp.get('taus')
        xp.N_sigmas = xp.sigmas.size
        xp.N_taus = xp.taus.size
        xp.gskw = dict(exp=xp.name, **xpp)
        self.xp = xp

    @property
    def quality(self):
        if not hasattr(self, '_quality'):
            return self._cs.get('quality')
        else:
            return self._quality

    def set_quality(self, quality):
        if isinstance(quality, list):
            self._quality = quality

    @staticmethod
    def _qstr(quality):
        return ''.join([str(q) for q in sorted(quality)])

    @property
    def qstr(self):
        return self._qstr(self.quality)

    # Fold management
    # ---------------

    @property
    def _folds(self):
        return get_folds(self._css, self._targets, self._cv.get('N_folds'))

    # Cross validation
    # ----------------

    # def cv(self):
    #     for n, data in enumerate(self._folds):
    #         self._set_fold_parameters(fold=n + 1, data=data)
    #         # Training
    #         self._cube_of_predictions()
    #         self._matrix_of_accuracies()
    #         self._max_entries(moa)
    #         # Testing
    #         self._vector_of_predictions()
    #         self._scalar_of_accuracy()
    #         # Save to disk
    #         self._save_fold_results()

    def cv(self):
        self._cv_time = time.time()
        getattr(self, '_cv_' + self.xp.name)()

    def _cv_any(self):
        for n, data in enumerate(self._folds):
            # Fold number
            fnum = n + 1
            # Unpack training and test fold (f: features; l: labels)
            (train_f, train_l), (test_f, test_l) = data
            try:
                # raise Exception

                # Training set
                cop = self._cop_any(train_f)
                moa = self._moa_any(cop, train_l)
                self._save_fold_results_any(moa=moa, ftype='train', fnum=fnum)

            except Exception as e:
                print '_cv_any:', fnum, 'train'
                print '_cv_any:', e.message
                print '_cv_any:', '-' * 50, '\n'

            try:
                # raise Exception

                # Test set
                cop = self._cop_any(test_f)
                moa = self._moa_any(cop, test_l)
                self._save_fold_results_any(moa=moa, ftype='test', fnum=fnum)

            except Exception as e:
                print '_cv_any:', fnum, 'test'
                print '_cv_any:', e.message
                print '_cv_any:', '-' * 50, '\n'

    # Grid search
    # -----------

    def _cube_of_predictions(self, features):
        """Store result of training with chosen experiment"""
        # self._cop = getattr(self, '_cop_' + self.xp.name)()
        return getattr(self, '_cop_' + self.xp.name)(features)

    def _cop_any(self, features):
        """Return result of training with experiment ANY"""
        N = features.shape[0]
        cop = np.zeros((self.xp.N_sigmas, self.xp.N_taus, N)).astype(bool)
        for k, cs in enumerate(features):
            cs.set_fname_gsp('prediction_Q-{}.csv'.format(self.qstr))
            if cs.has_gs_prediction and cs.gs_prediction_time > self._cv_time:
            # if cs.has_gs_prediction and cs.gs_prediction_time > self._cv_time - 2 * 3600.:
                cop[:, :, k] = cs.gs_prediction
            else:
                cs.load(**self._cs)
                # print 'cs.quality:', cs.quality
{'fraction_non_alphanumeric': 0.13471557682083998, 'fraction_numerical': 0.004253056884635832, 'mean_word_length': 1.8263221153846154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2119942', 'n_tokens_mistral': 3103, 'n_tokens_neox': 2923, 'n_words': 674}