示例#1
0
def test_changepoint_scaled():

    p = 150
    M = multiscale(p)
    M.minsize = 10
    X = ra.adjoint(M)

    Y = np.random.standard_normal(p)
    Y[20:50] += 8
    Y += 2
    meanY = Y.mean()

    lammax = np.fabs(np.sqrt(M.sizes) * X.adjoint_map(Y) / (1 + np.sqrt(np.log(M.sizes)))).max()

    penalty = rr.weighted_l1norm((1 + np.sqrt(np.log(M.sizes))) / np.sqrt(M.sizes), lagrange=0.5*lammax)
    loss = rr.squared_error(X, Y - meanY)
    problem = rr.simple_problem(loss, penalty)
    soln = problem.solve()
    Yhat = X.linear_map(soln)
    Yhat += meanY

    if INTERACTIVE:
        plt.scatter(np.arange(p), Y)
        plt.plot(np.arange(p), Yhat)
        plt.show()
示例#2
0
 def trumpet_plot(self, scan_rates, trumpets, **kwargs):
     if "ax" not in kwargs:
         ax = None
     else:
         ax = kwargs["ax"]
     if "label" not in kwargs:
         label = None
     else:
         label = kwargs["label"]
     if "description" not in kwargs:
         kwargs["description"] = False
     else:
         label = None
     if "log" not in kwargs:
         kwargs["log"] = np.log10
     if len(trumpets.shape) != 2:
         raise ValueError(
             "For plotting reductive and oxidative sweeps together")
     else:
         colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
         if ax == None:
             plt.scatter(kwargs["log"](scan_rates),
                         trumpets[:, 0],
                         label="Forward sim",
                         color=colors[0])
             plt.scatter(kwargs["log"](scan_rates),
                         trumpets[:, 1],
                         label="Reverse sim",
                         color=colors[0],
                         facecolors='none')
             plt.legend()
             plt.show()
         else:
             if kwargs["description"] == False:
                 if ax.collections:
                     pass
                 else:
                     self.color_counter = 0
                 ax.scatter(kwargs["log"](scan_rates),
                            trumpets[:, 0],
                            label=label,
                            color=colors[self.color_counter])
                 ax.scatter(kwargs["log"](scan_rates),
                            trumpets[:, 1],
                            color=colors[self.color_counter],
                            facecolors='none')
                 self.color_counter += 1
             else:
                 ax.scatter(kwargs["log"](scan_rates),
                            trumpets[:, 0],
                            label="$E_{p(ox)}-E^0$",
                            color=colors[0])
                 ax.scatter(kwargs["log"](scan_rates),
                            trumpets[:, 1],
                            label="$E_{p(red)}-E^0$",
                            color=colors[0],
                            facecolors='none')
示例#3
0
    def plot_slice(self,
                   neuron_type,
                   x_min=None,
                   x_max=None,
                   y_min=None,
                   y_max=None,
                   z_min=None,
                   z_max=None):

        cell_id = self.neuron_type_lookup[neuron_type]
        ok_idx = np.zeros((len(cell_id), ), dtype=int)
        ok_idx[cell_id] = 1

        if x_min is not None:
            ok_idx = np.logical_and(ok_idx,
                                    x_min < self.neuron_positions[:, 0])

        if x_max is not None:
            ok_idx = np.logical_and(ok_idx,
                                    self.neuron_positions[:, 0] < x_max)

        if y_min is not None:
            ok_idx = np.logical_and(ok_idx,
                                    y_min < self.neuron_positions[:, 1])

        if y_max is not None:
            ok_idx = np.logical_and(ok_idx,
                                    self.neuron_positions[:, 1] < y_max)

        if z_min is not None:
            ok_idx = np.logical_and(ok_idx,
                                    z_min < self.neuron_positions[:, 2])

        if z_max is not None:
            ok_idx = np.logical_and(ok_idx,
                                    self.neuron_positions[:, 2] < z_max)

        cell_pos = self.neuron_positions[ok_idx, :]

        fig = plt.figure()
        ax = fig.add_subplot(projection='3d')
        plt.scatter(x=cell_pos[:, 0], y=cell_pos[:, 1], z=cell_pos[:, 2])

        plt.show()
示例#4
0
文件: main.py 项目: wljSky/ai_explore
def plot_embeddings(embeddings,):
    X, Y = read_node_label('../data/wiki_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)

    for c, idx in color_idx.items():
        plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)
    plt.legend()
    plt.show()
示例#5
0
    def simulate(self, parameters, frequency_range):
        maxes=np.zeros(len(frequency_range))

        for j in range(0, len(frequency_range)):

            self.dim_dict["omega"]=frequency_range[j]
            self.nd_param=params(self.dim_dict)
            #self.calculate_times()
            #start=time.time()
            #volts=self.define_voltages()
            #start=time.time()

            current=super().simulate(parameters, [])
            f, b, net, potential=super().SW_peak_extractor(current)

            if self.simulation_options["synthetic_noise"]!=0:
                current=self.add_noise(net, self.simulation_options["synthetic_noise"]*max(net))
                #current=self.rolling_window(current, 8)
            else:
                current=net
            #plt.plot(potential, current)
                #current=self.rolling_window(current, 8)
            if self.simulation_options["record_exps"]==True:
                self.saved_sims["current"].append(current)
                self.saved_sims["voltage"].append(volts)
            first_half=tuple(np.where(potential<self.dim_dict["E_0"]))
            second_half=tuple(np.where(potential>self.dim_dict["E_0"]))
            data=[first_half, second_half]
            peak_pos=[0 ,0]
            for i in range(0, 2):
                maximum=max(current[data[i]])
                max_idx=potential[data[i]][np.where(current[data[i]]==maximum)]
                peak_pos[i]=max_idx
            maxes[j]=(peak_pos[1]-peak_pos[0])*1000
        if self.simulation_options["SWVtest"]==True:
            plt.scatter(1/frequency_range, maxes)
            plt.scatter(1/frequency_range, self.test)
            plt.show()
            #print(time.time()-start)
        return maxes
示例#6
0
    def simulate(self, parameters, frequency_range):
        start=time.time()
        maxes=np.zeros(len(frequency_range))
        if self.simulation_options["amplitudes_set"]!=True:
            raise ValueError("Need to define SW ampltidues")
        for j in range(0, len(frequency_range)):
            self.dim_dict["omega"]=frequency_range[j]
            delta_p=np.zeros(len(self.Esw_range))
            for q in range(0, len(self.Esw_range)):
                self.dim_dict["SW_amplitude"]=self.Esw_range[q]
                self.nd_param=params(self.dim_dict)
                #self.calculate_times()
                #start=time.time()
                #volts=self.define_voltages()
                current=super().simulate(parameters, [])
                f, b, net, potential=super().SW_peak_extractor(current)

                if self.simulation_options["synthetic_noise"]!=0:

                    current=self.add_noise(net, self.simulation_options["synthetic_noise"]*max(net))
                    #current=self.rolling_window(current, 8)
                else:
                    current=net

                #plt.plot(potential, current)
                    #current=self.rolling_window(current, 8)
                if self.simulation_options["record_exps"]==True:
                    self.saved_sims["current"].append(current)
                    self.saved_sims["voltage"].append(volts)
                delta_p[q]=(max(current))/self.Esw_range[q]
            #plt.show()
            maxes[j]=(self.Esw_range[np.where(delta_p==max(delta_p))])

        if self.simulation_options["SWVtest"]==True:
            plt.scatter(1/frequency_range, maxes)
            plt.scatter(1/frequency_range, self.test)
            plt.show()

        return maxes
"""
Created on Fri Dec  4 14:05:56 2020

@author: acn980
"""

from matplotlib.pyplot import plt

#Here all_hz is a DataFrame with as index dates and 4 different columns, on for each hazard type.
#The index goes from 1980-01-01 until 2016-01-01, with a daily timestep
#In each columns I put a 1,2,3 or 4 on the days where there was a tropical cyclone, flood, earthquake or volcano outburt, respectively

f = plt.figure(figsize=(10, 2))
ax = plt.axes()
#f.patch.set_visible(False)
plt.scatter(all_hz.index, all_hz['TC'], c='blue', marker="+", s=markersize)
plt.scatter(all_hz.index,
            all_hz['FL'],
            c='lightblue',
            marker="+",
            s=markersize)  #marker="|"
plt.scatter(all_hz.index, all_hz['EQ'], c='red', marker="+", s=markersize)
plt.scatter(all_hz.index,
            all_hz['VO'],
            c='saddlebrown',
            marker="+",
            s=markersize)
plt.ylim([0.5, 3])
plt.yticks([])
#plt.xticks(pd.date_range(start='1980-01-01', end='2016-01-01', freq='AS'))
ax.xaxis.set_minor_locator(years)
# the optimal number of clusters for this data comes out to be 4. Therefore lets take number of cluster for k means = 4
#--------------------applying K-Means Clustering-------------#

kmeans = KMeans(n_clusters=4, init='k-means++', random_state=42)
y_kmeans = kmeans.fit_predict(return_and_volatility_datfarame)

return_and_volatility_datfarame.reset_index(level=['Company Symbol'],
                                            inplace=True)
return_and_volatility_datfarame["Cluster Name"] = y_kmeans

####----------------------Visualising the S&P 500-----------####
numeric_values = return_and_volatility_datfarame.iloc[:, [1, 2]]

plt.scatter(numeric_values[y_kmeans == 0, 0],
            numeric_values[y_kmeans == 0, 1],
            s=100,
            c='red',
            label='Cluster 1')
plt.scatter(numeric_values[y_kmeans == 1, 0],
            numeric_values[y_kmeans == 1, 1],
            s=100,
            c='blue',
            label='Cluster 2')
plt.scatter(numeric_values[y_kmeans == 2, 0],
            numeric_values[y_kmeans == 2, 1],
            s=100,
            c='green',
            label='Cluster 3')
plt.scatter(numeric_values[y_kmeans == 3, 0],
            numeric_values[y_kmeans == 3, 1],
            s=100,