{ "cells": [ { "cell_type": "markdown", "id": "d0a6c6af", "metadata": {}, "source": [ "# Project-Scoped Dataset Splits Tutorial\n", "\n", "This notebook shows how to use the project-scoped split API in the Datamint Python client. It covers:\n", "\n", "- Listing project split assignments with `api.projects.get_splits()`\n", "- Assigning resources to a split with `api.projects.assign_splits()`\n", "- Inspecting one resource with `api.projects.get_resource_split()`\n", "- Loading a project-backed dataset and using `dataset.split()` with the new project-split mode\n", "\n", "For project-backed datasets, `dataset.split()` now prefers project-scoped assignments when you call it without ratios or explicit mode flags." ] }, { "cell_type": "markdown", "id": "0d6fcd9b", "metadata": {}, "source": [ "## Setup\n", "\n", "Update `datamint` first if needed, then configure your API key." ] }, { "cell_type": "code", "execution_count": null, "id": "c895412f", "metadata": {}, "outputs": [], "source": [ "%pip install -U datamint --quiet" ] }, { "cell_type": "code", "execution_count": null, "id": "57d4d6f7", "metadata": {}, "outputs": [], "source": [ "from datamint import Api\n", "\n", "PROJECT_NAME = \"FracAtlas\"\n", "\n", "api = Api()\n", "project = api.projects.get_by_name(PROJECT_NAME)\n", "if project is None:\n", " raise ValueError(f\"Project '{PROJECT_NAME}' was not found.\")\n", "\n", "project" ] }, { "cell_type": "markdown", "id": "defa40fc", "metadata": {}, "source": [ "## Fetch The Project Resources\n", "\n", "Project-scoped split assignments operate on project membership, so start by fetching the resources that belong to the project." ] }, { "cell_type": "code", "execution_count": null, "id": "2d5611ba", "metadata": {}, "outputs": [], "source": [ "resources = list(project.fetch_resources())\n", "print(f\"Loaded {len(resources)} project resources\")\n", "resources[:3]" ] }, { "cell_type": "markdown", "id": "b8c19409", "metadata": {}, "source": [ "## Assign Project Splits\n", "\n", "This example builds a deterministic `train` / `val` / `test` assignment from the project resource IDs, then writes those assignments back through `api.projects.assign_splits()`.\n", "\n", "Re-running the cell is safe for the listed resources because the split name is reassigned for the IDs you send." ] }, { "cell_type": "code", "execution_count": null, "id": "c2a8b56f", "metadata": {}, "outputs": [], "source": [ "from random import Random\n", "\n", "\n", "def partition_ids(resource_ids: list[str], ratios: dict[str, float], seed: int = 42) -> dict[str, list[str]]:\n", " total = sum(ratios.values())\n", " if abs(total - 1.0) > 1e-6:\n", " raise ValueError(f\"Split ratios must sum to 1.0, got {total}\")\n", "\n", " shuffled_ids = list(resource_ids)\n", " Random(seed).shuffle(shuffled_ids)\n", "\n", " assignments: dict[str, list[str]] = {}\n", " start = 0\n", " items = list(ratios.items())\n", " for index, (split_name, ratio) in enumerate(items):\n", " if index == len(items) - 1:\n", " end = len(shuffled_ids)\n", " else:\n", " end = start + round(ratio * len(shuffled_ids))\n", " assignments[split_name] = shuffled_ids[start:end]\n", " start = end\n", "\n", " return assignments\n", "\n", "resource_ids = [resource.id for resource in resources]\n", "split_ids = partition_ids(resource_ids, {\"train\": 0.7, \"val\": 0.15, \"test\": 0.15}, seed=42)\n", "{name: len(ids) for name, ids in split_ids.items()}" ] }, { "cell_type": "code", "execution_count": null, "id": "a7334f3a", "metadata": {}, "outputs": [], "source": [ "for split_name, split_resource_ids in split_ids.items():\n", " if not split_resource_ids:\n", " continue\n", " api.projects.assign_splits(split_resource_ids, split_name, project=project)\n", "\n", "print(\"Project-scoped split assignments updated.\")" ] }, { "cell_type": "markdown", "id": "726317a1", "metadata": {}, "source": [ "## Inspect Split Assignments\n", "\n", "Use `get_splits()` to list the assignments for the whole project or a single split, and `get_resource_split()` to inspect one resource." ] }, { "cell_type": "code", "execution_count": 9, "id": "b955bbf5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total assignments: 4083\n", "Train assignments: 2858\n" ] }, { "data": { "text/html": [ "
| Split Name | \n", "\n", " test\n", " | \n", "
|---|---|
| Project Id | \n", "\n", " f97c6fc8-f5fe-4191-a37b-eccfbb81dac7\n", " | \n", "
| Resource Id | \n", "\n", " 106748f4-7774-43e2-8fb3-017edef01e10\n", " | \n", "
| Created At | \n", "\n", " 2026-04-21T13:09:34.912Z\n", " | \n", "
| Created By | \n", "\n", " datamint-dev@mail.com\n", " | \n", "