示例#1
0
文件: SALS.py 项目: steaime/DSHpy
def LoadFromConfig(ConfigFile, input_sect='input', outFolder=None):
    """Loads a SALS object from a config file like the one exported with VelMaps.ExportConfig()
    
    Parameters
    ----------
    ConfigFile : full path of the config file to read
    outFolder : folder containing velocity and correlation maps. 
                if None, the value from the config file will be used
                if not None, the value from the config file will be discarded
                
    Returns
    -------
    a SALS object, eventually with an "empty" image MIfile (containing metadata but no actual image data)
    """
    config = cf.Config(ConfigFile)
    froot = config.Get('global', 'root', '', str)
    miin_fname = config.Get(input_sect, 'mi_file', None, str)
    miin_meta_fname = config.Get(input_sect, 'meta_file', None, str)
    input_stack = False
    if (miin_fname is not None):
        # if miin_fname is a string, let's use a single MIfile as input.
        # otherwise, it can be a list: in that case, let's use a MIstack as input
        if (isinstance(miin_fname, str)):
            miin_fname = os.path.join(froot, miin_fname)
        else:
            input_stack = True
            for i in range(len(miin_fname)):
                miin_fname[i] = os.path.join(froot, miin_fname[i])
    if (miin_meta_fname is not None):
        miin_meta_fname = os.path.join(froot, miin_meta_fname)
    elif input_stack:
        logging.error(
            'SALS.LoadFromConfig ERROR: medatada filename must be specified when loading a MIstack'
        )
        return None
    if input_stack:
        MIin = MIs.MIstack(miin_fname,
                           miin_meta_fname,
                           Load=True,
                           StackType='t')
    else:
        MIin = MI.MIfile(miin_fname, miin_meta_fname)
    ctrPos = config.Get('SALS_parameters', 'center_pos', None, float)
    if (ctrPos is None):
        logging.error(
            'SALS.LoadFromConfig ERROR: no SALS_parameters.center_pos parameter found in config file '
            + str(ConfigFile))
        return None
    else:
        r_max = ppf.MaxRadius(MIin.ImageShape(), ctrPos)
        radRange = sf.ValidateRange(config.Get('SALS_parameters', 'r_range',
                                               None, float),
                                    r_max,
                                    MinVal=1,
                                    replaceNone=True)
        angRange = sf.ValidateRange(config.Get('SALS_parameters', 'a_range',
                                               None, float),
                                    2 * np.pi,
                                    replaceNone=True)
        rSlices = np.geomspace(radRange[0],
                               radRange[1],
                               int(radRange[2]) + 1,
                               endpoint=True)
        aSlices = np.linspace(angRange[0],
                              angRange[1],
                              int(angRange[2]) + 1,
                              endpoint=True)
        if (outFolder is None):
            outFolder = config.Get(input_sect, 'out_folder', None, str)
            if (outFolder is not None):
                outFolder = os.path.join(config.Get('global', 'root', '', str),
                                         outFolder)
        mask = config.Get('SALS_parameters', 'px_mask', None, str)
        mask = MI.ReadBinary(
            sf.PathJoinOrNone(froot,
                              config.Get(input_sect, 'px_mask', mask, str)),
            MIin.ImageShape(), MIin.DataFormat(), 0)
        dark = MI.ReadBinary(
            sf.PathJoinOrNone(froot,
                              config.Get(input_sect, 'dark_bkg', None, str)),
            MIin.ImageShape(), MIin.DataFormat(), 0)
        opt = MI.ReadBinary(
            sf.PathJoinOrNone(froot,
                              config.Get(input_sect, 'opt_bkg', None, str)),
            MIin.ImageShape(), MIin.DataFormat(), 0)
        PD_data = sf.PathJoinOrNone(
            froot, config.Get(input_sect, 'pd_file', None, str))
        if (PD_data is not None):
            PD_data = np.loadtxt(PD_data, dtype=float)
        img_times = config.Get(input_sect, 'img_times', None, str)
        if img_times is not None:
            # if miin_fname is a string, let's use a single text file as input.
            # otherwise, it can be a list: in that case, let's open each text file and append all results
            if (isinstance(img_times, str)):
                img_times = np.loadtxt(os.path.join(froot, img_times),
                                       dtype=float,
                                       usecols=config.Get(
                                           'format', 'img_times_colidx', 0,
                                           int),
                                       skiprows=1)
            else:
                tmp_times = np.empty(shape=(0, ), dtype=float)
                for cur_f in img_times:
                    tmp_times = np.append(
                        tmp_times,
                        np.loadtxt(os.path.join(froot, cur_f),
                                   dtype=float,
                                   usecols=config.Get('format',
                                                      'img_times_colidx', 0,
                                                      int),
                                   skiprows=1))
                img_times = tmp_times
        exp_times = sf.PathJoinOrNone(
            froot, config.Get(input_sect, 'exp_times', None, str))
        if (exp_times is not None):
            exp_times = np.unique(
                np.loadtxt(exp_times,
                           dtype=float,
                           usecols=config.Get('format', 'exp_times_colidx', 0,
                                              int)))
        dlsLags = config.Get('SALS_parameters', 'dls_lags', None, int)
        tavgT = config.Get('SALS_parameters', 'timeavg_T', None, int)
        return SALS(MIin, outFolder, ctrPos, [rSlices, aSlices], mask,
                    [dark, opt, PD_data], exp_times, dlsLags, img_times, tavgT)
示例#2
0
文件: MIfile.py 项目: steaime/DSHpy
def Validate_zRange(zRange, zSize, replaceNone=True):
    return sf.ValidateRange(zRange, zSize, MinVal=0, replaceNone=replaceNone)