Example #1
0
def plot_cube(cube, name='voxel', angle=20, IMG_DIM=80, num_class=12):
    from mpl_toolkits.mplot3d import Axes3D

    # cube = normalize(cube)
    # Note that cm.Paired has 12 colors and Set2 has 8 colors
    # cube[np.where(cube > num_class)] = 10
    if num_class == 12:
        cube[cube < 0] = 0
        cube[cube == 255] = 0
        facecolors = cm.Paired((np.round(cube) / 13))
        facecolors[:, :, :, -1] = 0.2 * np.tanh(
            cube * 1000) + 0.7 * (cube > 5) + 0.1 * (cube == 2)
    elif num_class == 4:
        facecolors = cm.Dark2((np.round(cube) / 9))
        facecolors[:, :, :, -1] = 0.4 * np.tanh(cube * 1000)
    elif num_class == 3:
        # cube[cube == -1] = 3
        cube[cube < 0] = 0
        facecolors = cm.Set2((np.round(cube) / 9))
        facecolors[:, :, :,
                   -1] = 0.03 * np.tanh(cube * 1000) + 0.6 * (cube == 1)
    elif num_class == 1:
        cube[cube < 0] = 0
        facecolors = cm.Dark2((np.round(cube) / 1))
        facecolors[:, :, :, -1] = 0.5 * (cube == 1)

    # make the alpha channel more similar to each others while 0 is still 0
    facecolors = explode(facecolors)
    filled = facecolors[:, :, :, -1] != 0

    x, y, z = expand_coordinates(
        np.indices(np.array(filled.shape) + 1).astype(float))

    # Here is a loop for generating demo files
    for idx, val in enumerate(np.arange(160, 170, 10)):
        fig = plt.figure(figsize=(45 / 2.54, 45 / 2.54))  # , dpi=150)
        # plot
        ax1 = fig.add_subplot(111, projection='3d')
        # For samples in SUNCG, 20, -40 is a good choice for visualization
        # ax1.view_init(np.abs(90-val/2), val)
        ax1.view_init(angle, val)
        ax1.set_xlim(right=IMG_DIM * 2)
        ax1.set_ylim(top=IMG_DIM * 2)
        ax1.set_zlim(top=IMG_DIM * 2)
        ax1.set_axis_off()
        ax1.voxels(x,
                   y,
                   z,
                   filled,
                   facecolors=facecolors,
                   edgecolors=np.clip(2 * facecolors - 0.5, 0, 1),
                   linewidth=0.5)

        # plt.show()
        plt.savefig(name + '_' + format(idx, '03d') + '.png',
                    bbox_inches='tight',
                    pad_inches=0,
                    transparent=True)
        plt.close(fig)
    """
Example #2
0
def generate_html(ner_result):
    """ add <mark> tag to the entity """
    sentence = ner_result['sentence']
    entity = ner_result['entity']
    html = '<p class="bold"> Input sentence: </p>'
    html_entity = '<p class="bold"> Entities: </p>'
    last_end = 0

    # generate color map on the fly for better color pattern
    unique_type = list(set([i['type'] for i in entity]))
    color_map = cm.Dark2(range(len(unique_type)))
    color_bar = {t: colors.rgb2hex(c) for c, t in zip(color_map, unique_type)}

    for n, ent in enumerate(entity):
        s, e = ent['position']
        mention = ent['mention']
        _type = ent['type']
        html += sentence[last_end:s]
        html += '<span style="background:{0};color:white;">{1}</span>'.format(
            color_bar[_type], sentence[s:e])
        last_end = e
        html_entity += '* {0}. {1}: <span style="font-weight:bold;color:{2};">{3}</span> <br>'.format(
            n + 1, mention, color_bar[_type], _type)

    html += sentence[last_end:]
    html += '<br><br>'
    html += html_entity
    return html
Example #3
0
    def visualize(self, **kwargs):
        import matplotlib.pyplot as plt
        import matplotlib.cm as cmx

        strains = kwargs.get('strains', 0)
        if not isinstance(strains, np.ndarray):
            strains = np.array([strains])

        colors          = [cmx.Dark2(x) for x in np.linspace(0, 1, self.numAtoms)]
        atomIDs         = self.getAtomIDs()

        for strain in strains:
            plt.figure()
            atomsPlotted    = np.zeros_like(atomIDs)
            for j in range(self.numAtoms):
                if not atomsPlotted[atomIDs.index(self.atoms[j][0].ID)]:
                    label = self.atoms[j][0].ID
                    atomsPlotted[atomIDs.index(self.atoms[j][0].ID)] = True
                else:
                    label = '_nolegend_'

                l = plt.plot(1+j,self.atoms[j][1](strain), 'o', MarkerSize=10,
                    markeredgecolor=[0, 0, 0], markerfaceColor=colors[atomIDs.index(self.atoms[j][0].ID)],
                    label=label)

            plt.axis([0.1, self.numAtoms+0.9, -0.1, (1.1+np.max(strains))])
            plt.grid(True)

            plt.title('Strain: {:0.2f}%'.format(strain));
            plt.ylabel('relative Position');
            plt.xlabel('# Atoms');
            plt.legend()
            plt.show()
Example #4
0
def show_results(evaluations, metric):
    """ Plot evaluation results with error bar."""
    fig, ax = plt.subplots()
    colors = cm.Dark2(np.linspace(0, 1, len(evaluations)))
    results = {}
    for i, evaluation in enumerate(evaluations):
        res = evaluation.get_results(metric)
        mean, std = np.nanmean(res, axis=0), np.nanstd(res, axis=0)
        ax.errorbar(np.arange(mean.shape[0]), mean, yerr=std, color=colors[i], label=evaluation.name, fmt='-o')
        results[evaluation.name] = res

    # store the results on disk
    pwd = os.path.dirname(os.path.realpath(__file__))
    folder = '/results/' if evaluations[0].cache_folder == '' else \
        '/results/{}/'.format(evaluations[0].cache_folder)
    folder = pwd + folder
    pickle_save(folder+'/measurement_lost_{}_{}_results.pkl'.format(evaluations[0].models[0].measurement_lost, metric), results)

    # Now add the legend with some customizations.
    legend = ax.legend(loc='upper right')

    # Set the fontsize
    for label in legend.get_texts():
        label.set_fontsize('small')

    plt.show()
Example #5
0
def draw_3d(args):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    parser = argparse.ArgumentParser()
    parser.add_argument('workDir', type=str)
    parser.add_argument('--names', type=str, nargs='+', required=True)
    args = parser.parse_args()

    out = "{}/tsne_3d.pdf".format(args.workDir)
    if not os.path.exists(out):
        y = pd.read_csv("{}/labels.csv".format(args.workDir)).as_matrix()[:, 0]
        X = pd.read_csv("{}/reps.csv".format(args.workDir)).as_matrix()

        target_names = np.array(args.names)
        colors = cm.Dark2(np.linspace(0, 1, len(target_names)))

        X_pca = PCA(n_components=50).fit_transform(X, X)
        tsne = TSNE(n_components=3, init='random', random_state=0)
        X_r = tsne.fit_transform(X_pca)

        for c, i, target_name in zip(colors,
                                     list(range(1, len(target_names) + 1)),
                                     target_names):
            ax.scatter(X_r[y == i, 0], X_r[y == i, 1],
                       c=c, label=target_name)

        plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3,
                   fontsize=8)

        plt.savefig(out)
        print("Saved to: {}".format(out))
Example #6
0
def draw_2d(args):
    mpl.use('Agg')

    plt.style.use('bmh')

    out = "{}/tsne.pdf".format(args.workDir)
    if not os.path.exists(out):
        y = pd.read_csv("{}/labels.csv".format(args.workDir)).as_matrix()[:, 0]
        X = pd.read_csv("{}/reps.csv".format(args.workDir)).as_matrix()

        target_names = np.array(args.names)
        colors = cm.Dark2(np.linspace(0, 1, len(target_names)))

        X_pca = PCA(n_components=50).fit_transform(X, X)
        tsne = TSNE(n_components=2, init='random', random_state=0)
        X_r = tsne.fit_transform(X_pca)

        for c, i, target_name in zip(colors,
                                     list(range(1, len(target_names) + 1)),
                                     target_names):
            plt.scatter(X_r[y == i, 0], X_r[y == i, 1],
                        c=c, label=target_name)

        plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3,
                   fontsize=8)

        plt.savefig(out)
        print("Saved to: {}".format(out))
Example #7
0
def tsne(work_dir, dtype):
    out = "{}/{}_tsne.pdf".format(work_dir, dtype)
    print(work_dir)
    if os.path.exists(out):
        print(out)
        return True
    y = np.loadtxt("{}/{}_labels.csv".format(work_dir, dtype))
    X = np.loadtxt("{}/{}_embeddings.csv".format(work_dir, dtype))

    target_names = np.array(list(set(y)))
    colors = cm.Dark2(np.linspace(0, 1, len(target_names)))

    if X.shape[1] < 50:
        X_pca = PCA(n_components=int(X.shape[1] / 2)).fit_transform(X, X)
    else:
        X_pca = PCA(n_components=50, random_state=0).fit_transform(X, X)
    tsne = TSNE(n_components=2, init='random', random_state=0, verbose=0)
    X_r = tsne.fit_transform(X_pca)

    for c, i, target_name in zip(colors, list(range(1,
                                                    len(target_names) + 1)),
                                 target_names):
        plt.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=target_name)
    plt.legend()

    plt.savefig(out)
    print("Saved to: {}".format(out))
Example #8
0
def show_results(res_paths):
    results = {}
    for path in res_paths:
        result = pickle_load(path)
        for k, v in result.items():
            if k not in results.keys():
                results[k] = result[k]
    results = collections.OrderedDict(sorted(results.items()))
    fig, ax = plt.subplots(figsize=(9, 5.5))
    colors = cm.Dark2(np.linspace(0, 1, len(results)))
    count = 0
    for k, res in results.items():
        mean, std = np.nanmean(res, axis=0), np.nanstd(res, axis=0)
        # ax.errorbar(np.arange(mean.shape[0]), mean, yerr=std, color=colors[count], label=k, fmt='-o')
        plt.plot(np.arange(mean.shape[0]) + 1,
                 mean,
                 '-o',
                 color=colors[count],
                 label=k)
        count += 1
        print(np.array_str(mean[8:], precision=3))
        print("Average precision of %s for future prediction: %f" %
              (k, mean[8:].mean()))

    # Now add the legend with some customizations.
    legend = ax.legend(loc='upper right')

    ax.set_xlabel("time step")
    ax.set_ylabel("average precision")

    plt.axvline(x=8.5, color='r', linestyle='--')
    plt.text(3, 0.1, 'tracking', fontsize=18, color='grey')
    plt.text(11, 0.1, 'prediction', fontsize=18, color='grey')

    plt.show()
Example #9
0
 def add_traj_line(self, num_targets=1):
     """ Add 2D Lines for showing trajectories."""
     colors = cm.Dark2(np.linspace(0, 1, num_targets))
     # add lines for showing trajectories
     self.lines = map(
         lambda _: self.axes.add_line(
             Line2D([], [], zorder=14, color='grey')), range(num_targets))
Example #10
0
def show_traj(trajs):

    colors = cm.Dark2(np.linspace(0, 1, len(trajs)))
    for i, traj in enumerate(trajs):
        coordinate = traj.T.tolist()
        # starting point
        plt.plot(coordinate[0][0], coordinate[1][0], 'o', color=colors[i], zorder=13)
        plt.plot(coordinate[0], coordinate[1], color=colors[i], zorder=12)
Example #11
0
    def plot_patient_reconstructions(path,
                                     data,
                                     model,
                                     param_ind,
                                     max_patient_number=10,
                                     attribute_type=None):

        colors = cm.Dark2(np.linspace(0, 1, max_patient_number + 2))

        fig, ax = plt.subplots(1, 1)

        patient_values = model.compute_individual_tensorized(
            data.timepoints, param_ind, attribute_type)

        if type(max_patient_number) == int:
            patients_list = range(max_patient_number)
        else:
            patients_list = max_patient_number

        for i in patients_list:
            model_value = patient_values[
                i, 0:data.nb_observations_per_individuals[i], :]
            score = data.values[i,
                                0:data.nb_observations_per_individuals[i], :]
            ax.plot(data.timepoints[
                i, 0:data.nb_observations_per_individuals[i]].detach().numpy(),
                    model_value.detach().numpy(),
                    c=colors[i])
            ax.plot(data.timepoints[
                i, 0:data.nb_observations_per_individuals[i]].detach().numpy(),
                    score.detach().numpy(),
                    c=colors[i],
                    linestyle='--',
                    marker='o')

            if i > max_patient_number:
                break

        # Plot the mean also
        # min_time, max_time = torch.min(data.timepoints[data.timepoints>0.0]), torch.max(data.timepoints)

        min_time, max_time = np.percentile(
            data.timepoints[data.timepoints > 0.0].detach().numpy(), [10, 90])

        timepoints = np.linspace(min_time, max_time, 100)
        timepoints = torch.Tensor([timepoints])
        patient_values = model.compute_mean_traj(timepoints)
        for i in range(patient_values.shape[-1]):
            ax.plot(timepoints[0, :].detach().numpy(),
                    patient_values[0, :, i].detach().numpy(),
                    c="black",
                    linewidth=3,
                    alpha=0.3)

        plt.savefig(path)
        plt.close()

        return ax
    def propagate(self, motions, control, duration, state_out):
        del duration  # unused, multi-step propagation is handled inside propagateMotionsWhileValid

        # Convert from OMPL -> Numpy
        previous_states, previous_actions = self.motions_to_numpy(motions)
        previous_state = previous_states[-1]
        previous_ompl_state = motions[-1].getState()
        new_action = self.scenario_ompl.ompl_control_to_numpy(
            previous_ompl_state, control)
        np_final_state, final_classifier_probability = self.predict(
            previous_states, previous_actions, new_action)

        # Convert back Numpy -> OMPL
        self.scenario_ompl.numpy_to_ompl_state(np_final_state, state_out)

        if self.verbose >= 2:
            alpha = final_classifier_probability * 0.8 + 0.2
            classifier_probability_color = cm.Reds_r(
                final_classifier_probability)
            if len(previous_actions) == 0:
                random_color = cm.Dark2(self.control_sampler_rng.uniform(0, 1))
                RRT.propagate.r = random_color[0]
                RRT.propagate.g = random_color[1]
                RRT.propagate.b = random_color[2]
            elif not are_dicts_close_np(previous_actions[-1], new_action):
                random_color = cm.Dark2(self.control_sampler_rng.uniform(0, 1))
                RRT.propagate.r = random_color[0]
                RRT.propagate.g = random_color[1]
                RRT.propagate.b = random_color[2]

            statisfies_bounds = self.state_space.satisfiesBounds(state_out)
            if final_classifier_probability > 0.5 and statisfies_bounds:
                self.scenario.plot_tree_state(
                    np_final_state, color=classifier_probability_color)
            self.scenario.plot_current_tree_state(
                np_final_state,
                horizon=self.classifier_model.horizon,
                color=classifier_probability_color)

            self.scenario.plot_tree_action(previous_state,
                                           new_action,
                                           r=RRT.propagate.r,
                                           g=RRT.propagate.g,
                                           b=RRT.propagate.b,
                                           a=alpha)
Example #13
0
    def plot_patient_trajectory(self, model, results, indices, **kwargs):

        colors = kwargs['color'] if 'color' in kwargs.keys() else cm.Dark2(
            np.linspace(0, 1, model.dimension))
        labels = model.features
        if 'ax' in kwargs.keys():
            ax = kwargs['ax']
        else:
            (fig, ax) = plt.subplots(1, 1, figsize=(8, 8))

        if model.name in ['logistic', 'logistic_parallel']:
            plt.ylim(0, 1)

        if type(indices) is not list:
            indices = [indices]

        for idx in indices:
            indiv = results.data.get_by_idx(idx)
            timepoints = indiv.timepoints
            observations = np.array(indiv.observations)
            t = torch.Tensor(timepoints).unsqueeze(0)
            indiv_parameters = results.get_patient_individual_parameters(idx)

            trajectory = model.compute_individual_tensorized(
                t, indiv_parameters).squeeze(0)
            for dim in range(model.dimension):
                not_nans_idx = np.array(1 - np.isnan(observations[:, dim]),
                                        dtype=bool)
                ax.plot(np.array(timepoints),
                        trajectory.detach().numpy()[:, dim],
                        c=colors[dim])
                ax.plot(np.array(timepoints)[not_nans_idx],
                        observations[:, dim][not_nans_idx],
                        c=colors[dim],
                        linestyle='--')

        if 'save_as' in kwargs.keys():
            plt.savefig(os.path.join(self.output_path, kwargs['save_as']))

        if 'title' in kwargs.keys():
            plt.title(kwargs['title'])

        from matplotlib.lines import Line2D
        custom_lines = [
            Line2D([0], [0], color=colors[i % 8], lw=4)
            for i in range((model.dimension))
        ]
        print(custom_lines)
        ax.legend(custom_lines, labels, loc='upper right')

        if 'ax' not in kwargs.keys():
            plt.show()
            plt.close()
 def test_assign_colors(self):
     X = np.array([[1, 3], [4, 6], [7, 9], [10, 12]])
     Y = np.array([[100, 1000], [200, 2000], [300, 3000], [400, 4000]])
     ds = Dataset(X, Y, random_state=10)
     ds.assign_colors()
     arr = np.array([0., 1.])
     self.assertIn('f0', ds.feature_colors.keys())
     self.assertIn('f1', ds.feature_colors.keys())
     self.assertIn('t0', ds.target_colors.keys())
     self.assertIn('t1', ds.target_colors.keys())
     self.assertTrue(np.allclose(np.array(list(ds.feature_colors.values())),
                                 cm.tab10(arr)))
     self.assertTrue(np.allclose(np.array(list(ds.target_colors.values())),
                                 cm.Dark2(arr)))
Example #15
0
    def visualize(self, **kwargs):
        """visualize

        Allows for 3D presentation of unit cell by allow for a & b
        coordinate of atoms.
        Also add magnetization per atom.

        Todo: use the avogadro project as plugin
        Todo: create unit cell from CIF file e.g. by xrayutilities
        plugin.

        """
        import matplotlib.pyplot as plt
        import matplotlib.cm as cmx

        strains = kwargs.get('strains', 0)
        if not isinstance(strains, np.ndarray):
            strains = np.array([strains])

        colors = [cmx.Dark2(x) for x in np.linspace(0, 1, self.num_atoms)]
        atom_ids = self.get_atom_ids()

        for strain in strains:
            plt.figure()
            atoms_plotted = np.zeros_like(atom_ids)
            for j in range(self.num_atoms):
                if not atoms_plotted[atom_ids.index(self.atoms[j][0].id)]:
                    label = self.atoms[j][0].id
                    atoms_plotted[atom_ids.index(self.atoms[j][0].id)] = True
                    plt.plot(1+j, self.atoms[j][1](strain), 'o',
                             MarkerSize=10,
                             markeredgecolor=[0, 0, 0],
                             markerfaceColor=colors[atom_ids.index(self.atoms[j][0].id)],
                             label=label)
                else:
                    label = '_nolegend_'
                    plt.plot(1+j, self.atoms[j][1](strain), 'o',
                             MarkerSize=10,
                             markeredgecolor=[0, 0, 0],
                             markerfaceColor=colors[atom_ids.index(self.atoms[j][0].id)],
                             label=label)

            plt.axis([0.1, self.num_atoms+0.9, -0.1, (1.1+np.max(strains))])
            plt.grid(True)

            plt.title('Strain: {:0.2f}%'.format(strain))
            plt.ylabel('relative Position')
            plt.xlabel('# Atoms')
            plt.legend()
            plt.show()
Example #16
0
def color_mapping(number_of_wards, number_of_years):
    """
    Create our color map based on the number of wards

    :param number_of_wards: how many wards in dataframe
    :param number_of_years: how many years in dataframe
    :return:
    """
    start = 0.0
    stop = 1.0
    cm_subsection = np.linspace(start, stop, number_of_wards)
    gc.pie_colors = [cm.Dark2(x) for x in cm_subsection]
    # gc.pie_colors = [cm.hsv(x) for x in cm_subsection]

    cm_subsection = np.linspace(0.2, stop, number_of_years)
    gc.bar_colors = [cm.winter(x) for x in cm_subsection]
    gc.line_colors = [cm.winter(x) for x in cm_subsection]
Example #17
0
    def get_fermi_vs_pressure_plot(self,
                                   temps=[300],
                                   pressure_limit=(1e-5, 1)):
        """
        E_F vs p_O2 plot for different temperatures
        Args:
            temps: Temperatures in K as list
                Default is 300 K
            pressure_limit: Tuple with lower and upper limits on pressure.
                Defaults are 1e-5 and 1 (Note these are very high values)
        Returns:
            matplotlib plot object containg E_F vs p_O2 plot
        """
        po2s = []
        pressure = pressure_limit[0]
        while pressure < pressure_limit[1]:
            po2s.append(pressure)
            pressure *= 10
        fermi_levels = defaultdict(list)
        for T in temps:
            self._analyzer.set_T(T)
            for po2 in po2s:
                self._analyzer.set_gas_pressure(po2)
                self._analyzer.solve_for_fermi_energy(self._dos, self._gap)
                ef = self._analyzer.fermi_energy
                fermi_levels[T].append(ef)

        width, height = 12, 8
        import matplotlib.pyplot as plt
        plt.clf()
        import matplotlib.cm as cm
        colors = cm.Dark2(np.linspace(0, 1, len(temps)))
        for i, temp in enumerate(temps):
            plt.semilogx(po2s,
                         fermi_levels[temp],
                         linewidth=2,
                         color=colors[i])

        plt.legend(list(map(lambda x: str(x) + ' K', temps)),
                   fontsize=0.8 * width,
                   loc='best')
        plt.xlabel("$p_{O_2}$ (atm)", size=width)
        plt.ylabel("$E_F$ (eV)", size=width)
        #plt.title("Varition of Fermi level with T and $p_{O_2}$")
        return plt
Example #18
0
    def get_stoichiometry_vs_pressure_plot(self,
                                           specie,
                                           temps=[300],
                                           pressure_limit=(1e-5, 1)):
        """
        stoichiometry deviation vs p_O2 plot for different temperatures
        Args:
            temps: Temperatures in K as list
                Default is 300 K
            pressure_limit: Tuple with lower and upper limits on pressure.
                Defaults are 1e-5 and 1 (Note these are very high values)
        Returns:
            matplotlib plot object containg E_F vs p_O2 plot
        """
        po2s = []
        pressure = pressure_limit[0]
        while pressure < pressure_limit[1]:
            po2s.append(pressure)
            pressure *= 10
        del_x = defaultdict(list)
        for T in temps:
            self._analyzer.set_T(T)
            for po2 in po2s:
                self._analyzer.set_gas_pressure(po2)
                self._analyzer.solve_for_fermi_energy(self._dos, self._gap)
                del_x[T].append(
                    self._analyzer.get_stoichiometry_deviations()[specie])

        width, height = 12, 8
        import matplotlib.pyplot as plt
        plt.clf()
        import matplotlib.cm as cm
        colors = cm.Dark2(np.linspace(0, 1, len(temps)))
        for i, temp in enumerate(temps):
            plt.semilogx(po2s, del_x[temp], linewidth=2, color=colors[i])

        plt.legend(list(map(lambda x: str(x) + ' K', temps)),
                   fontsize=.8 * width,
                   loc='best')
        plt.xlabel("$p_{O_2}$ (atm)", size=width)
        plt.ylabel("$\Delta X$ of {}".format(specie), size=width)
        #plt.title("Deviation from stoichiometry")
        return plt
Example #19
0
 def __init__(self, size, nop):
     self.region = {}
     self.regionShape = None
     self.k = nop
     self.pods = {}
     self.size = size
     origin = Point(0, 0)
     offset = 1
     count = 1
     self.colorMap = {}
     colors = cm.Dark2(
         np.linspace(0, 1, self.k)
     )  # inferno rainbow ,agma Set1 # https://matplotlib.org/examples/color/colormaps_reference.html
     for i in range(self.k):
         self.colorMap['p' + str(i + 1)] = colors[i]
     self.regionShape = Polygon([
         (origin.x, origin.y),
         (origin.x, origin.y + offset),
         (origin.x + offset, origin.y + offset),
         (origin.x + offset, origin.y),
     ])
     for x in range(self.size):
         rowOrigin = origin
         for y in range(self.size):
             p = Polygon([(origin.x, origin.y),
                          (origin.x, origin.y + offset),
                          (origin.x + offset, origin.y + offset),
                          (origin.x + offset, origin.y)])
             centroid = p.centroid
             bg = BlockGroup.BlockGroup(count, p, centroid)
             self.region[count] = bg
             count += 1
             origin = Point(origin.x + offset, origin.y)
             if y == self.size - 1:
                 origin = Point(rowOrigin.x, rowOrigin.y + offset)
     self.getPodLocations()
     origin = Point(0, 0)
     self.regionShape = Polygon([(origin.x, origin.y),
                                 (origin.x, origin.y + size),
                                 (origin.x + size, origin.y + size),
                                 (origin.x + size, origin.y)])
     print(self.regionShape.exterior.coords[:])
Example #20
0
def make_artifact_plots(data, outname, pos_arts, neg_arts, stds):
    colors = [cm.Dark2(x) for x in np.linspace(0, 1, len(stds))]
    f, (ax1, ax2, ax3) = plt.subplots(3, 1)
    if len(pos_arts) == 0 and len(neg_arts) == 0:
        # nothing to do
        plt.savefig(outname + ".png")
        return
    for c, (poss, negs) in enumerate(zip(pos_arts, neg_arts)):
        extrema = np.array(poss + negs, dtype=int)
        for i in extrema:
            ax1.plot(data[i - 10:i + 10, c] / stds[c],
                     linewidth=0.5, color=colors[c])
            plt.sca(ax2)
            plt.hist(data[extrema, c] / stds[c], bins=20, fill=None, edgecolor=colors[c])
            ax3.vlines(extrema, 0, 1, color=colors[c])
        ax1.set_ylabel("standard deviation")
        ax1.set_title("artifacts")
        ax2.set_title("amplitude distribution")
        ax3.set_title("artifact locations")
    plt.savefig(outname + ".png")
Example #21
0
def showNeighbourdsInMap(neighbourhoods_df, city_address, lat=None, long=None):

    if lat and long:
        latitude = lat
        longitude = long
    else:
        geolocator = Nominatim(user_agent="coursera-battle-of-neighbourhood")
        location = geolocator.geocode(city_address)
        latitude = location.latitude
        longitude = location.longitude

    print('The geograpical coordinate of Rotterdam are {}, {}.'.format(
        latitude, longitude))

    map_clusters = folium.Map(location=[latitude, longitude], zoom_start=12)

    # set color scheme for the clusters
    x = np.arange(kclusters)
    ys = [i + x + (i * x)**2 for i in range(kclusters)]
    colors_array = cm.Dark2(np.linspace(0, 1, len(ys)))
    rainbow = [colors.rgb2hex(i) for i in colors_array]

    # add markers to the map
    # markers_colors = []
    for lat, lon, poi, poi_postal, cluster in zip(
            neighbourhoods_df['Latitude'], neighbourhoods_df['Longitude'],
            neighbourhoods_df.reset_index()['Neighbourhood'],
            neighbourhoods_df.reset_index()['Neighbourhood Postal Code'],
            neighbourhoods_df['Cluster Labels']):
        label = folium.Popup(str(poi) + '-\n' + str(poi_postal) + ' Cluster ' +
                             str(cluster),
                             parse_html=True)
        folium.CircleMarker([lat, lon],
                            radius=5,
                            popup=label,
                            color=rainbow[cluster - 1],
                            fill=True,
                            fill_color=rainbow[cluster - 1],
                            fill_opacity=0.7).add_to(map_clusters)

    return map_clusters
Example #22
0
    def plot_from_individual_parameters(self, model, indiv_parameters,
                                        timepoints, **kwargs):
        colors = kwargs['color'] if 'color' in kwargs.keys() else cm.Dark2(
            np.linspace(0, 1, model.dimension))
        labels = model.features
        fig, ax = plt.subplots(1, 1, figsize=(11, 6))

        trajectory = model.compute_individual_trajectory(
            timepoints, indiv_parameters).squeeze()
        for dim in range(model.dimension):
            ax.plot(timepoints,
                    trajectory[:, dim],
                    c=colors[dim],
                    label=labels[dim])

        if 'save_as' in kwargs.keys():
            plt.savefig(os.path.join(self.output_path, kwargs['save_as']))

        plt.legend()
        plt.show()
        plt.close()
Example #23
0
def slice_pdf(ax, particles):

    N_slices = 10
    cm_subsection = np.linspace(0.0, 1.0, N_slices)
    colors = [cm.Dark2(x) for x in cm_subsection]

    ax.cla()
    ax.set_ylabel(r'pdf slices')
    ax.set_xlabel(r'$v_x$')

    xlims = np.linspace(rpic.grid_xmin, rpic.grid_xmax, N_slices + 1)
    for i in range(len(xlims) - 1):
        #print "slice lims", xlims[i], xlims[i+1]
        indx1 = np.where(particles[:, 0] > xlims[i])
        indx2 = np.where(particles[:, 0] < xlims[i + 1])
        indx = np.intersect1d(indx1, indx2)

        vx_sample = particles[indx, 3]
        ax = plot_pdf(ax, vx_sample, color=colors[i])

    return ax
Example #24
0
def plot_points(centroids, data, clustered_class):
    plt.figure(figsize=[20, 10])
    legend = list()
    classes = np.unique(clustered_class)
    colors = cm.Dark2(np.linspace(0, 1, len(classes)))
    for i in range(len(classes)):
        cluster_data = data[np.where(clustered_class[:] == classes[i])]
        legend.append(
            plt.scatter(cluster_data[:, 0],
                        cluster_data[:, 1],
                        c=colors[i],
                        alpha=0.75,
                        s=10))
        plt.scatter(centroids[i, 0],
                    centroids[i, 1],
                    s=130,
                    marker="x",
                    c=colors[i])

    plt.legend(legend, classes)
    plt.title("KMeans")
    plt.show()
Example #25
0
    def get_diff_vs_temp_plot(self, pressures=[1e-5], temps_limit=(300, 1200)):
        """
        stoichiometry deviation vs p_O2 plot for different temperatures
        Args:
            temps: Temperatures in K as list
                Default is 300 K
            pressure_limit: Tuple with lower and upper limits on pressure.
                Defaults are 1e-5 and 1 (Note these are very high values)
        Returns:
            matplotlib plot object containg E_F vs p_O2 plot
        """
        temps = []
        T = temps_limit[0]
        while T < temps_limit[1]:
            temps.append(T)
            T += 50
        del_x = defaultdict(list)
        for pO2 in pressures:
            for T in temps:
                #D = get_diffusion_coefficient(fermi,
                del_x[pO2].append(
                    self._analyzer.get_stoichiometry_deviations()[specie])

        width, height = 12, 8
        import matplotlib.pyplot as plt
        plt.clf()
        import matplotlib.cm as cm
        colors = cm.Dark2(np.linspace(0, 1, len(temps)))
        for i, temp in enumerate(temps):
            plt.semilogx(po2s, del_x[temp], linewidth=3, color=colors[i])

        plt.legend(map(lambda x: str(x) + ' K', temps),
                   fontsize=1.8 * width,
                   loc='best')
        plt.xlabel("$p_{O_2}$ (atm)", size=2 * width)
        plt.ylabel("$\Delta X$ of {}".format(specie), size=2 * width)
        plt.title("Deviation from stoichiometry")
        return plt
Example #26
0
def tsne_plot_similar_words(title, labels, embedding_clusters, word_clusters,
                            a, filename):
    plt.figure(figsize=(16, 9))
    colors = cm.Dark2(np.linspace(0, 1, len(labels)))
    for label, embeddings, words, color in zip(labels, embedding_clusters,
                                               word_clusters, colors):
        x = embeddings[:, 0]
        y = embeddings[:, 1]
        plt.scatter(x, y, c=color, alpha=a, label=label)
        for i, word in enumerate(words):
            plt.annotate(word,
                         alpha=0.5,
                         xy=(x[i], y[i]),
                         xytext=(5, 2),
                         textcoords='offset points',
                         ha='right',
                         va='bottom',
                         size=12)
    plt.legend(loc=4)
    plt.title(title)
    plt.grid(True)
    if filename:
        plt.savefig(filename, format='png', dpi=150, bbox_inches='tight')
    plt.show()
Example #27
0
    def plot(self,
             mu_elts=None,
             xlim=None,
             ylim=None,
             ax_fontsize=1.3,
             lg_fontsize=1.,
             lg_position=None,
             fermi_level=None,
             title=None,
             saved=False):
        """
        Produce defect Formation energy vs Fermi energy plot
        Args:
            mu_elts:
                a dictionnary of {Element:value} giving the chemical
                potential of each element
            xlim:
                Tuple (min,max) giving the range of the x (fermi energy) axis
            ylim:
                Tuple (min,max) giving the range for the formation energy axis
            ax_fontsize:
                float  multiplier to change axis label fontsize
            lg_fontsize:
                float  multiplier to change legend label fontsize
            lg_position:
                Tuple (horizontal-position, vertical-position) giving the position
                to place the legend.
                Example: (0.5,-0.75) will likely put it below the x-axis.
            saved:


        Returns:
            a matplotlib object

        """
        if xlim is None:
            xlim = (-0.5, self.band_gap + 0.5)
        xy = {}
        lower_cap = -100.
        upper_cap = 100.
        y_range_vals = [
        ]  # for finding max/min values on y-axis based on x-limits
        for defnom, def_tl in self.transition_level_map.items():
            xy[defnom] = [[], []]
            if def_tl:
                org_x = list(def_tl.keys())  # list of transition levels
                org_x.sort()  # sorted with lowest first

                # establish lower x-bound
                first_charge = max(def_tl[org_x[0]])
                for chg_ent in self.stable_entries[defnom]:
                    if chg_ent.charge == first_charge:
                        form_en = chg_ent.formation_energy(
                            chemical_potentials=mu_elts, fermi_level=lower_cap)
                        fe_left = chg_ent.formation_energy(
                            chemical_potentials=mu_elts, fermi_level=xlim[0])

                xy[defnom][0].append(lower_cap)
                xy[defnom][1].append(form_en)
                y_range_vals.append(fe_left)

                # iterate over stable charge state transitions
                for fl in org_x:
                    charge = max(def_tl[fl])
                    for chg_ent in self.stable_entries[defnom]:
                        if chg_ent.charge == charge:
                            form_en = chg_ent.formation_energy(
                                chemical_potentials=mu_elts, fermi_level=fl)
                    xy[defnom][0].append(fl)
                    xy[defnom][1].append(form_en)
                    y_range_vals.append(form_en)

                # establish upper x-bound
                last_charge = min(def_tl[org_x[-1]])
                for chg_ent in self.stable_entries[defnom]:
                    if chg_ent.charge == last_charge:
                        form_en = chg_ent.formation_energy(
                            chemical_potentials=mu_elts, fermi_level=upper_cap)
                        fe_right = chg_ent.formation_energy(
                            chemical_potentials=mu_elts, fermi_level=xlim[1])
                xy[defnom][0].append(upper_cap)
                xy[defnom][1].append(form_en)
                y_range_vals.append(fe_right)
            else:
                # no transition - just one stable charge
                chg_ent = self.stable_entries[defnom][0]
                for x_extrem in [lower_cap, upper_cap]:
                    xy[defnom][0].append(x_extrem)
                    xy[defnom][1].append(
                        chg_ent.formation_energy(chemical_potentials=mu_elts,
                                                 fermi_level=x_extrem))
                for x_window in xlim:
                    y_range_vals.append(
                        chg_ent.formation_energy(chemical_potentials=mu_elts,
                                                 fermi_level=x_window))

        if ylim is None:
            window = max(y_range_vals) - min(y_range_vals)
            spacer = 0.1 * window
            ylim = (min(y_range_vals) - spacer, max(y_range_vals) + spacer)

        if len(xy) <= 8:
            colors = cm.Dark2(np.linspace(0, 1, len(xy)))
        else:
            colors = cm.gist_rainbow(np.linspace(0, 1, len(xy)))

        plt.figure()
        plt.clf()
        width, height = 12, 8
        # plot formation energy lines
        for_legend = []
        for cnt, defnom in enumerate(xy.keys()):
            plt.plot(xy[defnom][0],
                     xy[defnom][1],
                     linewidth=3,
                     color=colors[cnt])
            for_legend.append(self.stable_entries[defnom][0].copy())

        # plot transtition levels
        for cnt, defnom in enumerate(xy.keys()):
            x_trans, y_trans = [], []
            for x_val, chargeset in self.transition_level_map[defnom].items():
                x_trans.append(x_val)
                for chg_ent in self.stable_entries[defnom]:
                    if chg_ent.charge == chargeset[0]:
                        form_en = chg_ent.formation_energy(
                            chemical_potentials=mu_elts, fermi_level=x_val)
                y_trans.append(form_en)
            if len(x_trans):
                plt.plot(x_trans,
                         y_trans,
                         marker='*',
                         color=colors[cnt],
                         markersize=12,
                         fillstyle='full')

        # get latex-like legend titles
        legends_txt = []
        for dfct in for_legend:
            flds = dfct.name.split('_')
            if 'Vac' == flds[0]:
                base = '$Vac'
                sub_str = '_{' + flds[1] + '}$'
            elif 'Sub' == flds[0]:
                flds = dfct.name.split('_')
                base = '$' + flds[1]
                sub_str = '_{' + flds[3] + '}$'
            elif 'Int' == flds[0]:
                base = '$' + flds[1]
                sub_str = '_{inter}$'
            else:
                base = dfct.name
                sub_str = ''

            legends_txt.append(base + sub_str)

        if not lg_position:
            plt.legend(legends_txt, fontsize=lg_fontsize * width, loc=0)
        else:
            plt.legend(legends_txt,
                       fontsize=lg_fontsize * width,
                       ncol=3,
                       loc='lower center',
                       bbox_to_anchor=lg_position)

        plt.ylim(ylim)
        plt.xlim(xlim)

        plt.plot([xlim[0], xlim[1]], [0, 0],
                 'k-')  # black dashed line for Eformation = 0
        plt.axvline(x=0.0, linestyle='--', color='k',
                    linewidth=3)  # black dashed lines for gap edges
        plt.axvline(x=self.band_gap, linestyle='--', color='k', linewidth=3)

        if fermi_level is not None:
            plt.axvline(x=fermi_level, linestyle='-.', color='k',
                        linewidth=2)  # smaller dashed lines for gap edges

        plt.xlabel("Fermi energy (eV)", size=ax_fontsize * width)
        plt.ylabel("Defect Formation\nEnergy (eV)", size=ax_fontsize * width)
        if title:
            plt.title("{}".format(title), size=ax_fontsize * width)

        if saved:
            plt.savefig(str(title) + "FreyplnravgPlot.pdf")
        else:
            return plt
Example #28
0
#-----global.flag
web_get001txtFg = False  # @zt_web.web_get001txtFg

#-----bs4.findall
bs_get_ktag_kstr = ''

#--colors
#10,prism,brg,dark2,hsv,jet
#10,,hot,Vega10,Vega20
cors_brg = cm.brg(np.linspace(0, 1, 10))
cors_hot = cm.hot(np.linspace(0, 1, 10))
cors_hsv = cm.hsv(np.linspace(0, 1, 10))
cors_jet = cm.jet(np.linspace(0, 1, 10))
cors_prism = cm.prism(np.linspace(0, 1, 10))
cors_Dark2 = cm.Dark2(np.linspace(0, 1, 10))
#cors_Vega10=cm.Vega10(np.linspace(0,1,10))
#cors_Vega20=cm.Vega20(np.linspace(0,1,10))

#------str.xxx
sgnSP4 = '    '
sgnSP8 = sgnSP4 + sgnSP4

#-----FN.xxx
logFN = ''

#--------------dir
raiLib = '/aiLib/'
r_TDS = raiLib + 'TDS/'
#-------------------
#---zDat.....
Example #29
0
    def __init__(self, geo_data):

        # Set the values of matplotlib
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_xlim(0, 7)
        ax.get_yaxis().set_visible(False)
        ax.get_xaxis().set_visible(False)
        ax.axis('off')

        # Compute the anchor values for the number of series. This is a DataFrame
        self.anch_series, self.anch_formations, self.thick_series, self.thick_formations = set_anchor_points(
            geo_data)

        # We create the list that contains rectangles that represent our series ang are global
        global series_rect
        series_rect = {}

        # Define the initial value of each rectangle
        pos_anch = np.squeeze(self.anch_series.as_matrix())
        rects = ax.barh(
            pos_anch,
            np.ones_like(pos_anch) * 2,
            self.thick_series,
        )

        # We connect each rectangle
        for e, series in enumerate(geo_data.series.columns):
            # TODO Alex set the colors of the series accordingly

            rects[e].set_color(cm.Dark2(e))
            rects[e].set_label(series)
            dr = DraggableRectangle(rects[e], geo_data, series)
            dr.connect()
            dr.rect.f = None
            dr.rect.s = series

            series_rect[series] = dr

        global formation_rect
        formation_rect = {}

        # Define the initial value of each rectangle
        pos_anch = np.squeeze(self.anch_formations.as_matrix())
        rects = ax.barh(pos_anch, np.ones_like(pos_anch) * 2, .5, left=3.)

        color = 1
        # We connect each rectangle
        for e, formation in enumerate(self.anch_formations.columns):

            if 'aux' in formation:
                rects[e].set_alpha(.1)
                rects[e].set_color('gray')
            else:

                rects[e].set_color(cmap(color))
                rects[e].set_label(formation)
                color += 1

            dr = DraggableRectangle(rects[e], geo_data, formation)
            dr.connect()
            dr.rect.f = formation
            dr.rect.s = None

            formation_rect[formation] = dr
        plt.legend(bbox_to_anchor=(1.1, 1.05))
        plt.ion()
        ax.text(1,
                self.anch_series.max().values.max() + self.thick_series / 2 +
                2,
                r'Series',
                fontsize=15,
                fontweight='bold',
                bbox={
                    'facecolor': 'gray',
                    'alpha': 0.5,
                    'pad': 10
                },
                horizontalalignment='center')
        ax.text(4,
                self.anch_series.max().values.max() + self.thick_series / 2 +
                2,
                r'Faults/Formations',
                fontsize=15,
                fontweight='bold',
                bbox={
                    'facecolor': 'gray',
                    'alpha': 0.5,
                    'pad': 10
                },
                horizontalalignment='center')

        self.figure = plt.gcf()
Example #30
0
Note: This example assumes that `name i` corresponds to `label i`
in `labels.csv`.

""")

parser = argparse.ArgumentParser()
parser.add_argument('workDir', type=str)
parser.add_argument('--names', type=str, nargs='+', required=True)
args = parser.parse_args()

y = pd.read_csv("{}/labels.csv".format(args.workDir),
                header=None).as_matrix()[:, 0]
X = pd.read_csv("{}/reps.csv".format(args.workDir), header=None).as_matrix()

target_names = np.array(args.names)
colors = cm.Dark2(np.linspace(0, 1, len(target_names)))

nc = None if len(X) < 50 else 50
X_pca = PCA(n_components=nc).fit_transform(X, X)
for p in [2, 5, 10, 30, 50, 100]:
    tsne = TSNE(n_components=2, init='random', random_state=0, perplexity=p)
    X_r = tsne.fit_transform(X_pca)

    plt.figure()
    for c, i, target_name in zip(colors, list(range(1,
                                                    len(target_names) + 1)),
                                 target_names):
        plt.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=target_name)
    plt.legend()

    out = "{}/tsne_{}.pdf".format(args.workDir, p)