def load_default(self): self.params = np.array([ 51.9957, 81, 3.7998e-7, -0.204599, 0, 328.93, 88.8624, 11.7176, 1182.19, 2026.27, 7.14503, 0, 0, 89.1588, 87.5647, 0.278594 ]) energy = self.params[0] etalimit = self.params[1] / 180.0 * np.pi pos = self.params[2:5] orien = R.EulerZXZ2Mat(self.params[5:8] / 180.0 * np.pi) # strain=np.array([ 0.00228725, -0.00046991, -0.00013953, -0.0001595 , 0.00143135, # 0.00070123, 0.00089509, 0.00438468, 0.0014488 ])+np.array([ -1.66347153e-03, 9.19749189e-04, 8.19250063e-05, # -1.33069566e-04, -1.18838324e-03, -2.40553445e-04, # 1.67946465e-03, -8.97547675e-03, -8.87805809e-04]) strain = np.zeros(9) strain = strain.reshape((3, 3)) + np.eye(3) orien = strain.dot(orien) Det = G.Detector() Det.Move(self.params[8], self.params[9], self.params[10:13], R.EulerZXZ2Mat(self.params[13:16] / 180.0 * np.pi)) self._xrd.Setup(energy, etalimit, pos, orien, Det) self._xrd.Simulate() self.choosePeakID.setMinimum(0) self.choosePeakID.setMaximum(len(self._xrd.Peaks) - 1) try: self.res = pickle.load(open('tmp_res.pickle', 'r')) except: self.res = [None] * len(self._xrd.Peaks) self.l0.setText('Setup finished')
def plot_mic(snp, sw, plotType, minConfidence, maxConfidence, scattersize=2): ''' plot the mic file :param snp: :param sw: :param plotType: :param minConfidence: :return: ''' snp = snp[minConfidence <= snp[:, 9] <= maxConfidence, :] N = snp.shape[0] mat = np.empty([N, 3, 3]) quat = np.empty([N, 4]) rod = np.empty([N, 3]) if plotType == 2: fig, ax = plt.subplots() sc = ax.scatter(snp[:, 0], snp[:, 1], c=snp[:, 9], cmap='cool') plt.colorbar(sc) plt.show() if plotType == 3: print('h') for i in range(N): mat[i, :, :] = RotRep.EulerZXZ2Mat(snp[i, 6:9] / 180 * np.pi) #print mat[i,:,:] quat[i, :] = RotRep.quaternion_from_matrix(mat[i, :, :]) #print quat[i,:] rod[i, :] = RotRep.rod_from_quaternion(quat[i, :]) #print(rod) fig, ax = plt.subplots() ax.scatter(snp[:, 0], snp[:, 1], s=scattersize, facecolors=(rod + np.array([1, 1, 1])) / 2) ax.axis('scaled') plt.show()
def load_default(self): self.params = np.array([ 71.676, 81, 0, 0.0974279, 0, 89.5003, 80.7666, 266.397, 949.46, 1996.15, 6.67079, 0, 0, 89.4759, 90.2069, 359.273 ]) energy = self.params[0] etalimit = self.params[1] / 180.0 * np.pi pos = self.params[2:5] orien = R.EulerZXZ2Mat(self.params[5:8] / 180.0 * np.pi) # strain=np.array([ 0.00228725, -0.00046991, -0.00013953, -0.0001595 , 0.00143135, # 0.00070123, 0.00089509, 0.00438468, 0.0014488 ])+np.array([ -1.66347153e-03, 9.19749189e-04, 8.19250063e-05, # -1.33069566e-04, -1.18838324e-03, -2.40553445e-04, # 1.67946465e-03, -8.97547675e-03, -8.87805809e-04]) strain = np.zeros(9) strain = strain.reshape((3, 3)) + np.eye(3) orien = strain.dot(orien) Det = G.Detector() Det.Move(self.params[8], self.params[9], self.params[10:13], R.EulerZXZ2Mat(self.params[13:16] / 180.0 * np.pi)) self._xrd.Setup(energy, etalimit, pos, orien, Det) self._xrd.Simulate() self.choosePeakID.setMinimum(0) self.choosePeakID.setMaximum(len(self._xrd.Peaks) - 1) try: self.res = pickle.load(open('tmp_res.pickle', 'r')) except: self.res = [None] * len(self._xrd.Peaks) self.l0.setText('Setup finished')
def set_color_range(mic, N, indx, mat, quat, rod): """ Function for setting the color range of a plot. """ first = True #print(indx) for i in range(N): if i in indx: mat[i, :, :] = RotRep.EulerZXZ2Mat(mic.snp[i, 6:9] / 180.0 * np.pi) quat[i, :] = RotRep.quaternion_from_matrix(mat[i, :, :]) rod[i, :] = RotRep.rod_from_quaternion(quat[i, :]) if first: maxr = rod[i, 0] minr = rod[i, 0] maxg = rod[i, 1] ming = rod[i, 1] maxb = rod[i, 2] minb = rod[i, 2] maxri = i minri = i maxgi = i mingi = i maxbi = i minbi = i first = False else: if rod[i, 0] > maxr: maxr = rod[i, 0] maxri = i elif rod[i, 0] < minr: minr = rod[i, 0] minri = i if rod[i, 1] > maxg: maxg = rod[i, 1] maxgi = i elif rod[i, 1] < ming: ming = rod[i, 1] mingi = i if rod[i, 2] > maxb: maxb = rod[i, 2] maxbi = i elif rod[i, 2] < minb: minb = rod[i, 2] minbi = i else: rod[i, :] = [0.0, 0.0, 0.0] #print("Current rod values: ",rod) maxrgb = [maxr, maxg, maxb] minrgb = [minr, ming, minb] maxangs = [rod[maxri, 0], rod[maxgi, 1], rod[maxbi, 2]] minangs = [rod[minri, 0], rod[mingi, 1], rod[minbi, 2]] colors = rod for j in range(N): for k in range(0, 3): colors[j, k] = (rod[j, k] - minrgb[k]) / (maxrgb[k] - minrgb[k]) return colors, maxangs, minangs
def set_color_range_sq(smdCopy, x, y, indx, mat, quat, rod, anglelim): #print("indx: ",indx) first = True for i in range(mat.shape[0]): yi = int(i % x) xi = int(i / x) #print(xi,yi) if (xi, yi) in indx or not anglelim: quat[i, :] = RotRep.quaternion_from_matrix(mat[i, :, :]) rod[i, :] = RotRep.rod_from_quaternion(quat[i, :]) if first: maxr = rod[i, 0] minr = rod[i, 0] maxg = rod[i, 1] ming = rod[i, 1] maxb = rod[i, 2] minb = rod[i, 2] maxri = i minri = i maxgi = i mingi = i maxbi = i minbi = i first = False else: if rod[i, 0] > maxr: maxr = rod[i, 0] maxri = i elif rod[i, 0] < minr: minr = rod[i, 0] minri = i if rod[i, 1] > maxg: maxg = rod[i, 1] maxgi = i elif rod[i, 1] < ming: ming = rod[i, 1] mingi = i if rod[i, 2] > maxb: maxb = rod[i, 2] maxbi = i elif rod[i, 2] < minb: minb = rod[i, 2] minbi = i else: rod[i, :] = [0.0, 0.0, 0.0] maxrgb = [maxr, maxg, maxb] minrgb = [minr, ming, minb] maxangs = [rod[maxri, 0], rod[maxgi, 1], rod[maxbi, 2]] minangs = [rod[minri, 0], rod[mingi, 1], rod[minbi, 2]] colors = rod for j in range(mat.shape[0]): for k in range(0, 3): colors[j, k] = (rod[j, k] - minrgb[k]) / (maxrgb[k] - minrgb[k]) return colors, maxangs, minangs
def plot_mic_patches(self, plotType, minConfidence): indx = self.snp[:, 9] >= minConfidence minsw = self.sw / float(2**self.snp[0, 4]) tsw1 = minsw * 0.5 tsw2 = -minsw * 0.5 * 3**0.5 ntri = len(self.snp) if plotType == 2: fig, ax = plt.subplots() if self.bpatches == False: xy = self.snp[:, :2] tmp = np.asarray([[tsw1] * ntri, (-1)**self.snp[:, 3] * tsw2]).transpose() tris = np.asarray([[[0, 0]] * ntri, [[minsw, 0]] * ntri, tmp]) self.patches = np.swapaxes(tris + xy, 0, 1) self.bpatches = True p = PolyCollection(self.patches[indx], cmap='viridis') p.set_array(self.color2[indx]) p.set_edgecolor('face') ax.add_collection(p) ax.set_xlim([-0.6, 0.6]) ax.set_ylim([-0.6, 0.6]) fig.colorbar(p, ax=ax) plt.show() if plotType == 1: fig, ax = plt.subplots() N = len(self.snp) mat = np.empty([N, 3, 3]) quat = np.empty([N, 4]) rod = np.empty([N, 3]) if self.bcolor1 == False: for i in range(N): mat[i, :, :] = RotRep.EulerZXZ2Mat(self.snp[i, 6:9] / 180.0 * np.pi) quat[i, :] = RotRep.quaternion_from_matrix(mat[i, :, :]) rod[i, :] = RotRep.rod_from_quaternion(quat[i, :]) self.color1 = (rod + np.array([1, 1, 1])) / 2 self.bcolor1 = True if self.bpatches == False: xy = self.snp[:, :2] tmp = np.asarray([[tsw1] * ntri, (-1)**self.snp[:, 3] * tsw2]).transpose() tris = np.asarray([[[0, 0]] * ntri, [[minsw, 0]] * ntri, tmp]) self.patches = np.swapaxes(tris + xy, 0, 1) self.bpatches = True p = PolyCollection(self.patches[indx], cmap='viridis') p.set_color(self.color1[indx]) ax.add_collection(p) ax.set_xlim([-0.6, 0.6]) ax.set_ylim([-0.6, 0.6]) plt.show()
def check_points(self, data, xlim, ylim, misor_thresh): upx, upy = self.up rightx, righty = self.right downx, downy = self.down leftx, lefty = self.left if upx < 0 or upx > xlim or upy < 0 or upy > ylim: self.block_up() if rightx < 0 or rightx > xlim or righty < 0 or righty > ylim: self.block_right() if downx < 0 or downx > xlim or downy < 0 or downy > ylim: self.block_down() if leftx < 0 or leftx > xlim or lefty < 0 or lefty > ylim: self.block_left() angles = np.array([self.angles]) if not self.up_blocked: upangles = np.array( [[data[upx, upy, 3], data[upx, upy, 4], data[upx, upy, 5]]]) up_misor = RotRep.MisorinEulerZXZ(angles, upangles) if up_misor > misor_thresh: self.block_up() if not self.right_blocked: rightangles = np.array([[ data[rightx, righty, 3], data[rightx, righty, 4], data[rightx, righty, 5] ]]) right_misor = RotRep.MisorinEulerZXZ(angles, rightangles) if right_misor > misor_thresh: self.block_right() if not self.down_blocked: downangles = np.array([[ data[downx, downy, 3], data[downx, downy, 4], data[downx, downy, 5] ]]) down_misor = RotRep.MisorinEulerZXZ(angles, downangles) if down_misor > misor_thresh: self.block_down() if not self.left_blocked: leftangles = np.array([[ data[leftx, lefty, 3], data[leftx, lefty, 4], data[leftx, lefty, 5] ]]) left_misor = RotRep.MisorinEulerZXZ(angles, leftangles) if left_misor > misor_thresh: self.block_left()
def on_click(self, event): if event.inaxes == self.axes0: x = int(round(event.xdata)) y = int(round(event.ydata)) tmpid = self.mainwindow.ID0[self.idx0][y, x, 0] tmpci = self.mainwindow.ci0[self.idx0][y, x, 0] self.l0.setText("S0_ID= {:}, CI= {:}".format(tmpid, tmpci)) # tmpe=self.mainwindow.e0[tmpid-1] tmpe = self.mainwindow.ce0[self.idx0][y, x] # print tmpe self.l3.setText("disorientation is: {:} deg".format( R.Misorien2FZ1(R.EulerZXZ2Mat(self.lastgraineuler), R.EulerZXZ2Mat(tmpe), 'Cubic')[1] / np.pi * 180)) self.lastgraineuler = tmpe if self.bgrainlabel.isChecked(): self.s0labelID.append(tmpid) self.labelpoints.append(self.axes0.scatter([x], [y], c='r')) self.canvas.draw() if event.inaxes == self.axes1: x = int(round(event.xdata)) y = int(round(event.ydata)) tmpid = self.mainwindow.ID1[self.idx1][y, x, 0] tmpci = self.mainwindow.ci1[self.idx1][y, x, 0] # corid=self.mainwindow.backcorID[tmpid] self.l1.setText("S1_ID= {:}, CI= {:} ".format(tmpid, tmpci)) # self.l1.setText("S1_ID= {:}, CI= {:} (-> S0_ID= {:})".format(tmpid,tmpci,corid)) # tmpe=self.mainwindow.e1[tmpid-1] tmpe = self.mainwindow.ce1[self.idx1][y, x] # print tmpe self.l3.setText("disorientation is: {:} deg".format( R.Misorien2FZ1(R.EulerZXZ2Mat(self.lastgraineuler), R.EulerZXZ2Mat(tmpe), 'Cubic')[1] / np.pi * 180)) self.lastgraineuler = tmpe if self.bgrainlabel.isChecked(): self.s1labelID.append(tmpid) self.labelpoints.append(self.axes1.scatter([x], [y], c='r')) self.canvas.draw()
def plot_square_mic(SquareMic, squareMicData, minHitRatio, coords, misor_thresh): ''' plot the square mic data image already inverted, x-horizontal, y-vertical, x dow to up, y: left to right :param squareMicData: [NVoxelX,NVoxelY,10], each Voxel conatains 10 columns: 0-2: voxelpos [x,y,z] 3-5: euler angle 6: hitratio 7: maskvalue. 0: no need for recon, 1: active recon region 8: voxelsize 9: additional information :return: ''' assert (len(coords) == 0 or len(coords) == 2), "Please enter the correct number of coordinates" anglelim = not (coords == []) angles = [] fig, ax = plt.subplots() indx = [] points = [] smdCopy = squareMicData.copy() (x, y, z) = squareMicData.shape if anglelim: angles.append(squareMicData[coords[0], coords[1], 3]) angles.append(squareMicData[coords[0], coords[1], 4]) angles.append(squareMicData[coords[0], coords[1], 5]) points = square_angle_limiter(x, y, smdCopy, coords, misor_thresh) indx = index_from_points(points) for i in range(0, x): for j in range(0, y): if not (i, j) in indx: smdCopy[i, j, 6] = 0.0 smdCopy[i, j, 3] = angles[0] smdCopy[i, j, 4] = angles[1] smdCopy[i, j, 5] = angles[2] mat = RotRep.EulerZXZ2MatVectorized(smdCopy[:, :, 3:6].reshape([-1, 3]) / 180.0 * np.pi) quat = np.empty([mat.shape[0], 4]) rod = np.empty([mat.shape[0], 3]) colors, maxangs, minangs = set_color_range_sq(smdCopy, x, y, indx, mat, quat, rod, anglelim) hitRatioMask = (smdCopy[:, :, 6] > minHitRatio)[:, :, np.newaxis].repeat(3, axis=2) #img = ((colors + np.array([1, 1, 1])) / 2).reshape([squareMicData.shape[0],squareMicData.shape[1],3]) * hitRatioMask img = (colors).reshape([squareMicData.shape[0], squareMicData.shape[1], 3 ]) * hitRatioMask # make sure display correctly #img[:,:,:] = img[::-1,:,:] img = np.swapaxes(img, 0, 1) minX, minY = smdCopy[0, 0, 0:2] * 1000 maxX, maxY = smdCopy[-1, -1, 0:2] * 1000 #print(minX,maxX, minY,maxY) if anglelim: (xi_first, yi_first) = indx[0] xmin = smdCopy[xi_first, yi_first, 0] xmax = smdCopy[xi_first, yi_first, 0] ymin = smdCopy[xi_first, yi_first, 1] ymax = smdCopy[xi_first, yi_first, 1] for (xi, yi) in indx: if smdCopy[xi, yi, 0] <= xmin: xmin = smdCopy[xi, yi, 0] if smdCopy[xi, yi, 0] >= xmax: xmax = smdCopy[xi, yi, 0] if smdCopy[xi, yi, 1] <= ymin: ymin = smdCopy[xi, yi, 1] if smdCopy[xi, yi, 1] >= ymax: ymax = smdCopy[xi, yi, 1] if abs(xmax - xmin) > abs(ymax - ymin): side_length = abs(xmax - xmin) else: side_length = abs(ymax - ymin) side_length = side_length / smdCopy[0, 0, 8] * 10 xmin = xmin / smdCopy[0, 0, 8] * 10 ymin = ymin / smdCopy[0, 0, 8] * 10 ax.set_xlim([xmin - 10, xmin + side_length + 10]) ax.set_ylim([ymin - 10, ymin + side_length + 10]) print("-------------------------------------------------") print("MaxRod (R,G,B): ", maxangs) print("MinRod (R,G,B): ", minangs) ax.imshow(img, origin='lower', extent=[minX, maxX, minY, maxY]) plt.title('orientation in um') voxels = SquareVoxelClick(fig, squareMicData, SquareMic, minHitRatio, misor_thresh) voxels.connect() plt.show()
def plot_mic_patches(self, plotType, minConfidence, maxConfidence, indices): indx = [] for i in range(0, len(self.snp)): if self.snp[i, 9] >= minConfidence and self.snp[ i, 9] <= maxConfidence and i in indices: indx.append(i) #indx=minConfidence<=self.snp[:,9]<=maxConfidence minsw = self.sw / float(2**self.snp[0, 4]) tsw1 = minsw * 0.5 tsw2 = -minsw * 0.5 * 3**0.5 ntri = len(self.snp) if plotType == 2: fig, ax = plt.subplots() if self.bpatches == False: xy = self.snp[:, :2] tmp = np.asarray([[tsw1] * ntri, (-1)**self.snp[:, 3] * tsw2]).transpose() tris = np.asarray([[[0, 0]] * ntri, [[minsw, 0]] * ntri, tmp]) self.patches = np.swapaxes(tris + xy, 0, 1) self.bpatches = True p = PolyCollection(self.patches[indx], cmap='viridis') p.set_array(self.color2[indx]) p.set_edgecolor('face') ax.add_collection(p) ax.set_xlim([-0.6, 0.6]) ax.set_ylim([-0.6, 0.6]) fig.colorbar(p, ax=ax) plt.show() if plotType == 1: fig, ax = plt.subplots() N = len(self.snp) mat = np.empty([N, 3, 3]) quat = np.empty([N, 4]) rod = np.empty([N, 3]) if self.bcolor1 == False: maxr = 0.0 minr = 0.0 maxg = 0.0 ming = 0.0 maxb = 0.0 minb = 0.0 for i in range(N): if i in indx: mat[i, :, :] = RotRep.EulerZXZ2Mat(self.snp[i, 6:9] / 180.0 * np.pi) quat[i, :] = RotRep.quaternion_from_matrix( mat[i, :, :]) rod[i, :] = RotRep.rod_from_quaternion(quat[i, :]) if i == indx[0]: maxr = rod[i, 0] minr = rod[i, 0] maxg = rod[i, 1] ming = rod[i, 1] maxb = rod[i, 2] minb = rod[i, 2] else: if rod[i, 0] > maxr: maxr = rod[i, 0] elif rod[i, 0] < minr: minr = rod[i, 0] if rod[i, 1] > maxg: maxg = rod[i, 1] elif rod[i, 1] < ming: ming = rod[i, 1] if rod[i, 2] > maxb: maxb = rod[i, 2] elif rod[i, 2] < minb: minb = rod[i, 2] else: rod[i, :] = [0.0, 0.0, 0.0] print "Current rod values: ", rod maxrgb = [maxr, maxg, maxb] minrgb = [minr, ming, minb] colors = rod for j in range(N): for k in range(0, 3): colors[j, k] = (rod[j, k] - minrgb[k]) / (maxrgb[k] - minrgb[k]) self.color1 = colors print "Color: ", self.color1 #self.bcolor1=True if self.bpatches == False: xy = self.snp[:, :2] tmp = np.asarray([[tsw1] * ntri, (-1)**self.snp[:, 3] * tsw2]).transpose() tris = np.asarray([[[0, 0]] * ntri, [[minsw, 0]] * ntri, tmp]) self.patches = np.swapaxes(tris + xy, 0, 1) self.bpatches = True p = PolyCollection(self.patches[indx], cmap='viridis') p.set_color(self.color1[indx]) ax.add_collection(p) ax.set_xlim([-0.6, 0.6]) ax.set_ylim([-0.6, 0.6]) plt.show()