s0duku's picture
Add sample reading examples
8c9d5ac verified
|
Raw History Blame Contribute Delete
2.76 kB
# 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.