CaxtonEmeraldS commited on
Commit
37f6623
·
verified ·
1 Parent(s): fba04c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +529 -261
app.py CHANGED
@@ -1,265 +1,533 @@
1
- import streamlit as st
2
- from tensorflow import keras
 
 
 
 
 
 
 
 
 
3
  import os
4
- import matplotlib.pyplot as plt
5
- from io import BytesIO
6
- from NNVisualiser import NNVisualiser
7
- import glob
8
- import inspect
9
- from tensorflow.keras.models import save_model
10
- import tempfile
11
  import re
 
12
  import zipfile
13
- import io
 
 
 
 
14
 
15
- # Function to create a ZIP file of all PNG files
16
- def create_zip_of_png_files():
17
- # Get current working directory
18
- cwd = os.getcwd()
19
- png_files = [f for f in os.listdir(cwd) if f.endswith('.png')]
20
-
21
- # Create a BytesIO object to hold the ZIP file in memory
22
- zip_buffer = io.BytesIO()
23
-
24
- with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
25
- for png_file in png_files:
26
- zip_file.write(os.path.join(cwd, png_file), arcname=png_file)
27
-
28
- zip_buffer.seek(0) # Seek to the beginning of the BytesIO buffer
29
- return zip_buffer
30
-
31
- def generate_title_from_method_name(method_name):
32
- # Remove the "plot" prefix if it exists
33
- if method_name.startswith("plot"):
34
- method_name = method_name[4:] # Remove the first 4 characters ("plot")
35
-
36
- # Split the string at camel case boundaries
37
- words = re.findall(r'[A-Z][a-z]*', method_name)
38
-
39
- # Join the words with spaces and format the final string
40
- title = "Plotting " + " ".join(words[:]) + " Plot "
41
-
42
- return title
43
-
44
- def downloadKerasModel():
45
- with tempfile.NamedTemporaryFile(delete=False, suffix=".keras") as tmp_file:
46
- save_model(model, tmp_file.name)
47
- tmp_file.seek(0)
48
- model_data = tmp_file.read()
49
- return model_data
50
-
51
- # Function to build folder hierarchy up to the 6th level (excluding files and hidden folders)
52
- # @st.cache_data
53
- def generate_folder_hierarchy(root_folder, max_depth=7):
54
- folder_dict = {}
55
-
56
- # Traverse through the directory tree
57
- for dirpath, dirnames, filenames in os.walk(root_folder):
58
- # Get the relative path from the root folder
59
- rel_path = os.path.relpath(dirpath, root_folder)
60
- depth = rel_path.count(os.sep) + 1 # Calculate the depth level
61
-
62
- # Only include directories up to the max_depth (7th level)
63
- if depth > max_depth:
64
- continue
65
-
66
- # Filter out directories that start with a dot (e.g., .git)
67
- dirnames[:] = [d for d in dirnames if not d.startswith('.') and d != '1']
68
-
69
- sub_dict = folder_dict
70
- # Split the relative path into parts to create a nested structure
71
- for part in rel_path.split(os.sep):
72
- if part == '.' or part.startswith('.'):
73
- continue
74
- if part not in sub_dict:
75
- sub_dict[part] = {}
76
- sub_dict = sub_dict[part]
77
-
78
- return folder_dict
79
-
80
- @st.cache_data
81
- def getPlotMethods():
82
- return [name for name, func in inspect.getmembers(NNVisualiser, inspect.isfunction) if name.startswith('plot')]
83
-
84
- # Example usage
85
- root_folder = os.getcwd(); # Replace with your folder path
86
- folder_hierarchy = generate_folder_hierarchy(root_folder)
87
-
88
- # Streamlit app
89
- st.title("Repository : Simple ANN Models with UAT Architecture")
90
- st.write(f"A Collection of ANN Models with a 1-xReLU-1 Architecture for Basic 1D Functions on Bounded Intervals")
91
- #Commented
92
-
93
- # col1, col2, col3 = st.columns([4, 3, 3])
94
-
95
- # with col1:
96
- # # Level 1: Initialisation dropdown
97
- # initialisation = st.selectbox("Select Initialisation", list(folder_hierarchy.keys()))
98
-
99
- # with col2:
100
- # # Level 2: Sample size dropdown, based on selected initialisation
101
- # sampleSize = st.selectbox("Select Sample Size", list(folder_hierarchy[initialisation].keys()))
102
-
103
- # with col3:
104
- # # Level 3: Batch size dropdown, based on selected sample size
105
- # batchSize = st.selectbox("Select Batch Size", list(folder_hierarchy[initialisation][sampleSize].keys()))
106
-
107
-
108
- # col4, col5, col6 = st.columns([3, 4, 3])
109
-
110
- # with col4:
111
- # # Level 4: Epochs count dropdown, based on selected batch size
112
- # epochs = st.selectbox("Select Epochs Count", list(folder_hierarchy[initialisation][sampleSize][batchSize].keys()))
113
-
114
- # with col5:
115
- # # Level 5: Functions list dropdown, based on selected epochs count
116
- # functions = st.selectbox("Select Neurons Count", list(folder_hierarchy[initialisation][sampleSize][batchSize][epochs].keys()))
117
-
118
- # with col6:
119
- # # Level 6: Neurons count dropdown, based on selected function
120
- # neurons = st.selectbox("Select Neurons Count", list(folder_hierarchy[initialisation][sampleSize][batchSize][epochs][functions].keys()))
121
-
122
- repo = st.sidebar.selectbox("Select Model Repository",list(folder_hierarchy.keys()))
123
- initialisation = st.sidebar.selectbox("Select Initialisation", list(folder_hierarchy[repo].keys()))
124
- sampleSize = st.sidebar.selectbox("Select Sample Size", list(folder_hierarchy[repo][initialisation].keys()))
125
- batchSize = st.sidebar.selectbox("Select Batch Size", list(folder_hierarchy[repo][initialisation][sampleSize].keys()))
126
- epochs = st.sidebar.selectbox("Select Epochs Count", list(folder_hierarchy[repo][initialisation][sampleSize][batchSize].keys()))
127
- functions = st.sidebar.selectbox("Select Function", list(folder_hierarchy[repo][initialisation][sampleSize][batchSize][epochs].keys()))
128
- neurons = st.sidebar.selectbox("Select Neurons Count", list(folder_hierarchy[repo][initialisation][sampleSize][batchSize][epochs][functions].keys()))
129
-
130
- # Display the selected values
131
- st.write(f"You selected: {repo} : {initialisation} : {sampleSize} : {batchSize} : {epochs} : {functions} : {neurons}")
132
-
133
- modelPath = os.path.join(os.getcwd(), repo, initialisation, sampleSize, batchSize, epochs, functions, neurons);
134
- model = keras.models.load_model(modelPath);
135
-
136
- visualiser = NNVisualiser(model);
137
- visualiser.setSavePlots(True);
138
-
139
- # Function to get layer and neuron information
140
- def get_layer_info(model):
141
- layer_info = []
142
- for layer in model.layers:
143
- layer_info.append({
144
- 'index': len(layer_info),
145
- 'type': layer.__class__.__name__,
146
- 'units': getattr(layer, 'units', None), # Number of neurons
147
- })
148
- return layer_info
149
-
150
- layer_info = get_layer_info(model)
151
-
152
- # Extract layer indices and neuron counts
153
- layer_indices = [layer['index'] for layer in layer_info]
154
- neuron_counts = [layer['units'] for layer in layer_info]
155
-
156
- # Dropdown for selecting layer index
157
- #selected_layer_index = st.sidebar.selectbox("Select Layer Index", layer_indices)
158
-
159
- # Find the number of neurons for the selected layer
160
- #selected_layer_units = neuron_counts[selected_layer_index]
161
-
162
- # Dropdown for selecting neuron index in the selected layer
163
- #neuron_indices = list(range(selected_layer_units))
164
- #selected_neuron_index = st.sidebar.selectbox("Select Neuron Index", neuron_indices)
165
-
166
- # Dropdown for selecting plots from NNVisualiser
167
- plotMethods = getPlotMethods()
168
- selectedPlotMethod = st.sidebar.selectbox("Select Plot", plotMethods)
169
-
170
- #Removing earlier plots
171
- image_files = glob.glob("*.png")
172
- for file in image_files:
173
- try:
174
- os.remove(file)
175
- except Exception as e:
176
- st.write("Error in removing previous plots")
177
-
178
- st.session_state.title_text = generate_title_from_method_name(selectedPlotMethod)
179
- st.title(st.session_state.title_text)
180
-
181
- # Call your package's plot method (which directly plots without returning a figure)
182
- visualiser.setSavePlots(True);
183
- method = getattr(visualiser, selectedPlotMethod, None)
184
-
185
- if method is not None:
186
- if 'Neuron' in selectedPlotMethod:
187
- selected_layer_index = st.sidebar.selectbox("Select Layer Index", layer_indices)
188
- # Find the number of neurons for the selected layer
189
- selected_layer_units = neuron_counts[selected_layer_index]
190
- # Dropdown for selecting neuron index in the selected layer
191
- neuron_indices = list(range(selected_layer_units))
192
- selected_neuron_index = st.sidebar.selectbox("Select Neuron Index", neuron_indices)
193
- params = (selected_layer_index, selected_neuron_index)
194
- method(*params)
195
- elif 'Layer' in selectedPlotMethod:
196
- selected_layer_index = st.sidebar.selectbox("Select Layer Index", layer_indices)
197
- params = (selected_layer_index,)
198
- method(*params)
199
- else:
200
- method()
201
-
202
- st.session_state.kerasModelToDownload = downloadKerasModel()
203
- st.session_state.plotsToDownload = create_zip_of_png_files()
204
-
205
- @st.fragment()
206
- def downloads():
207
- st.download_button(
208
- label="Download Model",
209
- data = downloadKerasModel(),
210
- file_name="model.keras",
211
- mime="application/octet-stream"
212
- );
213
-
214
- st.download_button(
215
- label="Download Plots",
216
- data=create_zip_of_png_files(),
217
- file_name="images.zip",
218
- mime="application/zip"
219
- );
220
- # column = st.columns (2)
221
-
222
- # column[0].download_button(
223
- # label="Download Model",
224
- # data = downloadKerasModel(),
225
- # file_name="model.keras",
226
- # mime="application/octet-stream"
227
- # );
228
-
229
- # column[1].download_button(
230
- # label="Download Plots",
231
- # data=create_zip_of_png_files(),
232
- # file_name="images.zip",
233
- # mime="application/zip"
234
- # );
235
-
236
- with st.sidebar:
237
- downloads()
238
-
239
- # visualiser.plotFlowForNetwork();
240
-
241
- image_files = glob.glob("*.png")
242
-
243
- # Use Streamlit to display the image from the buffer
244
- st.image(image_files)
245
-
246
- # if st.sidebar.button("Download Keras model"):
247
- # downloadKerasModel()
248
-
249
-
250
- # if st.sidebar.download_button(
251
- # label="Download Keras Model",
252
- # data = downloadKerasModel(),
253
- # file_name="model.keras",
254
- # mime="application/octet-stream"
255
- # ):
256
- # st.sidebar.success(f"Model Downloaded Successfully")
257
-
258
- # # Button to create and download the ZIP file
259
- # if st.sidebar.download_button(
260
- # label="Download Plots",
261
- # data=create_zip_of_png_files(),
262
- # file_name="images.zip",
263
- # mime="application/zip"
264
- # ):
265
- # st.sidebar.success(f"Plots Downloaded Successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """JeevanRuth ANN Model Explorer — Streamlit research demonstration.
2
+
3
+ Supports the original seven-folder model hierarchy:
4
+ repository / initialisation / sample size / batch size / epochs /
5
+ function / initial neuron count / saved Keras artefact.
6
+
7
+ This app NEVER performs JRNNOA optimisation; it explores saved model variants.
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import io
12
  import os
 
 
 
 
 
 
 
13
  import re
14
+ import threading
15
  import zipfile
16
+ from pathlib import Path
17
+ from tempfile import TemporaryDirectory
18
+ from typing import Any
19
+
20
+ import streamlit as st
21
 
22
+ ROOT = Path(__file__).resolve().parent
23
+ FIELDS = (
24
+ "repository", "initialisation", "sample_size", "batch_size",
25
+ "epochs", "function", "neurons",
26
+ )
27
+ LABELS = {
28
+ "repository": "Model collection / variant",
29
+ "initialisation": "Weight initialisation",
30
+ "sample_size": "Training sample size",
31
+ "batch_size": "Batch size",
32
+ "epochs": "Training epochs",
33
+ "function": "Target function",
34
+ "neurons": "Initial hidden-neuron count (catalogue label)",
35
+ }
36
+ PLOTS = {
37
+ "Activation · network overview": ("plotActivationConsolidatedForNetwork", "network"),
38
+ "Scaling · network overview": ("plotScaleConsolidatedForNetwork", "network"),
39
+ "Translation · network overview": ("plotTranslationConsolidatedForNetwork", "network"),
40
+ "Inputs · network overview": ("plotInputConsolidatedForNetwork", "network"),
41
+ "Activation · selected layer": ("plotActivationConsolidatedForLayer", "layer"),
42
+ "Scaling · selected layer": ("plotScaleConsolidatedForLayer", "layer"),
43
+ "Activation · selected neuron": ("plotActivationForNeuron", "neuron"),
44
+ "Transformation flow · selected neuron": ("plotFlowForNeuron", "neuron"),
45
+ }
46
+
47
+
48
+ def natural_key(value: str) -> tuple:
49
+ """Sort mixed numerical text in human-friendly order."""
50
+ return tuple((0, int(part)) if part.isdigit() else (1, part.casefold())
51
+ for part in re.split(r"(\d+)", str(value)))
52
+
53
+
54
+ def resolve_saved_model(directory: Path) -> Path | None:
55
+ """Locate exactly one supported Keras artefact at a catalogue leaf.
56
+
57
+ A directory is accepted as TensorFlow SavedModel only when it contains
58
+ saved_model.pb. A single .keras/.h5 checkpoint is also supported.
59
+ Ambiguous directories are excluded rather than selecting a random file.
60
+ """
61
+ if (directory / "saved_model.pb").is_file():
62
+ return directory
63
+ candidates = sorted(
64
+ (p for p in directory.iterdir() if p.is_file() and p.suffix.lower() in {".keras", ".h5"}),
65
+ key=lambda p: natural_key(p.name),
66
+ )
67
+ return candidates[0] if len(candidates) == 1 else None
68
+
69
+
70
+ def scan_catalogue(root: Path) -> list[dict[str, str]]:
71
+ """Discover validated artefacts in the user's pre-existing hierarchy.
72
+
73
+ Does not traverse saved-model contents, hidden folders, or output folders.
74
+ """
75
+ records: list[dict[str, str]] = []
76
+ root = root.resolve()
77
+ for current, foldernames, filenames in os.walk(root):
78
+ path = Path(current)
79
+ relative = path.relative_to(root)
80
+ depth = len(relative.parts)
81
+ foldernames[:] = sorted(
82
+ [name for name in foldernames if not name.startswith(".")
83
+ and name not in {"__pycache__", ".venv", "venv", "plots", "outputs"}],
84
+ key=natural_key,
85
+ )
86
+ if depth == len(FIELDS):
87
+ foldernames.clear()
88
+ artefact = resolve_saved_model(path)
89
+ if artefact is not None:
90
+ item = dict(zip(FIELDS, relative.parts))
91
+ item["path"] = str(artefact)
92
+ records.append(item)
93
+ elif depth > len(FIELDS):
94
+ foldernames.clear()
95
+ records.sort(key=lambda item: tuple(natural_key(item[name]) for name in FIELDS))
96
+ return records
97
+
98
+
99
+ def choices(records: list[dict], field: str, **constraints: str) -> list[str]:
100
+ """Only show values that occur in valid artefact records."""
101
+ return sorted({row[field] for row in records
102
+ if all(row.get(k) == v for k, v in constraints.items())}, key=natural_key)
103
+
104
+
105
+ def matches(records: list[dict], **constraints: str) -> list[dict]:
106
+ return [row for row in records
107
+ if all(row.get(k) == v for k, v in constraints.items())]
108
+
109
+
110
+ def variant_name(repository: str) -> str:
111
+ """Human-friendly description; never assume which files were optimised."""
112
+ norm = repository.lower().replace("-", " ").replace("_", " ")
113
+ if re.search(r"un\s*optimi[sz]ed|non\s*optimi[sz]ed|original|baseline|before", norm):
114
+ return "Unoptimised / baseline (repository label)"
115
+ if re.search(r"optimi[sz]ed|compressed|after|post", norm):
116
+ return "Optimised (repository label)"
117
+ return "Model collection (optimisation status not inferred)"
118
+
119
+
120
+ def archive_model(path: Path) -> tuple[bytes, str, str]:
121
+ """Download the ORIGINAL artefact, rather than re-serialising the model."""
122
+ if path.is_file():
123
+ return path.read_bytes(), path.name, "application/octet-stream"
124
+ output = io.BytesIO()
125
+ with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
126
+ for file in sorted(path.rglob("*")):
127
+ if file.is_file():
128
+ archive.write(file, arcname=str(Path(path.name) / file.relative_to(path)))
129
+ return output.getvalue(), path.name + ".zip", "application/zip"
130
+
131
+
132
+ def plot_archive(images: list[tuple[str, bytes]]) -> bytes:
133
+ output = io.BytesIO()
134
+ with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
135
+ for filename, content in images:
136
+ archive.writestr(filename, content)
137
+ return output.getvalue()
138
+
139
+
140
+ @st.cache_data(show_spinner=False)
141
+ def cached_catalogue(root_string: str) -> list[dict[str, str]]:
142
+ return scan_catalogue(Path(root_string))
143
+
144
+
145
+ @st.cache_resource(show_spinner=False, max_entries=3)
146
+ def load_keras_model(path_string: str):
147
+ from tensorflow import keras
148
+ # compile=False avoids requiring the original optimiser/loss definitions.
149
+ return keras.models.load_model(path_string, compile=False)
150
+
151
+
152
+ @st.cache_resource(show_spinner=False)
153
+ def plot_lock() -> threading.RLock:
154
+ # NNVisualiser uses CLASS-WIDE output settings; serialise plot generation.
155
+ return threading.RLock()
156
+
157
+
158
+ def layer_sizes_for_visualiser(visualiser: Any) -> list[int]:
159
+ coordinate_network = visualiser.getNetworkCoordinates()
160
+ return [len(layer.getNeuronCoordinatesList())
161
+ for layer in coordinate_network.getLayerCoordinatesList()]
162
+
163
+
164
+ def render_plot(model: Any, method_name: str, level: str,
165
+ layer: int = 0, neuron: int = 0) -> list[tuple[str, bytes]]:
166
+ """Capture only the current request's files, never shared *.png files."""
167
+ import matplotlib.pyplot as plt
168
+ from NNVisualiser import NNVisualiser
169
+ with plot_lock():
170
+ previous_figures = set(plt.get_fignums())
171
+ with TemporaryDirectory(prefix="jrnn_plots_") as directory:
172
+ old_path = NNVisualiser.getFilePath()
173
+ old_save = NNVisualiser.isSavePlots()
174
+ old_format = NNVisualiser.getFileFormat()
175
+ try:
176
+ # NNVisualiser concatenates the directory and plot title.
177
+ NNVisualiser.setFilePath(directory + os.sep)
178
+ NNVisualiser.setFileFormat("png")
179
+ NNVisualiser.setSavePlots(True)
180
+ visualiser = NNVisualiser(model)
181
+ method = getattr(visualiser, method_name)
182
+ if level == "neuron":
183
+ method(layer, neuron)
184
+ elif level == "layer":
185
+ method(layer)
186
+ else:
187
+ method()
188
+ files = sorted(Path(directory).glob("*.png"), key=lambda x: natural_key(x.name))
189
+ return [(p.name, p.read_bytes()) for p in files]
190
+ finally:
191
+ NNVisualiser.setFilePath(old_path)
192
+ NNVisualiser.setSavePlots(old_save)
193
+ NNVisualiser.setFileFormat(old_format)
194
+ for figure_number in set(plt.get_fignums()) - previous_figures:
195
+ plt.close(figure_number)
196
+
197
+
198
+ def get_model_info(model: Any) -> dict:
199
+ layers = [(layer.__class__.__name__, getattr(layer, "units", None))
200
+ for layer in model.layers]
201
+ dense_units = [int(units) for kind, units in layers if kind == "Dense" and units is not None]
202
+ return {
203
+ "parameters": int(model.count_params()),
204
+ "dense_layers": len(dense_units),
205
+ "hidden_units": sum(dense_units[:-1]) if len(dense_units) >= 2 else None,
206
+ "input_shape": str(model.input_shape),
207
+ "output_shape": str(model.output_shape),
208
+ "layer_summary": ", ".join(f"{kind}({units})" if units is not None else kind
209
+ for kind, units in layers),
210
+ }
211
+
212
+
213
+ def display_metrics(model: Any, label: str):
214
+ info = get_model_info(model)
215
+ st.markdown(f"**{label}**")
216
+ a, b = st.columns(2)
217
+ a.metric("Parameters", f"{info['parameters']:,}")
218
+ b.metric("Hidden dense units", f"{info['hidden_units']:,}" if info['hidden_units'] is not None else "Not determined")
219
+ st.caption("Input: " + info["input_shape"] + " · Output: " + info["output_shape"])
220
+ with st.expander("Layer details", expanded=False):
221
+ st.code(info["layer_summary"])
222
+ return info
223
+
224
+
225
+ def selected_dropdown(field: str, records: list[dict], state: dict) -> str:
226
+ values = choices(records, field, **state)
227
+ if not values:
228
+ st.error("There are no available models for this configuration.")
229
+ st.stop()
230
+ chosen = st.selectbox(LABELS[field], values, key="choose_" + field)
231
+ state[field] = chosen
232
+ return chosen
233
+
234
+
235
+ def main() -> None:
236
+ st.set_page_config(
237
+ page_title="JeevanRuth ANN Model Explorer",
238
+ page_icon="🔬",
239
+ layout="wide",
240
+ initial_sidebar_state="expanded",
241
+ )
242
+ st.markdown("""<style>
243
+ .block-container {max-width:1180px; padding-top:1.7rem}
244
+ h1 {letter-spacing:-.035em}
245
+ div[data-testid="stMetric"] {background:#F4F8FB;padding:12px;border-radius:10px}
246
+ @media (max-width:700px) {.block-container {padding:1rem}}
247
+ </style>""", unsafe_allow_html=True)
248
+
249
+ st.title("JeevanRuth ANN Model Explorer")
250
+ st.caption("Explore original and JRNNOA-optimised shallow neural networks using NNVisualiser and NeuralNetworkCoordinates")
251
+ st.markdown("**Research demonstration** · Single-input, single-hidden-layer ReLU models · "
252
+ "[JRNNOA publication](https://doi.org/10.1007/s41870-025-02545-6)")
253
+
254
+ with st.sidebar:
255
+ st.header("Select an experiment")
256
+ st.caption("Options are read from saved model artefacts; unavailable combinations are excluded.")
257
+ if st.button("Refresh model catalogue", help="Rescan folders after uploading model files"):
258
+ cached_catalogue.clear()
259
+ st.rerun()
260
+
261
+ records = cached_catalogue(str(ROOT))
262
+ if not records:
263
+ st.error("No saved models found in the expected seven-level directory layout.")
264
+ with st.expander("Expected model directory structure", expanded=True):
265
+ st.code("collection/initialisation/sample_size/batch_size/epochs/function/neurons/saved_model.pb\n"
266
+ "# OR: .../neurons/model.keras", language="text")
267
+ st.write("Place the original model folders in the Space repository and press **Refresh model catalogue**.")
268
+ st.stop()
269
+
270
+ experiment: dict[str, str] = {}
271
+ with st.sidebar:
272
+ # The target function is more meaningful to visitors than technical parameters.
273
+ for field in ("function", "initialisation", "sample_size", "batch_size", "epochs", "neurons"):
274
+ selected_dropdown(field, records, experiment)
275
+ variants = choices(records, "repository", **experiment)
276
+ active_repo = st.selectbox("Model collection / variant", variants, key="choose_repository")
277
+ st.caption(variant_name(active_repo))
278
+
279
+ current = matches(records, repository=active_repo, **experiment)
280
+ if len(current) != 1:
281
+ st.error("The catalogue contains an ambiguous model selection. Please rescan the repository.")
282
+ st.stop()
283
+ record = current[0]
284
+ alternatives = matches(records, **experiment)
285
+ alternatives = [r for r in alternatives if r["repository"] != active_repo]
286
+ paired = None
287
+ if alternatives:
288
+ with st.sidebar:
289
+ compare = st.checkbox("Compare a second model", value=False)
290
+ if compare:
291
+ other_repos = sorted({r["repository"] for r in alternatives}, key=natural_key)
292
+ other = st.selectbox("Second collection", other_repos)
293
+ other_rows = [r for r in alternatives if r["repository"] == other]
294
+ if len(other_rows) == 1:
295
+ paired = other_rows[0]
296
+
297
+ signature = tuple(experiment.get(k) for k in ("function", "initialisation", "sample_size", "batch_size", "epochs", "neurons")) + (active_repo, paired["repository"] if paired else None)
298
+ if st.session_state.get("active_signature") != signature:
299
+ st.session_state.active_signature = signature
300
+ st.session_state.pop("plot_result", None)
301
+ st.session_state.pop("model_export", None)
302
+ st.session_state.pop("curve_result", None)
303
+
304
+ tab_explore, tab_curve, tab_methods, tab_papers, tab_guide = st.tabs([
305
+ "Explore & visualise", "Function comparison", "Methodology", "Publications & citation", "How to use",
306
+ ])
307
+ with tab_explore:
308
+ st.subheader("1. Inspect the selected model")
309
+ st.caption("Selected catalogue entry: " + " / ".join(record[k] for k in FIELDS))
310
+ with st.spinner("Loading the selected saved model…"):
311
+ try:
312
+ model = load_keras_model(record["path"])
313
+ except Exception as exc:
314
+ st.error("Model loading failed. Verify the Keras/TensorFlow versions and saved-model format.")
315
+ st.code(str(exc))
316
+ st.stop()
317
+ main_info = display_metrics(model, active_repo)
318
+ comparison_model = None
319
+ if paired:
320
+ st.divider()
321
+ st.subheader("2. Compare the matching catalogue entry")
322
+ st.caption("Models share the directory labels for function, initialisation, sample size, batch size, epochs and initial neuron count. "
323
+ "This does not, by itself, prove before/after lineage or equal predictions.")
324
+ try:
325
+ comparison_model = load_keras_model(paired["path"])
326
+ other_info = display_metrics(comparison_model, paired["repository"])
327
+ if main_info["parameters"] > 0:
328
+ delta = 100 * (main_info["parameters"] - other_info["parameters"]) / main_info["parameters"]
329
+ st.info(f"Parameter-count difference (selected minus second): {delta:+.1f}% of the selected model. "
330
+ "This is a structural comparison, not a validated accuracy result.")
331
+ except Exception as exc:
332
+ st.error("The second saved model could not be loaded: " + str(exc))
333
+ comparison_model = None
334
+
335
+ st.divider()
336
+ st.subheader("3. Visualise internal computations")
337
+ plot_title = st.selectbox("Visualisation", list(PLOTS), index=0)
338
+ method, level = PLOTS[plot_title]
339
+ layer = 0
340
+ neuron = 0
341
+ if level in {"layer", "neuron"}:
342
+ try:
343
+ from NNVisualiser import NNVisualiser
344
+ coordinates = NNVisualiser(model)
345
+ sizes = layer_sizes_for_visualiser(coordinates)
346
+ usable_layers = [i for i, count in enumerate(sizes) if count > 0]
347
+ layer = st.selectbox("Layer index (NNVisualiser coordinates)", usable_layers)
348
+ if level == "neuron":
349
+ neuron = st.selectbox("Neuron index", list(range(sizes[layer])))
350
+ except Exception as exc:
351
+ st.error("Could not inspect NNVisualiser layer/neuronal coordinates: " + str(exc))
352
+ st.stop()
353
+
354
+ if st.button("Generate visualisation", type="primary"):
355
+ results = []
356
+ try:
357
+ with st.spinner("Generating plots from model coordinates…"):
358
+ results.append((active_repo, render_plot(model, method, level, layer, neuron)))
359
+ if comparison_model is not None:
360
+ # A layer/neuron index may be invalid after optimisation.
361
+ if level == "network":
362
+ results.append((paired["repository"], render_plot(comparison_model, method, level)))
363
+ else:
364
+ from NNVisualiser import NNVisualiser
365
+ sizes_other = layer_sizes_for_visualiser(NNVisualiser(comparison_model))
366
+ can_plot = layer < len(sizes_other) and (level != "neuron" or neuron < sizes_other[layer])
367
+ if can_plot:
368
+ results.append((paired["repository"], render_plot(comparison_model, method, level, layer, neuron)))
369
+ else:
370
+ st.warning("The chosen layer or neuron does not exist in the second model, so its plot was omitted.")
371
+ st.session_state.plot_result = (signature, method, level, layer, neuron, results)
372
+ except Exception as exc:
373
+ st.session_state.pop("plot_result", None)
374
+ st.error("NNVisualiser could not generate this plot: " + str(exc))
375
+
376
+ cached_plot = st.session_state.get("plot_result")
377
+ if cached_plot and cached_plot[:5] == (signature, method, level, layer, neuron):
378
+ output = cached_plot[5]
379
+ if not any(images for _, images in output):
380
+ st.warning("No PNG output was generated by this NNVisualiser method.")
381
+ else:
382
+ columns = st.columns(len(output))
383
+ all_images = []
384
+ for column, (repo_name, images) in zip(columns, output):
385
+ with column:
386
+ st.markdown(f"**{repo_name}**")
387
+ for i, (filename, data) in enumerate(images):
388
+ st.image(data, caption=filename, use_container_width=True)
389
+ all_images.append((f"{repo_name}/{i+1:02d}_{filename}", data))
390
+ st.download_button("Download generated plots (.zip)",
391
+ data=plot_archive(all_images), file_name="jeevanruth_visualisations.zip",
392
+ mime="application/zip")
393
+ st.divider()
394
+ with st.expander("Download saved model artefact"):
395
+ st.caption("Exports the original checkpoint. SavedModel folders are packaged as ZIP; standalone Keras files retain their format.")
396
+ if st.button("Prepare model download"):
397
+ try:
398
+ st.session_state.model_export = (signature, archive_model(Path(record["path"])))
399
+ except Exception as exc:
400
+ st.error("Unable to prepare download: " + str(exc))
401
+ export = st.session_state.get("model_export")
402
+ if export and export[0] == signature:
403
+ payload, filename, mime = export[1]
404
+ st.download_button("Download model", data=payload, file_name=filename, mime=mime)
405
+
406
+ with tab_curve:
407
+ st.subheader("Prediction curves on a specified input domain")
408
+ st.caption("Choose the original experiment's input interval. The suggested −1 to 1 range is only an illustrative default, NOT a verified domain for these checkpoints.")
409
+ with st.form("domain_form"):
410
+ xmin_col, xmax_col, n_col = st.columns(3)
411
+ xmin = xmin_col.number_input("Minimum input x", value=-1.0, format="%.4f")
412
+ xmax = xmax_col.number_input("Maximum input x", value=1.0, format="%.4f")
413
+ samples = n_col.slider("Evaluation points", 101, 1001, 301, step=100)
414
+ evaluate = st.form_submit_button("Evaluate selected checkpoint(s)", type="primary")
415
+ if evaluate:
416
+ import numpy as np
417
+ if not (np.isfinite(xmin) and np.isfinite(xmax) and xmin < xmax):
418
+ st.error("Provide a finite input range with a strictly increasing minimum and maximum.")
419
+ else:
420
+ try:
421
+ xvals = np.linspace(xmin, xmax, samples, dtype=np.float32).reshape(-1, 1)
422
+ if model.input_shape[-1] != 1 or model.output_shape[-1] != 1:
423
+ raise ValueError("The selected model does not expose a scalar input and scalar output.")
424
+ y1 = np.asarray(model.predict(xvals, verbose=0)).reshape(-1)
425
+ if len(y1) != samples or not np.all(np.isfinite(y1)):
426
+ raise ValueError("Selected model produced missing or non-finite predictions.")
427
+ y2 = None
428
+ if comparison_model is not None:
429
+ if comparison_model.input_shape[-1] != 1 or comparison_model.output_shape[-1] != 1:
430
+ raise ValueError("The second model does not expose a scalar input and scalar output.")
431
+ y2 = np.asarray(comparison_model.predict(xvals, verbose=0)).reshape(-1)
432
+ if len(y2) != samples or not np.all(np.isfinite(y2)):
433
+ raise ValueError("Second model produced missing or non-finite predictions.")
434
+ st.session_state.curve_result = (signature, float(xmin), float(xmax), int(samples), xvals.flatten(), y1, y2)
435
+ except Exception as exc:
436
+ st.session_state.pop("curve_result", None)
437
+ st.error("Unable to evaluate these models on the requested interval: " + str(exc))
438
+ result = st.session_state.get("curve_result")
439
+ if result and result[0] == signature:
440
+ import numpy as np
441
+ import pandas as pd
442
+ _, left, right, num, xvalues, y_selected, y_second = result
443
+ data = {active_repo: y_selected}
444
+ if y_second is not None:
445
+ data[paired["repository"]] = y_second
446
+ frame = pd.DataFrame(data, index=pd.Index(xvalues, name="Input x"))
447
+ st.line_chart(frame)
448
+ st.caption(f"Evaluated on [{left:.4g}, {right:.4g}] at {num} evenly spaced inputs. Curves are model predictions, not true function values.")
449
+ if y_second is not None:
450
+ disagreement = np.asarray(y_selected) - np.asarray(y_second)
451
+ one, two = st.columns(2)
452
+ one.metric("MSE between predictions", f"{np.mean(disagreement ** 2):.5g}")
453
+ two.metric("Maximum absolute difference", f"{np.max(np.abs(disagreement)):.5g}")
454
+ st.warning("These are agreement statistics between two saved models. They are NOT MSE against a reference target and do not establish that JRNNOA preserved accuracy. Confirm checkpoint lineage before attributing differences to optimisation.")
455
+ output_frame = frame.reset_index()
456
+ st.download_button("Download sampled predictions (.csv)", data=output_frame.to_csv(index=False).encode("utf-8"),
457
+ file_name="jeevanruth_function_comparison.csv", mime="text/csv")
458
+
459
+ with tab_methods:
460
+ st.subheader("What this collection demonstrates")
461
+ st.markdown("""
462
+ **Neural network family.** The original catalogue describes single-input, single-hidden-layer
463
+ ReLU networks with one scalar output (often denoted **1–N–1**), trained to approximate
464
+ one-dimensional functions on bounded intervals. Folder labels identify the training
465
+ configuration and the original neuron-count setting.
466
+
467
+ **JRNNOA.** The JeevanRuth Neural Network Optimisation Algorithm investigates removal of
468
+ inactive neurons and merging of suitable neurons in the studied single-input,
469
+ single-output ReLU architecture. This Space **loads previously saved networks**;
470
+ it does not retrain or optimise them when you choose a model.
471
+
472
+ **Interpretability tools.** NeuralNetworkCoordinates provides coordinate-level
473
+ representations of neuron and layer transformations. NNVisualiser uses these coordinates
474
+ to display input, scaling, translation, activation and flow plots.
475
+
476
+ **Interpreting comparisons.** A reduction in stored parameters is a structural
477
+ observation, not evidence of equal predictive accuracy. Before/after equivalence
478
+ should be evaluated over the same input domain with independent error tests.
479
+ """)
480
+ st.info("Actual dataset provenance, function domains, optimisation settings and evaluation metrics must be taken from the corresponding experiment records. They are not inferred from folder names.")
481
+
482
+ with tab_papers:
483
+ st.subheader("Publications and software")
484
+ st.markdown("""
485
+ **Optimisation methodology**
486
+ Caxton Emerald S and T. Vengattaraman. *Neural network optimization via inactive neuron removal and neuron merging in single input single output architectures.* International Journal of Information Technology (2025).
487
+ [DOI: 10.1007/s41870-025-02545-6](https://doi.org/10.1007/s41870-025-02545-6)
488
+
489
+ **NNVisualiser**
490
+ Caxton Emerald S and T. Vengattaraman. *NNVisualiser: An Emerging Framework for Visualising Artificial Neural Networks.* IEEE ICDSAAI (2025).
491
+ [DOI: 10.1109/ICDSAAI65575.2025.11011573](https://doi.org/10.1109/ICDSAAI65575.2025.11011573)
492
+
493
+ **NeuralNetworkCoordinates**
494
+ Caxton Emerald S and T. Vengattaraman. *Neural Network Coordinates: A Python Package for Studying Artificial Neural Networks.* Springer proceedings (2026).
495
+ [DOI: 10.1007/978-981-95-0626-2_15](https://doi.org/10.1007/978-981-95-0626-2_15)
496
+
497
+ **Software:** [NNVisualiser on PyPI](https://pypi.org/project/NNVisualiser/) ·
498
+ [NeuralNetworkCoordinates on PyPI](https://pypi.org/project/NeuralNetworkCoordinates/) ·
499
+ [GitHub](https://github.com/CaxtonEmerald-S) ·
500
+ [Researcher ORCID](https://orcid.org/0000-0002-7763-3987)
501
+ """)
502
+ with st.expander("Citation (BibTeX)"):
503
+ st.code("""@article{EmeraldVengattaraman2025JRNNOA,
504
+ author = {Caxton Emerald S and T. Vengattaraman},
505
+ title = {Neural network optimization via inactive neuron removal and neuron merging in single input single output architectures},
506
+ journal = {International Journal of Information Technology},
507
+ year = {2025},
508
+ doi = {10.1007/s41870-025-02545-6}
509
+ }""", language="bibtex")
510
+
511
+ with tab_guide:
512
+ st.subheader("How to explore the model collection")
513
+ st.markdown("""
514
+ 1. Choose a **target function**, then the experimental training settings in the sidebar.
515
+ 2. Select an available **model collection**; a catalogue entry exists only if a saved checkpoint is present.
516
+ 3. Inspect the architecture and parameter count. Where another collection has the same
517
+ experiment labels, enable **Compare a second model**.
518
+ 4. Choose a network, layer or neuron visualisation and select **Generate visualisation**.
519
+ 5. Download plots or the original model checkpoint for further analysis.
520
+
521
+ **Important:** The initial neuron-count folder name need not equal the final number of
522
+ active neurons after JRNNOA. Use the model's reported architecture for the latter.
523
+
524
+ **Reproducibility:** For formal comparisons, report the target function and domain,
525
+ initialisation, sample size, batch size, epochs, original and optimised checkpoint
526
+ identifiers, the tested input grid and an error metric (e.g. MSE and maximum absolute
527
+ difference). This app does not compute fidelity metrics without those experimental inputs.
528
+ """)
529
+ st.caption("For research and educational use. Review the repository's licence before redistribution or commercial use.")
530
+
531
+
532
+ if __name__ == "__main__":
533
+ main()