Files
ai-med-edu/seminars/Тема 1.2/Т1.ipynb
T

272 lines
11 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "raw",
"id": "0",
"metadata": {},
"source": [
"Установка библиотек"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%pip install pydicom matplotlib nibabel gdown medmnist Pillow"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"# Download the 56 zip files in Images_png in batches\n",
"import urllib.request\n",
"\n",
"# URLs for the zip files\n",
"links = [\n",
" 'https://nihcc.box.com/shared/static/vfk49d74nhbxq3nqjg0900w5nvkorp5c.gz',\n",
" 'https://nihcc.box.com/shared/static/i28rlmbvmfjbl8p2n3ril0pptcmcu9d1.gz',\n",
" 'https://nihcc.box.com/shared/static/f1t00wrtdk94satdfb9olcolqx20z2jp.gz',\n",
"\t'https://nihcc.box.com/shared/static/0aowwzs5lhjrceb3qp67ahp0rd1l1etg.gz',\n",
" 'https://nihcc.box.com/shared/static/v5e3goj22zr6h8tzualxfsqlqaygfbsn.gz',\n",
"\t'https://nihcc.box.com/shared/static/asi7ikud9jwnkrnkj99jnpfkjdes7l6l.gz',\n",
"\t'https://nihcc.box.com/shared/static/jn1b4mw4n6lnh74ovmcjb8y48h8xj07n.gz',\n",
" 'https://nihcc.box.com/shared/static/tvpxmn7qyrgl0w8wfh9kqfjskv6nmm1j.gz',\n",
"\t'https://nihcc.box.com/shared/static/upyy3ml7qdumlgk2rfcvlb9k6gvqq2pj.gz',\n",
"\t'https://nihcc.box.com/shared/static/l6nilvfa9cg3s28tqv1qc1olm3gnz54p.gz',\n",
"\t'https://nihcc.box.com/shared/static/hhq8fkdgvcari67vfhs7ppg2w6ni4jze.gz',\n",
"\t'https://nihcc.box.com/shared/static/ioqwiy20ihqwyr8pf4c24eazhh281pbu.gz'\n",
"]\n",
"\n",
"for idx, link in enumerate(links):\n",
" fn = 'images_%02d.tar.gz' % (idx+1)\n",
" print('downloading'+fn+'...')\n",
" urllib.request.urlretrieve(link, fn) # download the zip file\n",
"\n",
"print(\"Download complete. Please check the checksums\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"import tarfile\n",
"import os\n",
"\n",
"# Распаковка\n",
"with tarfile.open('images_02.tar.gz', 'r:gz') as tar:\n",
" tar.extractall(path='images_01')\n",
"print(\"✅ Распаковано в папку 'images_01'\")\n",
"print(\"Количество файлов в папке:\", len(os.listdir('images_01')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from PIL import Image\n",
"import random\n",
"import os\n",
"\n",
"image_folder = 'images_01/images'\n",
"all_images = [f for f in os.listdir(image_folder) if f.endswith('.png')]\n",
"print(f\"Всего PNG-файлов: {len(all_images)}\")\n",
"\n",
"# Покажем 5 случайных\n",
"sample = random.sample(all_images, min(5, len(all_images)))\n",
"plt.figure(figsize=(15, 10))\n",
"for i, img_name in enumerate(sample):\n",
" img = Image.open(os.path.join(image_folder, img_name))\n",
" plt.subplot(1, 5, i+1)\n",
" plt.imshow(img, cmap='gray')\n",
" plt.title(img_name[:15])\n",
" plt.axis('off')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!wget -O dataset.zip 'https://storage.yandexcloud.net/aie20.selftest/production/Dataset_for_self-testing_of_AI_services_%28AIE20_SELFTEST%29.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=J0yLHxYnLMK5SrRghejm%2F20260222%2Fru-central1%2Fs3%2Faws4_request&X-Amz-Date=20260222T111520Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=b135a1c05ed66ade15b59a73bc29aef7380f9da179abd74267c4ec235462992a'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"%pip install pylibjpeg pylibjpeg-libjpeg"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"import random\n",
"import pydicom\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# ------------------------------------------------------------\n",
"# 1. Настройки\n",
"# ------------------------------------------------------------\n",
"# Путь к корневой папке с данными CT (измените, если структура иная)\n",
"ct_root = 'Dataset_for_self-testing_of_AI_services_(AIE20_SELFTEST)/AIE20_SELFTEST/FLG'\n",
"\n",
"# Количество случайных исследований для показа\n",
"num_samples = 5\n",
"\n",
"# ------------------------------------------------------------\n",
"# 2. Функция для рекурсивного поиска всех DICOM-файлов в папке\n",
"# ------------------------------------------------------------\n",
"def find_dicom_files(folder):\n",
" \"\"\"Возвращает список путей ко всем файлам (не папкам) внутри folder.\"\"\"\n",
" all_files = []\n",
" for root, dirs, files in os.walk(folder):\n",
" for f in files:\n",
" if not f.startswith('.'): # игнорируем скрытые файлы\n",
" all_files.append(os.path.join(root, f))\n",
" return all_files\n",
"\n",
"# ------------------------------------------------------------\n",
"# 3. Собираем все папки исследований (производитель/исследование)\n",
"# ------------------------------------------------------------\n",
"if not os.path.exists(ct_root):\n",
" print(f\"Ошибка: папка {ct_root} не найдена.\")\n",
"else:\n",
" # Получаем список производителей\n",
" manufacturers = [m for m in os.listdir(ct_root) \n",
" if os.path.isdir(os.path.join(ct_root, m))]\n",
" if not manufacturers:\n",
" print(\"Нет папок производителей.\")\n",
" else:\n",
" # Список всех полных путей к папкам исследований\n",
" study_folders = []\n",
" for man in manufacturers:\n",
" man_path = os.path.join(ct_root, man)\n",
" studies = [s for s in os.listdir(man_path) \n",
" if os.path.isdir(os.path.join(man_path, s))]\n",
" for study in studies:\n",
" study_folders.append(os.path.join(man_path, study))\n",
"\n",
" print(f\"Всего найдено папок исследований: {len(study_folders)}\")\n",
"\n",
" if not study_folders:\n",
" print(\"Нет доступных исследований.\")\n",
" else:\n",
" # Выбираем случайные папки (не больше, чем есть)\n",
" selected = random.sample(study_folders, min(num_samples, len(study_folders)))\n",
"\n",
" # ------------------------------------------------------------\n",
" # 4. Для каждой выбранной папки показываем случайное изображение\n",
" # ------------------------------------------------------------\n",
" plt.figure(figsize=(15, 10))\n",
" for i, folder in enumerate(selected):\n",
" # Рекурсивно ищем все файлы в этой папке\n",
" dicom_files = find_dicom_files(folder)\n",
" if not dicom_files:\n",
" print(f\"В папке {folder} нет файлов, пропускаем.\")\n",
" continue\n",
"\n",
" # Выбираем случайный файл\n",
" chosen = random.choice(dicom_files)\n",
"\n",
" try:\n",
" ds = pydicom.dcmread(chosen)\n",
" # Проверяем, есть ли массив пикселей\n",
" if not hasattr(ds, 'pixel_array'):\n",
" print(f\"Файл {chosen} не содержит изображения, пропускаем.\")\n",
" continue\n",
"\n",
" # Извлекаем метаданные для подписи\n",
" manufacturer = ds.get('Manufacturer', 'Unknown')\n",
" modality = ds.get('Modality', 'Unknown')\n",
" patient_id = ds.get('PatientID', 'Unknown')\n",
" study_desc = ds.get('StudyDescription', 'No description')\n",
" series_desc = ds.get('SeriesDescription', '')\n",
"\n",
" # Формируем подпись\n",
" title = (f\"{manufacturer}\\n\"\n",
" f\"Modality: {modality}\\n\"\n",
" f\"Patient: {patient_id}\\n\"\n",
" f\"Study: {study_desc[:30]}\\n\"\n",
" f\"Series: {series_desc[:20]}\\n\"\n",
" f\"File: {os.path.basename(chosen)}\")\n",
"\n",
" # Отображаем\n",
" plt.subplot(1, num_samples, i+1)\n",
" plt.imshow(ds.pixel_array, cmap='gray')\n",
" plt.title(title, fontsize=8)\n",
" plt.axis('off')\n",
"\n",
" except Exception as e:\n",
" print(f\"Ошибка при обработке файла {chosen}: {e}\")\n",
" continue\n",
"\n",
" plt.tight_layout()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "DataSphere Kernel",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}