Skip to content

Commit 7a4568c

Browse files
au650680au650680
andauthored
Fix clustering and model update (#35)
- Fixes model update function block - Updates model tracking function block - Removes duplication of MQTT clients in different function blocks. The design of distributed scenario is simplified to have fewer lines of glue code. - Adds spectral density function - Configuration formats updated and separated for different function blocks - Separates model from package source code - Updates MQTT record and replay code. A sample recording of five minutes of data is added. - Adds type hints to some functions --------- Co-authored-by: au650680 <au650680@uni.au.dk>
1 parent 53df587 commit 7a4568c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+21882
-92346
lines changed

USERGUIDE.md

Lines changed: 241 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,202 @@
22

33
This guide outlines configuration and execution of the _example-shm_ application. It provides detailed information on the configuration parameters, command-line options, and execution procedures.
44

5-
## Install
5+
There are two ways to run the package, either directly from the repository code as a developer or as a build package. The latter is described further down in this file in section 2.
66

7-
You can install the built package using pip:
7+
8+
## 1. Run package with developer setup.
9+
10+
## Step 1.1 Initiate virtual enviroment with poetry
11+
12+
```bash
13+
python -m venv .venv
14+
.\.venv\Scripts\Activate.ps1 # On Windows
15+
source .venv/bin/activate # On Linux
16+
17+
pip install poetry #Specifically install poetry to your system
18+
# If you have poetry installed globally
19+
poetry env activate # shows the command to activate venv
20+
poetry install # installs all required python packages
21+
```
22+
## Step 1.2 Setup configuration file
23+
config/ consist of a set of standard configuration files.
24+
`production.json.template` For general digital twin purposes
25+
`replay.json.template` For recording and replaying data
26+
`replay_production.json.template` For running function blocks with replayed data
27+
28+
Copy the needed config files and remove `.template` to use them.
29+
30+
Add MQTT connection information, credentials and topics.
31+
For production purposes the MQTT topics structure follows the pattern:
32+
33+
```txt
34+
cpsens/DAQ_ID/MODULE_ID/CH_ID/PHYSICS/ANALYSIS/DATA_TYPE
35+
```
36+
Where DATA_TYPE could be metadata or plain data.
37+
38+
39+
## Step 1.3 Change settings
40+
There are some settings that can be changed for the specific use case.
41+
42+
* **Sample time**
43+
This is how many samples should be used for sysid. The value uses the time in minutes and the sample frequency, fs,
44+
to calculate the correct ammount of samples.
45+
46+
Inside `examples/run_sysid.py`, `examples/run_mode_clustering.py`, `examples/run_mode_tracking.py`, `examples/run_model_update.py`
47+
the sample time can be changed: `number_of_minutes`.
48+
49+
* **Parameters**
50+
Inside `method/constans.py` the parameters for system identification, mode clustering, mode tracking and model updating
51+
can be changed.
52+
53+
* **Model**
54+
A digital YAFEM model can be added to `models/<your_model>`.
55+
Inside `method/constans.py` the model paramters can be set together with the paths to the model folder and the model function file.
56+
57+
58+
## Step 1.4 Run examples
59+
The following experiments can be run using the package.
60+
61+
* **acceleration_readings** demonstrates the use of `Accelerometer` class to extract
62+
accelerometer measurements from MQTT data stream.
63+
* **aligning_readings** demonstrates the use of `Aligner` class to collect and
64+
align accelerometer measurements from multiple MQTT data streams.
65+
66+
* **sysid** demonstrates the use of `sysid` with four cases:
67+
1. **sysid-and-plot**: plots natural frequencies.
68+
2. **sysid-and-print**: prints sysid output to console.
69+
3. **sysid-and-publish**: publishes one set of sysid output via MQTT to the config given under [sysid] config.
70+
4. **live-sysid-and-publish**: Continuously publishes sysid output via MQTT to the config given under [sysid] config.
71+
72+
* **Clustering** demonstrates the use of `clustering` with three cases:
73+
1. **clustering-with-local-sysid**: gets the sysid output by runing sysid
74+
locally, then runs the mode clustering.
75+
2. **clustering-with-remote-sysid**: gets sysid output by subscribing,
76+
then runs the mode clustering. This is a one time operation.
77+
3. **live-clustering-with-remote-sysid**: gets sysid output by subscribing,
78+
then runs the mode clustering. This operation runs in loop.
79+
4. **live-clustering-with-remote-sysid-and-publish**: gets sysid output by subscribing,
80+
then runs the mode clustering. The cluster results are published. This operation runs in loop.
81+
82+
* **mode-tracking** demonstrates the use of `mode_tracking` with three cases:
83+
1. **mode-tracking-with-local-sysid**: gets the sysid output by runing sysid
84+
locally, then runs mode clustering and mode tracking.
85+
2. **mode-tracking-with-remote-sysid**: gets sysid output by subscribing,
86+
then runs mode clustering and mode tracking. This is a one time operation.
87+
3. **live-mode-tracking-with-remote-sysid**: gets sysid output by subscribing,
88+
then runs mode clustering and mode tracking. This operation runs in loop.
89+
90+
* **model-update** demonstrates the use of `model_update` with two cases:
91+
1. **model-update-local-sysid**: gets the sysid output, then uses it to
92+
run update model and get updated system parameters.
93+
2. **live-model-update-with-remote-sysid**: gets the sysid output by subscribing to
94+
MQTT topic, then runs mode clustering to run update model and get updated system parameters.
95+
3. **live-model-update-with-remote-clustering**: gets the mode clustering output by subscribing to
96+
MQTT topic, then uses the mode clustering output to run update model and get updated system parameters.
897

998
```bash
10-
$pip install dist/example_shm-<version>-py3-none-any.whl
99+
python .\src\examples\example.py align-readings # run an experiment with real data (Needs "production.json" Config)
11100
```
12101

13-
Replace `<version>` with the actual version number (e.g., `0.5.0`).
102+
To run the examples with specified config, use
14103

15-
## Create Configuration
104+
```bash
105+
python .\src\examples\example.py --config .path_to\production.json align-readings
106+
```
107+
108+
Example,
109+
110+
```bash
111+
python .\src\examples\example.py --config .\config\production.json align-readings
112+
```
113+
114+
115+
116+
## 2. Install the Package from Poetry Build
117+
118+
To install the package built using `poetry`, follow these steps:
119+
120+
### Step 2.1: Build the Package
121+
122+
```bash
123+
poetry build
124+
```
125+
126+
This will create a `.whl` file in the `dist/` directory,
127+
e.g., `dist/cp_sens-0.6.0-py3-none-any.whl`.
128+
129+
### Step 2.2: Create and Activate a Virtual Environment
130+
131+
```py
132+
python -m venv .venv
133+
source .venv/bin/activate # On Linux/macOS
134+
.\.venv\Scripts\Activate.ps1 # On Windows
135+
```
136+
137+
### Step 2.3: Install the Built Package
138+
139+
```py
140+
pip install example_shm-<version>-py3-none-any.whl
141+
```
142+
143+
Replace `<version>` with the version number found in the `.whl`
144+
filename. (e.g `0.6.0`).
145+
146+
## Step 2.4: Create Configuration
16147

17148
The package requires a json configuration file and access to MQTT broker.
18149
The format of configuration file is,
19150

20151
```json
21152
{
22-
"MQTT": {
153+
"sysid": {
23154
"host": "",
24-
"port": ,
155+
"port": 0,
25156
"userId": "",
26157
"password": "",
27158
"ClientID": "NOT_NEEDED",
28159
"QoS": 1,
160+
"MetadataToSubscribe":["sensors/1/acc/raw/metadata"],
29161
"TopicsToSubscribe": [
30162
"sensors/1/acc/raw/data",
31-
"sensors/1/acc/raw/metadata",
32163
"sensors/2/acc/raw/data",
33164
"sensors/3/acc/raw/data",
34165
"sensors/4/acc/raw/data"
35-
]
166+
],
167+
"TopicsToPublish": ["sensors/1/acc/sysid/data"]
36168
},
37169

38-
"sysID": {
39-
"host": "",
40-
"port": ,
41-
"userId": "",
42-
"password": "",
43-
"ClientID": "NOT_NEEDED",
44-
"QoS": 2,
45-
"TopicsToSubscribe": ["sensors/1/acc/sysid/data"]
46-
},
47-
48170
"mode_cluster": {
49171
"host": "",
50-
"port": ,
172+
"port": 0,
51173
"userId": "",
52174
"password": "",
53175
"ClientID": "NOT_NEEDED",
54176
"QoS": 2,
55-
"TopicsToSubscribe": ["sensors/1/acc/mode_cluster/data"]
177+
"TopicsToSubscribe": ["sensors/1/acc/sysid/data"],
178+
"TopicsToPublish": ["sensors/1/acc/mode_cluster/data"]
56179
},
57180

58181
"model_update": {
59182
"host": "",
60-
"port": ,
183+
"port": 0,
61184
"userId": "",
62185
"password": "",
63186
"ClientID": "NOT_NEEDED",
64187
"QoS": 2,
65-
"TopicsToSubscribe": ["sensors/1/acc/model_update/data"]
188+
"TopicsToSubscribe": ["sensors/1/acc/mode_cluster/data"],
189+
"TopicsToPublish": ["sensors/1/acc/model_update/data"]
66190
}
67191
}
68192
```
193+
Where the MQTT topic structure follows the pattern:
194+
`PROJECT/CH_ID/PHYSICS/ANALYSIS/DATA_TYPE`
195+
Where `DATA_TYPE` could be metadata or plain data.
69196

70197
The file needs to be saved. The application looks for configuration in
71198
`config/production.json`.
72199

73-
## Use
200+
## Step 2.5: Use
74201

75202
Launch the bridge with the default configuration:
76203

@@ -86,38 +213,6 @@ using
86213
example-shm --config <config-file>
87214
```
88215

89-
The following experiments can be run using
90-
the package.
91-
92-
### Run Experiments
93-
94-
The following experiments can be run using
95-
the package.
96-
97-
* **acceleration_readings** demonstrates the extraction of
98-
accelerometer measurements from MQTT data stream.
99-
* **aligning_readings** collects and aligns the accelerometer measurements from multiple MQTT data streams.
100-
101-
* **sysid** demonstrates the use of `sys_id` with four cases:
102-
1. **sysid-and-plot**: plots natural frequencies.
103-
1. **sysid-and-print**: prints SysID results to console.
104-
1. **sysid-and-publish**: publishes one set of SysID results via MQTT to the config given under [sysid] config.
105-
1. **live-sysid-and-publish**: Continuously publishes SysID results via MQTT to the config given under [sysid] config.
106-
107-
* **mode-tracking** demonstrates the use of `mode_track` with three cases:
108-
1. **mode-tracking-with-local-sysid**: gets the pyOMA results by runing sysid
109-
locally, then runs the mode track.
110-
1. **mode-tracking-with-remote-sysid**: gets pyOMA results by subscribing,
111-
then runs the mode track. This is a one time operation.
112-
1. **live-mode-tracking-with-remote-sysid**: gets pyOMA results by subscribing,
113-
then runs the mode track. This operation runs in loop.
114-
115-
* **model-update** demonstrates the use of `model_update` with two cases:
116-
1. **model-update-local-sysid**: gets the mode track output, then uses it to
117-
run update model and get updated system parameters.
118-
1. **live-model-update-remote-sysid**: gets the mode track output by subscribing to
119-
MQTT topic, then uses the mode track output to run update model and get updated system parameters.
120-
121216
You can find the available experiments by running the program
122217

123218
```bash
@@ -131,15 +226,18 @@ Options:
131226
Commands:
132227
accelerometers
133228
align-readings
229+
align-readings-plot
134230
clustering-with-local-sysid
135231
clustering-with-remote-sysid
136232
live-clustering-with-remote-sysid
233+
live-clustering-with-remote-sysid-and-publish
137234
live-mode-tracking-with-remote-sysid
138-
live-model-update-remote-sysid
235+
live-model-update-with-remote-clustering
236+
live-model-update-with-remote-sysid
139237
live-sysid-publish
140238
mode-tracking-with-local-sysid
141239
mode-tracking-with-remote-sysid
142-
model-update-local-sysid
240+
model-update-with-local-sysid
143241
sysid-and-plot
144242
sysid-and-print
145243
sysid-and-publish
@@ -158,3 +256,88 @@ To run the examples with a custom config, use:
158256
$example-shm
159257
Usage: example-shm --config <config-file> accelerometers
160258
```
259+
260+
261+
262+
263+
264+
265+
266+
267+
## Distributed Setup Overview
268+
269+
This explains the setup needed to run the distributed version of the example-shm pipeline.
270+
271+
## Machine 1: Edge Layer – Raspberry Pi with Accelerometers
272+
273+
This machine connects to ADXL375 sensors and is responsible for acquiring raw sensor data.
274+
It performs calibration and continuously publishes sensor data over MQTT.
275+
276+
**Step 1**: Run calibration to find sensor offsets
277+
278+
```bash
279+
poetry run python src/scripts/find_offset.py
280+
```
281+
282+
**Step 2**: Start publishing raw accelerometer data
283+
284+
```bash
285+
poetry run python src/scripts/publish_samples.py
286+
```
287+
288+
**Record and replay**
289+
Record and replay data is also possible. Here a `config/replay.json` config file must be defined before hand.
290+
291+
Inside `record/record.py` some parameters must be specified:
292+
293+
```py
294+
config_path = "config/replay.json"
295+
config = load_config(config_path)
296+
MQTT_CONFIG = config["MQTT"]
297+
298+
RECORDINGS_DIR = "record/mqtt_recordings"
299+
FILE_NAME = "recording2.jsonl"
300+
301+
DURATION_SECONDS = 20 # This is how many seconds of data that are recorded
302+
```
303+
304+
The same goes for `record/replay.py`:
305+
306+
```py
307+
# MQTT Configuration
308+
CONFIG_PATH = "config/replay.json"
309+
310+
RECORDINGS_DIR = "record/mqtt_recordings"
311+
FILE_NAME = "recording.jsonl"
312+
313+
REPLAY_SPEED = 0.1 # Multiplier for replay speed
314+
```
315+
At the bottom the number of times to loop the replay function can be stated: `replay_mqtt_messages(loop=10) # Times to loop`
316+
317+
The files can then be run with:
318+
319+
```bash
320+
poetry run python record/record.py
321+
poetry run python record/replay.py
322+
```
323+
324+
## Machine 2: Fog Layer – Data Alignment and System Identification
325+
326+
This machine subscribes to MQTT topics from Machine 1. It aligns multi-channel data, runs system identification,
327+
and publishes the pyOMA output.
328+
329+
Run the aligner and system identification pipeline
330+
331+
```bash
332+
poetry run python src/examples/example.py sysid-and-publish
333+
```
334+
335+
## Machine 3: Cloud Layer – Mode Tracking and Model Update
336+
337+
This machine subscribes to the pyOMA output, performs mode clustering and updates the structural model.
338+
339+
Run mode clustering and model update
340+
341+
```bash
342+
poetry run python src/examples/example.py model-update-with-remote-sysid
343+
```

0 commit comments

Comments
 (0)