def load_config():
    config_parser = ConfigParser.RawConfigParser()
    config_parser.read(options['load'])
    if not config_parser.has_section('Result'):
        raise IOError("Invalid configuration file.")

    moments   = config.readMatrix(config_parser, 'Result', 'moments',   float)
    density = config.readMatrix(config_parser, 'Result', 'density', float)
    mpost     = config.readVector(config_parser, 'Result', 'mpost',     float)
    if config_parser.has_option('Result', 'bprob'):
        bprob = config.readVector(config_parser, 'Result', 'bprob',     float)
    else:
        bprob     = []
    result = {
        'moments'   : moments,
        'density' : density,
        'bprob'     : bprob,
        'mpost'     : mpost }
    return result
def parseConfig(config_file):
    config_parser = ConfigParser.RawConfigParser()
    config_parser.read(config_file)

    data = { 'K'     : 2,
             'L'     : 0,
             'gt'    : None,
             'alpha' : None,
             'beta'  : None,
             'gamma' : None }

    if config_parser.sections() == []:
        raise IOError("Invalid configuration file.")
    if config_parser.has_section('Ground Truth'):
        config.readVisualization(config_parser, 'Ground Truth', os.path.dirname(config_file), options)
        config.readFilter(config_parser, 'Ground Truth', os.path.dirname(config_file), options)
        config.readAlgorithm(config_parser, 'Ground Truth', os.path.dirname(config_file), options)
        config.readMgsSamples(config_parser, 'Ground Truth', os.path.dirname(config_file), options)
        data['gt'] = config.readVector(config_parser, 'Ground Truth', 'gt', float)
        data['L'] = len(data['gt'])
        data['alpha'], data['beta'], data['gamma'] = \
            config.getParameters(config_parser, 'Ground Truth', os.path.dirname(config_file), data['K'], data['L'])
        config.readStrategy(config_parser, 'Ground Truth', options)
        result = loadResult()
        if not result['states']:
            result['states'] = config.readSeeds(config_parser, 'Ground Truth', 'seeds')
        result = sample(result, data)
    if config_parser.has_section('Experiment'):
        config.readVisualization(config_parser, 'Experiment', os.path.dirname(config_file), options)
        config.readFilter(config_parser, 'Experiment', os.path.dirname(config_file), options)
        config.readAlgorithm(config_parser, 'Experiment', os.path.dirname(config_file), options)
        config.readMgsSamples(config_parser, 'Experiment', os.path.dirname(config_file), options)
        data['L'] = int(config_parser.get('Experiment', 'bins'))
        data['alpha'], data['beta'], data['gamma'] = \
            config.getParameters(config_parser, 'Experiment', os.path.dirname(config_file), data['K'], data['L'])
        config.readStrategy(config_parser, 'Experiment', options)
        result = loadResult()
        result = sample(result, data)
    if options['video']:
        save_video()
    if options['save']:
        saveResult(result)
    else:
        if options['savefig']:
            importMatplotlib('Agg')
            from matplotlib.pyplot import savefig
            vis.plotSampling(result, options, data)
#            savefig(options['savefig'], bbox_inches='tight', pad_inches=0)
            savefig(options['savefig'])
        else:
            importMatplotlib()
            from matplotlib.pyplot import show
            vis.plotSampling(result, options, data)
            show()
def parseConfig(config_file):
    config_parser = ConfigParser.RawConfigParser()
    config_parser.read(config_file)

    if config_parser.sections() == []:
        raise IOError("Invalid configuration file.")
    if config_parser.has_section('Counts'):
        config.readVisualization(config_parser, 'Counts', os.path.dirname(config_file), options)
        config.readAlgorithm(config_parser, 'Counts', os.path.dirname(config_file), options)
        config.readMgsSamples(config_parser, 'Counts', os.path.dirname(config_file), options)
        counts = config.readCounts(config_parser, 'Counts')
        K, L   = len(counts), len(counts[0])
        alpha, beta, gamma = config.getParameters(config_parser, 'Counts', os.path.dirname(config_file), K, L)
        result = call_posterior(counts, alpha, beta, gamma)
        if options['save']:
            saveResult(result)
        else:
            if options['savefig']:
                importMatplotlib('Agg')
                from matplotlib.pyplot import savefig
                vis.plotBinning(result, options)
                savefig(options['savefig'], bbox_inches='tight', pad_inches=0)
            else:
                importMatplotlib()
                from matplotlib.pyplot import show
                vis.plotBinning(result, options)
                show()
    if config_parser.has_section('Trials'):
        config.readVisualization(config_parser, 'Trials', os.path.dirname(config_file), options)
        config.readAlgorithm(config_parser, 'Trials', os.path.dirname(config_file), options)
        config.readMgsSamples(config_parser, 'Trials', os.path.dirname(config_file), options)
        binsize   = config_parser.getint('Trials', 'binsize')
        timings   = config.readMatrix(config_parser, 'Trials', 'timings', int)
        srange    = None
        if config_parser.has_option('Trials', 'range'):
            srange = config.readVector(config_parser, 'Trials', 'range', int)
        x, counts = timingsToCounts(timings, binsize, srange)
        K, L   = len(counts), len(counts[0])
        alpha, beta, gamma = config.getParameters(config_parser, 'Trials', os.path.dirname(config_file), K, L)
        result    = call_posterior(counts, alpha, beta, gamma)
        if options['save']:
            saveResult(result)
        else:
            if options['savefig']:
                importMatplotlib('Agg')
                from matplotlib.pyplot import savefig
                vis.plotBinningSpikes(x, timings, result, options)
                savefig(options['savefig'], bbox_inches='tight', pad_inches=0)
            else:
                importMatplotlib()
                from matplotlib.pyplot import show
                vis.plotBinningSpikes(x, timings, result, options)
                show()
def loadResult():
    if options['load']:
        config_parser = ConfigParser.RawConfigParser()
        config_parser.read(options['load'])
        if not config_parser.has_section('Sampling Result'):
            raise IOError("Invalid configuration file.")

        distances = []
        bprob     = []

        counts    = config.readMatrix(config_parser, 'Sampling Result', 'counts',  int)
        moments   = config.readMatrix(config_parser, 'Sampling Result', 'moments', float)
        density   = config.readMatrix(config_parser, 'Sampling Result', 'density', float)
        samples   = config.readVector(config_parser, 'Sampling Result', 'samples', int)
        mpost     = config.readVector(config_parser, 'Sampling Result', 'mpost',   float)
        states    = config.readStates(config_parser, 'Sampling Result', 'states')
        if config_parser.has_option('Sampling Result', 'distances'):
            distances = config.readVector(config_parser, 'Sampling Result', 'distances', float)
        if config_parser.has_option('Sampling Result', 'bprob'):
            bprob = config.readVector(config_parser, 'Sampling Result', 'bprob',   float)
        result = {
            'distances' : distances,
            'moments'   : moments,
            'density'   : density,
            'bprob'     : bprob,
            'mpost'     : mpost,
            'counts'    : counts,
            'samples'   : samples,
            'states'    : states }
    else:
        result = {
            'distances' : [],
            'moments'   : [],
            'density'   : [],
            'bprob'     : [],
            'mpost'     : [],
            'counts'    : [],
            'samples'   : [],
            'states'    : [] }
    return result