Add files via upload

This commit is contained in:
yalmansour1998
2024-06-12 14:16:57 +02:00
committed by GitHub
parent b744e1ad5f
commit d988de46b0

View File

@ -1,18 +1,14 @@
import asyncio
import dash
import plotly.graph_objects as go
from dash import html, dcc, callback, Input, Output, State
from dash import html, dcc, callback, Input, Output
import dash_bootstrap_components as dbc
from gng2 import GrowingNeuralGas
import numpy as np
from GNG_3D import *
from sklearn import datasets as sk
from pages.ascan import data_traits
dash.register_page(
__name__, path="/gng", title="GNG", name="GNG", description="Growing Neural Gas"
)
# Generate synthetic data
X, Y = sk.make_moons(n_samples=200, noise=0.1, random_state=0)
dash.register_page(__name__, path="/gng", title="GNG", name="GNG")
clics = None
@ -27,13 +23,25 @@ layout = html.Div(
),
dbc.Col(
[
dbc.Label("Noise: "),
dbc.Label("seuil: "),
dbc.Input(
placeholder="Noise",
value=0.1,
placeholder="seuil",
value=100,
type="number",
id="noise",
step=0.1,
id="seuil",
step=1,
),
]
),
dbc.Col(
[
dbc.Label("max neurons: "),
dbc.Input(
placeholder="max_neurons",
value=1000,
type="number",
id="max_neurons",
step=10,
),
]
),
@ -41,133 +49,165 @@ layout = html.Div(
[
dbc.Label("Iterations: "),
dbc.Input(
placeholder="Iterations",
placeholder="Iteration",
value=1000,
type="number",
id="iterations",
id="Iterations",
step=10,
),
]
),
dbc.Col(
[
dbc.Label("Max Nodes: "),
dbc.Label("Max_age: "),
dbc.Input(
placeholder="Max Nodes",
value=200,
placeholder="Max_of_age",
value=5,
type="number",
id="nodes",
id="Max_of_age",
step=1,
),
]
),
dbc.Col(
[
dbc.Label("distance entre le neurone le plus proche et le signal d'entrée: "),
dbc.Input(
placeholder="eb",
value=0.1,
type="number",
id="eb",
step=0.1,
),
]
),
dbc.Col(
[
dbc.Label("distance entre les neurones voisins et le signal d'entrée: "),
dbc.Input(
placeholder="en",
value=0.1,
type="number",
id="en",
step=0.1,
),
]
),
dbc.Col(
[
dbc.Label("l'erreur locale: "),
dbc.Input(
placeholder="alpha",
value=0.1,
type="number",
id="alpha",
step=0.1,
),
]
),
dbc.Col(
[
dbc.Label("l'erreur globale: "),
dbc.Input(
placeholder="beta",
value=0.1,
type="number",
id="beta",
step=0.1,
),
]
),
dbc.Col(
[
dbc.Label("L: "),
dbc.Input(
placeholder="l",
value=0.1,
type="number",
id="l",
step=0.1,
),
]
),
]
),
dcc.Graph(id="base-graph"),
#dcc.Graph(id="base-graph"),
dcc.Graph(id="gng-graph"),
]
)
@callback(
Output("base-graph", "figure", allow_duplicate=True),
[Input("noise", "value")],
prevent_initial_call=True,
)
def update_base_graph(noise):
X, Y = sk.make_moons(n_samples=200, noise=noise, random_state=0)
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=X[Y == 0, 0], y=X[Y == 0, 1], mode="markers", marker=dict(color="blue")
)
)
fig.add_trace(
go.Scatter(
x=X[Y == 1, 0], y=X[Y == 1, 1], mode="markers", marker=dict(color="red")
)
)
fig.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
xaxis=dict(visible=False),
yaxis=dict(visible=False),
title="Base Data",
)
return fig
@callback(
[Output("gng-graph", "figure"), Output("base-graph", "figure")],
[Output("gng-graph", "figure")],
[
Input("generate-gng", "n_clicks"),
Input("seuil", "value"),
Input("max_neurons", "value"),
Input("Iterations", "value"),
Input("Max_of_age", "value"),
Input("eb", "value"),
Input("en", "value"),
Input("alpha", "value"),
Input("beta", "value"),
Input("l", "value"),
],
[State("noise", "value"), State("iterations", "value"), State("nodes", "value")],
)
def generate_gng(n_clicks, noise, iterations, nodes):
global clics
X, Y = sk.make_moons(n_samples=200, noise=noise, random_state=0)
def generate_gng(n_clicks, seuil, max_neurons, max_iter, max_age, eb, en, alpha, beta, l):
fig2 = go.Figure()
fig2.add_trace(
go.Scatter(
x=X[Y == 0, 0], y=X[Y == 0, 1], mode="markers", marker=dict(color="blue")
)
)
fig2.add_trace(
go.Scatter(
x=X[Y == 1, 0], y=X[Y == 1, 1], mode="markers", marker=dict(color="red")
)
)
fig2.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
xaxis=dict(visible=False),
yaxis=dict(visible=False),
title="Base Data",
)
global clics
if n_clicks != clics:
gng = GrowingNeuralGas(input_dim=2, max_nodes=nodes)
asyncio.run(gng.fit(X, num_iterations=iterations))
data=np.array(data_traits)
fig = go.Figure()
for edge in gng.edges:
fig.add_trace(
go.Scatter(
x=[edge.nodes[0].position[0], edge.nodes[1].position[0]],
y=[edge.nodes[0].position[1], edge.nodes[1].position[1]],
mode="lines",
line=dict(width=2, color="white"),
)
)
for node in gng.nodes:
fig.add_trace(
go.Scatter(
x=[node.position[0]],
y=[node.position[1]],
mode="markers",
marker=dict(size=10, color="red"),
)
)
fig.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
xaxis=dict(visible=False),
yaxis=dict(visible=False),
title="GNG Model",
)
size_data=np.shape(data)
x1=[]
x2=[]
x3=[]
x4=[]
for i in range(0,size_data[0]):
res=data[i,:,:]
res=position_point_espace(data=res,seuil=200,y=i)
res = np.array(res)
x1.append(res)
gng = GrowingNeuralGas(int(max_neurons),int(max_iter),int(max_age), float(eb), float(en), float(alpha), float(beta),int(l), res)
gng_graph = gng.learn()
neuron_positions = np.array([vertex['weight'] for vertex in gng_graph.vs])
neuron_edge= np.array([(edge.source, edge.target) for edge in gng_graph.es])
for edge in gng_graph.es:
src, tgt = edge.source, edge.target
src_pos, tgt_pos = gng_graph.vs[src]['weight'], gng_graph.vs[tgt]['weight']
x3.append(src_pos)
x4.append(tgt_pos)
x2.append(neuron_positions)
res=np.vstack(x1)
neuron_positions=np.vstack(x2)
src_pos=np.vstack(x3)
tgt_pos=np.vstack(x4)
fig.add_trace(go.Scatter3d(
x=res[:, 0],
y=res[:, 1],
z=res[:, 2],
mode='markers',
marker=dict(size=1, color=res[:, 3], colorscale='Viridis', opacity=0.5),
name='Data'
))
fig.add_trace(go.Scatter3d(
x=neuron_positions[:, 0],
y=neuron_positions[:, 1],
z=neuron_positions[:, 2],
mode='markers',
marker=dict(size=4, color=neuron_positions[:,3]),
name='Neurons'
))
for src, tgt in zip(src_pos, tgt_pos):
fig.add_trace(go.Scatter3d(
x=[src[0], tgt[0]],
y=[src[1], tgt[1]],
z=[src[2], tgt[2]],
mode='lines',
line=dict(color="black", width=1),
showlegend=False,
))
clics = n_clicks
return [fig, fig2]
return [
go.Figure().update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
xaxis=dict(visible=False),
yaxis=dict(visible=False),
),
fig2,
]
return [fig]