# mbelotti-nuk/pk-neuralnet architecture

> Architecture diagram of the GitHub repository mbelotti-nuk/pk-neuralnet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mbelotti-nuk/pk-neuralnet
- Repository: https://github.com/mbelotti-nuk/pk-neuralnet

## Overview

1. **What this repo is**
   - `PK-neuralnet` is a Python package and command-line toolchain for a domain-specific neural network workflow: build a dataset, train a model, run predictions, and export a trained model into NACARTE/SpaceClaim-compatible form.
   - The package code lives under `pknn/`, with a small companion reader package under `nacarte/`.

2. **Top-level workflow**
   - The architecture is a linear pipeline: raw input files and resource tables are converted into a structured database, that database feeds training, the trained model is used for prediction, and a separate export path serializes the model for NACARTE.
   - The same YAML configuration style is used to drive each command, so configuration is a major boundary between user intent and implementation.

3. **CLI entry points**
   - The README exposes four user-facing commands: `databasepknn`, `trainpknn`, `predictpknn`, and `savepknn`.
   - These map to the main application stages and are the primary integration surface for users and automation scripts.

4. **Database creation subsystem**
   - `pknn/build_database.py` and `pknn/inp_process/` handle ingestion of raw inputs.
   - `binaryreader.py` and `database.py` suggest the data is parsed from binary/domain-specific formats into an internal tabular or file-backed database.
   - The examples include raw data folders, a generated `database/` folder, and resource files such as `massAttCoeff.xml` and conversion tables, so this stage is responsible for domain preprocessing and feature assembly.

5. **Training subsystem**
   - `pknn/train.py` is the training entry point.
   - `pknn/net/` contains the model and training machinery: `pk_nn.py` for the neural network, `train_functions.py` for optimization/training logic, `datamanager.py` for batching/data loading, and `pytorchtools.py` for training utilities such as checkpointing or early stopping.
   - Artifacts produced here include PyTorch model files (`.pt`), checkpoints, and a saved scaler object (`Scaler.pickle`), which indicates training persists both the network and preprocessing state.

6. **Prediction subsystem**
   - `pknn/predict.py` is the inference entry point.
   - Prediction reuses the trained model and the stored preprocessing artifacts, so the inference path depends on the same data normalization and model definition used during training.
   - The example layout includes a dedicated `prediction_example/` config and a `model_example/` directory, reinforcing that prediction is a separate operational mode rather than part of training.

7. **Export subsystem**
   - `pknn/export/` and `pknn/exportmodel.py` implement serialization to NACARTE-facing formats.
   - `exportsd.py` and `importsd.py` imply bidirectional translation between the package’s model representation and SpaceClaim/NACARTE artifacts.
   - This is the main integration boundary with external CAD/engineering tooling.

8. **NACARTE integration boundary**
   - The `nacarte/` package, especially `out_reader.py`, appears to read NACARTE outputs back into Python.
   - This suggests the repo supports not only exporting a model to NACARTE but also consuming downstream results for validation or post-processing.

9. **Shared utilities and presentation**
   - `pknn/functionalities/` contains cross-cutting helpers: `config.py` for configuration handling and `graphics.py` for plotting/visualization.
   - These are not core ML components, but they support the user experience around configuration, inspection, and result presentation.

10. **Data and artifact formats**
   - The project is heavily file-based: YAML configs, binary/raw inputs, generated database directories, text metadata files, XML resources, `.pt` model/checkpoint files, and pickle-based scaler state.
   - That means the architecture is centered on explicit on-disk artifacts rather than a long-running service or database server.

11. **Documentation and examples as architecture references**
   - The `docs/` and `make_docs/` trees are extensive and include generated API docs plus example walkthroughs for database creation, training, prediction, and export.
   - The `examples/` directory provides end-to-end sample inputs and outputs, which is useful for understanding the intended flow and module boundaries.

12. **Testing boundary**
   - `tests/test_reader.py` and `tests/testfiles/` indicate at least the input-reading layer is unit-tested.
   - This aligns with the repo’s highest-risk boundary: parsing domain-specific files into a reliable internal dataset.

Main entry points: [`pknn/train.py`](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/train.py) (Train), [`pknn/predict.py`](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/predict.py) (Inference), [`pknn/functionalities/config.py`](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/functionalities/config.py) (Config), [`pknn/inp_process/binaryreader.py`](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/inp_process/binaryreader.py) (Binary reader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI pipeline"]
  node_pknn_build_database["Build DB<br/>entrypoint<br/>[build_database.py]"]
  node_pknn_train["Train<br/>entrypoint<br/>[train.py]"]
  node_pknn_predict["Predict<br/>entrypoint<br/>[predict.py]"]
  node_pknn_exportmodel["Save model<br/>entrypoint<br/>[exportmodel.py]"]
end

subgraph group_ingest["Data prep"]
  node_binaryreader["Binary reader<br/>parser<br/>[binaryreader.py]"]
  node_database[("Database<br/>storage<br/>[database.py]")]
end

subgraph group_train["Training"]
  node_datamanager["Data manager<br/>data-loader<br/>[datamanager.py]"]
  node_pk_nn{{"NN model<br/>[pk_nn.py]"}}
  node_train_functions["Train logic<br/>optimizer<br/>[train_functions.py]"]
  node_pytorchtools["Torch utils<br/>training-utils<br/>[pytorchtools.py]"]
end

subgraph group_infer["Prediction"]
  node_pknn_predict_node["Inference<br/>runtime<br/>[predict.py]"]
end

subgraph group_export["Export"]
  node_exportsd["Export SD<br/>serializer<br/>[exportsd.py]"]
  node_importsd["Import SD<br/>adapter<br/>[importsd.py]"]
end

subgraph group_integration["NACARTE boundary"]
  node_nacarte_out_reader["Out reader<br/>[out_reader.py]"]
end

subgraph group_support["Shared support"]
  node_config["Config<br/>shared<br/>[config.py]"]
  node_graphics["Graphics<br/>visualization<br/>[graphics.py]"]
  node_test_reader["Reader test<br/>[test_reader.py]"]
end

node_pknn_build_database -->|"loads"| node_config
node_pknn_build_database -->|"uses"| node_binaryreader
node_binaryreader -->|"writes"| node_database
node_database -->|"feeds"| node_pknn_train
node_pknn_train -->|"loads"| node_config
node_pknn_train -->|"uses"| node_datamanager
node_datamanager -->|"feeds"| node_pk_nn
node_pknn_train -->|"runs"| node_train_functions
node_train_functions -->|"optimizes"| node_pk_nn
node_train_functions -->|"uses"| node_pytorchtools
node_pknn_train -->|"produces"| node_pknn_predict_node
node_pknn_predict_node -->|"loads"| node_config
node_pknn_predict_node -->|"loads"| node_pk_nn
node_pknn_predict_node -->|"reuses"| node_datamanager
node_pknn_exportmodel -->|"loads"| node_config
node_pknn_exportmodel -->|"serializes"| node_exportsd
node_exportsd -->|"round-trip"| node_importsd
node_importsd -->|"consumed by"| node_nacarte_out_reader
node_pknn_predict_node -->|"reports"| node_graphics
node_binaryreader -->|"covered by"| node_test_reader

click node_pknn_build_database "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/build_database.py"
click node_pknn_train "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/train.py"
click node_pknn_predict "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/predict.py"
click node_pknn_exportmodel "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/exportmodel.py"
click node_binaryreader "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/inp_process/binaryreader.py"
click node_database "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/inp_process/database.py"
click node_config "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/functionalities/config.py"
click node_datamanager "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/datamanager.py"
click node_pk_nn "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/pk_nn.py"
click node_train_functions "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/train_functions.py"
click node_pytorchtools "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/pytorchtools.py"
click node_pknn_predict_node "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/predict.py"
click node_exportsd "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/export/exportsd.py"
click node_importsd "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/export/importsd.py"
click node_nacarte_out_reader "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/nacarte/out_reader.py"
click node_graphics "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/functionalities/graphics.py"
click node_test_reader "https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/tests/test_reader.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pknn_build_database,node_pknn_train,node_pknn_predict,node_pknn_exportmodel,node_config,node_graphics,node_test_reader toneBlue
class node_binaryreader,node_database toneAmber
class node_datamanager,node_pk_nn,node_train_functions,node_pytorchtools toneMint
class node_pknn_predict_node toneRose
class node_exportsd,node_importsd toneIndigo
class node_nacarte_out_reader toneTeal
```

## Components

### CLI pipeline

User-facing commands that drive the main workflow.

- [Build DB](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/build_database.py): entrypoint · Database creation command that ingests raw inputs and resources. · `pknn/build_database.py`
- [Train](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/train.py): entrypoint · Training command for fitting the neural network. · `pknn/train.py`
- [Predict](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/predict.py): entrypoint · Inference command for running predictions from a saved model. · `pknn/predict.py`
- [Save model](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/exportmodel.py): entrypoint · Export command that packages a trained model for NACARTE use. · `pknn/exportmodel.py`

### Data prep

Builds the structured database from raw domain files and resources.

- [Binary reader](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/inp_process/binaryreader.py): parser · Parses domain-specific raw/binary inputs into internal records. · `pknn/inp_process/binaryreader.py`
- [Database](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/inp_process/database.py): storage · Internal database assembly and file-backed dataset structure. · `pknn/inp_process/database.py`

### Training

Model definition, batching, optimization, and training artifacts.

- [Data manager](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/datamanager.py): data-loader · Training data assembly, batching, and scaling hooks. · `pknn/net/datamanager.py`
- [NN model](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/pk_nn.py): model · Core neural network architecture. · `pknn/net/pk_nn.py`
- [Train logic](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/train_functions.py): optimizer · Training loop, loss handling, and optimization behavior. · `pknn/net/train_functions.py`
- [Torch utils](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/net/pytorchtools.py): training-utils · Checkpointing and other training support utilities. · `pknn/net/pytorchtools.py`

### Prediction

Inference path that reuses trained model and preprocessing state.

- [Inference](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/predict.py): runtime · Prediction execution that loads the trained network and scaler. · `pknn/predict.py`

### Export

Serialization into NACARTE / SpaceClaim-facing artifacts.

- [Export SD](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/export/exportsd.py): serializer · Serializes model state into SpaceClaim/NACARTE-compatible form. · `pknn/export/exportsd.py`
- [Import SD](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/export/importsd.py): adapter · Reads back SpaceClaim/NACARTE-side artifacts into Python. · `pknn/export/importsd.py`

### NACARTE boundary

Reader for downstream NACARTE outputs and round-trip validation.

- [Out reader](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/nacarte/out_reader.py): reader · Reads downstream NACARTE outputs for validation or post-processing. · `nacarte/out_reader.py`

### Shared support

Cross-cutting configuration, visualization, and test coverage.

- [Config](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/functionalities/config.py): shared · YAML configuration handling used by all major commands. · `pknn/functionalities/config.py`
- [Graphics](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/pknn/functionalities/graphics.py): visualization · Plotting and result presentation helpers. · `pknn/functionalities/graphics.py`
- [Reader test](https://github.com/mbelotti-nuk/pk-neuralnet/blob/main/tests/test_reader.py): test · Unit coverage for the parsing boundary. · `tests/test_reader.py`

## Connections

- Build DB → Config: loads · Database generation is driven by YAML configuration.
- Build DB → Binary reader: uses · Entry point invokes raw input parsing.
- Binary reader → Database: writes · Parsed inputs are assembled into the internal database.
- Database → Train: feeds · Generated database becomes training input.
- Train → Config: loads · Training uses the same configuration boundary.
- Train → Data manager: uses · Training prepares batches and scaled inputs.
- Data manager → NN model: feeds · Managed data drives the neural network.
- Train → Train logic: runs · Optimization loop and training control.
- Train logic → NN model: optimizes · Training logic updates the model.
- Train logic → Torch utils: uses · Checkpointing / early-stop utilities support training.
- Train → Inference: produces · Training artifacts are consumed by prediction.
- Inference → Config: loads · Prediction is also configuration-driven.
- Inference → NN model: loads · Inference reuses the trained model definition.
- Inference → Data manager: reuses · Prediction depends on the same preprocessing state.
- Save model → Config: loads · Export is configured through the shared YAML style.
- Save model → Export SD: serializes · Model export hands off to NACARTE serialization.
- Export SD → Import SD: round-trip · Export/import modules represent the external format boundary.
- Import SD → Out reader: consumed by · NACARTE outputs can be read back into Python.
- Inference → Graphics: reports · Prediction results can be visualized.
- Binary reader → Reader test: covered by · Parsing boundary is unit-tested.
