# Path: kwiklib/utils/logger.py
# def register(logger):
#     name = logger.name
#     if name not in LOGGERS:
#         LOGGERS[name] = logger
# 
# def unregister(logger):
#     name = logger.name
#     if name in LOGGERS:
#         LOGGERS[name].close()
#         del LOGGERS[name]
# 
# class FileLogger(Logger):
#     def __init__(self, filename=None, **kwargs):
#         kwargs['handler'] = logging.FileHandler(filename)
#         super(FileLogger, self).__init__(**kwargs)
#         
#     def close(self):
#         self.handler.close()
#         self._logger.removeHandler(self.handler)
#         del self.handler
#         del self._logger
# 
# def debug(self, msg):
#     self._logger.debug(self.get_message(msg))
# 
# def info(self, msg):
#     self._logger.info(self.get_message(msg))
# 
# def warn(self, msg):
#     self._logger.warn(self.get_message(msg))


import os
import os.path
import re
import xml.etree.ElementTree as ET
import numpy as np
import pandas as pd
from loader import (Loader, default_group_info, reorder, renumber_clusters,
    default_cluster_info)
from tools import (load_text, normalize,
    load_binary, load_pickle, save_text, get_array,
    first_row, load_binary_memmap)
from selection import (select, select_pairs, get_spikes_in_clusters,
    get_some_spikes_in_clusters, get_some_spikes, get_indices)
from kwiklib.utils.logger import (register, unregister, FileLogger, 
    debug, info, warn)
from kwiklib.utils.colors import COLORS_COUNT, generate_colors
    def read_fil(self):
        try:
            self.fil = read_dat(self.filename_fil, self.nchannels)
        except IOError:
            warn("The FIL file is missing.")
    
    # def read_stats(self):
        # self.ncorrbins = 100 #SETTINGS.get('correlograms.ncorrbins', 100)
        # self.corrbin = .001  #SETTINGS.get('correlograms.corrbin', .001)

        
    # Log file.
    # ---------
    def initialize_logfile(self):
        # filename = self.filename_fet.replace('.fet.', '.kvwlg.')
        self.logfile = FileLogger(self.filename_kvwlg, name='datafile', 
            level=self.userpref['loglevel_file'])
        # Register log file.
        register(self.logfile)
        
    
    # Public methods.
    # ---------------
    def read(self):
        self.initialize_logfile()
        # Load the similarity measure chosen by the user in the preferences
        # file: 'gaussian' or 'kl'.
        # Refresh the preferences file when a new file is opened.
        # USERPREF.refresh()
        self.similarity_measure = self.userpref['similarity_measure'] or 'gaussian'

# Path: kwiklib/utils/logger.py
# def register(logger):
#     name = logger.name
#     if name not in LOGGERS:
#         LOGGERS[name] = logger
# 
# def unregister(logger):
#     name = logger.name
#     if name in LOGGERS:
#         LOGGERS[name].close()
#         del LOGGERS[name]
# 
# class FileLogger(Logger):
#     def __init__(self, filename=None, **kwargs):
#         kwargs['handler'] = logging.FileHandler(filename)
#         super(FileLogger, self).__init__(**kwargs)
#         
#     def close(self):
#         self.handler.close()
#         self._logger.removeHandler(self.handler)
#         del self.handler
#         del self._logger
# 
# def debug(self, msg):
#     self._logger.debug(self.get_message(msg))
# 
# def info(self, msg):
#     self._logger.info(self.get_message(msg))
# 
# def warn(self, msg):
#     self._logger.warn(self.get_message(msg))


# Path: kwiklib/dataio/klustersloader.py
import os
import os.path
import re
import xml.etree.ElementTree as ET
import numpy as np
import pandas as pd
from loader import (Loader, default_group_info, reorder, renumber_clusters,
    default_cluster_info)
from tools import (load_text, normalize,
    load_binary, load_pickle, save_text, get_array,
    first_row, load_binary_memmap)
from selection import (select, select_pairs, get_spikes_in_clusters,
    get_some_spikes_in_clusters, get_some_spikes, get_indices)
from kwiklib.utils.logger import (register, unregister, FileLogger, 
    debug, info, warn)
from kwiklib.utils.colors import COLORS_COUNT, generate_colors

    def read_fil(self):
        try:
            self.fil = read_dat(self.filename_fil, self.nchannels)
        except IOError:
            warn("The FIL file is missing.")
    
    # def read_stats(self):
        # self.ncorrbins = 100 #SETTINGS.get('correlograms.ncorrbins', 100)
        # self.corrbin = .001  #SETTINGS.get('correlograms.corrbin', .001)

        
    # Log file.
    # ---------
    def initialize_logfile(self):
        # filename = self.filename_fet.replace('.fet.', '.kvwlg.')
        self.logfile = FileLogger(self.filename_kvwlg, name='datafile', 
            level=self.userpref['loglevel_file'])
        # Register log file.
        register(self.logfile)
        
    
    # Public methods.
    # ---------------
    def read(self):
        self.initialize_logfile()
        # Load the similarity measure chosen by the user in the preferences
        # file: 'gaussian' or 'kl'.
        # Refresh the preferences file when a new file is opened.
        # USERPREF.refresh()
        self.similarity_measure = self.userpref['similarity_measure'] or 'gaussian'

        debug("Similarity measure: {0:s}.".format(self.similarity_measure))