File size: 2,757 Bytes
8c9d5ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Reading sample Parquet files

Each `samples/train-xxxxx-of-00064.parquet` file contains:

```text
sample_id       string
repo_id         string
revision_id     string
kind            dependency | target_context
text            large_string
files           list<struct<path, relation, content_policy, role, language, bytes, part, parts>>
token_count     int32
metadata_json   large_string containing a JSON object
```

Use streaming for full-dataset inspection. The sample files are independent
Parquet files, so the examples work with a single shard or `samples/*.parquet`.

## Command-line inspector

```bash
cd stackgo
PYTHONPATH=. python examples/read_samples.py ../samples/train-*.parquet --limit 3
```

Filter one repository or one sample kind:

```bash
PYTHONPATH=. python examples/read_samples.py ../samples \
  --repo owner/project --kind dependency --limit 5
```

Print full text and JSONL records:

```bash
PYTHONPATH=. python examples/read_samples.py ../samples \
  --text-chars 0 --json
```

## PyArrow streaming

```python
import json
import pyarrow.dataset as ds

dataset = ds.dataset("samples", format="parquet")
scanner = dataset.scanner(
    columns=["sample_id", "repo_id", "revision_id", "kind", "text", "files",
             "token_count", "metadata_json"],
    filter=(ds.field("kind") == "dependency") & (ds.field("repo_id") == "owner/project"),
    batch_size=512,
)
for batch in scanner.to_batches():
    for row in batch.to_pylist():
        print(row["sample_id"], row["token_count"])
        print([item["path"] for item in row["files"]])
        print(json.loads(row["metadata_json"]))
        print(row["text"][:500])
```

## Hugging Face datasets

For a local directory or a dataset repository containing `samples/*.parquet`,
use the Parquet loader with streaming enabled so the full dataset is not loaded
into memory:

```python
from datasets import load_dataset

samples = load_dataset(
    "parquet",
    data_files="samples/*.parquet",
    split="train",
    streaming=True,
)
for row in samples.take(3):
    print(row["repo_id"], row["kind"], row["token_count"])
    print(row["text"][:500])
```

If the files are hosted under a dataset repository, point `data_files` at the
repository-relative glob:

```python
samples = load_dataset(
    "parquet",
    data_files="hf://datasets/s0duku/the-stack-go/samples/*.parquet",
    split="train",
    streaming=True,
)
```

## File provenance

`files` is the authoritative list of files included in the sample. Use
`relation` to distinguish the target from dependencies/repository context and
`content_policy` to distinguish full content from metadata-only entries.
`metadata_json` contains sample-type-specific fields such as the dependency
component or target context paths.