<commit_before>"""
predict labels for birdsong syllables,
using already-trained models specified in config file
"""

import os
import glob

# from dependencies
import numpy as np
import scipy.io as scio # to load matlab files
from sklearn.externals import joblib
# from sklearn import neighbors, SVC

# from hvc
import hvc.featureextract
from .parseconfig import parse_config
from .utils import timestamp

# used in loop below, see there for explanation
SHOULD_BE_DOUBLE = ['Fs',
                    'min_dur',
                    'min_int',
                    'offsets',
                    'onsets',
                    'sm_win',
                    'threshold']

model_str_rep_map = {
    "<class 'sklearn.neighbors.classification.KNeighborsClassifier'>": 'knn',
    "svm thingy": 'svm',
    "keras thingy": 'flatwindow'
}


def predict(config_file):
    """main function that does prediction
    Does not return anything, just runs through directories specified in config_file
    and classifies syllables using model.
    
    Parameters
    ----------
    config_file : string
        filename of YAML file that configures label prediction   
    """

    predict_config = parse_config(config_file, 'predict')
    print('parsed predict config')

    home_dir = os.getcwd()

    for todo in predict_config['todo_list']:

        output_dir = 'predict_output_' + timestamp()
        output_dir_with_path = os.path.join(todo['output_dir'], output_dir)
        if not os.path.isdir(output_dir_with_path):
            os.mkdir(output_dir_with_path)

        model_file = joblib.load(todo['model_file'])

        extract_params = {
            'bird_ID': todo['bird_ID'],
            'feature_list': model_file['model_feature_list'],
            'output_dir': output_dir_with_path,
            'home_dir': home_dir,
            'data_dirs': todo['data_dirs'],
            'labelset': 'all',
            'file_format': todo['file_format']
        }

        model_feature_file = model_file['feature_file']
        model_feature_file = joblib.load(model_feature_file)
        extract_params['segment_params'] = model_feature_file['segment_params']
        extract_params['spect_params'] = model_feature_file['spect_params']

        hvc.featureextract._extract(extract_params, make_summary_file=False)

        os.chdir(output_dir_with_path)
        ftr_files = glob.glob('features_from*')
        clf = model_file['clf']
        scaler = model_file['scaler']

        #clf_type = model_str_rep_map[str(clf)]
        # if clf_type == 'knn':
        #     if 'neighbors.KNeighborsClassifier' not in locals():
        #         import neighbors.KNeighborsClassifier
        # elif clf_type == 'svm':
        #     if SVC not in locals():
        #         from sklearn.svm import SVC
        # elif clf_type == flatwindow

        for ftr_file in ftr_files:
            ftr_file_dict = joblib.load(ftr_file)
            features = ftr_file_dict['features']
            # check classifier type without importing every model
            if str(type(clf)) == \
                    "<class 'sklearn.neighbors.classification.KNeighborsClassifier'>":<commit_msg>clearer variable names in labelpredict<commit_after>"""
predict labels for birdsong syllables,
using already-trained models specified in config file
"""

import os
import glob

# from dependencies
import numpy as np
import scipy.io as scio # to load matlab files
from sklearn.externals import joblib
# from sklearn import neighbors, SVC

# from hvc
import hvc.featureextract
from .parseconfig import parse_config
from .utils import timestamp

# used in loop below, see there for explanation
SHOULD_BE_DOUBLE = ['Fs',
                    'min_dur',
                    'min_int',
                    'offsets',
                    'onsets',
                    'sm_win',
                    'threshold']

model_str_rep_map = {
    "<class 'sklearn.neighbors.classification.KNeighborsClassifier'>": 'knn',
    "svm thingy": 'svm',
    "keras thingy": 'flatwindow'
}


def predict(config_file):
    """main function that does prediction
    Does not return anything, just runs through directories specified in config_file
    and classifies syllables using model.
    
    Parameters
    ----------
    config_file : string
        filename of YAML file that configures label prediction   
    """

    predict_config = parse_config(config_file, 'predict')
    print('parsed predict config')

    home_dir = os.getcwd()

    for todo in predict_config['todo_list']:

        output_dir = 'predict_output_' + timestamp()
        output_dir_with_path = os.path.join(todo['output_dir'], output_dir)
        if not os.path.isdir(output_dir_with_path):
            os.mkdir(output_dir_with_path)

        model_file = joblib.load(todo['model_file'])

        extract_params = {
            'bird_ID': todo['bird_ID'],
            'feature_list': model_file['model_feature_list'],
            'output_dir': output_dir_with_path,
            'home_dir': home_dir,
            'data_dirs': todo['data_dirs'],
            'labelset': 'all',
            'file_format': todo['file_format']
        }

        feature_file_for_model = model_file['feature_file']
        feature_file = joblib.load(feature_file_for_model)
        extract_params['segment_params'] = feature_file['segment_params']
        extract_params['spect_params'] = feature_file['spect_params']

        hvc.featureextract._extract(extract_params, make_summary_file=False)

        os.chdir(output_dir_with_path)
        ftr_files = glob.glob('features_from*')
        import pdb;pdb.set_trace()
        clf = model_file['clf']
        scaler = model_file['scaler']

        #clf_type = model_str_rep_map[str(clf)]
        # if clf_type == 'knn':
        #     if 'neighbors.KNeighborsClassifier' not in locals():
        #         import neighbors.KNeighborsClassifier
        # elif clf_type == 'svm':
        #     if SVC not in locals():
        #         from sklearn.svm import SVC
        # elif clf_type == flatwindow

        for ftr_file in ftr_files:
            ftr_file_dict = joblib.load(ftr_file)
            features = ftr_file_dict['features']
            # check classifier type without importing every model
            if str(type(clf)) == \
                    "<class 'sklearn.neighbors.classification.KNeighborsClassifier'>":
{'fraction_non_alphanumeric': 0.09848961611076149, 'fraction_numerical': 0.0, 'mean_word_length': 2.5474330357142856, 'pattern_counts': {'":': 8, '<': 7, '<?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': '3263004', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1861, 'n_words': 433}