コード例 #1
0
def formatdata(path):
    #%%
    dirname,filename = os.path.split(path)
    filename, ext = os.path.splitext(filename)
    hyppath = path
    path = os.path.join(dirname,filename)
    specpath = path+'_X_axis.asc'
    filepath = path+'_SEM image after carto.tif'
    data = pd.read_csv(hyppath,delimiter='\t',header=None,dtype=np.float).to_numpy()
    #%%
    xlen = int(data[0,0])
    ylen = int(data[1,0])
    wavelenght = pd.read_csv(specpath,header=None,dtype=np.float,nrows=2048).to_numpy()[:,0]
    wavelenght = wavelenght[:2048]
    xcoord = data[0,1:]
    # print(np.unique(xcoord).size)
    ycoord = data[1,1:]
    
    #nombre de pixel par ligne (pour trouver la ligne incomplete)
    pixelNumber = np.array([xcoord[ycoord==y].size for y in np.unique(ycoord)])
    completerow = np.where(pixelNumber==xlen)[0]
    print(data[2:,1:completerow.size*xlen].size)
    # print(np.unique(ycoord).size)
    CLdata = data[2:,1:] #tableau de xlen * ylen points (espace) et 2048 longueur d'onde CLdata[:,n] n = numero du spectr
    #%%
    hypSpectrum = np.transpose(np.reshape(np.transpose(CLdata),(completerow.size,xlen ,len(wavelenght))), (0, 1, 2))
    average_axis = 0 #1 on moyenne le long du fil, 0 transversalement
    linescan = np.sum(hypSpectrum,axis=average_axis)
    xscale_CL,yscale_CL,acc,image = scaleSEMimage(filepath)
    newX = np.linspace(xscale_CL[int(xcoord.min())],xscale_CL[int(xcoord.max())],len(xscale_CL))
    newY = np.linspace(yscale_CL[int(ycoord.min())],yscale_CL[int(ycoord.max())],len(yscale_CL))
    X = np.linspace(np.min(newX),np.max(newX),hypSpectrum.shape[1])
    print(min(X),max(X))
    print(min(newX),max(newX))
    Y = np.linspace(np.min(newY),np.max(newY),hypSpectrum.shape[0])
    return X,Y,hypSpectrum,wavelenght
コード例 #2
0
def make_linescan(path,
                  save=False,
                  autoclose=False,
                  log=False,
                  threshold=0,
                  deadPixeltol=2,
                  aspect="auto",
                  EnergyRange=[],
                  normalise=False,
                  Linescan=False):
    #    path=r'D:/M2 Internship/data/2019-03-08 - T2601 - 300K/Fil2/HYP1-T2601-300K-Vacc5kV-spot7-zoom6000x-gr600-slit0-2-t5ms-Fil1-cw380nm/Hyp.dat'
    #    path = r"D:/M2 Internship/data\2019-03-22 - T2594 - Rampe\300K\HYP1-T2594-310K-Vacc5kV-spot7-zoom6000x-gr600-slit0-2-t5ms-cw440nm\Hyp.dat"
    #    path=r'D:/M2 Internship/data/2019-04-29 - T2594 Al - 300K/Fil 2/HYP-T2594Al-300K-Vacc5kV-spot5-zoom10000x-gr600-slit0-2-t005ms-cw450nm/Hyp.dat'
    #    path=r'D:/M2 Internship/data/2019-05-16 - T2455 Al - 300K/FULLSCREEN HYP2-T2455-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t005ms-cw440nm/Hyp.dat'
    if autoclose:
        plt.ioff()
    else:
        plt.ion()
    dirname, filename = os.path.split(path)
    filename, ext = os.path.splitext(filename)
    try:
        infos = os.path.basename(dirname).split("-")
        T = '' if not [x for x in infos
                       if ('K' in x)] else [x for x in infos if ('K' in x)][0]
        Sample = infos[1]
        wire = '' if not [x for x in infos if ('fil' in x.lower())] else [
            x for x in infos if ('fil' in x.lower())
        ][0]
        if not wire:
            wire = os.path.basename(os.path.dirname(dirname))
        wire = wire.lower().replace('fil', 'Wire')
    except:
        T = ''
        Sample = ''
        wire = ''
    hyppath = path
    specpath = os.path.join(dirname, filename + '_X_axis.asc')
    filepath = os.path.join(dirname, filename + '_SEM image after carto.tif')
    data = np.loadtxt(hyppath)
    xlen = int(data[0, 0])  #nbr de pts selon x
    ylen = int(data[1, 0])  #nbr de pts selon y
    wavelenght = np.loadtxt(specpath)
    wavelenght = wavelenght[:2048]  #bins du spectro
    xcoord = data[0, 1:]
    ycoord = data[1, 1:]
    CLdata = data[
        2:,
        1:]  #tableau de xlen * ylen points (espace) et 2048 longueur d'onde CLdata[:,n] n = numero du spectr

    # hypSpectrum[y,x,spec]
    hypSpectrum = np.transpose(
        np.reshape(np.transpose(CLdata), (ylen, xlen, len(wavelenght))),
        (0, 1, 2))
    #    process = psutil.Process(os.getpid())
    #    print("Memory usage : %d mo" %(process.memory_info().rss//2**20))

    #correct dead / wrong pixels
    hypSpectrum, hotpixels = correct_dead_pixel(hypSpectrum, tol=deadPixeltol)

    #reduce the spectrum if wanted
    if len(EnergyRange) == 2:
        EnergyRange = eV_To_nm / np.array(EnergyRange)  # en nm
        EnergyRange.sort()
        lidx = np.argmin(np.abs(wavelenght - EnergyRange[0]))
        ridx = np.argmin(np.abs(wavelenght - EnergyRange[1]))
        wavelenght = wavelenght[lidx:ridx]
        hypSpectrum = hypSpectrum[:, :, lidx:ridx]  # mask*hypSpectrum


#        mask = np.zeros(hypSpectrum.shape)
#        mask[:,:,lidx:ridx] = 1

    linescan = np.sum(hypSpectrum, axis=0)
    if normalise:
        linescan = linescan / np.max(linescan, axis=1, keepdims=True)

    xscale_CL, yscale_CL, acceleration, image = scaleSEMimage(filepath)
    if Linescan:
        fig, (ax, bx,
              cx) = plt.subplots(3,
                                 1,
                                 sharex=True,
                                 gridspec_kw={'height_ratios': [1, 1, 3]})
    else:
        fig, (ax, bx, cx) = plt.subplots(3, 1, sharex=True, sharey=True)

    fig.patch.set_alpha(0)  #Transparency style
    fig.subplots_adjust(top=0.9,
                        bottom=0.12,
                        left=0.15,
                        right=0.82,
                        hspace=0.1,
                        wspace=0.05)

    newX = np.linspace(xscale_CL[int(xcoord.min())],
                       xscale_CL[int(xcoord.max())], len(xscale_CL))
    newY = np.linspace(yscale_CL[int(ycoord.min())],
                       yscale_CL[int(ycoord.max())], len(yscale_CL))
    X = np.linspace(np.min(newX), np.max(newX), hypSpectrum.shape[1])
    Y = np.linspace(np.min(newY), np.max(newY), hypSpectrum.shape[0])
    nImage = np.array(
        image.crop((xcoord.min(), ycoord.min(), xcoord.max(), ycoord.max())))

    ax.imshow(nImage,
              cmap='gray',
              vmin=0,
              vmax=65535,
              extent=[np.min(newX),
                      np.max(newX),
                      np.max(newY),
                      np.min(newY)])
    hypSpectrum = hypSpectrum - threshold
    hypSpectrum = hypSpectrum * (hypSpectrum >= 0)
    hypimage = np.sum(hypSpectrum, axis=2)
    hypimage -= hypimage.min()
    if log:
        hypimage = np.log10(hypimage + 1)
        linescan = np.log10(linescan + 1)
    lumimage = bx.imshow(
        hypimage,
        cmap='jet',
        extent=[np.min(newX),
                np.max(newX),
                np.max(newY),
                np.min(newY)])
    if Linescan:
        #        extent = [np.min(newX),np.max(newX),eV_To_nm/wavelenght.max(),eV_To_nm/wavelenght.min()]
        #        im=cx.imshow(linescan.T,cmap='jet',extent=extent)
        im = cx.pcolormesh(X, eV_To_nm / wavelenght, linescan.T, cmap='jet')

        def format_coord(x, y):
            xarr = X
            yarr = eV_To_nm / wavelenght
            if ((x > xarr.min()) & (x <= xarr.max()) & (y > yarr.min()) &
                (y <= yarr.max())):
                col = np.argmin(abs(xarr - x))  #np.searchsorted(xarr, x)-1
                row = np.argmin(abs(yarr - y))  #np.searchsorted(yarr, y)-1
                z = linescan.T[row, col]
                return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}, z={z:1.2e}   [{row},{col}]'
            else:
                return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}'

        cx.format_coord = format_coord
        cx.set_ylabel("Energy (eV)")
        cx.set_xlabel("distance (µm)")
        cx.set_aspect('auto')
    else:
        im = cx.imshow(
            wavelenght[np.argmax(hypSpectrum, axis=2)],
            cmap='viridis',
            extent=[np.min(newX),
                    np.max(newX),
                    np.max(newY),
                    np.min(newY)])
        cx.set_aspect(aspect)
    bx.set_aspect(aspect)
    ax.set_aspect(aspect)
    #    cx.set_aspect(aspect)
    ax.get_shared_y_axes().join(ax, bx)
    ax.set_ylabel("distance (µm)")
    #    fig.text(ax.get_position().bounds[0]-0.11, ax.get_position().bounds[1],'distance (µm)',fontsize=16, va='center', rotation='vertical')

    pos = cx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
    fig.colorbar(im, cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))

    pos = bx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
    fig.colorbar(lumimage, cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))
    if save == True:
        savedir = os.path.join(dirname, 'Saved')
        if not os.path.exists(savedir):
            os.mkdir(savedir)
        savename = 'SEM+Hyp+Linescan'
        if ((len(T) > 0) & (len(Sample) > 0) & (len(wire) > 0)):
            savename = '%s_%s_%s_%s' % (savename, Sample, T, wire)
        fig.savefig(os.path.join(dirname, 'Saved', savename + ".png"), dpi=300)

    if autoclose == True:
        plt.close(fig)
    return hypSpectrum, wavelenght, X, Y
コード例 #3
0
ファイル: merge_carto.py プロジェクト: SF73/PhD
def merge(path,
          save=False,
          autoclose=False,
          log=False,
          threshold=0,
          deadPixeltol=100,
          aspect="auto",
          EnergyRange=[]):
    #path = [r'F:\data\2019-05-28 - T2597 - 300K\HYP-T2597-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t010ms-cw440nm/Hyp.dat',r'F:\data\2019-05-28 - T2597 - 300K\HYP-T2597-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t010ms-cw400nm/Hyp.dat']
    #    path1=r'F:\data\2019-05-28 - T2597 - 300K\HYP-T2597-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t010ms-cw460nm/Hyp.dat'
    #    path2=r'F:\data\2019-05-28 - T2597 - 300K\HYP-T2597-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t010ms-cw360nm/Hyp.dat'
    #
    path = [
        r"C:\Users\sylvain.finot\Cloud Neel\Data\2019-03-22 - T2581 - 005K\Fil 1\HYP1-T2581-005K-Vacc5kV-spot7-zoom4000x-gr600-slit0-2-t5ms-cw460nm\Hyp.dat",
        r"C:\Users\sylvain.finot\Cloud Neel\Data\2019-03-22 - T2581 - 005K\Fil 1\HYP1-T2581-005K-Vacc5kV-spot7-zoom4000x-gr600-slit0-2-t5ms-cw540nm\Hyp.dat"
    ]
    dirname = list()
    wavelenght = list()
    CLdata = list()
    xlen = list()
    ylen = list()
    xcoord = list()
    ycoord = list()
    filepath = list()
    for i in range(len(path)):
        dirname.append(os.path.dirname(path[i]))
        hyppath = path[i]
        specpath = os.path.join(dirname[i], 'Hyp_X_axis.asc')
        filepath.append(
            os.path.join(dirname[i], 'Hyp_SEM image after carto.tif'))
        data = np.loadtxt(hyppath)
        xlen.append(int(data[0, 0]))
        ylen.append(int(data[1, 0]))
        wavelenght.append(np.loadtxt(specpath)[:2048])
        #        wavelenght1 = wavelenght1[:2048]
        xcoord.append(data[0, 1:])
        ycoord.append(data[1, 1:])
        CLdata.append(data[2:, 1:])

    wavelenght = np.array(wavelenght)
    inds = wavelenght.argsort(axis=0)[:, 0]
    CLdata = list(np.array(CLdata)[inds])
    xlen = np.array(xlen)[inds]
    ylen = np.array(ylen)[inds]
    xcoord = np.array(xcoord)[inds]
    ycoord = np.array(ycoord)[inds]
    wavelenght = list(wavelenght[inds])

    #on suppose que deltaLambda est identique partout
    WaveStep = wavelenght[0][-1] - wavelenght[0][-2]
    while (len(CLdata) > 1):
        ridx = abs(wavelenght[0] - wavelenght[1].min()).argmin()
        patch = np.arange(wavelenght[0][ridx], wavelenght[1][0], WaveStep)
        nwavelenght = np.concatenate(
            (wavelenght[0][:ridx], patch, wavelenght[1]))
        patch = np.zeros(
            (len(patch),
             CLdata[1].shape[1]))  #* np.min((CLdata[0],CLdata[1])) -10
        nCLdata = np.concatenate((CLdata[0][:ridx], patch, CLdata[1]))
        wavelenght = [nwavelenght, *wavelenght[2:]]
        CLdata = [nCLdata, *CLdata[2:]]

    wavelenght = np.array(wavelenght)[0]
    CLdata = np.array(CLdata)[0]
    hypSpectrum = np.transpose(
        np.reshape(np.transpose(CLdata), (ylen[0], xlen[0], len(wavelenght))),
        (0, 1, 2))
    hypSpectrum, hotpixels = correct_dead_pixel(hypSpectrum, tol=deadPixeltol)

    if len(EnergyRange) == 2:
        EnergyRange = 1239.842 / np.array(EnergyRange)
        EnergyRange.sort()
        lidx = np.argmin(np.abs(wavelenght - EnergyRange[0]))
        ridx = np.argmin(np.abs(wavelenght - EnergyRange[1]))
        wavelenght = wavelenght[lidx:ridx]
        hypSpectrum = hypSpectrum[:, :, lidx:ridx]

    average_axis = 0  #1 on moyenne le long du fil, 0 transversalement
    linescan = np.sum(hypSpectrum, axis=average_axis)
    linescan -= linescan.min()
    #    linescan = np.log10(linescan)
    xscale_CL, yscale_CL, acc, image = scaleSEMimage(filepath[0])
    fig, (ax, bx, cx) = plt.subplots(3,
                                     1,
                                     sharex=True,
                                     gridspec_kw={'height_ratios': [1, 1, 3]})
    fig.patch.set_alpha(0)  #Transparency style
    fig.subplots_adjust(top=0.9,
                        bottom=0.12,
                        left=0.15,
                        right=0.82,
                        hspace=0.1,
                        wspace=0.05)
    newX = np.linspace(xscale_CL[int(xcoord[0].min())],
                       xscale_CL[int(xcoord[0].max())], len(xscale_CL))
    newY = np.linspace(yscale_CL[int(ycoord[0].min())],
                       yscale_CL[int(ycoord[0].max())], len(yscale_CL))
    nImage = np.array(
        image.crop((xcoord[0].min(), ycoord[0].min(), xcoord[0].max(),
                    ycoord[0].max())))  #-np.min(image)
    ax.imshow(nImage,
              cmap='gray',
              vmin=0,
              vmax=65535,
              extent=[np.min(newX),
                      np.max(newX),
                      np.max(newY),
                      np.min(newY)])
    ax.set_ylabel("distance (µm)")
    hypSpectrum = hypSpectrum - threshold
    hypSpectrum = hypSpectrum * (hypSpectrum >= 0)
    hypimage = np.sum(hypSpectrum, axis=2)
    hypimage -= hypimage.min()
    if log:
        hypimage = np.log10(hypimage + 1)
        linescan = np.log10(linescan + 1)
    lumimage = bx.imshow(
        hypimage,
        cmap='jet',
        extent=[np.min(newX),
                np.max(newX),
                np.max(newY),
                np.min(newY)])
    if average_axis == 1:
        extent = [
            1239.842 / wavelenght.max(), 1239.842 / wavelenght.min(),
            np.max(newY),
            np.min(newY)
        ]
        im = bx.imshow(linescan, cmap='jet', extent=extent)
        bx.set_xlabel("energy (eV)")
        bx.set_ylabel("distance (µm)")
    else:
        extent = [
            np.min(newX),
            np.max(newX), 1239.842 / wavelenght.max(),
            1239.842 / wavelenght.min()
        ]
        im = cx.imshow(linescan.T, cmap='jet', extent=extent)
        cx.set_ylabel("Energy (eV)")
        cx.set_xlabel("distance (µm)")

    cx.set_aspect('auto')
    bx.set_aspect(aspect)
    ax.set_aspect(aspect)
    ax.get_shared_y_axes().join(ax, bx)
    pos = cx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
    fig.colorbar(im, cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))

    pos = bx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
    fig.colorbar(lumimage, cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))
    if save == True:
        savedir = os.path.join(dirname, 'Saved')
        if not os.path.exists(savedir):
            os.mkdir(savedir)
        fig.savefig(os.path.join(dirname, 'Saved',
                                 'SEM+Hyp+Linescan_merged.png'),
                    dpi=300)
    if autoclose == True:
        plt.close(fig)
コード例 #4
0
ファイル: hyperProcessing.py プロジェクト: SF73/PhD
def loadData(paths, deadPixeltol=None, removeSpikes=True):
    basenameL = list()
    wavelenghtL = list()
    infoL = list()
    CLdataL = list()
    xlenL = list()
    ylenL = list()
    xcoordL = list()
    ycoordL = list()
    semImageL = list()
    semCartoL = list()
    if not isinstance(paths, (list, tuple, np.ndarray)):
        paths = [paths]
    #%% Prepare data
    for i in range(len(paths)):
        dirname, filename = os.path.split(paths[i])
        filename, ext = os.path.splitext(filename)
        basenameL.append(os.path.join(dirname, filename))
        hyppath = paths[i]
        specpath = basenameL[i] + '_X_axis.asc'
        semImageL.append(basenameL[i] + '_SEM image before carto.tif')
        if os.path.exists(basenameL[i] + '_SEM_carto.asc'):
            semCartoL.append(basenameL[i] + '_SEM_carto.asc')
        #data = np.loadtxt(hyppath)
        data = pd.read_csv(hyppath,
                           delimiter='\t',
                           header=None,
                           dtype=np.float).to_numpy()  #faster
        xlenL.append(int(data[0, 0]))  #number of pixel in x dir
        ylenL.append(int(data[1, 0]))  #number of pixel in y dir
        #spec=np.loadtxt(specpath)[:2048]
        spec = pd.read_csv(specpath, header=None, dtype=np.float,
                           nrows=2048).to_numpy()[:, 0]  #faster
        wavelenghtL.append(spec)
        xcoordL.append(data[0, 1:])  # x's pixels of the carto
        ycoordL.append(data[1, 1:])  # y's pixels of the carto
        CLdataL.append(data[2:, 1:])  # spectra of each point
        spectroInfos = getSpectro_info(basenameL[i] + '_spectro_info.asc')
        #'Grating','Entrance slit (mm)', 'Exposure time (s)','High voltage (V)','Spot size'
        logger.info('\n %s' % spectroInfos)
        infoL.append(spectroInfos[:, 1])

    #self.add_meta_data({"Description":str(dict(spectroInfos))})
    wavelenghtL = np.array(wavelenghtL)
    inds = wavelenghtL.argsort(axis=0)[:, 0]
    CLdataL = list(np.array(CLdataL)[inds])
    xlen = np.array(xlenL)[inds]
    ylen = np.array(ylenL)[inds]
    xcoord = np.array(xcoordL)[inds]
    ycoord = np.array(ycoordL)[inds]
    wavelenght = list(wavelenghtL[inds])

    SameConditions = compareManyArrays(infoL) & compareManyArrays(
        xcoordL) & compareManyArrays(ycoordL)
    logger.info('Same Conditions : %s', SameConditions)

    # Filling gap spectra's gap with 0
    WaveStep = wavelenght[0][-1] - wavelenght[0][-2]
    while (len(CLdataL) > 1):
        #On cherche l'index de wavelenght[1] minimisant l'écart avec wavelenght[0]
        ridx = abs(wavelenght[0] - wavelenght[1].min()).argmin()
        #On créé les longueurs d'onde manquantes dans l'écart
        patch = np.arange(wavelenght[0][ridx], wavelenght[1][0], WaveStep)
        #On assemble les 2 spectres avec le patch au milieu
        nwavelenght = np.concatenate(
            (wavelenght[0][:ridx], patch, wavelenght[1]))
        # patch des données CL
        patch = np.zeros(
            (len(patch), CLdataL[1].shape[1]),
            dtype=np.float) * np.nan  #* np.min((CLdata[0],CLdata[1])) -10
        nCLdata = np.concatenate((CLdataL[0][:ridx], patch, CLdataL[1]))
        #On remplace les spectres et les données des deux premiers par le nouveau combiné
        wavelenght = [nwavelenght, *wavelenght[2:]]
        CLdataL = [nCLdata, *CLdataL[2:]]

    wavelenght = np.array(wavelenght)[0]
    CLdata = np.array(CLdataL)[0]
    xlen = np.array(xlenL)[0]
    ylen = np.array(ylenL)[0]
    xcoord = np.array(xcoordL)[0]
    ycoord = np.array(ycoordL)[0]
    filepath = semImageL[0]
    if len(semCartoL) > 0:
        semCarto = np.loadtxt(semCartoL[0])
    else:
        semCarto = None
        trueSEM = False
    hypSpectrum = np.transpose(
        np.reshape(np.transpose(CLdata), (ylen, xlen, len(wavelenght))),
        (0, 1, 2))
    if removeSpikes:
        hypSpectrum = _removeSpikes(hypSpectrum)
    #correct dead / wrong pixels
    if len(paths) == 1 and deadPixeltol:
        if deadPixeltol < 100:
            logger.debug("Cleaning deadpixel with %d sigmas tol", deadPixeltol)
            hypSpectrum, hotpixels = correct_dead_pixel(hypSpectrum,
                                                        tol=deadPixeltol)
    wavelenght = ma.masked_array(wavelenght, mask=False)  #Nothing is masked
    #np.resize(self.wavelenght.mask,self.hypSpectrum.shape)
    #self.hypSpectrum = ma.masked_array(self.hypSpectrum,mask=hypmask)
    hypSpectrum = ma.masked_invalid(hypSpectrum)

    xscale_CL, yscale_CL, acceleration, image = scaleSEMimage(filepath)

    newX = np.linspace(xscale_CL[int(xcoord.min())],
                       xscale_CL[int(xcoord.max())], len(xscale_CL))
    newY = np.linspace(yscale_CL[int(ycoord.min())],
                       yscale_CL[int(ycoord.max())], len(yscale_CL))
    X = np.linspace(np.min(newX), np.max(newX), hypSpectrum.shape[1])
    Y = np.linspace(np.min(newY), np.max(newY), hypSpectrum.shape[0])
    # logger.debug("newX")
    # logger.debug(newX)
    # logger.debug(newX.shape)

    # logger.debug("X")
    # logger.debug(X)
    # logger.debug(X.shape)
    #SEM image / carto
    nImage = np.array(
        image.crop((xcoord.min(), ycoord.min(), xcoord.max(), ycoord.max())))
    results = {}
    results["wavelenght"] = wavelenght
    results["hyper"] = hypSpectrum
    results["X"] = X
    results["Y"] = Y
    results["semImage"] = nImage
    results["semCarto"] = semCarto

    #return wavelenght, hypSpectrum, X, Y, nImage
    return results
コード例 #5
0
    def __init__(self,
                 path,
                 deadPixeltol=2,
                 aspect="auto",
                 Linescan=True,
                 autoclose=False,
                 save=False):
        self.shift_is_held = False
        self.path = path
        self.deadPixeltol = deadPixeltol
        self.aspect = aspect
        self.Linescan = Linescan
        self.leftpressed = False
        if autoclose:
            plt.ioff()
        else:
            plt.ion()

        dirname, filename = os.path.split(self.path)
        filename, ext = os.path.splitext(filename)

        hyppath = self.path
        specpath = os.path.join(dirname, filename + '_X_axis.asc')
        filepath = os.path.join(dirname,
                                filename + '_SEM image after carto.tif')
        start = time.time()
        data = pd.read_csv(hyppath, delimiter='\t', header=None).to_numpy()
        end = time.time()
        print(end - start)
        #data = np.loadtxt(hyppath)
        xlen = int(data[0, 0])  #nbr de pts selon x
        ylen = int(data[1, 0])  #nbr de pts selon y
        wavelenght = np.loadtxt(specpath)
        self.wavelenght = wavelenght[:2048]  #bins du spectro
        xcoord = data[0, 1:]
        ycoord = data[1, 1:]
        CLdata = data[
            2:,
            1:]  #tableau de xlen * ylen points (espace) et 2048 longueur d'onde CLdata[:,n] n = numero du spectr
        self.hypSpectrum = np.transpose(
            np.reshape(np.transpose(CLdata),
                       (ylen, xlen, len(self.wavelenght))), (0, 1, 2))

        #correct dead / wrong pixels
        self.hypSpectrum, self.hotpixels = correct_dead_pixel(
            self.hypSpectrum, tol=self.deadPixeltol)

        self.wavelenght = ma.masked_array(self.wavelenght, mask=False)
        hypmask = np.resize(self.wavelenght.mask, self.hypSpectrum.shape)
        self.hypSpectrum = ma.masked_array(self.hypSpectrum, mask=hypmask)

        xscale_CL, yscale_CL, acceleration, image = scaleSEMimage(filepath)
        if self.Linescan:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(
                3, 1, sharex=True, gridspec_kw={'height_ratios': [1, 1, 3]})
        else:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(3,
                                                                 1,
                                                                 sharex=True,
                                                                 sharey=True)
        self.fig.patch.set_alpha(0)  #Transparency style
        self.fig.subplots_adjust(top=0.9,
                                 bottom=0.12,
                                 left=0.15,
                                 right=0.82,
                                 hspace=0.1,
                                 wspace=0.05)
        newX = np.linspace(xscale_CL[int(xcoord.min())],
                           xscale_CL[int(xcoord.max())], len(xscale_CL))
        newY = np.linspace(yscale_CL[int(ycoord.min())],
                           yscale_CL[int(ycoord.max())], len(yscale_CL))
        self.X = np.linspace(np.min(newX), np.max(newX),
                             self.hypSpectrum.shape[1])
        self.Y = np.linspace(np.min(newY), np.max(newY),
                             self.hypSpectrum.shape[0])

        nImage = np.array(
            image.crop(
                (xcoord.min(), ycoord.min(), xcoord.max(), ycoord.max())))
        self.ax.imshow(
            nImage,
            cmap='gray',
            vmin=0,
            vmax=65535,
            interpolation="None",
            extent=[np.min(newX),
                    np.max(newX),
                    np.max(newY),
                    np.min(newY)])

        self.hypimage = np.nansum(self.hypSpectrum, axis=2)
        jet = cm.get_cmap("jet")
        jet.set_bad(color='k')
        self.hyperspectralmap = cm.ScalarMappable(cmap=jet)
        self.hyperspectralmap.set_clim(vmin=np.nanmin(self.hypimage),
                                       vmax=np.nanmax(self.hypimage))
        self.lumimage = self.bx.imshow(
            self.hypimage,
            cmap=self.hyperspectralmap.cmap,
            norm=self.hyperspectralmap.norm,
            interpolation="None",
            extent=[np.min(newX),
                    np.max(newX),
                    np.max(newY),
                    np.min(newY)])
        if self.Linescan:
            self.linescan = np.nansum(self.hypSpectrum, axis=0)
            self.linescanmap = cm.ScalarMappable(cmap=jet)
            self.linescanmap.set_clim(vmin=np.nanmin(self.linescan),
                                      vmax=np.nanmax(self.linescan))
            # ATTENTION pcolormesh =/= imshow
            #imshow takes values at pixel
            #pcolormesh takes values between
            temp = np.linspace(newX.min(), newX.max(), self.X.size + 1)
            self.im = self.cx.pcolormesh(temp,
                                         eV_To_nm / self.wavelenght,
                                         self.linescan.T,
                                         cmap=self.linescanmap.cmap,
                                         shading='flat',
                                         rasterized=True)

            #dX=np.diff(self.X).mean()
            #newX = np.arange(min(self.X),max(self.X)+dX,dX)-dX/2
            #            dW = np.diff(self.wavelenght).mean()
            #            newW = np.arange(min(self.wavelenght),max(self.wavelenght)+dW,dW)-dW/2
            #self.im=self.cx.pcolormesh(newX,eV_To_nm/self.wavelenght,self.linescan.T,cmap=self.linescanmap.cmap,shading = 'flat',rasterized=True)
            def format_coord(x, y):
                xarr = self.X
                yarr = eV_To_nm / self.wavelenght
                if ((x > xarr.min()) and (x <= xarr.max()) and (y > yarr.min())
                        and (y <= yarr.max())):
                    col = np.argmin(abs(xarr - x))  #np.searchsorted(xarr, x)-1
                    row = np.argmin(abs(yarr - y))  #np.searchsorted(yarr, y)-1
                    z = self.linescan.T[row, col]
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}, z={z:1.2e}   [{row},{col}]'
                else:
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}'

            self.cx.format_coord = format_coord
            self.cx.set_ylabel("Energy (eV)")
            self.cx.set_xlabel("distance (µm)")
            self.cx.set_aspect(self.aspect)
        else:
            self.im = self.cx.imshow(self.wavelenght[np.argmax(
                self.hypSpectrum, axis=2)],
                                     cmap='viridis',
                                     extent=[
                                         np.min(newX),
                                         np.max(newX),
                                         np.max(newY),
                                         np.min(newY)
                                     ])
            self.cx.set_aspect(aspect)
        self.bx.set_aspect(self.aspect)
        self.ax.set_aspect(self.aspect)
        self.ax.get_shared_y_axes().join(self.ax, self.bx)
        self.ax.set_ylabel("distance (µm)")
        #    fig.text(ax.get_position().bounds[0]-0.11, ax.get_position().bounds[1],'distance (µm)',fontsize=16, va='center', rotation='vertical')

        pos = self.cx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.linescanmap, cax=cbar_ax)
        cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))

        pos = self.bx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.hyperspectralmap, cax=cbar_ax)
        cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))
        if save == True:
            self.fig.savefig(os.path.join(dirname, filename + ".png"), dpi=300)

        if autoclose == True:
            plt.close(self.fig)
            return None
        else:
            self.spec_fig, self.spec_ax = plt.subplots()
            self.spec_data, = self.spec_ax.plot(
                eV_To_nm / self.wavelenght, np.zeros(self.wavelenght.shape[0]))
            self.spec_ax.set_ylim((0, self.hypSpectrum.max()))
            self.spec_ax.set_xlabel('Energy (eV)')
            self.spec_ax.set_ylabel('Intensity (a.u)')
            self.spec_fig.subplots_adjust(top=0.885,
                                          bottom=0.125,
                                          left=0.13,
                                          right=0.94)
            self.spec_ax.ticklabel_format(axis='y',
                                          style='sci',
                                          scilimits=(0, 0))
            self.spec_minbar = self.spec_ax.axvline(eV_To_nm /
                                                    self.wavelenght.max())
            self.spec_maxbar = self.spec_ax.axvline(eV_To_nm /
                                                    self.wavelenght.min())

            #if Linescan:
            #self.cursor = MultiCursor(self.fig.canvas, (self.ax, self.bx), color='r', lw=1,horizOn=True, vertOn=True)
            def onmotion(event):
                if self.leftpressed:
                    x = event.xdata
                    y = event.ydata
                    if ((event.inaxes is not None) and (x > self.X.min())
                            and (x <= self.X.max()) and (y > self.Y.min())
                            and (y <= self.Y.max())):
                        indx = np.argmin(abs(x - self.X))
                        indy = np.argmin(abs(y - self.Y))
                        self.spec_data.set_ydata(self.hypSpectrum.data[indy,
                                                                       indx])
                        self.spec_fig.canvas.draw_idle()

            def onclick(event):
                if event.button == 1:
                    self.leftpressed = True
                    #self.cursor.active = True
                    #if event.dblclick:


#                        if not(self.cursor.visible):
#                            self.fig.canvas.blit(self.fig.bbox)
#self.cursor.active = not(self.cursor.active)
#                        self.cursor.visible = not(self.cursor.visible)
#                        self.fig.canvas.draw_idle()
#                        self.fig.canvas.blit(self.fig.bbox)
#                    elif self.cursor.active:
#                    x = event.xdata
#                    y = event.ydata
#                    if ((event.inaxes is not None) and (x > self.X.min()) and (x <= self.X.max()) and
#                        (y > self.Y.min()) and (y <= self.Y.max())):
#                        indx = np.argmin(abs(x-self.X))
#                        indy=np.argmin(abs(y-self.Y))
#                        self.spec_data.set_ydata(self.hypSpectrum.data[indy,indx])
#                        self.spec_fig.canvas.draw_idle()
                elif event.button == 3:
                    self.wavelenght = ma.masked_array(self.wavelenght.data,
                                                      mask=False)
                    hypmask = np.resize(self.wavelenght.mask,
                                        self.hypSpectrum.shape)
                    self.hypSpectrum = ma.masked_array(self.hypSpectrum.data,
                                                       mask=hypmask)
                    self.hypimage = np.nansum(self.hypSpectrum, axis=2)
                    self.lumimage.set_array(self.hypimage)
                    self.hyperspectralmap.set_clim(
                        vmin=np.nanmin(self.hypimage),
                        vmax=np.nanmax(self.hypimage))
                    self.cx.set_ylim(eV_To_nm / self.wavelenght.max(),
                                     eV_To_nm / self.wavelenght.min())
                    # TODO : A recoder
                    self.linescan = np.nansum(self.hypSpectrum, axis=0)
                    self.spec_maxbar.set_xdata(
                        np.repeat(eV_To_nm / self.wavelenght.min(), 2))
                    self.spec_minbar.set_xdata(
                        np.repeat(eV_To_nm / self.wavelenght.max(), 2))
                    self.im.set_array(
                        (self.linescan.T[:, :]).ravel())  #flat shading
                    #self.im.set_array((self.linescan.T).ravel())#gouraud shading
                    self.linescanmap.set_clim(vmin=np.nanmin(self.linescan),
                                              vmax=np.nanmax(self.linescan))
                    self.im.set_norm(self.linescanmap.norm)
                    #self.im.set_cmap(self.linescanmap.cmap)
                    self.fig.canvas.draw_idle()
                    self.spec_fig.canvas.draw_idle()
                    self.fig.canvas.blit(self.fig.bbox)

            def onrelease(event):
                if event.button == 1:
                    self.leftpressed = False
                    #self.cursor.active = False
            def onselect(ymin, ymax):
                indmin = np.argmin(abs(eV_To_nm / self.wavelenght - ymin))
                indmax = np.argmin(abs(eV_To_nm / self.wavelenght - ymax))
                #indmin resp.max est lindex tel que wavelenght[indmin] minimise
                # la distance entre la position du clic et la longueur d'onde
                #                print(eV_To_nm/self.wavelenght[indmin])
                #                print(eV_To_nm/self.wavelenght[indmax])
                if abs(indmax - indmin) < 1: return

                self.wavelenght = ma.masked_outside(self.wavelenght.data,
                                                    eV_To_nm / ymax,
                                                    eV_To_nm / ymin)
                hypmask = np.resize(self.wavelenght.mask,
                                    self.hypSpectrum.shape)
                self.hypSpectrum = ma.masked_array(self.hypSpectrum.data,
                                                   mask=hypmask)
                self.hypimage = np.nansum(self.hypSpectrum, axis=2)
                self.lumimage.set_array(self.hypimage)
                self.hyperspectralmap.set_clim(vmin=np.nanmin(self.hypimage),
                                               vmax=np.nanmax(self.hypimage))
                self.cx.set_ylim(eV_To_nm / self.wavelenght.max(),
                                 eV_To_nm / self.wavelenght.min())
                self.linescan = np.nansum(self.hypSpectrum, axis=0)
                self.spec_maxbar.set_xdata(
                    np.repeat(eV_To_nm / self.wavelenght.min(), 2))
                self.spec_minbar.set_xdata(
                    np.repeat(eV_To_nm / self.wavelenght.max(), 2))
                self.im.set_array(
                    (self.linescan.T[:, :]).ravel())  #flat shading
                #self.im.set_array((self.linescan.T).ravel())#gouraud shading
                self.linescanmap.set_clim(vmin=np.nanmin(self.linescan),
                                          vmax=np.nanmax(self.linescan))
                self.im.set_norm(self.linescanmap.norm)
                #self.im.set_cmap(self.linescanmap.cmap)
                self.fig.canvas.draw_idle()
                self.spec_fig.canvas.draw_idle()
                self.fig.canvas.blit(self.fig.bbox)

            self.span = None
            if Linescan:
                self.span = SpanSelector(self.cx,
                                         onselect,
                                         'vertical',
                                         useblit=True,
                                         rectprops=dict(alpha=0.5,
                                                        facecolor='red'),
                                         button=1)
            self.fig.canvas.mpl_connect('button_press_event', onclick)
            self.fig.canvas.mpl_connect('motion_notify_event', onmotion)
            self.fig.canvas.mpl_connect('button_release_event', onrelease)
            plt.show(block=True)
コード例 #6
0
    def __init__(self,
                 path,
                 deadPixeltol=2,
                 aspect="auto",
                 Linescan=True,
                 autoclose=False):
        self.shift_is_held = False
        self.path = path
        self.deadPixeltol = deadPixeltol
        self.aspect = aspect
        self.Linescan = Linescan

        if autoclose:
            plt.ioff()
        else:
            plt.ion()

        dirname, filename = os.path.split(self.path)
        filename, ext = os.path.splitext(filename)
        #    try:
        #        infos = os.path.basename(dirname).split("-")
        #        T = '' if not [x for x in infos if ('K' in x)] else [x for x in infos if ('K' in x)][0]
        #        Sample = infos[1]
        #        wire = '' if not [x for x in infos if ('fil' in x.lower())] else [x for x in infos if ('fil' in x.lower())][0]
        #        if not wire:
        #            wire = os.path.basename(os.path.dirname(dirname))
        #        wire = wire.lower().replace('fil','Wire')
        #    except:
        #        T=''
        #        Sample = ''
        #        wire = ''
        hyppath = self.path
        specpath = os.path.join(dirname, filename + '_X_axis.asc')
        filepath = os.path.join(dirname,
                                filename + '_SEM image after carto.tif')
        data = np.loadtxt(hyppath)
        xlen = int(data[0, 0])  #nbr de pts selon x
        ylen = int(data[1, 0])  #nbr de pts selon y
        wavelenght = np.loadtxt(specpath)
        self.wavelenght = wavelenght[:2048]  #bins du spectro
        xcoord = data[0, 1:]
        ycoord = data[1, 1:]
        CLdata = data[
            2:,
            1:]  #tableau de xlen * ylen points (espace) et 2048 longueur d'onde CLdata[:,n] n = numero du spectr
        self.hypSpectrum = np.transpose(
            np.reshape(np.transpose(CLdata),
                       (ylen, xlen, len(self.wavelenght))), (0, 1, 2))

        #correct dead / wrong pixels
        self.hypSpectrum, self.hotpixels = correct_dead_pixel(
            self.hypSpectrum, tol=self.deadPixeltol)

        #reduce the spectrum if wanted
        #    if len(EnergyRange)==2:
        #
        #        EnergyRange = eV_To_nm/np.array(EnergyRange) # en nm
        #        EnergyRange.sort()
        #        wavelenght = ma.masked_outside(wavelenght,*EnergyRange)
        #        hypmask = np.resize(wavelenght.mask,hypSpectrum.shape)
        #        hypSpectrum = ma.masked_array(hypSpectrum,mask=hypmask)
        ##        lidx = np.argmin(np.abs(wavelenght-EnergyRange[0]))
        ##        ridx = np.argmin(np.abs(wavelenght-EnergyRange[1]))
        ##        hypmask = np.zeros(hypSpectrum.shape,dtype=bool)
        ##        hypmask[:,:,indmin:indmax] = True
        ##        specmask = np.zeros(wavelenght.shape,dtype=bool)
        ##        specmask[lidx:ridx] = True
        #    else:
        self.wavelenght = ma.masked_array(self.wavelenght, mask=False)
        hypmask = np.resize(self.wavelenght.mask, self.hypSpectrum.shape)
        self.hypSpectrum = ma.masked_array(self.hypSpectrum, mask=hypmask)

        self.linescan = np.sum(self.hypSpectrum, axis=0)
        #    if normalise:
        #        linescan = linescan/np.max(linescan,axis=1,keepdims=True)
        #    linescan -= linescan.min()
        xscale_CL, yscale_CL, acceleration, image = scaleSEMimage(filepath)
        if self.Linescan:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(
                3, 1, sharex=True, gridspec_kw={'height_ratios': [1, 1, 3]})
        else:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(3,
                                                                 1,
                                                                 sharex=True,
                                                                 sharey=True)
        self.fig.patch.set_alpha(0)  #Transparency style
        self.fig.subplots_adjust(top=0.9,
                                 bottom=0.12,
                                 left=0.15,
                                 right=0.82,
                                 hspace=0.1,
                                 wspace=0.05)
        newX = np.linspace(xscale_CL[int(xcoord.min())],
                           xscale_CL[int(xcoord.max())], len(xscale_CL))
        newY = np.linspace(yscale_CL[int(ycoord.min())],
                           yscale_CL[int(ycoord.max())], len(yscale_CL))
        self.X = np.linspace(np.min(newX), np.max(newX),
                             self.hypSpectrum.shape[1])
        self.Y = np.linspace(np.min(newY), np.max(newY),
                             self.hypSpectrum.shape[0])

        nImage = np.array(
            image.crop(
                (xcoord.min(), ycoord.min(), xcoord.max(), ycoord.max())))
        self.ax.imshow(
            nImage,
            cmap='gray',
            vmin=0,
            vmax=65535,
            interpolation="None",
            extent=[np.min(newX),
                    np.max(newX),
                    np.max(newY),
                    np.min(newY)])
        #        self.hypSpectrum = hypSpectrum-threshold
        #        hypSpectrum = hypSpectrum*(hypSpectrum>=0)
        self.hypimage = np.sum(self.hypSpectrum, axis=2)
        self.hypimage -= self.hypimage.min()
        #    if log:
        #        hypimage=np.log10(hypimage+1)
        #        linescan = np.log10(linescan+1)
        self.lumimage = self.bx.imshow(
            self.hypimage,
            cmap='jet',
            interpolation="None",
            extent=[np.min(newX),
                    np.max(newX),
                    np.max(newY),
                    np.min(newY)])
        #        self.lumimage = self.bx.pcolormesh(self.X,self.Y,self.hypimage,cmap='jet')
        if self.Linescan:
            self.im = self.cx.pcolormesh(self.X,
                                         eV_To_nm / self.wavelenght,
                                         self.linescan.T,
                                         cmap='jet')

            def format_coord(x, y):
                xarr = self.X
                yarr = eV_To_nm / self.wavelenght
                if ((x > xarr.min()) and (x <= xarr.max()) and (y > yarr.min())
                        and (y <= yarr.max())):
                    col = np.argmin(abs(xarr - x))  #np.searchsorted(xarr, x)-1
                    row = np.argmin(abs(yarr - y))  #np.searchsorted(yarr, y)-1
                    z = self.linescan.T[row, col]
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}, z={z:1.2e}   [{row},{col}]'
                else:
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}'

            self.cx.format_coord = format_coord
            self.cx.set_ylabel("Energy (eV)")
            self.cx.set_xlabel("distance (µm)")
            self.cx.set_aspect('auto')
        else:
            self.im = self.cx.imshow(self.wavelenght[np.argmax(
                self.hypSpectrum, axis=2)],
                                     cmap='viridis',
                                     extent=[
                                         np.min(newX),
                                         np.max(newX),
                                         np.max(newY),
                                         np.min(newY)
                                     ])
            self.cx.set_aspect(aspect)
        self.bx.set_aspect(self.aspect)
        self.ax.set_aspect(self.aspect)
        self.ax.get_shared_y_axes().join(self.ax, self.bx)
        self.ax.set_ylabel("distance (µm)")
        #    fig.text(ax.get_position().bounds[0]-0.11, ax.get_position().bounds[1],'distance (µm)',fontsize=16, va='center', rotation='vertical')

        pos = self.cx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.im, cax=cbar_ax)
        cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))

        pos = self.bx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.lumimage, cax=cbar_ax)
        cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))
        #    if save==True:
        #        savedir = os.path.join(dirname,'Saved')
        #        if not os.path.exists(savedir):
        #            os.mkdir(savedir)
        #        savename = 'SEM+Hyp+Linescan'
        #        if ((len(T)>0) & (len(Sample)>0) & (len(wire)>0)):
        #            savename='%s_%s_%s_%s'%(savename,Sample,T,wire)
        #        fig.savefig(os.path.join(dirname,'Saved',savename+".png"),dpi=300)

        if autoclose == True:
            plt.close(self.fig)
            return None
        else:
            self.spec_fig, self.spec_ax = plt.subplots()
            self.spec_data, = self.spec_ax.plot(
                eV_To_nm / self.wavelenght, np.zeros(self.wavelenght.shape[0]))
            self.spec_ax.set_ylim((0, self.hypSpectrum.max()))
            if Linescan:
                #            multi = MyMultiCursor(fig.canvas,(ax, bx,cx), color='r',lw=1,horizOn=[ax,bx], vertOn=[ax,bx,cx],useblit=True)#, lw=1, horizOn=[ax,bx], vertOn=[ax,bx,cx])
                #            multi = Cursor(bx, color='r',lw=1,useblit=True)#, lw=1, horizOn=[ax,bx], vertOn=[ax,bx,cx])
                self.cursor = MultiCursor(self.fig.canvas, (self.ax, self.bx),
                                          color='r',
                                          lw=1,
                                          horizOn=True,
                                          vertOn=True)

            def onclick(event):
                if event.button == 1:
                    if event.dblclick:
                        self.cursor.active = not (self.cursor.active)
                    elif self.cursor.active:
                        x = event.xdata
                        y = event.ydata
                        if ((event.inaxes is not None) and (x > self.X.min())
                                and (x <= self.X.max()) and (y > self.Y.min())
                                and (y <= self.Y.max())):
                            indx = np.argmin(abs(x - self.X))
                            indy = np.argmin(abs(y - self.Y))
                            self.spec_data.set_ydata(
                                self.hypSpectrum.data[indy, indx])
                            self.spec_fig.canvas.draw_idle()
                elif event.button == 3:
                    self.wavelenght = ma.masked_array(self.wavelenght.data,
                                                      mask=False)
                    hypmask = np.resize(self.wavelenght.mask,
                                        self.hypSpectrum.shape)
                    self.hypSpectrum = ma.masked_array(self.hypSpectrum.data,
                                                       mask=hypmask)
                    self.hypimage = np.sum(self.hypSpectrum, axis=2)
                    self.hypimage -= self.hypimage.min()
                    self.lumimage.set_array(self.hypimage)
                    self.cx.set_ylim(eV_To_nm / self.wavelenght.max(),
                                     eV_To_nm / self.wavelenght.min())
                    self.fig.canvas.draw_idle()

            def onselect(ymin, ymax):
                indmin = np.argmin(abs(eV_To_nm / self.wavelenght - ymin))
                indmax = np.argmin(abs(eV_To_nm / self.wavelenght - ymax))
                #
                ##            thisx = x[indmin:indmax]
                if abs(indmax - indmin) < 1: return
                #            indmin, indmax = np.sort((indmax,indmin))
                #            thiswave = wavelenght[indmin:indmax]
                #            mask = np.zeros(hypSpectrum.shape)
                #            mask[:,:,indmin:indmax] = 1
                #            hypimage=np.sum(hypSpectrum*mask,axis=2)
                #            hypimage -= hypimage.min()
                #            lumimage.set_array(hypimage)
                self.wavelenght = ma.masked_outside(self.wavelenght,
                                                    eV_To_nm / ymin,
                                                    eV_To_nm / ymax)
                hypmask = np.resize(self.wavelenght.mask,
                                    self.hypSpectrum.shape)
                self.hypSpectrum = ma.masked_array(self.hypSpectrum,
                                                   mask=hypmask)
                self.hypimage = np.sum(self.hypSpectrum, axis=2)
                self.hypimage -= self.hypimage.min()
                self.lumimage.set_array(self.hypimage)
                self.cx.set_ylim(eV_To_nm / self.wavelenght.max(),
                                 eV_To_nm / self.wavelenght.min())
                self.fig.canvas.draw_idle()

            self.span = None
            if Linescan:
                self.span = SpanSelector(self.cx,
                                         onselect,
                                         'vertical',
                                         useblit=True,
                                         rectprops=dict(alpha=0.5,
                                                        facecolor='red'),
                                         button=1)
            self.fig.canvas.mpl_connect('button_press_event', onclick)


#            return self.hypSpectrum, self.wavelenght, spec_fig, spec_ax, cursor, span

#def main():
#    path = input("Enter the path of your file: ")
#    path=path.replace('"','')
#    path=path.replace("'",'')
##    path = r'C:/Users/sylvain.finot/Documents/data/2019-03-11 - T2597 - 5K/Fil3/TRCL-cw455nm/TRCL.dat'
#    global hyp, wavelenght,spec_fig,spec_ax,cursor, span
#    hyp, wavelenght, spec_fig, spec_ax, cursor, span = make_linescan(path,save=False,deadPixeltol=200,normalise=False,Linescan=True)
#
#if __name__ == '__main__':
#    main()
コード例 #7
0
    def __init__(self,
                 path,
                 deadPixeltol=2,
                 aspect="auto",
                 Linescan=True,
                 autoclose=False,
                 save=False,
                 lognorm=False,
                 trueSEM=True):
        #%% Init
        self.shift_is_held = False
        if type(path) == str:
            self.path = [path]
        else:
            self.path = path
        self.deadPixeltol = deadPixeltol
        self.aspect = aspect
        self.Linescan = Linescan
        self.leftpressed = False

        if autoclose:
            plt.ioff()
        else:
            plt.ion()
        dirnameL = list()
        wavelenghtL = list()
        infoL = list()
        CLdataL = list()
        xlenL = list()
        ylenL = list()
        xcoordL = list()
        ycoordL = list()
        semImageL = list()
        semCartoL = list()
        #%% Prepare data
        for i in range(len(self.path)):
            dirname, filename = os.path.split(self.path[i])
            filename, ext = os.path.splitext(filename)
            dirnameL.append(os.path.join(dirname, filename))
            hyppath = self.path[i]
            specpath = os.path.join(dirnameL[i] + '_X_axis.asc')
            semImageL.append(
                os.path.join(dirnameL[i] + '_SEM image after carto.tif'))
            if os.path.exists(os.path.join(dirnameL[i] + '_SEM_carto.asc')):
                semCartoL.append(os.path.join(dirnameL[i] + '_SEM_carto.asc'))
            #data = np.loadtxt(hyppath)
            data = pd.read_csv(hyppath,
                               delimiter='\t',
                               header=None,
                               dtype=np.float).to_numpy()  #faster
            xlenL.append(int(data[0, 0]))  #number of pixel in x dir
            ylenL.append(int(data[1, 0]))  #number of pixel in y dir
            #spec=np.loadtxt(specpath)[:2048]
            spec = pd.read_csv(specpath,
                               header=None,
                               dtype=np.float,
                               nrows=2048).to_numpy()[:, 0]  #faster
            wavelenghtL.append(spec)
            xcoordL.append(data[0, 1:])  # x's pixels of the carto
            ycoordL.append(data[1, 1:])  # y's pixels of the carto
            CLdataL.append(data[2:, 1:])  # spectra of each point
            spectroInfos = getSpectro_info(
                os.path.join(dirnameL[i] + '_spectro_info.asc'))
            #'Grating','Entrance slit (mm)', 'Exposure time (s)','High voltage (V)','Spot size'
            logger.info('\n %s' % spectroInfos)
            infoL.append(spectroInfos[:, 1])

        add_meta_data({"Description": str(dict(spectroInfos))})
        wavelenghtL = np.array(wavelenghtL)
        inds = wavelenghtL.argsort(axis=0)[:, 0]
        CLdataL = list(np.array(CLdataL)[inds])
        xlen = np.array(xlenL)[inds]
        ylen = np.array(ylenL)[inds]
        xcoord = np.array(xcoordL)[inds]
        ycoord = np.array(ycoordL)[inds]
        wavelenght = list(wavelenghtL[inds])

        SameConditions = compareManyArrays(infoL) & compareManyArrays(
            xcoordL) & compareManyArrays(xcoordL)
        print('Same Conditions : ', SameConditions)

        # Filling gap spectra's gap with 0
        WaveStep = wavelenght[0][-1] - wavelenght[0][-2]
        while (len(CLdataL) > 1):
            #On cherche l'index de wavelenght[1] minimisant l'écart avec wavelenght[0]
            ridx = abs(wavelenght[0] - wavelenght[1].min()).argmin()
            #On créé les longueurs d'onde manquantes dans l'écart
            patch = np.arange(wavelenght[0][ridx], wavelenght[1][0], WaveStep)
            #On assemble les 2 spectres avec le patch au milieu
            nwavelenght = np.concatenate(
                (wavelenght[0][:ridx], patch, wavelenght[1]))
            # patch des données CL
            patch = np.zeros(
                (len(patch), CLdataL[1].shape[1]),
                dtype=np.float) * np.nan  #* np.min((CLdata[0],CLdata[1])) -10
            nCLdata = np.concatenate((CLdataL[0][:ridx], patch, CLdataL[1]))
            #On remplace les spectres et les données des deux premiers par le nouveau combiné
            wavelenght = [nwavelenght, *wavelenght[2:]]
            CLdataL = [nCLdata, *CLdataL[2:]]

        self.wavelenght = np.array(wavelenght)[0]
        CLdata = np.array(CLdataL)[0]
        xlen = np.array(xlenL)[0]
        ylen = np.array(ylenL)[0]
        xcoord = np.array(xcoordL)[0]
        ycoord = np.array(ycoordL)[0]
        filepath = semImageL[0]
        semCarto = np.loadtxt(semCartoL[0])
        self.hypSpectrum = np.transpose(
            np.reshape(np.transpose(CLdata),
                       (ylen, xlen, len(self.wavelenght))), (0, 1, 2))
        #correct dead / wrong pixels
        #if len(self.path)==1:
        #    self.hypSpectrum, self.hotpixels = correct_dead_pixel(self.hypSpectrum,tol=self.deadPixeltol)
        self.wavelenght = ma.masked_array(self.wavelenght,
                                          mask=False)  #Nothing is masked
        #np.resize(self.wavelenght.mask,self.hypSpectrum.shape)
        #self.hypSpectrum = ma.masked_array(self.hypSpectrum,mask=hypmask)
        self.hypSpectrum = ma.masked_invalid(self.hypSpectrum)

        xscale_CL, yscale_CL, acceleration, image = scaleSEMimage(filepath)

        newX = np.linspace(xscale_CL[int(xcoord.min())],
                           xscale_CL[int(xcoord.max())], len(xscale_CL))
        newY = np.linspace(yscale_CL[int(ycoord.min())],
                           yscale_CL[int(ycoord.max())], len(yscale_CL))
        self.X = np.linspace(np.min(newX), np.max(newX),
                             self.hypSpectrum.shape[1])
        self.Y = np.linspace(np.min(newY), np.max(newY),
                             self.hypSpectrum.shape[0])

        #SEM image / carto
        nImage = np.array(
            image.crop(
                (xcoord.min(), ycoord.min(), xcoord.max(), ycoord.max())))
        #%% Plot
        if self.Linescan:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(
                3, 1, sharex=True, gridspec_kw={'height_ratios': [1, 1, 3]})
        else:
            self.fig, (self.ax, self.bx, self.cx) = plt.subplots(3,
                                                                 1,
                                                                 sharex=True,
                                                                 sharey=True)
        self.fig.patch.set_alpha(0)  #Transparency style
        self.fig.subplots_adjust(top=0.9,
                                 bottom=0.12,
                                 left=0.15,
                                 right=0.82,
                                 hspace=0.1,
                                 wspace=0.05)

        if (len(semCarto) > 0) & (trueSEM):
            logger.info("True SEM = %s" % str(trueSEM))
            self.ax.imshow(semCarto,cmap='gray',interpolation = 'nearest',\
                           extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
        else:
            self.ax.imshow(nImage,cmap='gray',vmin=0,vmax=65535,\
                           interpolation = 'nearest',\
                               extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
        self.hypimage = np.nansum(self.hypSpectrum, axis=2)
        jet = cm.jet
        jet.set_bad(color='k')
        self.hyperspectralmap = cm.ScalarMappable(cmap=jet)
        self.hyperspectralmap.set_clim(vmin=np.nanmin(self.hypimage),
                                       vmax=np.nanmax(self.hypimage))

        self.lumimage = self.bx.imshow(self.hypimage,cmap=self.hyperspectralmap.cmap,\
                                           norm=self.hyperspectralmap.norm,\
                                           interpolation = 'nearest',\
                                           extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
        if self.Linescan:
            self.linescan = np.sum(self.hypSpectrum, axis=0)
            if lognorm:
                norm = matplotlib.colors.LogNorm(
                )  #vmin=np.nanmin(self.linescan),vmax=np.nanmax(self.linescan))
            else:
                norm = matplotlib.colors.Normalize(
                )  #vmin=np.nanmin(self.linescan),vmax=np.nanmax(self.linescan))
            self.linescanmap = cm.ScalarMappable(cmap=jet, norm=norm)
            self.linescanmap.set_clim(vmin=np.nanmin(self.linescan),
                                      vmax=np.nanmax(self.linescan))
            # ATTENTION pcolormesh =/= imshow
            #imshow takes values at pixel
            #pcolormesh takes values between
            temp = np.linspace(newX.min(), newX.max(), self.X.size + 1)
            self.im=self.cx.pcolormesh(temp,eV_To_nm/self.wavelenght,self.linescan.T,\
                                       cmap=self.linescanmap.cmap,norm=self.linescanmap.norm,\
                                           rasterized=True)

            def format_coord(x, y):
                xarr = self.X
                yarr = eV_To_nm / self.wavelenght
                if ((x > xarr.min()) and (x <= xarr.max()) and (y > yarr.min())
                        and (y <= yarr.max())):
                    col = np.searchsorted(xarr,
                                          x) - 1  #np.argmax(abs(xarr-x))#
                    row = np.argmin(abs(yarr -
                                        y))  #np.searchsorted(yarr, y)-1#
                    z = self.linescan.T[row, col]
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}, z={z:1.2e}   [{row},{col}]'
                else:
                    return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}'

            self.cx.format_coord = format_coord
            self.cx.set_ylabel("Energy (eV)")
            self.cx.set_xlabel("distance (µm)")
            self.cx.set_aspect(self.aspect)
        else:
            self.im = self.cx.imshow(self.wavelenght[np.nanargmax(self.hypSpectrum,axis=2)],\
                                     cmap='viridis',\
                                    extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
            self.cx.set_aspect(aspect)
        self.bx.set_aspect(self.aspect)
        self.ax.set_aspect(self.aspect)
        self.ax.get_shared_y_axes().join(self.ax, self.bx)
        self.ax.set_ylabel("distance (µm)")

        #Colorbar du linescan
        pos = self.cx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.linescanmap, cax=cbar_ax)
        if not lognorm:
            cbar_ax.ticklabel_format(axis='both',
                                     style='sci',
                                     scilimits=(0, 0))

        #Colorbar de l'image
        pos = self.bx.get_position().bounds
        cbar_ax = self.fig.add_axes([0.85, pos[1], 0.05, pos[-1] * 0.9])
        self.fig.colorbar(self.hyperspectralmap, cax=cbar_ax)
        cbar_ax.ticklabel_format(axis='both', style='sci', scilimits=(0, 0))
        #%%
        if save == True:
            self.fig.savefig(os.path.join(dirname, filename + ".png"), dpi=300)
        if autoclose == True:
            plt.close(self.fig)
            return None
        self.Spectrum = Spectrum(self.wavelenght, np.nanmax(self.hypSpectrum))
        self.Spectrum.plot_meanSpectrum(self.wavelenght,
                                        np.mean(self.hypSpectrum, axis=(0, 1)))

        #if Linescan:
        #self.cursor = MultiCursor(self.fig.canvas, (self.ax, self.bx), color='r', lw=1,horizOn=True, vertOn=True)

        def on_xlims_change(axes):
            print("updated xlims: ", axes)

        def onmotion(event):
            if self.leftpressed:
                x = event.xdata
                y = event.ydata
                if ((event.inaxes is not None) and (x > self.X.min())
                        and (x <= self.X.max()) and (y > self.Y.min())
                        and (y <= self.Y.max())):
                    indx = np.argmin(abs(x - self.X))
                    indy = np.argmin(abs(y - self.Y))
                    self.Spectrum.set_y(self.hypSpectrum.data[indy, indx])
                    self.Spectrum.update()

        def onclick(event):
            if event.button == 1:
                self.leftpressed = True
                onmotion(event)
            elif event.button == 3:
                self.update()

        def onrelease(event):
            if event.button == 1:
                self.leftpressed = False
                #self.cursor.active = False
        def onselect(ymin, ymax):
            indmin = np.argmin(abs(eV_To_nm / self.wavelenght - ymin))
            indmax = np.argmin(abs(eV_To_nm / self.wavelenght - ymax))
            #indmin resp.max est lindex tel que wavelenght[indmin] minimise
            # la distance entre la position du clic et la longueur d'onde
            #                print(eV_To_nm/self.wavelenght[indmin])
            #                print(eV_To_nm/self.wavelenght[indmax])
            if abs(indmax - indmin) < 1: return
            self.update(ymin, ymax)

        self.span = None
        if Linescan:
            self.span = SpanSelector(self.cx,
                                     onselect,
                                     'vertical',
                                     useblit=True,
                                     rectprops=dict(alpha=0.5,
                                                    facecolor='red'),
                                     button=1)
        self.fig.canvas.mpl_connect('button_press_event', onclick)
        self.fig.canvas.mpl_connect('motion_notify_event', onmotion)
        self.fig.canvas.mpl_connect('button_release_event', onrelease)
        #            return self.hypSpectrum, self.wavelenght, spec_fig, spec_ax, cursor, span
        plt.show(block=True)
コード例 #8
0
def make_linescan(path,save=False,autoclose=False,log=False,threshold=0,deadPixeltol = 2,aspect="auto",EnergyRange = [],normalise=False,Linescan=False):
#    path=r'D:/M2 Internship/data/2019-03-08 - T2601 - 300K/Fil2/HYP1-T2601-300K-Vacc5kV-spot7-zoom6000x-gr600-slit0-2-t5ms-Fil1-cw440nm/Hyp.dat'
#    path = r"C:\Users\sylvain.finot\Documents\data\2019-03-22 - T2594 - Rampe\300K\HYP1-T2594-310K-Vacc5kV-spot7-zoom6000x-gr600-slit0-2-t5ms-cw440nm\Hyp.dat"
#    path=r'C:/Users/sylvain.finot/Documents/data/2019-04-29 - T2594 Al - 300K/Fil 2/HYP-T2594Al-300K-Vacc5kV-spot5-zoom10000x-gr600-slit0-2-t005ms-cw450nm/Hyp.dat'
#    path=r'D:/M2 Internship/data/2019-05-16 - T2455 Al - 300K/FULLSCREEN HYP2-T2455-300K-Vacc5kV-spot5-zoom8000x-gr600-slit0-2-t005ms-cw440nm/Hyp.dat'
    if autoclose:
        plt.ioff()
    else:
        plt.ion()
    dirname,filename = os.path.split(path)
    filename, ext = os.path.splitext(filename)
    try:
        infos = os.path.basename(dirname).split("-")
        T = '' if not [x for x in infos if ('K' in x)] else [x for x in infos if ('K' in x)][0]
        Sample = infos[1]
        wire = '' if not [x for x in infos if ('fil' in x.lower())] else [x for x in infos if ('fil' in x.lower())][0]
        if not wire:
            wire = os.path.basename(os.path.dirname(dirname))
        wire = wire.lower().replace('fil','Wire')
    except:
        T=''
        Sample = ''
        wire = ''
    hyppath = path
    specpath = os.path.join(dirname,filename+'_X_axis.asc')
    filepath = os.path.join(dirname,filename+'_SEM image after carto.tif')
    data = np.loadtxt(hyppath)
    xlen = int(data[0,0])   #nbr de pts selon x
    ylen = int(data[1,0])   #nbr de pts selon y
    wavelenght = np.loadtxt(specpath)
    wavelenght = wavelenght[:2048] #bins du spectro
    xcoord = data[0,1:]
    ycoord = data[1,1:]
    CLdata = data[2:,1:] #tableau de xlen * ylen points (espace) et 2048 longueur d'onde CLdata[:,n] n = numero du spectr
    hypSpectrum = np.transpose(np.reshape(np.transpose(CLdata),(ylen,xlen ,len(wavelenght))), (0, 1, 2))
    
    
    #correct dead / wrong pixels
    hypSpectrum, hotpixels = correct_dead_pixel(hypSpectrum,tol=deadPixeltol)

    #reduce the spectrum if wanted
    if len(EnergyRange)==2:
        
        EnergyRange = eV_To_nm/np.array(EnergyRange) # en nm
        EnergyRange.sort()
        wavelenght = ma.masked_outside(wavelenght,*EnergyRange)
        hypmask = np.resize(wavelenght.mask,hypSpectrum.shape)
        hypSpectrum = ma.masked_array(hypSpectrum,mask=hypmask)
#        lidx = np.argmin(np.abs(wavelenght-EnergyRange[0]))
#        ridx = np.argmin(np.abs(wavelenght-EnergyRange[1]))
#        hypmask = np.zeros(hypSpectrum.shape,dtype=bool)
#        hypmask[:,:,indmin:indmax] = True
#        specmask = np.zeros(wavelenght.shape,dtype=bool)
#        specmask[lidx:ridx] = True
    else:
        wavelenght = ma.masked_array(wavelenght,mask=False)
        hypmask = np.resize(wavelenght.mask,hypSpectrum.shape)
        hypSpectrum = ma.masked_array(hypSpectrum,mask=hypmask)
        
    
    linescan = np.sum(hypSpectrum,axis=0)
    if normalise:
        linescan = linescan/np.max(linescan,axis=1,keepdims=True)
#    linescan -= linescan.min()
    xscale_CL,yscale_CL,acceleration,image = scaleSEMimage(filepath)
    if Linescan:
        fig,(ax,bx,cx)=plt.subplots(3,1,sharex=True,gridspec_kw={'height_ratios': [1,1, 3]})
    else:
        fig,(ax,bx,cx)=plt.subplots(3,1,sharex=True,sharey=True)
    fig.patch.set_alpha(0) #Transparency style
    fig.subplots_adjust(top=0.9,bottom=0.12,left=0.15,right=0.82,hspace=0.1,wspace=0.05)
    newX = np.linspace(xscale_CL[int(xcoord.min())],xscale_CL[int(xcoord.max())],len(xscale_CL))
    newY = np.linspace(yscale_CL[int(ycoord.min())],yscale_CL[int(ycoord.max())],len(yscale_CL))
    X = np.linspace(np.min(newX),np.max(newX),hypSpectrum.shape[1])
    Y = np.linspace(np.min(newY),np.max(newY),hypSpectrum.shape[0])
    
    nImage = np.array(image.crop((xcoord.min(),ycoord.min(),xcoord.max(),ycoord.max())))
    ax.imshow(nImage,cmap='gray',vmin=0,vmax=65535,extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
    hypSpectrum = hypSpectrum-threshold
    hypSpectrum = hypSpectrum*(hypSpectrum>=0)
    hypimage=np.sum(hypSpectrum,axis=2)
    hypimage -= hypimage.min()
    if log:
        hypimage=np.log10(hypimage+1)
        linescan = np.log10(linescan+1)
    lumimage = bx.imshow(hypimage,cmap='jet',extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])
    if Linescan:
#        im=cx.imshow(linescan.T,cmap='jet',extent=[np.min(newX),np.max(newX),eV_To_nm/wavelenght.max(),eV_To_nm/wavelenght.min()])
        im=cx.pcolormesh(X,eV_To_nm/wavelenght,linescan.T,cmap='jet')
        def format_coord(x, y):
            xarr = X
            yarr = eV_To_nm/wavelenght
            if ((x > xarr.min()) & (x <= xarr.max()) & 
                (y > yarr.min()) & (y <= yarr.max())):
                col = np.argmin(abs(xarr-x))#np.searchsorted(xarr, x)-1
                row = np.argmin(abs(yarr-y))#np.searchsorted(yarr, y)-1
                z = linescan.T[row, col]
                return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}, z={z:1.2e}   [{row},{col}]'
            else:
                return f'x={x:1.4f}, y={y:1.4f}, lambda={eV_To_nm/y:1.2f}'

        cx.format_coord = format_coord
        cx.set_ylabel("Energy (eV)")
        cx.set_xlabel("distance (µm)")
        cx.set_aspect('auto')
    else:
        im = cx.imshow(wavelenght[np.argmax(hypSpectrum,axis=2)],cmap='viridis',extent=[np.min(newX),np.max(newX),np.max(newY),np.min(newY)])    
        cx.set_aspect(aspect)
    bx.set_aspect(aspect)
    ax.set_aspect(aspect)
    ax.get_shared_y_axes().join(ax, bx)
    ax.set_ylabel("distance (µm)")
#    fig.text(ax.get_position().bounds[0]-0.11, ax.get_position().bounds[1],'distance (µm)',fontsize=16, va='center', rotation='vertical')
    
    pos = cx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1]*0.9])  
    fig.colorbar(im, cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both',style='sci',scilimits=(0,0))
    
    pos = bx.get_position().bounds
    cbar_ax = fig.add_axes([0.85, pos[1], 0.05, pos[-1]*0.9])
    fig.colorbar(lumimage,cax=cbar_ax)
    cbar_ax.ticklabel_format(axis='both',style='sci',scilimits=(0,0))
    if save==True:
        savedir = os.path.join(dirname,'Saved')
        if not os.path.exists(savedir):
            os.mkdir(savedir)
        savename = 'SEM+Hyp+Linescan'
        if ((len(T)>0) & (len(Sample)>0) & (len(wire)>0)):
            savename='%s_%s_%s_%s'%(savename,Sample,T,wire)
        fig.savefig(os.path.join(dirname,'Saved',savename+".png"),dpi=300)
            
    if autoclose==True:
        plt.close(fig)
        return None
    else:
        spec_fig, spec_ax = plt.subplots()
        spec_data, = spec_ax.plot(eV_To_nm/wavelenght,np.zeros(wavelenght.shape[0]))
        spec_ax.set_ylim((0,hypSpectrum.max()))
        if Linescan:    
#            multi = MyMultiCursor(fig.canvas,(ax, bx,cx), color='r',lw=1,horizOn=[ax,bx], vertOn=[ax,bx,cx],useblit=True)#, lw=1, horizOn=[ax,bx], vertOn=[ax,bx,cx])
#            multi = Cursor(bx, color='r',lw=1,useblit=True)#, lw=1, horizOn=[ax,bx], vertOn=[ax,bx,cx])
            multi = MultiCursor(fig.canvas, (ax, bx), color='r', lw=1,horizOn=True, vertOn=True)
        def onclick(event):
            print(event)
            if event.dblclick:
                cursor.active = not(cursor.active)
            elif cursor.active:
                x = event.xdata
                y = event.ydata
                if ((event.inaxes is not None) and (x > X.min()) & (x <= X.max()) & 
                    (y > Y.min()) & (y <= Y.max())):
                    indx = np.argmin(abs(x-X))
                    indy=np.argmin(abs(y-Y))
                    spec_data.set_ydata(hypSpectrum[indy,indx])
                    spec_fig.canvas.draw_idle()
        def onselect(ymin, ymax,wavelenght,hypSpectrum):
#            indmin = np.argmin(abs(eV_To_nm/wavelenght-ymin))
#            indmax = np.argmin(abs(eV_To_nm/wavelenght-ymax))
#        
##            thisx = x[indmin:indmax]
#            if abs(indmax-indmin)<1:return
#            indmin, indmax = np.sort((indmax,indmin))
#            thiswave = wavelenght[indmin:indmax]
#            mask = np.zeros(hypSpectrum.shape)
#            mask[:,:,indmin:indmax] = 1
#            hypimage=np.sum(hypSpectrum*mask,axis=2)
#            hypimage -= hypimage.min()
#            lumimage.set_array(hypimage)
            wavelenght = ma.masked_outside(wavelenght,eV_To_nm/ymin,eV_To_nm/ymax)
            hypmask = np.resize(wavelenght.mask,hypSpectrum.shape)
            hypSpectrum = ma.masked_array(hypSpectrum,mask=hypmask)
            hypimage=np.sum(hypSpectrum,axis=2)
            hypimage -= hypimage.min()
            lumimage.set_array(hypimage)
            cx.set_ylim(eV_To_nm/wavelenght.max(), eV_To_nm/wavelenght.min())
            fig.canvas.draw_idle()
        span = None
        if Linescan:
            span = SpanSelector(cx, lambda x: onselect(x, wavelenght,hypSpectrum), 'vertical', useblit=True,
                                rectprops=dict(alpha=0.5, facecolor='red'))
        fig.canvas.mpl_connect('button_press_event', onclick)
        return hypSpectrum, wavelenght, spec_fig, spec_ax, multi, span