File size: 4,340 Bytes
55dff4e
 
 
a344a1a
55dff4e
 
 
 
 
 
23a91b1
 
 
 
2b035e4
d2cb6f7
4ed4f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b035e4
4ed4f59
 
 
 
 
de5560e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fa3289
de5560e
2b035e4
 
 
 
 
 
de5560e
2b035e4
 
 
 
 
1ab9161
 
 
1ce491e
2b035e4
1ce491e
1ab9161
1ce491e
1ab9161
1ce491e
1ab9161
2b035e4
1ab9161
2b035e4
1ab9161
2b035e4
1ab9161
2b035e4
1ab9161
2b035e4
1ab9161
2b035e4
1ce491e
2b035e4
1ce491e
2b035e4
1ce491e
2b035e4
1ce491e
973e469
 
 
 
4446ed7
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
---
task_categories:
- summarization
- text-generation
language:
- en
tags:
- code
size_categories:
- 10K<n<100K
---

# Overview

This dataset contains 25,000+ rows of code-docstring-ast data along with additional metadata. Data was gathered from various Python libraries and frameworks and their
publicly available GitHub repos. This dataset was created for the purpose of training the [CodeT5+](https://arxiv.org/abs/2305.07922) transformer on AST-enhanced code-to-doc tasks.

# Sources

The dataset was gathered from various GitHub repos sampled from [this repo by Vinta.](https://github.com/vinta/awesome-python)

The 26 repos are:
- matplotlib
- pytorch
- cryptography
- django
- prospector
- scikit-learn
- pandas
- numpy
- uvicorn
- feincms
- algorithms
- scrapy
- authlib
- seaborn
- coconut
- tensorflow
- flexx
- salmon
- mongo-python-driver
- virtualenv
- sphinx
- schema
- kornia
- scipy
- cherrypy
- pygame

Sampling was at random; I simply browsed through each category from Vinta's list and chose one from a random interesting category. 

# Dataset Instance

An instance of the dataset is as follows:

```
{
  <library> : <The library from which the source code came from>,
  <name> : <The name of the function/class/method>,
  <source_code> : <The raw source code itself stripped of its docstrings and comments>,
  <docstring> : <The corresponding docstring of the code>,
  <type> : <Whether it's a function, method, or class>,
  <file_path> : <The relative path of the file containing the function>,
  <ast_sequence> : <The ast sequence of the raw source code. Scroll down for more info about this>
}
```

# The AST Sequence

A function recursively converts the AST tree into a linear sequence. It uses depth markers (β”œ1>, β””2>, etc.) to show parent-child relationships. It also adds node identifiers by pairing
each node type with a meaningful identifier. Furthermore, pruning is also applied to irrelevant and shallow identifiers to denoise the dataset. 

Here's an example of how the AST sequence is generated:

Example Code
```
def calculate_area(radius):
    """
    Calculate the area of a circle.
    
    Parameters:
        radius (float): The radius of the circle
        
    Returns:
        float: The area of the circle
    """
    PI = 3.14159
    area = PI * radius * radius
    return area
```

Resulting AST Sequence
```
FunctionDef:calculate_area
β”œ1> args:[radius]
β”œ1> Assign:PI
β”‚ β””2> Constant:
β”œ1> Assign:area
β”‚ β””2> BinOp:
β”‚   β”œ3> BinOp:
β”‚   β”‚ β”œ4> Name:PI
β”‚   β”‚ β””4> Name:radius
β”‚   β””3> Name:radius
β””1> Return:
  β””2> Name:area
```

1. The code is parsed via Python's `ast` module
2. A method traverses this tree and linearizes the sequence
3. Each node is then converted to a string with type-identifier keys
4. Structural relationships are preserved using the depth markers
5. Denoising of irrelevant and shallow nodes are applied

# Preprocessing

The dataset generally follows [CodeBERT's code2nl](https://github.com/microsoft/CodeBERT/tree/master/CodeBERT/code2nl) dataset cleaning standards which are as follows:

- Removed comments from the code
- Removed examples where code cannot be parsed into an AST
- Remove examples that codes cannot be parsed into an abstract syntax tree.
- Remove examples where the number of tokens of documents is < 3 or >256
- Remove examples that documents contain special tokens (e.g. <img ...> or https:...)
- Remove examples that documents are not English

Furthermore, the following cleaning steps specific to this dataset were applied:
- Docstrings from source codes was stripped
- Standard text cleaning procedures (normalized whitespaces and special characters)
- Removed abnormally long AST sequences (>100)
- Structural pruning of the AST sequence (removing irrelevant or shallow identifiers/nodes)

# Final Statistics

## Final Count : 25,489

## Average Docstring Length : 51

## Average Source Code Length : 591

## Average AST Sequence Length : 72

## Type Distribution

**Methods: 13,149(52.1%)**

**Functions: 8,671(34.3%)**

**Classes: 3,444(13.6%)**

## Major Contributors

**TensorFlow (5,455)**

**PyTorch (5,448)**

**Matplotlib (2,190)**

**Django (2,138)**

**Scipy (1,715)**

# NOTE

This dataset is *imperfect*. Due to the varying degrees of code complexity, some fields are inaccurate representations.