From 22b50bdca7d869a1acb315bcaf6f6ea55184aabf Mon Sep 17 00:00:00 2001
From: Hammouda Elbez <hammouda.elbez@univ-lille.fr>
Date: Fri, 9 Jun 2023 13:28:02 +0200
Subject: [PATCH] Fix layout and added Current Epoch info

---
 src/Global_Var.py                |  1 +
 src/Modules/General/callbacks.py | 27 ++++++++++++++++++++++-----
 src/Modules/General/layout.py    | 10 ++++++++--
 src/Modules/General/spark.py     |  1 +
 src/templates/index.html         |  2 +-
 5 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/src/Global_Var.py b/src/Global_Var.py
index 747e8cb..67fc12f 100755
--- a/src/Global_Var.py
+++ b/src/Global_Var.py
@@ -27,6 +27,7 @@ class Global_Var():
     data_loaded = None
     db = None
     updateInterval = 1.0  # 1 second
+    InputPerEpoch = 1
     stepMax = 0
     Max = 0
     nbrClasses = 0
diff --git a/src/Modules/General/callbacks.py b/src/Modules/General/callbacks.py
index 75bd78d..ff68593 100755
--- a/src/Modules/General/callbacks.py
+++ b/src/Modules/General/callbacks.py
@@ -32,7 +32,7 @@ class callbacks(callbacksOp):
 
             try:
 
-                def processGeneralGraph(data, sliderValue, generalGraphFilter, generalLayerFilter):
+                def processGeneralGraph(data, sliderValue, generalGraphFilter, generalLayerFilter, Epoch):
                     """ Create general graph components.
 
                     Args:
@@ -40,6 +40,7 @@ class callbacks(callbacksOp):
                         sliderValue (int): value of the slider
                         generalGraphFilter (list): actual filter of GeneralGraph visualization
                         generalLayerFilter (list): selected layers for GeneralGraph visualization
+                        Epoch (int): Epoch number
 
                     Returns:
                         general graph content
@@ -205,6 +206,15 @@ class callbacks(callbacksOp):
                                             "width": 2},
                                     ),row=1, col=1)
                         
+                        fig.add_annotation(dict(font=dict(color='rgb(68, 71, 99)',size=15),
+                                        x=0,
+                                        y=-0.12,
+                                        showarrow=False,
+                                        text="Epoch "+str(Epoch) if Epoch != -1 else "",
+                                        textangle=0,
+                                        xanchor='left',
+                                        xref="paper",
+                                        yref="paper"))
                         return fig
 
                     except Exception:
@@ -406,8 +416,8 @@ class callbacks(callbacksOp):
                 @app.callback(
                     [Output("general-graph", "figure")
                      ], [Input("v-step", "children")],
-                    [State("interval", "value"), State("GeneralGraphFilter", "value"), State("GeneralLayerFilter", "value"), State('general-graph-switch', 'on')])
-                def progressGeneralGraph(sliderValue, updateInterval, generalGraphFilter, generalLayerFilter, generalGraphSwitchIsOn):
+                    [State("interval", "value"), State("GeneralGraphFilter", "value"), State("GeneralLayerFilter", "value"), State('general-graph-switch', 'on'),State("InputPerEpoch","value")])
+                def progressGeneralGraph(sliderValue, updateInterval, generalGraphFilter, generalLayerFilter, generalGraphSwitchIsOn,InputPerEpoch):
                     """ Update the general graph.
 
                     Args:
@@ -416,6 +426,7 @@ class callbacks(callbacksOp):
                         generalGraphFilter (list): actual filter of GeneralGraph visualization
                         generalLayerFilter (list): selected layers for GeneralGraph visualization
                         generalGraphSwitchIsOn (bool): general graph switch value
+                        InputPerEpoch (int): Number of inputs per epoch
 
                     Raises:
                         no_update: in case we don't want to update the content we rise this execption 
@@ -424,6 +435,11 @@ class callbacks(callbacksOp):
                         content of the graph that contains general information on the network activity
                     """
                     if generalGraphSwitchIsOn:
+                        labelData = getNetworkInput(int(sliderValue)*float(updateInterval), g.updateInterval)
+                        if labelData != None:
+                            labelData = (labelData[1] // InputPerEpoch)+1 if (labelData[1] % InputPerEpoch) != 0 else (labelData[1] // InputPerEpoch)
+                        else:
+                            labelData = -1
                         if len(super.xAxisLabel) > 0 and "["+g.getLabelTime(g.updateInterval, sliderValue)+","+g.getLabelTime(g.updateInterval, sliderValue+1)+"]" == super.xAxisLabel[-1]:
                             return no_update
 
@@ -436,7 +452,7 @@ class callbacks(callbacksOp):
                                     None, sliderValue, generalGraphFilter, generalLayerFilter)
                             else:
                                 generalGraph = processGeneralGraph(
-                                    generalData, sliderValue, generalGraphFilter, generalLayerFilter)
+                                    generalData, sliderValue, generalGraphFilter, generalLayerFilter,labelData)
 
                             return [generalGraph]
 
@@ -447,7 +463,7 @@ class callbacks(callbacksOp):
                                 generalData = GeneralModuleData(
                                     int(sliderValue)*float(updateInterval), generalGraphFilter, generalLayerFilter)
                                 generalGraph = processGeneralGraph(
-                                    generalData, int(sliderValue), generalGraphFilter, generalLayerFilter)
+                                    generalData, int(sliderValue), generalGraphFilter, generalLayerFilter,labelData)
                                 return [generalGraph]
                     else:
                         return no_update
@@ -737,6 +753,7 @@ class callbacks(callbacksOp):
 
                 L = dict({i["_id"]: i["C"] for i in labels})
 
+                print(timestamp, interval, Max)
                 return [L, Max]
 
             def getSpike(timestamp, interval, layer, perNeuron):
diff --git a/src/Modules/General/layout.py b/src/Modules/General/layout.py
index 446c73d..dedc4e1 100755
--- a/src/Modules/General/layout.py
+++ b/src/Modules/General/layout.py
@@ -158,7 +158,11 @@ class layout(layoutOp):
                                                 value=[str(i) for i in (
                                                     i["layer"] for i in self.g.LayersNeuronsInfo)],
                                                 multi=True,
-                                                style={"minWidth": "20%","marginLeft": "5px", "textAlign": "start"})], className="d-flex", style={"paddingLeft": "20px", 'width': '100%'})
+                                                style={"minWidth": "20%","marginLeft": "5px", "textAlign": "start"}),
+                                                # Epoch value
+                                                html.P("Input/Epoch: ", style={
+                                                   "textAlign": "start", "marginLeft": "20px", "marginTop": "4px"}),
+                                                dbc.Input(type="number", id="InputPerEpoch", value=self.g.InputPerEpoch, min=100, step=100, style={"width": "auto","marginLeft": "10px", "textAlign": "start"})], className="d-flex", style={"paddingLeft": "20px", 'width': '100%'})
                                         ], className="col-12")
                                     ], className="d-flex"),
                                     html.Div([dcc.Graph(id='general-graph', config={"displaylogo": False})])], className="col-lg-9 col-sm-12 col-xs-12" if(self.g.labelsExistance) else "col-lg-12 col-sm-12 col-xs-12"),
@@ -206,6 +210,8 @@ class layout(layoutOp):
                                             id='cytoscape-compound',
                                             layout={'name': 'preset'},
                                             boxSelectionEnabled=False,
+                                            #panningEnabled= False,
+                                            responsive=True,
                                             style={'width': '100%',
                                                    'height': '100%'},
                                             stylesheet=[
@@ -308,7 +314,7 @@ class layout(layoutOp):
                         html.Div([html.A("Information", target="_blank", rel="noopener noreferrer", href="https://gitlab.univ-lille.fr/bioinsp/VS2N", style={
                                  "color": "rgb(217, 220, 255)"})], className="col-sm-2 col-lg-2 align-self-center", style={"position": "fixed", "textAlign": "start"}),
                         html.Div([html.A("Log Out", href="/logout", style={"color": "rgb(217, 220, 255)"})], className="col-sm-2 col-lg-2 align-self-center", style={
-                                 "position": "fixed", "right": "0px", "textAlign": "end"})
+                                 "position": "fixed", "right": "10px", "textAlign": "end"})
                         ], className="col-12", style={"marginRight": "0px", "marginLeft": "0px", "paddingRight": "0px", "paddingLeft": "0px"})
             ], style={"background": "rgb(68, 71, 99)"}),
             dbc.Container([
diff --git a/src/Modules/General/spark.py b/src/Modules/General/spark.py
index eb175ab..f57c6a3 100755
--- a/src/Modules/General/spark.py
+++ b/src/Modules/General/spark.py
@@ -68,6 +68,7 @@ class spark(sparkOp):
                     self.g.labelsExistance = True
                     M = max(M, pymongo.collection.Collection(
                         self.g.db, 'labels').find_one(sort=[("T", -1)])["T"])
+                    self.g.InputPerEpoch = pymongo.collection.Collection(self.g.db, 'labels').count_documents({})
                 else:
                     print("No labels")
             
diff --git a/src/templates/index.html b/src/templates/index.html
index 61fd8e2..b7aca40 100755
--- a/src/templates/index.html
+++ b/src/templates/index.html
@@ -21,7 +21,7 @@
         <div class="col-12"><a class="navbar-brand" href="#" style="width: 100%;text-align: center;">VS2N</a></div>
         <div class="col-sm-2 col-lg-2 align-self-center" style="position: fixed;text-align: start;"><a href="https://gitlab.univ-lille.fr/bioinsp/VS2N" style="
     color: rgb(217, 220, 255);" target="_blank" rel="noopener noreferrer">Information</a></div>
-    <div class="col-sm-2 col-lg-2 align-self-center" style="position: fixed;right:0px;text-align: end;"><a href="{{url_for('logout')}}" style="color: rgb(217, 220, 255);">Log Out</a></div>    
+    <div class="col-sm-2 col-lg-2 align-self-center" style="position: fixed;right:10px;text-align: end;"><a href="{{url_for('logout')}}" style="color: rgb(217, 220, 255);">Log Out</a></div>    
 
 </div></nav>
     <!--    End NavBar     -->
-- 
GitLab