示例#1
0
def generate_maps2():
    import random
    print 'Generating optic aberration maps using Proper'
    wfo = proper.prop_begin(tp.diam, 1., tp.grid_size, tp.beam_ratio)
    # rms_error = 5e-6#500.e-9       # RMS wavefront error in meters
    # c_freq = 0.005             # correlation frequency (cycles/meter)
    # high_power = 1.          # high frewquency falloff (r^-high_power)
    rms_error = 2.5e-3  # 500.e-9       # RMS wavefront error in meters
    c_freq = 0.000005  # correlation frequency (cycles/meter)
    high_power = 1.  # high frewquency falloff (r^-high_power)

    # tp.abertime = [0.5,2,10] # characteristic time for each aberation in secs
    tp.abertime = [
        100
    ]  # if beyond numframes then abertime will be auto set to duration of simulation
    abercubes = []
    aber_cube = np.zeros((ap.numframes, tp.grid_size, tp.grid_size))
    print 'here'

    perms = np.random.rand(ap.numframes, tp.grid_size, tp.grid_size) - 0.5
    perms *= 1e-7

    phase = 2 * np.pi * np.random.uniform(size=(tp.grid_size,
                                                tp.grid_size)) - np.pi
    aber_cube[0] = proper.prop_psd_errormap(
        wfo,
        rms_error,
        c_freq,
        high_power,
        MAP="prim_map",
        PHASE_HISTORY=phase)  # FILE=td.aberdir+'/telzPrimary_Map.fits')
    print 'lol'
    # quicklook_im(aber_cube[0], logAmp=False)

    for a in range(1, ap.numframes):
        # quicklook_im(aber_cube[a], logAmp=False)
        perms = np.random.rand(tp.grid_size, tp.grid_size) - 0.5
        perms *= 0.05
        phase += perms
        # print phase[:5,:5]
        aber_cube[a] = proper.prop_psd_errormap(wfo,
                                                rms_error,
                                                c_freq,
                                                high_power,
                                                MAP="prim_map",
                                                PHASE_HISTORY=phase)

    plt.plot(aber_cube[:, 20, 20])
    plt.show()

    for a in range(0, ap.numframes - 1, 100):
        quicklook_im(aber_cube[a], logAmp=False)

    if not os.path.isdir(iop.aberdir):
        os.mkdir(iop.aberdir)
    for f in range(0, ap.numframes, 1):
        # print 'saving frame #', f
        if f % 100 == 0: misc.progressBar(value=f, endvalue=ap.numframes)
        rawImageIO.saveFITS(aber_cube[f],
                            '%stelz%f.fits' % (iop.aberdir, f * cp.frame_time))
示例#2
0
def get_Iratio(LCmap, xlocs, ylocs, xinspect, yinspect, inspect):
    Iratio = np.zeros((len(xlocs), len(ylocs)))
    Ic = np.zeros((len(xlocs), len(ylocs)))
    Is = np.zeros((len(xlocs), len(ylocs)))
    mIratio = np.zeros((len(xlocs), len(ylocs)))
    for ix, xloc in enumerate(xlocs):
        for iy, yloc in enumerate(ylocs):
            if (ix * len(ylocs) + iy) % 100 == 0:
                misc.progressBar(value=(ix * len(ylocs) + iy),
                                 endvalue=len(xlocs) * len(ylocs))
            ints = LCmap[ix, iy]

            ID = pipe.get_intensity_dist(ints)
            bincent = (ID['binsS'] + np.roll(ID['binsS'], 1)) / 2.
            bincent = np.array(bincent)[1:]

            guessIc = np.mean(ints) * 0.7
            guessIs = np.mean(ints) * 0.3

            Imean = np.mean(ints)
            Istd = np.std(ints)
            Ic[ix, iy] = np.sqrt(Imean**2 - Istd**2)
            Is[ix, iy] = Imean - Ic[ix, iy]
            Iratio[ix, iy] = Ic[ix, iy] / Is[ix, iy]
            m = (np.sum(ints) - (Ic[ix, iy] + Ic[ix, iy])) / (
                np.sqrt(Is[ix, iy]**2 + 2 * Ic[ix, iy] + Is[ix, iy]) *
                len(ints))
            mIratio[ix, iy] = m**-1 * (Iratio[ix, iy])
            if inspect == True and xloc in yinspect and yloc in xinspect:
                plt.figure()
                plt.plot(ints)
                print xloc, yloc
                plt.figure()
                plt.step(bincent, ID['histS'])
                # plt.plot(bincent, MR(bincent, Ic[ix, iy], Is[ix, iy]), 'r--')
                print Imean, Istd, Ic[ix, iy], Is[ix,
                                                  iy], Iratio[ix,
                                                              iy], Imean, Istd
                try:
                    popt, _ = curve_fit(MR,
                                        bincent,
                                        ID['histS'],
                                        p0=[guessIc, guessIs])
                    # Ic[ix, iy] = popt[0]
                    # Is[ix, iy] = popt[1]
                    # Iratio[ix, iy] = popt[0] / popt[1]
                    # m = (np.sum(ints) - (popt[0] + popt[0])) / (
                    #     np.sqrt(popt[1] ** 2 + 2 * popt[0] + popt[1]) * len(ints))
                    # mIratio[ix, iy] = m ** -1 * (Iratio[ix, iy])
                    plt.plot(bincent, MR(bincent, *popt), 'b--')
                    print popt, popt[0] / popt[1]
                except RuntimeError:
                    pass

                plt.show()

    return Ic, Is, Iratio, mIratio
示例#3
0
文件: pipeline.py 项目: lbusoni/MEDIS
def make_intensity_map_packets(packets):
    print('Making a map of photon locations for all phases and times')
    int_map = np.zeros((mp.xnum, mp.ynum))
    for ip, p in enumerate(packets):
        x = np.int_(p[3])
        y = np.int_(p[4])
        int_map[x, y] += 1
        if len(packets) >= 1e7 and ip % 10000 == 0:
            misc.progressBar(value=ip, endvalue=len(packets))
    return int_map
示例#4
0
文件: pipeline.py 项目: lbusoni/MEDIS
def arange_into_cube(packets, size):
    # print 'Sorting packets into xy grid (no phase or time sorting)'
    cube = [[[] for i in range(size[0])] for j in range(size[1])]
    # dprint(np.shape(cube))
    # plt.hist(packets[:,1], bins=100)
    # plt.show()
    for ip, p in enumerate(packets):
        x = np.int_(p[2])
        y = np.int_(p[3])
        cube[x][y].append([p[0], p[1]])
        if len(packets) >= 1e7 and ip % 10000 == 0:
            misc.progressBar(value=ip, endvalue=len(packets))
    # print cube[x][y]
    # cube = time_sort(cube)
    return cube
示例#5
0
def make_LC_map():
    LCmap = np.zeros((len(xlocs), len(ylocs), num_chunks * num_ints))
    for i in range(num_chunks):

        start = i * max_photons
        # print start, start+max_photons
        packets = pipe.read_obs(max_photons=max_photons, start=start)

        # newpackets = pipe.isolate_interval(packets,[0,0.1])
        # print np.shape(newpackets)
        # cube = pipe.arange_into_cube(newpackets)
        # image = pipe.make_intensity_map(cube)
        # image = pipe.make_intensity_map_packets(packets)
        # # loop_frames
        # quicklook_im(image)
        # print image[58,67]

        # xloc, yloc = 58., 67.
        # xloc, yloc = 60., 60.
        # xloc, yloc = 60., 60.
        start = packets[0, 2]
        end = packets[-1, 2]
        # print start, end

        for ix, xloc in enumerate(xlocs):
            for iy, yloc in enumerate(ylocs):
                print np.shape(packets)
                LC, packets = pipe.get_lightcurve(packets,
                                                  xloc,
                                                  yloc,
                                                  start,
                                                  end + cp.frame_time,
                                                  bin_time=bin_time,
                                                  speed_up=True)
                # print LC['intensity']
                LCmap[ix, iy,
                      i * num_ints:(i + 1) * num_ints] = LC['intensity']
                if (ix * len(ylocs) + iy) % 10 == 0:
                    misc.progressBar(value=(ix * len(ylocs) + iy),
                                     endvalue=len(xlocs) * len(ylocs))
    with open(LCmapFile, 'wb') as handle:
        pickle.dump(LCmap, handle, protocol=pickle.HIGHEST_PROTOCOL)
示例#6
0
def scale_phasemaps():
    # filenames = rawImageIO.read_folder(cp.atmosdir)
    import multiprocessing
    filenames = glob.glob(cp.atmosdir + '*0.067*')
    scidata, hdr = rawImageIO.read_image(filenames[0], prob_map=False)
    scalefactor = 5.19751  #np.pi * 1e-6 / np.max(np.abs(scidata))  # *0.8 * 4./3 #kludge for now until you include AO etc
    # print filenames
    # # print 'Stretching the phase maps to size (%i,%i)' % (size,size)
    print('Scaling the phase maps by a factor %s' % scalefactor)
    p = multiprocessing.Pool(10)
    for ifn, filename in enumerate(filenames):
        if ifn % 10 == 0: misc.progressBar(value=ifn, endvalue=len(filenames))
        # scidata, hdr = rawImageIO.read_image(filename, prob_map=False)
        # scidata = rawImageIO.resize_image(scidata, (size,size), warn=False)
        # pyfits.update(filename, scidata, hdr,0)

        # rawImageIO.scale_image(filename, scalefactor)
        p.apply_async(rawImageIO.scale_image, (filename, scalefactor))
    p.close()
    p.join()
示例#7
0
文件: stats.py 项目: lbusoni/MEDIS
def get_skew(LCmap, xlocs=None, ylocs=None, xinspect=None, yinspect=None, inspect=None):
    if xlocs == None or ylocs == None:
        xlocs = list(range(LCmap.shape[0]))
        ylocs = list(range(LCmap.shape[1]))

    skews = np.zeros((len(xlocs), len(ylocs)))
    for ix, xloc in enumerate(xlocs):
        for iy, yloc in enumerate(ylocs):
            if (ix * len(ylocs) + iy) % 100 == 0: misc.progressBar(value=(ix * len(ylocs) + iy),
                                                                   endvalue=len(xlocs) * len(ylocs))
            ints = LCmap[ix, iy]
            if np.var(ints) !=0:
                # skews[ix,iy] = np.mean(ints)/np.var(ints)
                skews[ix,iy] = stats.skew(ints)
                print(ix, iy, skews[ix,iy])

            if inspect == True and xloc in yinspect and yloc in xinspect:
                plt.hist(ints, bins = 25)
                print(skews[ix,iy])
                plt.show()
            # skews[skews>=10] = 10
            # skews[skews<=-10] = -10
    return skews
示例#8
0
def plot_LC_map(xlocs, ylocs, LCmapFile, inspect=False):

    with open(LCmapFile, 'rb') as handle:
        LCmap = pickle.load(handle)

    # print np.shape(LCmap)
    LCmap = LCmap[:, :, :]
    # plt.plot(LCmap[60,40])
    # plt.figure()
    # LCmap = temp.downsample(LCmap, factor=10)
    # plt.plot(LCmap[60,40])
    # plt.show()

    print np.shape(LCmap)

    # xinspect = range(35,45)
    # yinspect = range(85,95)

    total_map = np.sum(LCmap, axis=2)
    median_map = np.median(LCmap, axis=2)
    interval_map = np.sum(LCmap[:, :, :100], axis=2)

    # if inspect:
    #     plt.imshow(median_map[yinspect[0]:yinspect[-1],xinspect[0]:xinspect[-1]])
    #     plt.show()

    quicklook_im(total_map, logAmp=True, show=False)
    quicklook_im(median_map, logAmp=True, show=False)
    quicklook_im(interval_map, logAmp=True, show=False)

    if os.path.isfile(IratioFile):
        with open(IratioFile, 'rb') as handle:
            Ic, Is, Iratio, mIratio = pickle.load(handle)
        print np.shape(Iratio)
    else:
        Iratio = np.zeros((len(xlocs), len(ylocs)))
        Ic = np.zeros((len(xlocs), len(ylocs)))
        Is = np.zeros((len(xlocs), len(ylocs)))
        mIratio = np.zeros((len(xlocs), len(ylocs)))
        for ix, xloc in enumerate(xlocs):
            for iy, yloc in enumerate(ylocs):
                if (ix * len(ylocs) + iy) % 100 == 0:
                    misc.progressBar(value=(ix * len(ylocs) + iy),
                                     endvalue=len(xlocs) * len(ylocs))
                ints = LCmap[ix, iy]

                ID = pipe.get_intensity_dist(ints)
                bincent = (ID['binsS'] + np.roll(ID['binsS'], 1)) / 2.
                bincent = np.array(bincent)[1:]

                # popt, _ = curve_fit(gaussian, ID['binsS'][:-1], ID['histS'])
                # plt.plot(ID['binsS'][:-1], gaussian(ID['binsS'][:-1], *popt), 'r--')
                # popt, _ = curve_fit(poisson, ID['binsS'][:-1], ID['histS'])
                # plt.plot(ID['binsS'][:-1], poisson(ID['binsS'][:-1], *popt), 'g--')
                # bincent = np.linspace(0,100000,len(bincent))
                # gauss = gaussian2(bincent, 1000,np.mean(ints)) + 0.0001 * np.random.normal(size=bincent.size)
                # popt, _ = curve_fit(gaussian2, bincent, gauss, p0=[100,100])
                # print popt

                # plt.plot(bincent, gauss)
                # plt.plot(bincent, gaussian2(bincent, *popt), 'g--')
                # print sum(gauss)
                # print np.mean(ints)

                guessIc = np.mean(ints) * 0.7
                guessIs = np.mean(ints) * 0.3

                # popt, _ = curve_fit(MR, bincent, gauss, p0=[guessIc,guessIs])
                # print popt
                # plt.plot(bincent, MR(bincent, *popt), 'g--')
                # print sum(MR(bincent, *popt))

                # popt, _ = curve_fit(func, bincent, ID['histS'])
                # plt.plot(bincent, func(bincent, *popt), 'r--')
                try:
                    popt, _ = curve_fit(MR,
                                        bincent,
                                        ID['histS'],
                                        p0=[guessIc, guessIs])
                    Ic[ix, iy] = popt[0]
                    Is[ix, iy] = popt[1]
                    Iratio[ix, iy] = popt[0] / popt[1]
                    m = (np.sum(ints) - (popt[0] + popt[0])) / (
                        np.sqrt(popt[1]**2 + 2 * popt[0] + popt[1]) *
                        len(ints))
                    mIratio[ix, iy] = m**-1 * (Iratio[ix, iy])
                except RuntimeError:
                    pass
                # print np.shape(ints)
                # EI = np.sum(ints)
                # # print EI
                # EI2 = EI**2
                # # print EI2
                # var = np.var(ints)
                # # print var
                # Is[ix,iy] = EI-np.sqrt(EI2-var)
                # # print Is[ix,iy]
                # Ic[ix,iy] = EI-Is[ix,iy]
                # # print Ic[ix,iy]
                # Iratio[ix,iy] = Ic[ix,iy]/Is[ix,iy]
                # exit()
                if inspect == True:  # and xloc in yinspect and yloc in xinspect:
                    plt.figure()
                    plt.plot(ints)
                    print xloc, yloc
                    plt.figure()
                    plt.step(bincent, ID['histS'])
                    plt.plot(bincent, MR(bincent, *popt), 'b--')
                    print popt, popt[0] / popt[1]
                    plt.show()

        with open(IratioFile, 'wb') as handle:
            pickle.dump([Ic, Is, Iratio, mIratio],
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

    quicklook_im(Ic, logAmp=True, show=False)  #, vmax=25)#
    quicklook_im(Is, logAmp=True, show=False)  #,vmax=5,)#
    quicklook_im(Iratio, logAmp=True, show=False)  #,vmax=25,)#
    quicklook_im(mIratio, logAmp=True, show=False)  #, vmax=5,)#
    quicklook_im(mIratio * Iratio, logAmp=True, show=False)  #, vmax=500,)#
    plt.show()
    return total_map, median_map, interval_map, Iratio, mIratio
示例#9
0
文件: stats.py 项目: lbusoni/MEDIS
def get_Iratio(LCmap, xlocs=None, ylocs=None, xinspect=None, yinspect=None, inspect=None):
    if xlocs == None or ylocs == None:
        xlocs = list(range(LCmap.shape[0]))
        ylocs = list(range(LCmap.shape[1]))

    Iratio = np.zeros((len(xlocs), len(ylocs)))
    Ic = np.zeros((len(xlocs), len(ylocs)))
    Is = np.zeros((len(xlocs), len(ylocs)))
    mIratio = np.zeros((len(xlocs), len(ylocs)))
    # from Utils.plot_tools import loop_frames
    # loop_frames(np.transpose(LCmap))
    for ix, xloc in enumerate(xlocs):
        for iy, yloc in enumerate(ylocs):
            if (ix * len(ylocs) + iy) % 100 == 0: misc.progressBar(value=(ix * len(ylocs) + iy),
                                                                   endvalue=len(xlocs) * len(ylocs))
            ints = LCmap[ix, iy]

            guessIc = np.mean(ints) * 0.7
            guessIs = np.mean(ints) * 0.3

            Imean = np.mean(ints)
            Istd = np.std(ints)
            if Imean < Istd:
                Ic[ix, iy] = 1e-5
            else:
                Ic[ix, iy] = np.sqrt(Imean ** 2 - Istd ** 2)
            Is[ix, iy] = Imean - Ic[ix, iy]
            Iratio[ix, iy] = Ic[ix, iy] / Is[ix, iy]
            m = (np.sum(ints) - (Ic[ix, iy] + Ic[ix, iy])) / (
                np.sqrt(Is[ix, iy] ** 2 + 2 * Ic[ix, iy] + Is[ix, iy]) * len(ints))
            mIratio[ix, iy] = m ** -1 * (Iratio[ix, iy])
            # if ix == 13 and iy == 13:
            #     print ints, Imean, Istd, Ic[ix, iy], Is[ix, iy], Iratio[ix, iy]
            if inspect == True and xloc in yinspect and yloc in xinspect:
                ID = pipe.get_intensity_dist(ints)
                bincent = (ID['binsS'] + np.roll(ID['binsS'], 1)) / 2.
                bincent = np.array(bincent)[1:]
                plt.figure()
                plt.plot(ints)
                print(xloc, yloc)
                plt.figure()
                plt.step(bincent, ID['histS'])
                # plt.plot(bincent, MR(bincent, Ic[ix, iy], Is[ix, iy]), 'r--')
                print(Imean, Istd, Ic[ix, iy], Is[ix, iy], Iratio[ix, iy], Imean, Istd)
                try:
                    popt, _ = curve_fit(MR, bincent, ID['histS'], p0=[guessIc, guessIs])
                    # Ic[ix, iy] = popt[0]
                    # Is[ix, iy] = popt[1]
                    # Iratio[ix, iy] = popt[0] / popt[1]
                    # m = (np.sum(ints) - (popt[0] + popt[0])) / (
                    #     np.sqrt(popt[1] ** 2 + 2 * popt[0] + popt[1]) * len(ints))
                    # mIratio[ix, iy] = m ** -1 * (Iratio[ix, iy])
                    plt.plot(bincent, MR(bincent, *popt), 'b--')
                    print(popt, popt[0] / popt[1])
                except RuntimeError:
                    pass

                plt.show()

    # LCmap_max = np.sum(np.argsort(-LCmap[:,:,0])[:100])
    # Iratio_max = np.sum(np.argsort(-Iratio)[:100])
    # print np.argsort(-LCmap[0])[:100]
    # LCmap_max = np.max(-LCmap[:,:,0])
    # Iratio_max = np.max(-Iratio)
    stacked = np.mean(LCmap, axis=2)
    LCmap_max = stacked[64,57]
    Iratio_max = Iratio[64,57]
    scale = LCmap_max/Iratio_max
    print(LCmap_max, Iratio_max, scale)
    # scale = np.max(LCmap)/np.max(Iratio)
    Iratio *= scale#np.mean(Is)
    return Ic, Is, Iratio, mIratio
示例#10
0
def generate_maps():
    import random
    print 'Generating optic aberration maps using Proper'
    wfo = proper.prop_begin(tp.diam, 1., tp.grid_size, tp.beam_ratio)
    # rms_error = 5e-6#500.e-9       # RMS wavefront error in meters
    # c_freq = 0.005             # correlation frequency (cycles/meter)
    # high_power = 1.          # high frewquency falloff (r^-high_power)
    rms_error = 2.5e-3  #500.e-9       # RMS wavefront error in meters
    c_freq = 0.000005  # correlation frequency (cycles/meter)
    high_power = 1.  # high frewquency falloff (r^-high_power)

    # tp.abertime = [0.5,2,10] # characteristic time for each aberation in secs
    tp.abertime = [
        100
    ]  # if beyond numframes then abertime will be auto set to duration of simulation
    abercubes = []
    for abertime in tp.abertime:
        # ap.numframes = 100
        aberfreq = 1. / abertime
        # tp.abertime=2 # aberfreq: number of frames goals per sec?
        num_longframes = aberfreq * ap.numframes * cp.frame_time
        print num_longframes, ap.numframes, cp.frame_time
        aber_cube = np.zeros((ap.numframes + 1, tp.grid_size, tp.grid_size))
        lin_size = tp.grid_size**2
        # spacing = int(ap.numframes/num_longframes)
        # frame_idx = np.int_(np.linspace(0,ap.numframes,num_longframes+1))
        c = range(0, ap.numframes)
        print num_longframes
        frame_idx = np.sort(random.sample(c, int(num_longframes + 1 - 2)))
        # frame_idx = np.int_(np.sort(np.round(np.random.uniform(0,ap.numframes,num_longframes+1-2))))
        frame_idx = np.hstack(([0], frame_idx, [ap.numframes]))
        # frame_idx = [0,  15,   69,  278,  418,  703, 1287, 1900, 3030, 3228, 5000]
        print frame_idx
        for f in frame_idx:
            aber_cube[f] = proper.prop_psd_errormap(
                wfo, rms_error, c_freq, high_power,
                MAP="prim_map")  #FILE=td.aberdir+'/telzPrimary_Map.fits')
            # quicklook_im(aber_cube[f], logAmp=False)
        for i, f in enumerate(frame_idx[:-1]):
            spacing = int(frame_idx[i + 1] - frame_idx[i])
            # quicklook_im(aber_cube[f], logAmp=False, show=False)

            frame1 = aber_cube[f]
            frame2 = aber_cube[frame_idx[i + 1]]
            lin_map = [
                np.linspace(f1, f2, spacing) for f1, f2 in zip(
                    frame1.reshape(lin_size), frame2.reshape(lin_size))
            ]
            interval_cube = np.array(lin_map).reshape(tp.grid_size,
                                                      tp.grid_size, spacing)
            interval_cube = np.transpose(interval_cube)
            print i, f, frame_idx[i], frame_idx[i + 1], np.shape(interval_cube)
            # loop_frames(interval_cube, logAmp=False)
            aber_cube[f:frame_idx[i + 1]] = interval_cube
        abercubes.append(aber_cube)
        plt.plot(aber_cube[:, 20, 20])
        plt.show()
    abercubes = np.array(abercubes)
    # print abercubes.shape
    # plt.plot(aber_cube[:,20,20])
    aber_cube = np.sum(abercubes, axis=0)
    plt.plot(aber_cube[:, 20, 20])
    plt.show()
    if not os.path.isdir(iop.aberdir):
        os.mkdir(iop.aberdir)
    for f in range(0, ap.numframes, 1):
        # print 'saving frame #', f
        if f % 100 == 0: misc.progressBar(value=f, endvalue=ap.numframes)
        rawImageIO.saveFITS(aber_cube[f],
                            '%stelz%f.fits' % (iop.aberdir, f * cp.frame_time))
        # quicklook_im(aber_cube[f], logAmp=False, show=True)

    plt.show()