# 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
                    clu = f.read()
                with open(self.filename_clu.replace('.clu.', 
                    '.clu_original.'), 'w') as f:
                    f.write(clu)
            if os.path.exists(self.filename_aclu):
                # Save the original clu file if it does not exist yet.
                with open(self.filename_aclu, 'r') as f:
                    clu = f.read()
                with open(self.filename_aclu.replace('.aclu.', 
                    '.aclu_original.'), 'w') as f:
                    f.write(clu)
    
    
    # Internal read methods.
    # ----------------------
    def read_metadata(self):
        try:
            self.metadata = read_xml(self.filename_xml, self.fileindex)
        except:
            # Die if no XML file is available for this dataset, as it contains
            # critical metadata.
            raise IOError("The XML file is missing.")
            
        self.nsamples = self.metadata.get('nsamples')
        self.nchannels = self.metadata.get('nchannels')
        self.fetdim = self.metadata.get('fetdim')
        self.freq = self.metadata.get('freq')
        
    def read_probe(self):
        if self.filename_probe is None:

# 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

                    clu = f.read()
                with open(self.filename_clu.replace('.clu.', 
                    '.clu_original.'), 'w') as f:
                    f.write(clu)
            if os.path.exists(self.filename_aclu):
                # Save the original clu file if it does not exist yet.
                with open(self.filename_aclu, 'r') as f:
                    clu = f.read()
                with open(self.filename_aclu.replace('.aclu.', 
                    '.aclu_original.'), 'w') as f:
                    f.write(clu)
    
    
    # Internal read methods.
    # ----------------------
    def read_metadata(self):
        try:
            self.metadata = read_xml(self.filename_xml, self.fileindex)
        except:
            # Die if no XML file is available for this dataset, as it contains
            # critical metadata.
            raise IOError("The XML file is missing.")
            
        self.nsamples = self.metadata.get('nsamples')
        self.nchannels = self.metadata.get('nchannels')
        self.fetdim = self.metadata.get('fetdim')
        self.freq = self.metadata.get('freq')
        
    def read_probe(self):
        if self.filename_probe is None:

            info("No probe file has been found.")