Example #1
0
    def get_color_from_field(self, grain):
        """Get the color of the given grain according to the chosen field.

        This function will return the color associated with the given grain.
        Depending on how the pole figure has been configured (see the
        `set_map_field` function), it will be obtained from:

         * the grain id, according to the `Microstructure.rand_cmap` function
         * ipf the colour will reflect the orientation according to the IPF
            coloring scheme
         * the field value mapped on a pyplot color map if the lut field of
            the PoleFigure instance is a string.
         * a color directly read from the lut field; in this case the field
            value must reflect the category of the given grain.

        :param grain: the `Grain` instance.
        :return: the color as a 3 element numpy array representing the rgb values.
        
        """
        if self.map_field:
            if self.map_field == 'grain_id':
                col = Microstructure.rand_cmap().colors[grain['idnumber']]
            elif self.map_field == 'ipf':
                if self.axis == 'X':
                    axis = np.array([1., 0., 0.])
                elif self.axis == 'Y':
                    axis = np.array([0., 1., 0.])
                else:
                    axis = np.array([0., 0., 1.])
                col = Orientation.from_rodrigues(
                    grain['orientation']).get_ipf_colour(axis=axis)
            else:
                # retrieve the position of the grain in the list
                rank = self.microstructure.get_grain_ids().tolist().index(
                    grain['idnumber'])
                if type(self.lut) is str:
                    # get the color map from pyplot
                    color_map = cm.get_cmap(self.lut, 256)
                    # use the field value for this grain and the field range bounds
                    color = int(255 * max(
                        min((self.field[rank] - self.field_min_level) /
                            float(self.field_max_level - self.field_min_level),
                            1.0), 0.0))
                    col = color_map(np.arange(256))[color]
                else:
                    col = self.lut[
                        self.field[rank]]  # directly access the color
            return col
        else:
            return np.array([0., 0., 0.])
if __name__ == '__main__':
    '''
    This example first demonstrate how to plot a slice of a 3d image. Here
    we use a custom random colormap to display grains nicely.
    The example also shows how to modify the coordinate formatter to
    display the pixel value when moving the mouse above the plotted image.
    Run this example interactively to try it.
    '''
    display = False
    data_dir = '../data'
    scan_name = 'pure_Ti_216x216x141_uint16.raw'
    scan_path = os.path.join(data_dir, scan_name)
    # read only the first slice of the volume and make it a 2d array
    data = HST_read(scan_path, autoparse_filename=True, zrange=range(0, 1), verbose=True)[:, :, 0]

    rand_cmap = Microstructure.rand_cmap(N=2048, first_is_black=True)
    fig, ax = plt.subplots()
    ax = AxShowPixelValue(ax)
    ax.imshow(data.T, cmap=rand_cmap, interpolation='nearest', origin='upper')

    image_name = os.path.splitext(__file__)[0] + '.png'
    plt.savefig(image_name)
    print 'writting %s' % image_name

    from matplotlib import image

    image.thumbnail(image_name, 'thumb_' + image_name, 0.2)

    # display the plot in interactive mode
    if display: plt.show()
Example #3
0
g = Grain(50, Orientation.from_euler((12.293, 149.266, -167.068)))
micro.add_grains([(12.293, 149.266, -167.068)], grain_ids=[50])

ipf = PoleFigure(proj='stereo', microstructure=micro)
ipf.mksize = 100
ipf.set_map_field('grain_id')

fig = plt.figure(1, figsize=(6, 5))  # for IPF
ax1 = fig.add_subplot(111, aspect='equal')
print('** plotting the initial orientation (with label for legend) **')
ipf.plot_sst(ax=ax1, mk='.', col='k', ann=False)
ax1.set_title('grain rotation in tension')
axis = np.array([0, 0, 1])

grain = micro.get_grain(50)
cgid = Microstructure.rand_cmap().colors[grain.id]  # color by grain id
g = grain.orientation_matrix()
axis_rot_sst_prev = np.array(ipf.sst_symmetry_cubic(g.dot(axis)))
print('** plotting ipf loading axis trajectory **')
for k in range(0, max_step, step):
    rot = np.array([[R11[k], R12[k], R13[k]], [R21[k], R22[k], R23[k]],
                    [R31[k], R32[k], R33[k]]])
    # apply R^t to the initial orientation given by g
    new_g = rot.transpose().dot(g)
    axis_rot_sst = ipf.sst_symmetry_cubic(new_g.dot(axis))
    ipf.plot_line_between_crystal_dir(axis_rot_sst_prev,
                                      axis_rot_sst,
                                      ax=ax1,
                                      col=cgid,
                                      steps=2)
    axis_rot_sst_prev = axis_rot_sst