예제 #1
0
    def plot_Qpeaks(self):
        proj3d.persp_transformation = self.orthogonal_proj
        try:
            # Find size of screen
            import curses
            stdscr = curses.initscr()
            self.screen_x, self.screen_y = stdscr.getmaxyx()
            self.screen_x = min(self.screen_x, self.screen_y)
            self.screen_y = self.screen_x
        except:
            self.screen_x = 40
            self.screen_y = 40
        plt.rcParams.update({'font.size': 10})
        if len(self.mod) == 0:
            IndexPeaks(PeaksWorkspace=self.peaks_ws,
                       Tolerance=self.tolerance,
                       RoundHKLs=False,
                       CommonUBForAll=True)
        elif len(self.mod) > 0:
            IndexPeaksWithSatellites(PeaksWorkspace=self.peaks_ws,
                                     RoundHKLs=False,
                                     ModVector1=self.mod,
                                     MaxOrder=1)
        npeaksTotal = self.peaks_ws.getNumberPeaks()
        self.c = []
        self.x = []
        self.y = []
        self.z = []
        self.s = []
        for i in range(npeaksTotal):
            peak = self.peaks_ws.getPeak(i)
            hklp = V3D(peak.getH() - math.floor(peak.getH()),
                       peak.getK() - math.floor(peak.getK()),
                       peak.getL() - math.floor(peak.getL()))
            if peak.getSigmaIntensity() != 0.0:
                IsigI = peak.getIntensity() / peak.getSigmaIntensity()
            else:
                IsigI = 0.0
            #if IsigI < 50:
            #continue
            intensity = max(self.minIntensity, peak.getIntensity())
            for i in range(-1, 1):
                for j in range(-1, 1):
                    for k in range(-1, 1):
                        if (abs(hklp[0] + i) < 1.0 and abs(hklp[1] + j) < 1.0
                                and abs(hklp[2] + k) < 1.0):
                            self.x.append(hklp[0] + i)
                            self.y.append(hklp[1] + j)
                            self.z.append(hklp[2] + k)
                            self.c.append(intensity)
                            self.s.append(5)
        nPts = 100
        x_grid = np.linspace(-1, 1, nPts + 1)
        x_half = np.zeros(nPts)
        for j in range(nPts):
            x_half[j] = 0.5 * (x_grid[j] + x_grid[j + 1])
        sumIntensityX = np.zeros(nPts)
        for i in range(len(self.x)):
            for j in range(nPts):
                if self.x[i] > x_grid[j] and self.x[i] < x_grid[j + 1]:
                    sumIntensityX[j] = sumIntensityX[j] + self.c[i]
        sumIntensityY = np.zeros(nPts)
        for i in range(len(self.y)):
            for j in range(nPts):
                if self.y[i] > x_grid[j] and self.y[i] < x_grid[j + 1]:
                    sumIntensityY[j] = sumIntensityY[j] + self.c[i]
        sumIntensityZ = np.zeros(nPts)
        for i in range(len(self.z)):
            for j in range(nPts):
                if self.z[i] > x_grid[j] and self.z[i] < x_grid[j + 1]:
                    sumIntensityZ[j] = sumIntensityZ[j] + self.c[i]
        # Plot figure with subplots of different sizes
        fig = plt.figure("Modulated Structures",
                         figsize=(self.screen_x, self.screen_y))
        # set up subplot grid
        gridspec.GridSpec(3, 3)
        ax = plt.subplot2grid((3, 3), (0, 2))
        plt.plot(x_half, sumIntensityX)
        maxGrid = "max\n"
        for i in range(2, nPts - 2):
            if abs(x_half[i]) > 0.55 or abs(x_half[i]) < 0.05:
                continue
            if sumIntensityX[i] > sumIntensityX[
                    i - 1] and sumIntensityX[i] > sumIntensityX[
                        i + 1] and sumIntensityX[i - 1] > sumIntensityX[
                            i - 2] and sumIntensityX[i + 1] > sumIntensityX[i +
                                                                            2]:
                maxGrid += "{:f}\n".format(x_half[i])
        ax.text(0.05, 0.60, maxGrid, fontsize=10, transform=ax.transAxes)
        ax.set_yscale('log')
        ax.set_xlabel('H')
        ax.set_ylabel(r'$\Sigma$I')
        ax = plt.subplot2grid((3, 3), (1, 2))
        plt.plot(x_half, sumIntensityY)
        maxGrid = "max\n"
        for i in range(2, nPts - 2):
            if abs(x_half[i]) > 0.5 or abs(x_half[i]) < 0.05:
                continue
            if sumIntensityY[i] > sumIntensityY[
                    i - 1] and sumIntensityY[i] > sumIntensityY[
                        i + 1] and sumIntensityY[i - 1] > sumIntensityY[
                            i - 2] and sumIntensityY[i + 1] > sumIntensityY[i +
                                                                            2]:
                maxGrid += "{:f}\n".format(x_half[i])
        ax.text(0.05, 0.60, maxGrid, fontsize=10, transform=ax.transAxes)
        ax.set_yscale('log')
        ax.set_xlabel('K')
        ax.set_ylabel(r'$\Sigma$I')
        ax = plt.subplot2grid((3, 3), (2, 2))
        plt.plot(x_half, sumIntensityZ)
        maxGrid = "max\n"
        for i in range(2, nPts - 2):
            if abs(x_half[i]) > 0.5 or abs(x_half[i]) < 0.05:
                continue
            if sumIntensityZ[i] > sumIntensityZ[
                    i - 1] and sumIntensityZ[i] > sumIntensityZ[
                        i + 1] and sumIntensityZ[i - 1] > sumIntensityZ[
                            i - 2] and sumIntensityZ[i + 1] > sumIntensityZ[i +
                                                                            2]:
                maxGrid += "{:f}\n".format(x_half[i])
        ax.text(0.05, 0.60, maxGrid, fontsize=10, transform=ax.transAxes)
        ax.set_yscale('log')
        ax.set_xlabel('L')
        ax.set_ylabel(r'$\Sigma$I')

        vmin = min(self.c)
        vmax = max(self.c)
        logNorm = colors.LogNorm(vmin=vmin, vmax=vmax)
        self.axP = plt.subplot2grid((3, 3), (0, 0),
                                    colspan=2,
                                    rowspan=3,
                                    projection='3d')
        sp = self.axP.scatter(self.x,
                              self.y,
                              self.z,
                              c=self.c,
                              vmin=vmin,
                              vmax=vmax,
                              norm=logNorm,
                              s=self.s,
                              cmap='rainbow',
                              picker=True,
                              alpha=0.2)

        self.props = dict(boxstyle='round', facecolor='wheat', alpha=1.0)

        #cid = fig.canvas.mpl_connect('pick_event', self.onpick3)
        pid = fig.canvas.mpl_connect('key_press_event', self.onpress)

        try:
            lattice = self.peaks_ws.sample().getOrientedLattice()
        except:
            lattice = OrientedLattice(1, 1, 1)
        astar = V3D(0.5, 0, 0)
        bstar = V3D(0, 0.5, 0)
        cstar = V3D(0, 0, 0.5)
        hkls = np.array(
            [np.array([x, y, z]) for x, y, z in zip(self.x, self.y, self.z)])
        labels, centroids = self.dbscan(hkls, eps=.0425, min_points=6)

        i = 0
        modTest = str(self.peaks_ws.getPeak(0).getRunNumber())
        lastRun = str(self.peaks_ws.getPeak(npeaksTotal - 1).getRunNumber())
        if lastRun != modTest:
            modTest = modTest + "-" + lastRun
        modTest = modTest + " Type a, b, or c for axis alignment"
        for x in centroids:
            self.axP.plot([x[0]], [x[1]], [x[2]],
                          'wo',
                          ms=40,
                          markerfacecolor="None",
                          markeredgecolor='red',
                          markeredgewidth=3)
            if x[0] * x[0] + x[1] * x[1] + x[2] * x[2] > 0.01 and x[
                    0] >= -0.5 and x[1] >= -0.5 and x[2] >= -0.5 and x[
                        0] <= 0.5 and x[1] <= 0.5 and x[2] <= 0.5:
                modTest += "\nModulation Vector = " + "{:.3f}".format(
                    x[0]) + ", " + "{:.3f}".format(
                        x[1]) + ", " + "{:.3f}".format(x[2])
                if len(self.mod) == 0:
                    self.mod = "{:.3f}".format(x[0]) + "," + "{:.3f}".format(
                        x[1]) + "," + "{:.3f}".format(x[2])
                    IndexPeaksWithSatellites(PeaksWorkspace=self.peaks_ws,
                                             RoundHKLs=False,
                                             ModVector1=self.mod,
                                             MaxOrder=1)

        self.axP.plot([0, astar[0]], [0, astar[1]], [0, astar[2]], color='r')
        self.axP.text(astar[0],
                      astar[1],
                      astar[2],
                      '%s' % ('a*'),
                      size=20,
                      zorder=1,
                      color='r')
        self.axP.plot([0, bstar[0]], [0, bstar[1]], [0, bstar[2]], color='b')
        self.axP.text(bstar[0],
                      bstar[1],
                      bstar[2],
                      '%s' % ('b*'),
                      size=20,
                      zorder=1,
                      color='b')
        self.axP.plot([0, cstar[0]], [0, cstar[1]], [0, cstar[2]], color='g')
        self.axP.text(cstar[0],
                      cstar[1],
                      cstar[2],
                      '%s' % ('c*'),
                      size=20,
                      zorder=1,
                      color='g')

        self.axP.text2D(0.05,
                        0.80,
                        modTest + "\nLattice = " + " " +
                        "{:.3f}".format(lattice.a()) + " " +
                        "{:.3f}".format(lattice.b()) + " " +
                        "{:.3f}".format(lattice.c()) + " " +
                        "{:.3f}".format(lattice.alpha()) + " " +
                        "{:.3f}".format(lattice.beta()) + " " +
                        "{:.3f}".format(lattice.gamma()) + "\nError = " + " " +
                        "{:.3E}".format(lattice.errora()) + " " +
                        "{:.3E}".format(lattice.errorb()) + " " +
                        "{:.3E}".format(lattice.errorc()) + " " +
                        "{:.3E}".format(lattice.erroralpha()) + " " +
                        "{:.3E}".format(lattice.errorbeta()) + " " +
                        "{:.3E}".format(lattice.errorgamma()),
                        fontsize=10,
                        transform=self.axP.transAxes)
        self.axP.set_xlabel('H')
        self.axP.set_ylabel('K')
        self.axP.set_zlabel('L')

        # Create cubic bounding box to simulate equal aspect ratio
        max_range = max([
            max(self.x) - min(self.x),
            max(self.y) - min(self.y),
            max(self.z) - min(self.z)
        ])
        Xb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][0].flatten(
        ) + 0.5 * (max(self.x) + min(self.x))
        Yb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][1].flatten(
        ) + 0.5 * (max(self.y) + min(self.y))
        Zb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][2].flatten(
        ) + 0.5 * (max(self.z) + min(self.z))
        # Comment or uncomment following both lines to test the fake bounding box:
        for xb, yb, zb in zip(Xb, Yb, Zb):
            self.axP.plot([xb], [yb], [zb], 'w')
        plt.show()
        #fig.canvas.mpl_disconnect(cid)
        fig.canvas.mpl_disconnect(pid)