Example #1
0
def OpenG2M1s(froot, expt_idx=None, roi_idx=None):
    res = []
    filter_str = ''
    if roi_idx is not None:
        filter_str += '_' + ROI_PREFIX + str(roi_idx).zfill(ROI_IDXLEN)
    if expt_idx is not None:
        filter_str += '_' + EXP_PREFIX + +str(expt_idx).zfill(EXP_IDXLEN)
    fnames_list = sf.FindFileNames(froot,
                                   Prefix=G2M1_PREFIX,
                                   Ext='.dat',
                                   FilterString=filter_str,
                                   Sort='ASC')
    ROI_list = [sf.FirstIntInStr(name) for name in fnames_list]
    exptime_list = [sf.LastIntInStr(name) for name in fnames_list]
    lagtimes = []
    imgtimes = []
    for i, fname in enumerate(fnames_list):
        res_g2m1, res_hdr = sf.LoadResFile(os.path.join(froot, fname),
                                           delimiter=TXT_DELIMITER,
                                           comments=TXT_COMMENT,
                                           readHeader=True,
                                           isolateFirst=0)
        res.append(res_g2m1[:, 1::2].T)
        lagtimes.append(res_g2m1[:, ::2].T)
        imgtimes.append(
            np.asarray([
                sf.FirstFloatInStr(res_hdr[j])
                for j in range(1, len(res_hdr), 2)
            ]))
    return res, lagtimes, imgtimes, ROI_list, exptime_list
Example #2
0
def LoadFolder(mi_folder,
               config_fname,
               config_section='MIfile',
               mi_prefix='',
               mi_ext='.dat',
               mi_sort='ASC',
               open_mifiles=True,
               stack_type='tau'):
    """Searches for MIfiles in a folder and loads them
        
    Parameters
    ----------
    mi_folder      : folder to search.
    config_fname   : full path of configuration filename
    config_section : title of the configuration section
    mi_prefix      : filter only files in folder whose filename begins with mi_prefix
    mi_ext         : filter only files in folder whose filename ends with mi_ext
    mi_sort        : sort filenames in ascending (ASC) or descending (DESC) order
    open_mifiles   : if True, open all MIfiles for reading
    stack_type     : {'tau','t'}, sets MIstack.StackType
    
    Returns
    -------
    conf_cmaps: configuration file for correlation maps
    cmap_mifiles: list of correlation maps, one per time delay
    all_lagtimes: list of lagtimes
    """
    assert os.path.isdir(
        mi_folder), 'MIfile folder ' + str(mi_folder) + ' not found.'
    assert os.path.isfile(
        config_fname), 'Configuration file ' + str(config_fname) + ' not found'
    all_mi_fnames = sf.FindFileNames(mi_folder,
                                     Prefix=mi_prefix,
                                     Ext=mi_ext,
                                     Sort=mi_sort,
                                     AppendFolder=True)
    mi_stack = MIstack(MIfiles=all_mi_fnames,
                       MetaData=config_fname,
                       MetaDataSection=config_section,
                       Load=True,
                       OpenFiles=open_mifiles,
                       StackType=stack_type)
    return mi_stack
Example #3
0
def OpenCIs(froot):
    res = []
    fnames_list = sf.FindFileNames(froot,
                                   Prefix=CI_PREFIX + '_',
                                   Ext='.dat',
                                   Sort='ASC')
    ROI_list = [sf.FirstIntInStr(name) for name in fnames_list]
    exptime_list = [sf.LastIntInStr(name) for name in fnames_list]
    lagtimes = []
    imgtimes = []
    for i, fname in enumerate(fnames_list):
        res_cI, res_hdr, col_times = sf.LoadResFile(os.path.join(froot, fname),
                                                    delimiter=TXT_DELIMITER,
                                                    comments=TXT_COMMENT,
                                                    readHeader=True,
                                                    isolateFirst=1)
        res.append(res_cI)
        lagtimes.append(np.asarray([sf.FirstIntInStr(hdr) for hdr in res_hdr]))
        imgtimes.append(col_times)
    return res, imgtimes, lagtimes, ROI_list, exptime_list
Example #4
0
    def AverageG2M1(self):
        if self.timeAvg_T is None:
            self.timeAvg_T = self.NumTimes()
        cI_fnames = sf.FindFileNames(self.outFolder,
                                     Prefix=CI_PREFIX + '_',
                                     Ext='.dat')

        for cur_f in cI_fnames:
            cur_cI, cur_times, roi_idx, exp_idx = self.ReadCIfile(cur_f)
            tavg_num = cur_cI.shape[0] // self.timeAvg_T
            logging.debug(
                'cI time averages will be performed by dividing the {0} images into {1} windows of {2} images each'
                .format(self.NumTimes(), tavg_num, self.timeAvg_T))

            if self.IsTimingConstant(cur_times):
                g2m1_lags = np.true_divide(self.dlsLags, self.MIinput.GetFPS())
                g2m1 = np.nan * np.ones(
                    (self.NumLagtimes(), tavg_num), dtype=float)
                for tavgidx in range(tavg_num):
                    g2m1[:, tavgidx] = np.nanmean(
                        cur_cI[tavgidx * self.timeAvg_T:(tavgidx + 1) *
                               self.timeAvg_T, :],
                        axis=0)
                str_hdr_g = self.txt_comment + 'dt' + ''.join([
                    TXT_DELIMITER +
                    't{0:.2f}'.format(cur_times[tavgidx * self.timeAvg_T])
                    for tavgidx in range(tavg_num)
                ])
                g2m1_out = np.append(g2m1_lags.reshape((-1, 1)), g2m1,
                                     axis=0).T
            else:
                g2m1_alllags, g2m1_laglist = self.FindTimelags(times=cur_times)
                g2m1 = np.zeros(
                    (tavg_num, np.max([len(l) for l in g2m1_laglist])),
                    dtype=float)
                g2m1_lags = np.nan * np.ones_like(g2m1, dtype=float)
                g2m1_avgnum = np.zeros_like(g2m1, dtype=int)
                for tidx in range(cur_cI.shape[0]):
                    cur_tavg_idx = tidx // self.timeAvg_T
                    #print((tidx, self.timeAvg_T, cur_tavg_idx, len(cur_tavg_idx)))
                    g2m1_lags[
                        cur_tavg_idx, :len(g2m1_laglist[cur_tavg_idx]
                                           )] = g2m1_laglist[cur_tavg_idx]
                    for lidx in range(cur_cI.shape[1]):
                        if (tidx < len(g2m1_alllags[lidx])):
                            cur_lagidx = np.argmin(
                                np.abs(
                                    np.subtract(g2m1_laglist[cur_tavg_idx],
                                                g2m1_alllags[lidx][tidx])))
                            if (~np.isnan(cur_cI[tidx, lidx])):
                                g2m1_avgnum[cur_tavg_idx, cur_lagidx] += 1
                                g2m1[cur_tavg_idx, cur_lagidx] += cur_cI[tidx,
                                                                         lidx]
                g2m1 = np.divide(g2m1, g2m1_avgnum)

                str_hdr_g = str(TXT_DELIMITER).join([
                    'dt' + TXT_DELIMITER +
                    't{0:.2f}'.format(cur_times[tavgidx * self.timeAvg_T])
                    for tavgidx in range(tavg_num)
                ])
                g2m1_out = np.empty((g2m1.shape[1], 2 * tavg_num), dtype=float)
                g2m1_out[:, 0::2] = g2m1_lags.T
                g2m1_out[:, 1::2] = g2m1.T

            np.savetxt(os.path.join(self.outFolder, G2M1_PREFIX + cur_f[2:]),
                       g2m1_out,
                       header=str_hdr_g,
                       **self.savetxt_kwargs)
Example #5
0
    def GetCorrMaps(self,
                    openMIfiles=True,
                    getAutocorr=True,
                    check_lagtimes=False):
        """Searches for MIfile correlation maps
        
        Parameters
        ----------
        openMIfiles: if true, it opens all MIfiles for reading.
        getAutocorr: if True, returns d0 in the list of correlation maps
                    otherwise, returns None instead of the autocorrelation map
        check_lagtimes: if true, checks that the lagtimes extracted from the filenames match with self.lagList
        
        Returns
        -------
        corr_config: configuration file for correlation maps
        corr_mifiles: list of correlation maps, one per time delay
        lag_list: list of lagtimes
        """

        if not self._corrmaps_loaded:

            assert os.path.isdir(
                self.outFolder), 'Correlation map folder ' + str(
                    self.outFolder) + ' not found.'
            config_fname = os.path.join(self.outFolder, 'CorrMapsConfig.ini')
            assert os.path.isfile(config_fname), 'Configuration file ' + str(
                config_fname) + ' not found'
            self.conf_cmaps = cf.Config(config_fname)

            all_cmap_fnames = sf.FindFileNames(self.outFolder,
                                               Prefix='CorrMap_d',
                                               Ext='.dat',
                                               Sort='ASC',
                                               AppendFolder=True)
            self.cmap_mifiles = []
            self.all_lagtimes = []
            for i in range(len(all_cmap_fnames)):
                cur_lag = sf.LastIntInStr(all_cmap_fnames[i])
                self.all_lagtimes.append(cur_lag)
                self.cmap_mifiles.append(
                    MI.MIfile(
                        all_cmap_fnames[i],
                        self.conf_cmaps.ToDict(section='corrmap_metadata')))
                self.cmap_mifiles[-1].OpenForReading()

            # Check lagtimes for consistency
            if (check_lagtimes):
                print(
                    'These are all lagtimes. They should be already sorted and not contain 0:'
                )
                print(self.all_lagtimes)
                for cur_lag in self.lagList:
                    if (cur_lag not in self.all_lagtimes):
                        print(
                            'WARNING: no correlation map found for lagtime ' +
                            str(cur_lag))

            self._corrmaps_loaded = True

        if (self.all_lagtimes[0] == 0 and getAutocorr == False):
            return self.conf_cmaps, [
                None
            ] + self.cmap_mifiles[1:], self.all_lagtimes
        else:
            return self.conf_cmaps, self.cmap_mifiles, self.all_lagtimes