{ "cells": [ { "cell_type": "markdown", "metadata": {}, "id": "overview", "source": [ "# Validate crop files, joins and sampled pixels\n\nRequire exact post-QC coverage of the available CellProfiler single-cell profiles. Check MorphEm\nprofiles and UMAP subsets when present; report absent optional inputs as skipped, never verified.\nRecompute sampled crops from TIFFs and compare against a fresh JPEG encode/decode in this environment.\nNo profiles or embeddings are modified, recomputed or required for crop generation itself.\n" ] }, { "cell_type": "markdown", "metadata": {}, "id": "heading-0", "source": [ "## Paths and crop integrity\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "id": "code-0", "source": [ "import json\n", "import os\n", "import pathlib\n", "import pandas as pd\n", "from crop_warehouse import (\n", " FINAL_PROFILE_KEY, POSITION_KEY, PATH_COLUMNS, join_crop_paths, require_unique_keys,\n", " validate_crop_files, validate_profile_join, validate_sample_pixels,\n", ")\n", "from path_utils import get_root_dir\n", "\n", "root_dir = get_root_dir()\n", "module_dir = root_dir / '5a.object_image_crops'\n", "warehouse_dir = pathlib.Path(os.environ.get('PCCMA_CROPS_DIR',\n", " str(module_dir / 'data/object_image_crops'))).expanduser().resolve()\n", "report_path = warehouse_dir / 'validation.json'\n", "report_path.unlink(missing_ok=True)\n", "index_df = pd.read_parquet(warehouse_dir / 'crops_index.parquet')\n", "require_unique_keys(index_df, FINAL_PROFILE_KEY, 'Crop index')\n", "require_unique_keys(index_df, POSITION_KEY, 'Crop positions')\n", "validate_crop_files(index_df, warehouse_dir)\n", "report = {'cells': len(index_df), 'jpg_files': len(index_df) * len(PATH_COLUMNS)}\n", "\n", "local = root_dir / '4.preprocess_features/data/single_cell_profiles'\n", "mount = pathlib.Path('~/mnt/bandicoot/PCCMA_data/Atlas_v2_profiles/single_cell_profiles').expanduser()\n", "profile_dir = pathlib.Path(os.environ.get('PCCMA_PROFILES_DIR',\n", " str(local if any(local.glob('BR*_sc_feature_selected.parquet')) else mount))).expanduser()" ] }, { "cell_type": "markdown", "metadata": {}, "id": "heading-1", "source": [ "## CellProfiler profiles: both directions, one-to-one\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "id": "code-1", "source": [ "profile_paths = sorted(profile_dir.glob('BR*_sc_feature_selected.parquet'))\n", "if not profile_paths:\n", " raise FileNotFoundError(f'No CellProfiler single-cell profiles in {profile_dir}')\n", "profile_plates = {path.name.split('_')[0] for path in profile_paths}\n", "if not set(index_df['Metadata_Plate']).issubset(profile_plates):\n", " raise ValueError('Crop plates without CellProfiler profiles')\n", "for path in profile_paths:\n", " plate = path.name.split('_')[0]\n", " profiles = pd.read_parquet(path, columns=FINAL_PROFILE_KEY)\n", " validate_profile_join(index_df.loc[index_df['Metadata_Plate'] == plate], profiles, f'CellProfiler {plate}')\n", " print(f'CellProfiler {plate}: {len(profiles)} profile rows matched one-to-one')\n", "report['cellprofiler'] = 'verified'" ] }, { "cell_type": "markdown", "metadata": {}, "id": "heading-2", "source": [ "## Optional MorphEm and UMAP joins\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "id": "code-2", "source": [ "morphem_dir = root_dir / '5.morphem_featurization/data/morphem_single_cell_profiles'\n", "morphem_paths = sorted(morphem_dir.glob('BR*_morphem_sc_feature_selected.parquet'))\n", "if morphem_paths:\n", " checked = []\n", " for path in morphem_paths:\n", " plate = path.name.split('_')[0]\n", " profiles = pd.read_parquet(path, columns=FINAL_PROFILE_KEY)\n", " validate_profile_join(index_df.loc[index_df['Metadata_Plate'] == plate], profiles, f'MorphEm {plate}')\n", " checked.append(plate)\n", " report['morphem'] = {'verified_plates': checked,\n", " 'missing_plates': sorted(profile_plates - set(checked))}\n", " print(f'MorphEm: checked {len(checked)} available plates')\n", "else:\n", " report['morphem'] = 'skipped: profiles unavailable'\n", " print('MorphEm joins skipped: profiles unavailable')\n", "\n", "umap_dir = root_dir / '6.profile_analysis/results/3.single_cell_embedding'\n", "report['umap'] = {}\n", "for filename in ('single_cell_umap_coordinates_by_cancer_group.parquet', 'single_cell_umap_example_cells.parquet'):\n", " path = umap_dir / filename\n", " if not path.is_file():\n", " report['umap'][filename] = 'skipped: table unavailable'\n", " print(f'UMAP join skipped: {filename} unavailable')\n", " continue\n", " table = pd.read_parquet(path)\n", " joined = join_crop_paths(table, index_df)\n", " report['umap'][filename] = len(joined)\n", " print(f'UMAP {filename}: all {len(joined)} cells matched one-to-one')" ] }, { "cell_type": "markdown", "metadata": {}, "id": "heading-3", "source": [ "## Recompute sampled pixels and save the validation report\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "id": "code-3", "source": [ "images_dir = pathlib.Path(os.environ.get('PCCMA_IMAGES_DIR',\n", " '~/mnt/bandicoot/PCCMA_data/Atlas_v2_screen')).expanduser()\n", "report['pixel_samples'] = validate_sample_pixels(index_df, warehouse_dir,\n", " root_dir / '2.illumination_correction/illum_directory', images_dir)\n", "temporary = report_path.with_suffix('.tmp.json')\n", "temporary.write_text(json.dumps(report, indent=2) + '\\n')\n", "temporary.replace(report_path)\n", "print(f\"Checked all six images for {report['pixel_samples']} sampled cells against recomputed JPEG pixels\")\n", "print(f'Saved {report_path}')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "file_extension": ".py", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "nbconvert_exporter": "python" } }, "nbformat": 4, "nbformat_minor": 5 }