File size: 7,905 Bytes
6921c45
 
3630fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97a30ea
 
 
 
6921c45
3630fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97a30ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3630fe5
 
 
 
 
 
 
97a30ea
 
 
3630fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
---
license: cc-by-4.0
pretty_name: Code4ML Annotated Subset with SonarQube and MLScent Ratings
language:
  - en
tags:
  - code
  - python
  - machine-learning
  - code4ml
  - kaggle
  - code-quality
  - static-analysis
  - sonarqube
task_categories:
  - text-generation
  - text-classification
size_categories:
  - 1K<n<10K
configs:
  - config_name: default
    data_files:
      - split: train
        path: code4ml_annotated.csv
  - config_name: training
    data_files:
      - split: train
        path: code4ml_annotated_training.csv
---

# Code4ML Annotated Subset with SonarQube and MLScent Ratings

The 7,947 manually annotated Python snippets from the **Code4ML** corpus
([Zenodo record 7312803](https://zenodo.org/records/7312803)), joined with their task-class
names and annotated with structural code-quality ratings from **SonarQube** and **MLScent**.

Code4ML contains 2,599,355 code cells from 23,103 Kaggle notebooks. Only this annotated
subset carries a human-assigned task label, so it is the subset usable for building
instruction–code pairs. This repository holds that subset in one file. Each snippet was scanned on its own, so
the ratings describe the snippet, not its whole notebook.

## Dataset summary

| | |
|---|---|
| Records | 7,947 |
| Source file | Code4ML `markup_data.csv`, labels from `vertices.csv` |
| Task classes | 12 classes, 70 subclasses |
| Format | single comma-delimited CSV with header |
| Language | Python |

## Columns

| column | type | description |
|---|---|---|
| `id` | int | row index from Code4ML `markup_data.csv` |
| `code_block` | string | the Python snippet (one notebook cell, may span multiple lines) |
| `too_long` | string | `Yes`/`No` flag from the Code4ML annotators |
| `marks` | int | annotator confidence, 1 (lowest) to 5 (highest) |
| `graph_vertex_id` | int | task-label id from Code4ML `vertices.csv` |
| `graph_vertex_class` | string | task class, e.g. `Data_Transform`, `Model_Train` |
| `graph_vertex_subclass` | string | task subclass, e.g. `choose_model_class` |
| `kernel_id` | int or empty | Kaggle notebook id, recovered by matching `code_block` text against Code4ML `code_blocks.csv`; empty when the text appears in more than one notebook or none |
| `sqale_rating` | float | SonarQube SQALE rating (1.0 = A … 5.0 = E) |
| `maintainability_rating` | string | SonarQube maintainability grade `A`–`E` |
| `code_smells` | int | SonarQube code smells |
| `complexity` | int | SonarQube cyclomatic complexity |
| `cognitive_complexity` | int | SonarQube cognitive complexity |
| `bugs` | int | SonarQube bugs |
| `sqale_index` | int | SonarQube technical debt (minutes) |
| `ncloc` | int or empty | non-comment lines of code; empty when SonarQube did not analyse the snippet |
| `mlscent_analysis_status` | string | `ok`, `no_ml_import` (no ML library import in the snippet or its recovered notebook imports), or `invalid_python` |
| `mlscent_smell_count` | int | number of MLScent findings |
| `mlscent_unique_smell_count` | int | number of distinct MLScent smell types |
| `mlscent_findings_json` | JSON string | all MLScent findings |
| `mlscent_ml_rating` | string | `A`–`E` from ML-specific findings only, by severity; `N/A` when MLScent did not analyse the snippet |
| `mlscent_ml_rating_basis` | string | findings and severities behind `mlscent_ml_rating` |
| `mlscent_ml_findings_json` | JSON string | ML-specific findings used for the rating |
| `mlscent_ignored_findings_json` | JSON string | finding types not used for the rating |
| `context_line_count` | int | lines of recovered notebook ML imports prepended for MLScent analysis |
| `combined_rating` | string | worse of `maintainability_rating` and `mlscent_ml_rating`; `N/A` when either is missing |

## Statistics

Task classes:

| `graph_vertex_class` | Snippets |
|---|---:|
| Data_Transform | 2,387 |
| Debug | 1,446 |
| Visualization | 862 |
| Model_Train | 816 |
| Data_Extraction | 517 |
| Environment | 441 |
| Other | 372 |
| Model_Evaluation | 354 |
| Data_Export | 350 |
| EDA | 231 |
| Hyperparam_Tuning | 160 |
| Model_Interpretation | 11 |

Annotator confidence (`marks`):

| 1 | 2 | 3 | 4 | 5 |
|---:|---:|---:|---:|---:|
| 44 | 210 | 903 | 1,419 | 5,371 |

Other properties:

| Property | Value |
|---|---|
| `too_long` = `Yes` | 1,360 |
| Duplicate `code_block` values | 108 |
| Lines per snippet (median / p75 / max) | 2 / 6 / 582 |

Ratings:

| Rating | `maintainability_rating` | `mlscent_ml_rating` | `combined_rating` |
|---|---:|---:|---:|
| A | 7,816 | 3,045 | 2,996 |
| B | 84 | 952 | 962 |
| C | 40 | 56 | 89 |
| D | 7 | 111 | 117 |
| E | 0 | 8 | 8 |
| N/A | — | 3,775 | 3,775 |

| Rating property | Value |
|---|---|
| `kernel_id` recovered | 4,373 |
| `ncloc` empty (not analysed by SonarQube) | 1,003 |
| `mlscent_analysis_status` = `ok` / `no_ml_import` / `invalid_python` | 4,172 / 3,551 / 224 |

No rows were removed or modified; duplicates and short snippets are kept as in the source.

## How the ratings were produced

1. **SonarQube** Community Edition, default Python profile, through a FastAPI wrapper around
   `sonar-scanner` (`/scan-batch`).
2. **MLScent** (`ml-code-smell-detector==0.1.2`), with recovered notebook ML imports
   prepended as context, through a local rating service (`/rate-batch`).
3. The data was split into two halves, each rated by its own SonarQube + MLScent instance
   pair, then merged back by `id`.

## Training file

`code4ml_annotated_training.csv` holds 3,063 instruction–code pairs built from
`code4ml_annotated.csv` for fine-tuning:

| Step | Rule | Rows dropped |
|---|---|---:|
| 1 | Drop task classes without ML logic: `Debug`, `Environment`, `Other` | 2,259 |
| 2 | Drop `combined_rating` = N/A | 2,502 |
| 2 | Drop `combined_rating` = D or E (quality floor) | 121 |
| 3 | Drop duplicate `code_block` | 0 |
| 4 | Turn the subclass label into an instruction, e.g. `train_model` → "Write Python code to train the model." | — |
| 5 | Drop snippets whose 8-grams overlap a DS-1000 reference solution by ≥ 50% | 2 |
| 6 | Split 80/10/10 per task class with seed 42 | — |

| Column | Description |
|---|---|
| `id` | row `id` in `code4ml_annotated.csv` |
| `split` | `train` (2,451), `validation` (308), or `test` (304) |
| `graph_vertex_class`, `graph_vertex_subclass` | Code4ML task label |
| `instruction` | instruction sentence built from the subclass label |
| `code` | the snippet |
| `combined_rating` | `A` (2,122), `B` (868), or `C` (73) |

## Usage

```python
from datasets import load_dataset

ds = load_dataset("dimsmauls/code4ml-annotated-rated", split="train")
print(ds[0]["graph_vertex_class"], ds[0]["combined_rating"], ds[0]["code_block"])

train = load_dataset("dimsmauls/code4ml-annotated-rated", "training", split="train")
train_set = train.filter(lambda row: row["split"] == "train")
```

## Provenance & attribution

- **Source corpus:** Code4ML, [Zenodo record 7312803](https://zenodo.org/records/7312803),
  released under CC-BY-4.0.
- **Original code:** the snippets are Kaggle notebook cells, each published under the
  **Apache-2.0** license. See `NOTICE.txt`.
- **This derivative** joins `markup_data.csv` with the class names from `vertices.csv` and adds
  SonarQube and MLScent rating columns. No code was changed.

## Citation

Please cite the original Code4ML work:

```bibtex
@article{drozdova2023code4ml,
  title={Code4ML: a Large-scale Dataset of Annotated Machine Learning Code},
  author={Drozdova, Anastasia and Trofimova, Ekaterina and Guseva, Polina and Scherbakova, Anna and Ustyuzhanin, Andrey},
  journal={PeerJ Computer Science},
  year={2023},
  doi={10.7717/peerj-cs.1230}
}
```

## License

Released under [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/), the same license
as Code4ML. The underlying `code_block` snippets are Kaggle notebook code under
**Apache-2.0**; that attribution is preserved in `NOTICE.txt`.