CPSBench 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cpsbench-0.1.0/LICENSE +21 -0
- cpsbench-0.1.0/PKG-INFO +110 -0
- cpsbench-0.1.0/README.md +84 -0
- cpsbench-0.1.0/pyproject.toml +40 -0
- cpsbench-0.1.0/setup.cfg +4 -0
- cpsbench-0.1.0/setup.py +3 -0
- cpsbench-0.1.0/src/CPSBench.egg-info/PKG-INFO +110 -0
- cpsbench-0.1.0/src/CPSBench.egg-info/SOURCES.txt +19 -0
- cpsbench-0.1.0/src/CPSBench.egg-info/dependency_links.txt +1 -0
- cpsbench-0.1.0/src/CPSBench.egg-info/requires.txt +9 -0
- cpsbench-0.1.0/src/CPSBench.egg-info/top_level.txt +1 -0
- cpsbench-0.1.0/src/cpsbench/__init__.py +76 -0
- cpsbench-0.1.0/src/cpsbench/__main__.py +38 -0
- cpsbench-0.1.0/src/cpsbench/datasets.py +130 -0
- cpsbench-0.1.0/src/cpsbench/download.py +160 -0
- cpsbench-0.1.0/src/cpsbench/preprocess.py +158 -0
- cpsbench-0.1.0/src/cpsbench/py.typed +1 -0
- cpsbench-0.1.0/src/cpsbench/specs.py +179 -0
- cpsbench-0.1.0/src/cpsbench/v2x.py +142 -0
- cpsbench-0.1.0/src/cpsbench/windows.py +111 -0
- cpsbench-0.1.0/tests/test_api.py +69 -0
cpsbench-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Md Hasan Shahriar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cpsbench-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: CPSBench
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cyber-physical security datasets that download, preprocess, and load like MNIST.
|
|
5
|
+
Author: Md Hasan Shahriar
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shahriar0651/CPSBench
|
|
8
|
+
Keywords: dataset,cps,can,ids,vehicular,cybersecurity,pytorch
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: numpy>=1.23
|
|
18
|
+
Requires-Dist: pandas>=2.0
|
|
19
|
+
Requires-Dist: pyarrow>=14
|
|
20
|
+
Requires-Dist: joblib>=1.3
|
|
21
|
+
Requires-Dist: torch>=2.0
|
|
22
|
+
Requires-Dist: tqdm>=4.60
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# CPSBench
|
|
28
|
+
|
|
29
|
+
Installable loader for cyber-physical security datasets. It downloads the raw traces, builds the windowed tensors, and returns a PyTorch dataset with the same contract as MNIST: `(window, label)`.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from cpsbench import SynCAN
|
|
33
|
+
from torch.utils.data import DataLoader
|
|
34
|
+
|
|
35
|
+
train = SynCAN(root="./data", split="train", download=True)
|
|
36
|
+
test = SynCAN(root="./data", split="test", download=True)
|
|
37
|
+
|
|
38
|
+
window, label = train[0] # window: (1, time, signals), label: 0 benign / 1 attack
|
|
39
|
+
loader = DataLoader(train, batch_size=64, shuffle=True)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or load by name:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import cpsbench
|
|
46
|
+
|
|
47
|
+
dataset = cpsbench.load("road", root="./data", split="test", download=True)
|
|
48
|
+
print(dataset.input_shape) # (channels, window, signals)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install cpsbench
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
That works after the package is published on PyPI. Until then, install this repository directly:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install "git+https://github.com/shahriar0651/CPSBench.git"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
From a local clone, for development:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install -e .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Python 3.10+. SynCAN also needs `git` on `PATH`. ROAD is fetched from Zenodo with the standard library, so `wget` is not required.
|
|
70
|
+
|
|
71
|
+
## Datasets
|
|
72
|
+
|
|
73
|
+
| Name | Status | What you get |
|
|
74
|
+
| --- | --- | --- |
|
|
75
|
+
| `syncan` | ready, auto-download | Synthetic CAN intrusion traces |
|
|
76
|
+
| `road` | ready, auto-download | ROAD dynamometer CAN traces |
|
|
77
|
+
| `misbehaviorx` | loader ready, manual files | V2X misbehavior (also accepted as `vasp`) |
|
|
78
|
+
| `x-canids` | registered, not implemented | Raises a clear error until a loader is added |
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
python -m cpsbench list
|
|
82
|
+
python -m cpsbench info syncan
|
|
83
|
+
python -m cpsbench download syncan --root ./data --split train
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Downloaded files land in `<root>/<name>/{ambient,attacks}` plus a fitted min/max scaler under `<root>/<name>/scaler`. Later calls reuse those files.
|
|
87
|
+
|
|
88
|
+
## Overrides
|
|
89
|
+
|
|
90
|
+
Windowing defaults live in the library so a new project does not need the old Hydra YAML. Override them per call:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from cpsbench import ROAD
|
|
94
|
+
|
|
95
|
+
dataset = ROAD(root="./data", split="train", download=True, window_size=50, step_size=5)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Pass `return_meta=True` if you also need the source file and row index: `(window, label, {"file", "idx"})`.
|
|
99
|
+
|
|
100
|
+
## Layout
|
|
101
|
+
|
|
102
|
+
Each sample is a min-max scaled window with a channel axis, so the same convolutional IDS can run on every dataset. Shape is always `(channels, window_size, num_signals)`. Label `0` is benign and `1` is attack (any attack flag inside the window).
|
|
103
|
+
|
|
104
|
+
## Adding a dataset
|
|
105
|
+
|
|
106
|
+
1. Add a `DatasetSpec` in `src/cpsbench/specs.py`.
|
|
107
|
+
2. Add a downloader in `src/cpsbench/download.py` if the files can be fetched automatically.
|
|
108
|
+
3. Register the class in `src/cpsbench/datasets.py` and `_CLASSES` in `__init__.py`.
|
|
109
|
+
|
|
110
|
+
The IDS experiments that consume this package live in the sibling [RobIDS](https://github.com/shahriar0651/robids) repo.
|
cpsbench-0.1.0/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# CPSBench
|
|
2
|
+
|
|
3
|
+
Installable loader for cyber-physical security datasets. It downloads the raw traces, builds the windowed tensors, and returns a PyTorch dataset with the same contract as MNIST: `(window, label)`.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from cpsbench import SynCAN
|
|
7
|
+
from torch.utils.data import DataLoader
|
|
8
|
+
|
|
9
|
+
train = SynCAN(root="./data", split="train", download=True)
|
|
10
|
+
test = SynCAN(root="./data", split="test", download=True)
|
|
11
|
+
|
|
12
|
+
window, label = train[0] # window: (1, time, signals), label: 0 benign / 1 attack
|
|
13
|
+
loader = DataLoader(train, batch_size=64, shuffle=True)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Or load by name:
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import cpsbench
|
|
20
|
+
|
|
21
|
+
dataset = cpsbench.load("road", root="./data", split="test", download=True)
|
|
22
|
+
print(dataset.input_shape) # (channels, window, signals)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install cpsbench
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
That works after the package is published on PyPI. Until then, install this repository directly:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install "git+https://github.com/shahriar0651/CPSBench.git"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
From a local clone, for development:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install -e .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Python 3.10+. SynCAN also needs `git` on `PATH`. ROAD is fetched from Zenodo with the standard library, so `wget` is not required.
|
|
44
|
+
|
|
45
|
+
## Datasets
|
|
46
|
+
|
|
47
|
+
| Name | Status | What you get |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `syncan` | ready, auto-download | Synthetic CAN intrusion traces |
|
|
50
|
+
| `road` | ready, auto-download | ROAD dynamometer CAN traces |
|
|
51
|
+
| `misbehaviorx` | loader ready, manual files | V2X misbehavior (also accepted as `vasp`) |
|
|
52
|
+
| `x-canids` | registered, not implemented | Raises a clear error until a loader is added |
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
python -m cpsbench list
|
|
56
|
+
python -m cpsbench info syncan
|
|
57
|
+
python -m cpsbench download syncan --root ./data --split train
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Downloaded files land in `<root>/<name>/{ambient,attacks}` plus a fitted min/max scaler under `<root>/<name>/scaler`. Later calls reuse those files.
|
|
61
|
+
|
|
62
|
+
## Overrides
|
|
63
|
+
|
|
64
|
+
Windowing defaults live in the library so a new project does not need the old Hydra YAML. Override them per call:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from cpsbench import ROAD
|
|
68
|
+
|
|
69
|
+
dataset = ROAD(root="./data", split="train", download=True, window_size=50, step_size=5)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Pass `return_meta=True` if you also need the source file and row index: `(window, label, {"file", "idx"})`.
|
|
73
|
+
|
|
74
|
+
## Layout
|
|
75
|
+
|
|
76
|
+
Each sample is a min-max scaled window with a channel axis, so the same convolutional IDS can run on every dataset. Shape is always `(channels, window_size, num_signals)`. Label `0` is benign and `1` is attack (any attack flag inside the window).
|
|
77
|
+
|
|
78
|
+
## Adding a dataset
|
|
79
|
+
|
|
80
|
+
1. Add a `DatasetSpec` in `src/cpsbench/specs.py`.
|
|
81
|
+
2. Add a downloader in `src/cpsbench/download.py` if the files can be fetched automatically.
|
|
82
|
+
3. Register the class in `src/cpsbench/datasets.py` and `_CLASSES` in `__init__.py`.
|
|
83
|
+
|
|
84
|
+
The IDS experiments that consume this package live in the sibling [RobIDS](https://github.com/shahriar0651/robids) repo.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "CPSBench"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Cyber-physical security datasets that download, preprocess, and load like MNIST."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Md Hasan Shahriar" }]
|
|
13
|
+
keywords = ["dataset", "cps", "can", "ids", "vehicular", "cybersecurity", "pytorch"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"numpy>=1.23",
|
|
23
|
+
"pandas>=2.0",
|
|
24
|
+
"pyarrow>=14",
|
|
25
|
+
"joblib>=1.3",
|
|
26
|
+
"torch>=2.0",
|
|
27
|
+
"tqdm>=4.60",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = ["pytest>=7"]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/shahriar0651/CPSBench"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
cpsbench = ["py.typed"]
|
cpsbench-0.1.0/setup.cfg
ADDED
cpsbench-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: CPSBench
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cyber-physical security datasets that download, preprocess, and load like MNIST.
|
|
5
|
+
Author: Md Hasan Shahriar
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shahriar0651/CPSBench
|
|
8
|
+
Keywords: dataset,cps,can,ids,vehicular,cybersecurity,pytorch
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: numpy>=1.23
|
|
18
|
+
Requires-Dist: pandas>=2.0
|
|
19
|
+
Requires-Dist: pyarrow>=14
|
|
20
|
+
Requires-Dist: joblib>=1.3
|
|
21
|
+
Requires-Dist: torch>=2.0
|
|
22
|
+
Requires-Dist: tqdm>=4.60
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# CPSBench
|
|
28
|
+
|
|
29
|
+
Installable loader for cyber-physical security datasets. It downloads the raw traces, builds the windowed tensors, and returns a PyTorch dataset with the same contract as MNIST: `(window, label)`.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from cpsbench import SynCAN
|
|
33
|
+
from torch.utils.data import DataLoader
|
|
34
|
+
|
|
35
|
+
train = SynCAN(root="./data", split="train", download=True)
|
|
36
|
+
test = SynCAN(root="./data", split="test", download=True)
|
|
37
|
+
|
|
38
|
+
window, label = train[0] # window: (1, time, signals), label: 0 benign / 1 attack
|
|
39
|
+
loader = DataLoader(train, batch_size=64, shuffle=True)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or load by name:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import cpsbench
|
|
46
|
+
|
|
47
|
+
dataset = cpsbench.load("road", root="./data", split="test", download=True)
|
|
48
|
+
print(dataset.input_shape) # (channels, window, signals)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install cpsbench
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
That works after the package is published on PyPI. Until then, install this repository directly:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install "git+https://github.com/shahriar0651/CPSBench.git"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
From a local clone, for development:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install -e .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Python 3.10+. SynCAN also needs `git` on `PATH`. ROAD is fetched from Zenodo with the standard library, so `wget` is not required.
|
|
70
|
+
|
|
71
|
+
## Datasets
|
|
72
|
+
|
|
73
|
+
| Name | Status | What you get |
|
|
74
|
+
| --- | --- | --- |
|
|
75
|
+
| `syncan` | ready, auto-download | Synthetic CAN intrusion traces |
|
|
76
|
+
| `road` | ready, auto-download | ROAD dynamometer CAN traces |
|
|
77
|
+
| `misbehaviorx` | loader ready, manual files | V2X misbehavior (also accepted as `vasp`) |
|
|
78
|
+
| `x-canids` | registered, not implemented | Raises a clear error until a loader is added |
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
python -m cpsbench list
|
|
82
|
+
python -m cpsbench info syncan
|
|
83
|
+
python -m cpsbench download syncan --root ./data --split train
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Downloaded files land in `<root>/<name>/{ambient,attacks}` plus a fitted min/max scaler under `<root>/<name>/scaler`. Later calls reuse those files.
|
|
87
|
+
|
|
88
|
+
## Overrides
|
|
89
|
+
|
|
90
|
+
Windowing defaults live in the library so a new project does not need the old Hydra YAML. Override them per call:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from cpsbench import ROAD
|
|
94
|
+
|
|
95
|
+
dataset = ROAD(root="./data", split="train", download=True, window_size=50, step_size=5)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Pass `return_meta=True` if you also need the source file and row index: `(window, label, {"file", "idx"})`.
|
|
99
|
+
|
|
100
|
+
## Layout
|
|
101
|
+
|
|
102
|
+
Each sample is a min-max scaled window with a channel axis, so the same convolutional IDS can run on every dataset. Shape is always `(channels, window_size, num_signals)`. Label `0` is benign and `1` is attack (any attack flag inside the window).
|
|
103
|
+
|
|
104
|
+
## Adding a dataset
|
|
105
|
+
|
|
106
|
+
1. Add a `DatasetSpec` in `src/cpsbench/specs.py`.
|
|
107
|
+
2. Add a downloader in `src/cpsbench/download.py` if the files can be fetched automatically.
|
|
108
|
+
3. Register the class in `src/cpsbench/datasets.py` and `_CLASSES` in `__init__.py`.
|
|
109
|
+
|
|
110
|
+
The IDS experiments that consume this package live in the sibling [RobIDS](https://github.com/shahriar0651/robids) repo.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
src/CPSBench.egg-info/PKG-INFO
|
|
6
|
+
src/CPSBench.egg-info/SOURCES.txt
|
|
7
|
+
src/CPSBench.egg-info/dependency_links.txt
|
|
8
|
+
src/CPSBench.egg-info/requires.txt
|
|
9
|
+
src/CPSBench.egg-info/top_level.txt
|
|
10
|
+
src/cpsbench/__init__.py
|
|
11
|
+
src/cpsbench/__main__.py
|
|
12
|
+
src/cpsbench/datasets.py
|
|
13
|
+
src/cpsbench/download.py
|
|
14
|
+
src/cpsbench/preprocess.py
|
|
15
|
+
src/cpsbench/py.typed
|
|
16
|
+
src/cpsbench/specs.py
|
|
17
|
+
src/cpsbench/v2x.py
|
|
18
|
+
src/cpsbench/windows.py
|
|
19
|
+
tests/test_api.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cpsbench
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""CPSBench datasets, loaded like MNIST.
|
|
2
|
+
|
|
3
|
+
Example::
|
|
4
|
+
|
|
5
|
+
from cpsbench import SynCAN
|
|
6
|
+
from torch.utils.data import DataLoader
|
|
7
|
+
|
|
8
|
+
train = SynCAN(root="./data", split="train", download=True)
|
|
9
|
+
window, label = train[0]
|
|
10
|
+
loader = DataLoader(train, batch_size=64, shuffle=True)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Optional
|
|
16
|
+
|
|
17
|
+
from .datasets import MisbehaviorX, ROAD, SynCAN, VehicularDataset
|
|
18
|
+
from .specs import DatasetSpec, get_spec, list_specs
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"SynCAN",
|
|
22
|
+
"ROAD",
|
|
23
|
+
"MisbehaviorX",
|
|
24
|
+
"VehicularDataset",
|
|
25
|
+
"load",
|
|
26
|
+
"list_datasets",
|
|
27
|
+
"describe",
|
|
28
|
+
"get_spec",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
__version__ = "0.1.0"
|
|
32
|
+
|
|
33
|
+
_CLASSES = {
|
|
34
|
+
"syncan": SynCAN,
|
|
35
|
+
"road": ROAD,
|
|
36
|
+
"misbehaviorx": MisbehaviorX,
|
|
37
|
+
"vasp": MisbehaviorX,
|
|
38
|
+
"veremi": MisbehaviorX,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def list_datasets(status: Optional[str] = None) -> list[dict]:
|
|
43
|
+
"""Return name, status, family, and shape for every registered dataset."""
|
|
44
|
+
rows = []
|
|
45
|
+
for spec in list_specs(status):
|
|
46
|
+
rows.append(
|
|
47
|
+
{
|
|
48
|
+
"name": spec.name,
|
|
49
|
+
"status": spec.status,
|
|
50
|
+
"family": spec.family,
|
|
51
|
+
"input_shape": spec.input_shape if spec.features else None,
|
|
52
|
+
"downloadable": spec.downloadable,
|
|
53
|
+
"description": spec.description,
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
return rows
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def describe(name: str) -> DatasetSpec:
|
|
60
|
+
return get_spec(name)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def load(name: str, **kwargs) -> VehicularDataset:
|
|
64
|
+
"""Load a dataset by name.
|
|
65
|
+
|
|
66
|
+
``kwargs`` are forwarded to the dataset class (``root``, ``split``,
|
|
67
|
+
``download``, ``window_size``, ...).
|
|
68
|
+
"""
|
|
69
|
+
key = name.strip().lower()
|
|
70
|
+
if key == "x-canids":
|
|
71
|
+
spec = get_spec(key)
|
|
72
|
+
raise NotImplementedError(f"{spec.name} is not implemented yet. {spec.notes}")
|
|
73
|
+
if key not in _CLASSES:
|
|
74
|
+
known = ", ".join(sorted(set(_CLASSES) | {"x-canids"}))
|
|
75
|
+
raise KeyError(f"Unknown dataset '{name}'. Known datasets: {known}")
|
|
76
|
+
return _CLASSES[key](**kwargs)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Small command-line helper: ``python -m cpsbench list``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
|
|
7
|
+
from . import describe, list_datasets, load
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main() -> None:
|
|
11
|
+
parser = argparse.ArgumentParser(description="Download and inspect vehicular IDS datasets.")
|
|
12
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
13
|
+
|
|
14
|
+
sub.add_parser("list", help="List registered datasets")
|
|
15
|
+
|
|
16
|
+
info = sub.add_parser("info", help="Show one dataset specification")
|
|
17
|
+
info.add_argument("name")
|
|
18
|
+
|
|
19
|
+
fetch = sub.add_parser("download", help="Download and preprocess a split")
|
|
20
|
+
fetch.add_argument("name")
|
|
21
|
+
fetch.add_argument("--root", default="./data")
|
|
22
|
+
fetch.add_argument("--split", default="train", choices=["train", "test"])
|
|
23
|
+
|
|
24
|
+
args = parser.parse_args()
|
|
25
|
+
if args.command == "list":
|
|
26
|
+
for row in list_datasets():
|
|
27
|
+
shape = row["input_shape"] or "n/a"
|
|
28
|
+
print(f"{row['name']:16} {row['status']:8} {row['family']:4} {shape}")
|
|
29
|
+
elif args.command == "info":
|
|
30
|
+
spec = describe(args.name)
|
|
31
|
+
print(spec)
|
|
32
|
+
else:
|
|
33
|
+
dataset = load(args.name, root=args.root, split=args.split, download=True, verbose=True)
|
|
34
|
+
print(f"Loaded {len(dataset)} windows with shape {dataset.input_shape}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
if __name__ == "__main__":
|
|
38
|
+
main()
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Public dataset classes.
|
|
2
|
+
|
|
3
|
+
Usage matches torchvision:
|
|
4
|
+
|
|
5
|
+
from cpsbench import SynCAN
|
|
6
|
+
|
|
7
|
+
train = SynCAN(root="./data", split="train", download=True)
|
|
8
|
+
window, label = train[0]
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
from torch.utils.data import Dataset
|
|
17
|
+
|
|
18
|
+
from .download import dataset_root, ensure_downloaded, split_dir
|
|
19
|
+
from .preprocess import prepare_can_split
|
|
20
|
+
from .specs import DatasetSpec, get_spec
|
|
21
|
+
from .v2x import V2XWindowDataset, prepare_v2x_split
|
|
22
|
+
from .windows import WindowDataset
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class VehicularDataset(Dataset):
|
|
26
|
+
"""Base class for datasets that download, preprocess, and yield windows."""
|
|
27
|
+
|
|
28
|
+
spec_name: str = ""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
root: str | Path = "./data",
|
|
33
|
+
split: str = "train",
|
|
34
|
+
download: bool = True,
|
|
35
|
+
transform=None,
|
|
36
|
+
target_transform=None,
|
|
37
|
+
window_size: Optional[int] = None,
|
|
38
|
+
step_size: Optional[int] = None,
|
|
39
|
+
sampling_period: Optional[int] = None,
|
|
40
|
+
return_meta: bool = False,
|
|
41
|
+
data_dir: Optional[str | Path] = None,
|
|
42
|
+
scaler_dir: Optional[str | Path] = None,
|
|
43
|
+
n_jobs: Optional[int] = None,
|
|
44
|
+
verbose: bool = False,
|
|
45
|
+
) -> None:
|
|
46
|
+
spec = get_spec(self.spec_name).with_overrides(window_size, step_size, sampling_period)
|
|
47
|
+
if spec.status == "planned":
|
|
48
|
+
raise NotImplementedError(f"{spec.name} is not implemented yet. {spec.notes}")
|
|
49
|
+
|
|
50
|
+
self.spec = spec
|
|
51
|
+
self.root = Path(root).expanduser().resolve()
|
|
52
|
+
self.split = split
|
|
53
|
+
self.transform = transform
|
|
54
|
+
self.target_transform = target_transform
|
|
55
|
+
self.return_meta = return_meta
|
|
56
|
+
|
|
57
|
+
if data_dir is None and download:
|
|
58
|
+
ensure_downloaded(spec, self.root)
|
|
59
|
+
|
|
60
|
+
self.data_dir = Path(data_dir) if data_dir is not None else split_dir(self.root, spec.name, split)
|
|
61
|
+
if scaler_dir is not None:
|
|
62
|
+
self.scaler_path = Path(scaler_dir) / f"min_max_values_{spec.name}.csv"
|
|
63
|
+
else:
|
|
64
|
+
self.scaler_path = dataset_root(self.root, spec.name) / "scaler" / f"min_max_values_{spec.name}.csv"
|
|
65
|
+
|
|
66
|
+
fit_scaler = split.strip().lower() in {"train", "training", "ambient"}
|
|
67
|
+
if spec.family == "can":
|
|
68
|
+
prepare_can_split(spec, self.data_dir, self.scaler_path, fit_scaler, n_jobs=n_jobs)
|
|
69
|
+
self._base = WindowDataset(spec, self.data_dir, self.scaler_path, return_meta, verbose)
|
|
70
|
+
elif spec.family == "v2x":
|
|
71
|
+
prepare_v2x_split(spec, self.data_dir, self.scaler_path, fit_scaler)
|
|
72
|
+
self._base = V2XWindowDataset(spec, self.data_dir, self.scaler_path, return_meta, verbose)
|
|
73
|
+
else:
|
|
74
|
+
raise ValueError(f"Unsupported dataset family: {spec.family}")
|
|
75
|
+
|
|
76
|
+
def __len__(self) -> int:
|
|
77
|
+
return len(self._base)
|
|
78
|
+
|
|
79
|
+
def __getitem__(self, idx: int):
|
|
80
|
+
item = self._base[idx]
|
|
81
|
+
window, label = item[0], item[1]
|
|
82
|
+
if self.transform is not None:
|
|
83
|
+
window = self.transform(window)
|
|
84
|
+
if self.target_transform is not None:
|
|
85
|
+
label = self.target_transform(label)
|
|
86
|
+
if self.return_meta:
|
|
87
|
+
return window, label, item[2]
|
|
88
|
+
return window, label
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def classes(self) -> tuple[str, ...]:
|
|
92
|
+
return self.spec.classes
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def input_shape(self) -> tuple[int, int, int]:
|
|
96
|
+
return self.spec.input_shape
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def num_signals(self) -> int:
|
|
100
|
+
return self.spec.num_signals
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def window_size(self) -> int:
|
|
104
|
+
return self.spec.window_size
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def channels(self) -> int:
|
|
108
|
+
return self.spec.channels
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def features(self) -> tuple[str, ...]:
|
|
112
|
+
return self.spec.features
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class SynCAN(VehicularDataset):
|
|
116
|
+
"""SynCAN intrusion dataset. ``split`` is ``'train'`` or ``'test'``."""
|
|
117
|
+
|
|
118
|
+
spec_name = "syncan"
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class ROAD(VehicularDataset):
|
|
122
|
+
"""ROAD CAN intrusion dataset. ``split`` is ``'train'`` or ``'test'``."""
|
|
123
|
+
|
|
124
|
+
spec_name = "road"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class MisbehaviorX(VehicularDataset):
|
|
128
|
+
"""V2X misbehavior dataset. Requires a local copy; download is manual."""
|
|
129
|
+
|
|
130
|
+
spec_name = "misbehaviorx"
|