Esempio n. 1
0
def read_mc_chain(f):
    """
    Reads the mcmc chain created with emcee
    :param f: chain_file
    :return:
    """

    # read the file
    lines = read_text_file(f)

    # key counter and ouput dictionary
    chainlog = {}
    hkcounter = 0

    # define header keys
    head_keys = ['name', 'component', 'group']
    for l in lines:
        d = l.split()
        # print d
        for hk in head_keys:
            if l.find(hk) > -1:
                # groups are integers of course
                if hk == 'group':
                    d[2:] = map(int, d[2:])
                else:
                    d[2:] = d[2:]

                # append the header info
                chainlog[hk] = d[2:]
                hkcounter += 1
                break

        # once we read all data, we end
        if hkcounter == 3:
            break

    # load the file
    d = np.loadtxt(f)

    # get fit properties
    nwalkers = int(np.max(d[:, 0])) + 1
    niter = len(d[:, 0]) / nwalkers
    npars = len(d[0]) - 2

    # remove the first column with numbering
    chainlog['data'] = d[:, 1:]
    return chainlog, nwalkers, niter, npars
Esempio n. 2
0
def read_mc_chain(f):
    """
    Reads the mcmc chain created with emcee
    :param f: chain_file
    :return:
    """

    # read the file
    lines = read_text_file(f)

    # key counter and ouput dictionary
    chainlog = {}
    hkcounter = 0

    # define header keys
    head_keys = ['name', 'component', 'group']
    for l in lines:
        d = l.split()
        # print d
        for hk in head_keys:
            if l.find(hk) > -1:
                # groups are integers of course
                if hk == 'group':
                    d[2:] = map(int, d[2:])
                else:
                    d[2:] = d[2:]

                # append the header info
                chainlog[hk] = d[2:]
                hkcounter += 1
                break

        # once we read all data, we end
        if hkcounter == 3:
            break

    # load the file
    d = np.loadtxt(f)

    # get fit properties
    nwalkers = int(np.max(d[:, 0])) + 1
    niter = len(d[:, 0]) / nwalkers
    npars = len(d[0]) - 2

    # remove the first column with numbering
    chainlog['data'] = d[:, 1:]
    return chainlog, nwalkers, niter, npars
Esempio n. 3
0
def read_fitlog(f):
    """
    Reads the fitting log and stores it within a dictionary.
    :param f:
    :return:
    """
    # read the file
    lines = read_text_file(f)

    # key counter and ouput dictionary
    fitlog = {}
    hkcounter = 0

    # define header keys
    head_keys = ['name', 'component', 'group']
    for l in lines:
        d = l.split()
        # print d
        for hk in head_keys:
            if l.find(hk) > -1:
                # groups are integers of course
                if hk == 'group':
                    d[2:] = map(int, d[2:])
                else:
                    d[2:] = d[2:]

                # append the header info
                fitlog[hk] = d[2:]
                hkcounter += 1
                break

        # once we read all data, we end
        if hkcounter == 3:
            break

    # print fitlog
    # append data
    fitlog['data'] = np.loadtxt(f)

    return fitlog
Esempio n. 4
0
def read_fitlog(f):
    """
    Reads the fitting log and stores it within a dictionary.
    :param f:
    :return:
    """
    # read the file
    lines = read_text_file(f)

    # key counter and ouput dictionary
    fitlog = {}
    hkcounter = 0

    # define header keys
    head_keys = ['name', 'component', 'group']
    for l in lines:
        d = l.split()
        # print d
        for hk in head_keys:
            if l.find(hk) > -1:
                # groups are integers of course
                if hk == 'group':
                    d[2:] = map(int, d[2:])
                else:
                    d[2:] = d[2:]

                # append the header info
                fitlog[hk] = d[2:]
                hkcounter += 1
                break

        # once we read all data, we end
        if hkcounter == 3:
            break

    # print fitlog
    # append data
    fitlog['data'] = np.loadtxt(f)

    return fitlog
Esempio n. 5
0
    def load(self, f):
        """
        Loads the text representation of the class from
        a file f.
        :param f
        :return:
        """

        # read the file
        lines = read_text_file(f)
        data_start = len(lines)
        for i, l in enumerate(lines):
            if l.find('FITTER') > -1:
                data_start = i
                break

        # check that there are actually some data in the file
        if data_start >= len(lines):
            return False

        # create the class
        fitter = Fitter()

        name = None
        fit_kwargs = {}
        # from here the file is actually being read
        for i, l in enumerate(lines[data_start + 1:]):

            # once we reach FITTER again we end
            if l.find('FITTER') > -1:
                break
            # split the line
            d = l.split()
            # print d
            # save the name
            if d[0].find('fitter:') > -1:
                name = d[1]

            # save the kwargs
            elif d[0].find('fit_parameters:') > -1:
                d = d[1:]
                if len(d) < 2:
                    continue
                fit_kwargs = {
                    d[i].strip(':'): float(d[i + 1])
                    for i in range(0, len(d), 2)
                }

            # do the same for enviromental keys
            if d[0].find('env_keys:') > -1:
                # the first string is just identification
                d = d[1:]

                # secure corrct types
                recs = ['debug', 'verbose', 'fitlog']
                cast_types = [string2bool, string2bool, str]
                cdict = {
                    d[i].rstrip(':'): d[i + 1]
                    for i in range(0, len(d), 2)
                }
                for k in cdict.keys():
                    if k in recs:
                        i = recs.index(k)
                        ctype = cast_types[i]
                        cdict[k] = ctype(cdict[k])

                    # assign the vlues
                    setattr(fitter, k, cdict[k])

        # choose the fitter
        if name != 'None':
            fitter.choose_fitter(name, **fit_kwargs)
        else:
            return False

        # finally assign everything to self
        attrs = ['debug', 'fittername', 'verbose', 'fitlog', 'fit_kwargs']
        for attr in attrs:
            setattr(self, attr, getattr(fitter, attr))

        # if we got here, we loaded the data
        return True
Esempio n. 6
0
    def load(self, f):
        """
        Loads the text representation of the class from
        a file f.
        :param f
        :return:
        """

        # read the file
        lines = read_text_file(f)
        data_start = len(lines)
        for i, l in enumerate(lines):
            if l.find('FITTER') > -1:
                data_start = i
                break

        # check that there are actually some data in the file
        if data_start >= len(lines):
            return False

        # create the class
        fitter = Fitter()

        name = None
        fit_kwargs = {}
        # from here the file is actually being read
        for i, l in enumerate(lines[data_start+1:]):

            # once we reach FITTER again we end
            if l.find('FITTER') > -1:
                break
            # split the line
            d = l.split()
            # print d
            # save the name
            if d[0].find('fitter:') > -1:
                name = d[1]

            # save the kwargs
            elif d[0].find('fit_parameters:') > -1:
                d = d[1:]
                if len(d) < 2:
                    continue
                fit_kwargs = {d[i].strip(':'): float(d[i+1]) for i in range(0, len(d), 2)}

            # do the same for enviromental keys
            if d[0].find('env_keys:') > -1:
                # the first string is just identification
                d = d[1:]

                # secure corrct types
                recs = ['debug', 'verbose', 'fitlog']
                cast_types = [string2bool, string2bool, str]
                cdict = {d[i].rstrip(':'): d[i+1] for i in range(0, len(d), 2)}
                for k in cdict.keys():
                    if k in recs:
                        i = recs.index(k)
                        ctype = cast_types[i]
                        cdict[k] = ctype(cdict[k])

                    # assign the vlues
                    setattr(fitter, k, cdict[k])

        # choose the fitter
        if name != 'None':
            fitter.choose_fitter(name, **fit_kwargs)
        else:
            return False

        # finally assign everything to self
        attrs = ['debug', 'fittername', 'verbose', 'fitlog', 'fit_kwargs']
        for attr in attrs:
            setattr(self, attr, getattr(fitter, attr))

        # if we got here, we loaded the data
        return True