Beispiel #1
0
 def __init__(self, raw_data : pd.DataFrame, n_clusters : int) -> None:
     """Initialize the object.
     
     Arguments:
         raw_data -- pandas DataFrame with timestamp as index.
         n_clusters -- number of clusters to be used in clustering.
     """
     
     self.graph = StateGraph(n_clusters)
     self.data = self.graph.fit_transform(raw_data)
     self.norm_data = pd.DataFrame(
             data=self.graph.normalisation.transform(raw_data),
             index=raw_data.index,
             columns=raw_data.columns)
     self.labels = np.array([self.data["label"].to_numpy()]).T
Beispiel #2
0
class Visualization():
    """
    Maintains information of a single dataset
    and provides methods for its visualization.
    """
    def __init__(self, raw_data: pd.DataFrame, n_clusters: int) -> None:
        """Initialize the object.
        
        Arguments:
            raw_data -- pandas DataFrame with timestamp as index.
            n_clusters -- number of clusters to be used in clustering.
        """

        self.graph = StateGraph(n_clusters)
        data = self.graph.fit_transform(raw_data)
        self.norm_data = pd.DataFrame(
            data=self.graph.normalisation.transform(raw_data),
            index=raw_data.index,
            columns=raw_data.columns)
        self.labels = np.array([data["label"].to_numpy()]).T

    def get_parallel_plot(self) -> go.Figure:
        """Returns a Figure containing the parallel plot of
        clusters' centroids.
        """

        fig = px.parallel_coordinates(self.graph.centroids,
                                      color=self.graph.centroids.index,
                                      dimensions=self.graph.centroids.columns)
        return fig

    def get_PCA(self) -> go.Figure:
        """Returns a Figure containing a PCA plot of clustered data."""

        pca = PCA(n_components=2)
        pca_result = pca.fit_transform(self.norm_data)
        plot_data = pd.DataFrame(np.concatenate((pca_result, self.labels),
                                                axis=1),
                                 columns=["pca_x", "pca_y", "cluster"])
        return px.scatter(plot_data, x="pca_x", y="pca_y", color="cluster")

    def get_TSNE(self, perplexity: int = 100, n_iter: int = 3000) -> go.Figure:
        """Returns a Figure containing a TSNE plot of clustered data.
        Function arguments correspond to standard TSNE arguments.
        """

        tsne = TSNE(n_components=2,
                    verbose=1,
                    perplexity=perplexity,
                    n_iter=n_iter)
        tsne_result = tsne.fit_transform(self.norm_data)
        plot_data = pd.DataFrame(np.concatenate((tsne_result, self.labels),
                                                axis=1),
                                 columns=["tsne_x", "tsne_y", "cluster"])
        return px.scatter(plot_data, x="tsne_x", y="tsne_y", color="cluster")
Beispiel #3
0
 def get_all_graphs_infos(cls):
     for (_, _, filenames) in os.walk(FOLDER_GRAPH):
         for filename in filenames:
             if '.gz' in filename:
                 graph_name = filename.replace('.gz',
                                               '')  # Remove .gz extension.
                 domain_name = filename.split('-', 1)[0]
                 options_list = [
                     '--domain', domain_name, '--graph', graph_name
                 ]
                 exp_params = ExpParams.get_exp_params(options_list)
                 g = StateGraph.load(exp_params)
                 g.print_stats()
         break
Beispiel #4
0
    def create_back_range(cls, exp_params):
        g = StateGraph.load(exp_params)
        g.compute_bfs()
        # prob_keep_range = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]
        prob_keep_range = [x / 100.0 for x in EXP_BACK_RANGE]
        prob_keep_range_inc = []
        currently_kept = 1.0
        for p in prob_keep_range:
            must_keep_inc = p / currently_kept
            prob_keep_range_inc.append(must_keep_inc)
            currently_kept = p
        # print 'Going to trim back edges by these percentages:'
        # print prob_keep_range_inc

        for i in range(len(prob_keep_range)):  # pylint: disable=consider-using-enumerate
            filename_suffix = '-back-%d' % (prob_keep_range[i] * 100)
            g.trim_back_edges(prob_keep_range_inc[i])
            g.save(exp_params, filename_suffix)
            print ''
Beispiel #5
0
class Visualization():
    """
    Maintains information of a single dataset
    and provides methods for its visualization.
    """
    
    def __init__(self, raw_data : pd.DataFrame, n_clusters : int) -> None:
        """Initialize the object.
        
        Arguments:
            raw_data -- pandas DataFrame with timestamp as index.
            n_clusters -- number of clusters to be used in clustering.
        """
        
        self.graph = StateGraph(n_clusters)
        self.data = self.graph.fit_transform(raw_data)
        self.norm_data = pd.DataFrame(
                data=self.graph.normalisation.transform(raw_data),
                index=raw_data.index,
                columns=raw_data.columns)
        self.labels = np.array([self.data["label"].to_numpy()]).T
        
        
    def get_parallel_plot(self) -> go.Figure:
        """Returns a Figure containing the parallel plot of
        clusters' centroids.
        """
        
        fig = px.parallel_coordinates(
                self.graph.centroids,
                color=self.graph.centroids.index,
                dimensions=self.graph.centroids.columns
                )
        return fig
    
    def get_PCA(self) -> go.Figure:
        """Returns a Figure containing a PCA plot of clustered data."""
        
        pca = PCA(n_components=2)
        pca_result = pca.fit_transform(self.norm_data)
        plot_data = pd.DataFrame(
                np.concatenate((pca_result,self.labels),axis=1),
                columns=["pca_x","pca_y","cluster"])
        return px.scatter(plot_data,x="pca_x",y="pca_y",color="cluster")
    
    def get_TSNE(self, perplexity : int = 100, n_iter : int = 3000) -> go.Figure:
        """Returns a Figure containing a TSNE plot of clustered data.
        Function arguments correspond to standard TSNE arguments.
        """
        
        tsne = TSNE(n_components=2,
                    verbose=1,
                    perplexity=perplexity,
                    n_iter=n_iter)
        tsne_result = tsne.fit_transform(self.norm_data)
        plot_data = pd.DataFrame(
                np.concatenate((tsne_result,self.labels),axis=1),
                columns=["tsne_x","tsne_y","cluster"])
        return px.scatter(plot_data,x="tsne_x",y="tsne_y",color="cluster")
    
    def get_histograms(self,cluster : int) -> go.Figure:
        """Returns a figure containing a histogram for each sensor 
        for the data in a given cluster (cluster index starts at 0).
        """

        if cluster not in self.labels.flatten():
            raise ValueError(f"cluster must be between 0 and {np.amax(self.labels)}")
            
        filtered = self.data[self.data["label"] == cluster]
        cols = filtered.columns.values[:-1]
        
        fig = make_subplots(rows=len(cols)//2,cols=2,subplot_titles=cols)
        for index, sensor in enumerate(cols):
            x = filtered[sensor].values
            fig.append_trace(
                    go.Histogram(x=x, name=sensor),
                    row=index//2+1,
                    col=index%2+1
                    )
            fig.update_xaxes(title_text=sensor, row=index//2+1, col=index%2+1)
            fig.update_yaxes(title_text="Count", row=index//2+1, col=index%2+1)

        fig.update_layout(autosize=False,width=1500,height=200*len(cols),
                          showlegend=False)
        return fig
Beispiel #6
0
from sys import stdin, argv
from os import system, name
from state_graph import StateGraph
from maze import Maze


def clear_screen():
    if name == 'posix':
        system('clear')
    else:
        system('cls')


try:
    with open(argv[1], 'r') as file:
        graph = StateGraph(file.read())
except:
    print("Falha ao ler o arquivo, verifique o formato passado")
    exit()

clear_screen()

print("Escolha um dos algoritmos disponíveis:")
print("1 - A*")
print("2 - Busca em Aprofundamento Iterativo")

try:
    while True:
        inp = input()
        if inp == '1':
            path, cost = graph.astar()
Beispiel #7
0
        # print prob_keep_range_inc

        for i in range(len(prob_keep_range)):  # pylint: disable=consider-using-enumerate
            filename_suffix = '-back-%d' % (prob_keep_range[i] * 100)
            g.trim_back_edges(prob_keep_range_inc[i])
            g.save(exp_params, filename_suffix)
            print ''

    @classmethod
    def get_all_graphs_infos(cls):
        for (_, _, filenames) in os.walk(FOLDER_GRAPH):
            for filename in filenames:
                if '.gz' in filename:
                    graph_name = filename.replace('.gz',
                                                  '')  # Remove .gz extension.
                    domain_name = filename.split('-', 1)[0]
                    options_list = [
                        '--domain', domain_name, '--graph', graph_name
                    ]
                    exp_params = ExpParams.get_exp_params(options_list)
                    g = StateGraph.load(exp_params)
                    g.print_stats()
            break


if __name__ == '__main__':
    exp_params = ExpParams.get_exp_params_from_command_line_args()
    StateGraph.print_graph_info(exp_params)
#    ManipGraph.create_back_range(exp_params)
#    ManipGraph.get_all_graphs_infos()
            s = item[1]
            k_score = self.compute_k_neighbours_fittness(
                s, self.recent_restores_size)
            if s.fitness_score > highest_score:
                fittest_node = s
                highest_score = s.fitness_score

                print s.uid + " k_neighbour_fitness: " + str(s)
        return fittest_node.uid


# Some tests
if __name__ == '__main__':

    # Initial case -> empty
    graph = StateGraph()
    graph.add_node('1')
    node = graph.retrieve('1')
    node.fitness_score = 100
    graph.dump()

    strategy = CircularRestoreStrategy(graph, 3)
    print strategy.get_fittest_state()  # expected state 1
    print strategy
    print strategy.get_fittest_state()  # expected state 1
    print strategy

    # Building up, graph has less states than size of queue
    graph.add_node('2')
    node = graph.retrieve('2')
    node.fitness_score = 90