Spaces:
Running
Running
File size: 74,467 Bytes
c4c873f | 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 | import logging
import os
import textwrap
import ctk
import qt
import vtk
import slicer
from slicer.i18n import tr as _
from slicer.i18n import translate
from slicer.ScriptedLoadableModule import *
from slicer.util import computeChecksum, extractAlgoAndDigest, TESTING_DATA_URL
#
# SampleData methods
def downloadFromURL(uris=None, fileNames=None, nodeNames=None, checksums=None, loadFiles=None,
customDownloader=None, loadFileTypes=None, loadFileProperties={}):
"""Download and optionally load data into the application.
:param uris: Download URL(s).
:param fileNames: File name(s) that will be downloaded (and loaded).
:param nodeNames: Node name(s) in the scene.
:param checksums: Checksum(s) formatted as ``<algo>:<digest>`` to verify the downloaded file(s). For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
:param loadFiles: Boolean indicating if file(s) should be loaded. By default, the function decides.
:param customDownloader: Custom function for downloading.
:param loadFileTypes: file format name(s) (if not specified then the default file reader will be used).
:param loadFileProperties: custom properties passed to the IO plugin.
If the given ``fileNames`` are not found in the application cache directory, they
are downloaded using the associated URIs.
See ``slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory()``
If not explicitly provided or if set to ``None``, the ``loadFileTypes`` are
guessed based on the corresponding filename extensions.
If a given fileName has the ``.mrb`` or ``.mrml`` extension, it will **not** be loaded
by default. To ensure the file is loaded, ``loadFiles`` must be set.
The ``loadFileProperties`` are common for all files. If different properties
need to be associated with files of different types, downloadFromURL must
be called for each.
"""
return SampleDataLogic().downloadFromURL(
uris, fileNames, nodeNames, checksums, loadFiles, customDownloader, loadFileTypes, loadFileProperties)
def downloadSample(sampleName):
"""For a given sample name this will search the available sources
and load it if it is available. Returns the first loaded node.
"""
return SampleDataLogic().downloadSamples(sampleName)[0]
def downloadSamples(sampleName):
"""For a given sample name this will search the available sources
and load it if it is available. Returns the loaded nodes.
"""
return SampleDataLogic().downloadSamples(sampleName)
#
# SampleData
#
class SampleData(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = _("Sample Data")
self.parent.categories = [translate("qSlicerAbstractCoreModule", "Informatics")]
self.parent.dependencies = []
self.parent.contributors = ["Steve Pieper (Isomics), Benjamin Long (Kitware), Jean-Christophe Fillion-Robin (Kitware)"]
self.parent.helpText = _("""
This module provides data sets that can be used for testing 3D Slicer.
""")
self.parent.helpText += self.getDefaultModuleDocumentationLink()
self.parent.acknowledgementText = _("""
<p>This work was was funded in part by Cancer Care Ontario
and the Ontario Consortium for Adaptive Interventions in Radiation Oncology (OCAIRO)</p>
<p>MRHead, CBCT-MR Head, and CT-MR Brain data sets were donated to 3D Slicer project by the persons visible in the images, to be used without any restrictions.</p>
<p>CTLiver dataset comes from <a href="http://medicaldecathlon.com/">Medical Decathlon project</a> (imagesTr/liver_100.nii.gz in Task03_Liver collection)
with a permissive copyright-license (<a href="https://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>), allowing for data to be shared, distributed and improved upon.</p>
<p>CTA abdomen (Panoramix) dataset comes from <a href="https://www.osirix-viewer.com/resources/dicom-image-library/">Osirix DICOM image library</a>
and is exclusively available for research and teaching. You are not authorized to redistribute or sell it, or
use it for commercial purposes.</p>
""")
if slicer.mrmlScene.GetTagByClassName("vtkMRMLScriptedModuleNode") != "ScriptedModule":
slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode())
# Trigger the menu to be added when application has started up
if not slicer.app.commandOptions().noMainWindow:
slicer.app.connect("startupCompleted()", self.addMenu)
# allow other modules to register sample data sources by appending
# instances or subclasses SampleDataSource objects on this list
try:
slicer.modules.sampleDataSources
except AttributeError:
slicer.modules.sampleDataSources = {}
def addMenu(self):
a = qt.QAction(_("Download Sample Data"), slicer.util.mainWindow())
a.setToolTip(_("Go to the SampleData module to download data from the network"))
a.connect("triggered()", self.select)
fileMenu = slicer.util.lookupTopLevelWidget("FileMenu")
if fileMenu:
for action in fileMenu.actions():
if action.objectName == "FileSaveSceneAction":
fileMenu.insertAction(action, a)
fileMenu.insertSeparator(action)
def select(self):
m = slicer.util.mainWindow()
m.moduleSelector().selectModule("SampleData")
#
# SampleDataSource
#
class SampleDataSource:
"""Describe a set of sample data associated with one or multiple URIs and filenames.
Example::
import SampleData
from slicer.util import TESTING_DATA_URL
dataSource = SampleData.SampleDataSource(
nodeNames='fixed',
fileNames='fixed.nrrd',
uris=TESTING_DATA_URL + 'SHA256/b757f9c61c1b939f104e5d7861130bb28d90f33267a012eb8bb763a435f29d37')
loadedNode = SampleData.SampleDataLogic().downloadFromSource(dataSource)[0]
"""
def __init__(self, sampleName=None, sampleDescription=None, uris=None, fileNames=None, nodeNames=None,
checksums=None, loadFiles=None,
customDownloader=None, thumbnailFileName=None,
loadFileTypes=None, loadFileProperties=None,
loadFileType=None):
"""
:param sampleName: Name identifying the data set.
:param sampleDescription: Displayed name of data set in SampleData module GUI. (default is ``sampleName``)
:param thumbnailFileName: Displayed thumbnail of data set in SampleData module GUI,
:param uris: Download URL(s).
:param fileNames: File name(s) that will be downloaded (and loaded).
:param nodeNames: Node name(s) in the scene.
:param checksums: Checksum(s) formatted as ``<algo>:<digest>`` to verify the downloaded file(s). For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
:param loadFiles: Boolean indicating if file(s) should be loaded.
:param customDownloader: Custom function for downloading.
:param loadFileTypes: file format name(s) (if not specified then the default file reader will be used).
:param loadFileProperties: custom properties passed to the IO plugin.
:param loadFileType: deprecated, use ``loadFileTypes`` instead.
"""
# For backward compatibility (allow using "loadFileType" instead of "loadFileTypes")
if (loadFileType is not None) and (loadFileTypes is not None):
raise ValueError("loadFileType and loadFileTypes cannot be specified at the same time")
if (loadFileType is not None) and (loadFileTypes is None):
loadFileTypes = loadFileType
self.sampleName = sampleName
if sampleDescription is None:
sampleDescription = sampleName
self.sampleDescription = sampleDescription
if isinstance(uris, list) or isinstance(uris, tuple):
if isinstance(loadFileTypes, str) or loadFileTypes is None:
loadFileTypes = [loadFileTypes] * len(uris)
if nodeNames is None:
nodeNames = [None] * len(uris)
if loadFiles is None:
loadFiles = [None] * len(uris)
if checksums is None:
checksums = [None] * len(uris)
elif isinstance(uris, str):
uris = [uris]
fileNames = [fileNames]
nodeNames = [nodeNames]
loadFiles = [loadFiles]
loadFileTypes = [loadFileTypes]
checksums = [checksums]
if loadFileProperties is None:
loadFileProperties = {}
self.uris = uris
self.fileNames = fileNames
self.nodeNames = nodeNames
self.loadFiles = loadFiles
self.customDownloader = customDownloader
self.thumbnailFileName = thumbnailFileName
self.loadFileTypes = loadFileTypes
self.loadFileProperties = loadFileProperties
self.checksums = checksums
if not len(uris) == len(fileNames) == len(nodeNames) == len(loadFiles) == len(loadFileTypes) == len(checksums):
raise ValueError(
f"All fields of sample data source must have the same length\n"
f" uris : {uris}\n"
f" len(uris) : {len(uris)}\n"
f" len(fileNames) : {len(fileNames)}\n"
f" len(nodeNames) : {len(nodeNames)}\n"
f" len(loadFiles) : {len(loadFiles)}\n"
f" len(loadFileTypes) : {len(loadFileTypes)}\n"
f" len(checksums) : {len(checksums)}\n",
)
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
output = [
"sampleName : %s" % self.sampleName,
"sampleDescription : %s" % self.sampleDescription,
"thumbnailFileName : %s" % self.thumbnailFileName,
"loadFileProperties: %s" % self.loadFileProperties,
"customDownloader : %s" % self.customDownloader,
"",
]
for fileName, uri, nodeName, loadFile, fileType, checksum in zip(
self.fileNames, self.uris, self.nodeNames, self.loadFiles, self.loadFileTypes, self.checksums, strict=True):
output.extend([
" fileName : %s" % fileName,
" uri : %s" % uri,
" checksum : %s" % checksum,
" nodeName : %s" % nodeName,
" loadFile : %s" % loadFile,
" loadFileType : %s" % fileType,
"",
])
return "\n".join(output)
#
# SampleData widget
#
class TextEditFinishEventFilter(qt.QObject):
def __init__(self, *args, **kwargs):
qt.QObject.__init__(self, *args, **kwargs)
self.textEditFinishedCallback = None
def eventFilter(self, object, event):
if self.textEditFinishedCallback is None:
return
if (event.type() == qt.QEvent.KeyPress and event.key() in (qt.Qt.Key_Return, qt.Qt.Key_Enter)
and (event.modifiers() & qt.Qt.ControlModifier)):
self.textEditFinishedCallback()
return True # stop further handling
return False
class SampleDataWidget(ScriptedLoadableModuleWidget):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
# This module is often used in developer mode, therefore
# collapse reload & test section by default.
if hasattr(self, "reloadCollapsibleButton"):
self.reloadCollapsibleButton.collapsed = True
self.logic = SampleDataLogic(self.logMessage)
self.categoryLayout = qt.QVBoxLayout()
self.categoryLayout.setContentsMargins(0, 0, 0, 0)
self.layout.addLayout(self.categoryLayout)
SampleDataWidget.setCategoriesFromSampleDataSources(self.categoryLayout, slicer.modules.sampleDataSources, self.logic)
if self.developerMode is False:
self.setCategoryVisible(self.logic.developmentCategoryName, False)
customFrame = ctk.ctkCollapsibleGroupBox()
self.categoryLayout.addWidget(customFrame)
customFrame.title = _("Load data from URL")
customFrameLayout = qt.QVBoxLayout()
customFrame.setLayout(customFrameLayout)
self.customSampleLabel = qt.QLabel(_("Download URLs:"))
customFrameLayout.addWidget(self.customSampleLabel)
self.customSampleUrlEdit = qt.QTextEdit()
self.customSampleUrlEdit.toolTip = _("Enter one or more URLs (one per line) to download and load the corresponding data sets. "
"Press Ctrl+Enter or click 'Load' button to start loading.")
# Install event filter to catch Ctrl+Enter keypress to start loading
self.customSampleUrlEditFinishedEventFilter = TextEditFinishEventFilter()
self.customSampleUrlEditFinishedEventFilter.textEditFinishedCallback = self.onCustomDataDownload
self.customSampleUrlEdit.installEventFilter(self.customSampleUrlEditFinishedEventFilter)
customFrameLayout.addWidget(self.customSampleUrlEdit)
self.customDownloadButton = qt.QPushButton(_("Load"))
self.customDownloadButton.toolTip = _("Download the dataset from the given URL and import it into the scene")
self.customDownloadButton.default = True
customFrameLayout.addWidget(self.customDownloadButton)
self.showCustomDataFolderButton = qt.QPushButton(_("Show folder"))
self.showCustomDataFolderButton.toolTip = _("Show folder where custom data sets are downloaded ({path}).").format(path=slicer.app.cachePath)
customFrameLayout.addWidget(self.showCustomDataFolderButton)
customFrame.collapsed = True
self.customDownloadButton.connect("clicked()", self.onCustomDataDownload)
self.showCustomDataFolderButton.connect("clicked()", self.onShowCustomDataFolder)
self.log = qt.QTextEdit()
self.log.readOnly = True
self.layout.addWidget(self.log)
# Add spacer to layout
self.layout.addStretch(1)
def cleanup(self):
SampleDataWidget.setCategoriesFromSampleDataSources(self.categoryLayout, {}, self.logic)
def onCustomDataDownload(self):
uris = self.customSampleUrlEdit.toPlainText().strip()
if not uris:
return
uriList = [u.strip() for u in uris.splitlines() if u.strip()]
totalCount = 0
errorCount = 0
for uri in uriList:
self.logMessage(f"Downloading data from {uri} ...", logging.INFO)
if not uri:
continue
try:
totalCount += 1
# Get filename from URL
import urllib
parsedUrl = urllib.parse.urlparse(uri)
basename, ext = os.path.splitext(os.path.basename(parsedUrl.path))
filename = basename + ext
# Download
result = self.logic.downloadFromURL(uris=uri, fileNames=filename, forceDownload=True)
if not result:
errorCount += 1
except Exception as e:
errorCount += 1
self.logMessage(f"Data download from {uri} failed: {str(e)}", logging.ERROR)
import traceback
traceback.print_exc()
if totalCount > 1:
self.logMessage("", logging.INFO)
if errorCount == 0:
self.logMessage(_("All {totalCount} data sets were loaded successfully.").format(totalCount=totalCount), logging.INFO)
else:
self.logMessage(_("Failed to load {errorCount} out of {totalCount} data sets.").format(errorCount=errorCount, totalCount=totalCount), logging.ERROR)
def onShowCustomDataFolder(self):
qt.QDesktopServices.openUrl(qt.QUrl("file:///" + slicer.app.cachePath, qt.QUrl.TolerantMode))
@staticmethod
def removeCategories(categoryLayout):
"""Remove all categories from the given category layout."""
while categoryLayout.count() > 0:
frame = categoryLayout.itemAt(0).widget()
frame.visible = False
categoryLayout.removeWidget(frame)
frame.setParent(0)
del frame
@staticmethod
def setCategoriesFromSampleDataSources(categoryLayout, dataSources, logic):
"""Update categoryLayout adding buttons for downloading dataSources.
Download buttons are organized in collapsible GroupBox with one GroupBox
per category.
"""
iconPath = os.path.join(os.path.dirname(__file__).replace("\\", "/"), "Resources", "Icons")
mainWindow = slicer.util.mainWindow()
if mainWindow:
# Set thumbnail size from default icon size. This results in toolbutton size that makes
# two columns of buttons fit into the size of the Welcome module's minimum width
# on screens with a various resolution and scaling (see qt.QDesktopWidget().size,
# desktop.devicePixelRatioF(), qt.QDesktopWidget().physicalDpiX())
iconSize = qt.QSize(int(mainWindow.iconSize.width() * 6), int(mainWindow.iconSize.height() * 4))
else:
# There is no main window in the automated tests
screens = slicer.app.screens()
primaryScreen = screens[0] if screens else None
if primaryScreen:
mainScreenSize = primaryScreen.availableGeometry.size()
iconSize = qt.QSize(
int(mainScreenSize.width() / 15),
int(mainScreenSize.height() / 10),
)
else:
# Absolute fallback (should never happen, but keeps tests robust)
iconSize = qt.QSize(128, 128)
categories = sorted(dataSources.keys())
# Ensure "builtIn" category is always first
if logic.builtInCategoryName in categories:
categories.remove(logic.builtInCategoryName)
categories.insert(0, logic.builtInCategoryName)
# Clear category layout
SampleDataWidget.removeCategories(categoryLayout)
# Populate category layout
for category in categories:
frame = ctk.ctkCollapsibleGroupBox(categoryLayout.parentWidget())
categoryLayout.addWidget(frame)
frame.title = category
frame.name = "%sCollapsibleGroupBox" % category
layout = ctk.ctkFlowLayout()
layout.preferredExpandingDirections = qt.Qt.Vertical
frame.setLayout(layout)
for source in dataSources[category]:
name = source.sampleDescription
if not name:
name = source.nodeNames[0]
b = qt.QToolButton()
b.setText(name)
# Set thumbnail
if source.thumbnailFileName:
# Thumbnail provided
thumbnailImage = source.thumbnailFileName
else:
# Look for thumbnail image with the name of any node name with .png extension
thumbnailImage = None
for nodeName in source.nodeNames:
if not nodeName:
continue
thumbnailImageAttempt = os.path.join(iconPath, nodeName + ".png")
if os.path.exists(thumbnailImageAttempt):
thumbnailImage = thumbnailImageAttempt
break
if thumbnailImage and os.path.exists(thumbnailImage):
b.setIcon(qt.QIcon(thumbnailImage))
b.setIconSize(iconSize)
b.setToolButtonStyle(qt.Qt.ToolButtonTextUnderIcon)
qSize = qt.QSizePolicy()
qSize.setHorizontalPolicy(qt.QSizePolicy.Expanding)
b.setSizePolicy(qSize)
b.name = "%sPushButton" % name
layout.addWidget(b)
if source.customDownloader:
b.connect("clicked()", lambda s=source: s.customDownloader(s))
else:
b.connect("clicked()", lambda s=source: logic.downloadFromSource(s))
def logMessage(self, message, logLevel=logging.DEBUG):
# Format based on log level
if logLevel >= logging.ERROR:
message = '<b><font color="red">' + message + "</font></b>"
elif logLevel >= logging.WARNING:
message = '<b><font color="orange">' + message + "</font></b>"
# Show message in status bar
doc = qt.QTextDocument()
doc.setHtml(message)
slicer.util.showStatusMessage(doc.toPlainText(), 3000)
logging.log(logLevel, doc.toPlainText())
# Show message in log window at the bottom of the module widget
self.log.insertHtml(message)
self.log.insertPlainText("\n")
self.log.ensureCursorVisible()
self.log.repaint()
slicer.app.processEvents(qt.QEventLoop.ExcludeUserInputEvents)
def isCategoryVisible(self, category):
"""Check the visibility of a SampleData category given its name.
Returns False if the category is not visible or if it does not exist,
otherwise returns True.
"""
if not SampleDataLogic.sampleDataSourcesByCategory(category):
return False
return slicer.util.findChild(self.parent, "%sCollapsibleGroupBox" % category).isVisible()
def setCategoryVisible(self, category, visible):
"""Update visibility of a SampleData category given its name.
The function is a no-op if the category does not exist.
"""
if not SampleDataLogic.sampleDataSourcesByCategory(category):
return
slicer.util.findChild(self.parent, "%sCollapsibleGroupBox" % category).setVisible(visible)
#
# SampleData logic
#
class SampleDataLogic:
"""Manage the slicer.modules.sampleDataSources dictionary.
The dictionary keys are categories of sample data sources.
The BuiltIn category is managed here. Modules or extensions can
register their own sample data by creating instances of the
SampleDataSource class. These instances should be stored in a
list that is assigned to a category following the model
used in registerBuiltInSampleDataSources below.
Checksums are expected to be formatted as a string of the form
``<algo>:<digest>``. For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
"""
@staticmethod
def registerCustomSampleDataSource(category="Custom",
sampleName=None, uris=None, fileNames=None, nodeNames=None,
customDownloader=None, thumbnailFileName=None,
loadFileTypes=None, loadFiles=None, loadFileProperties={},
checksums=None,
loadFileType=None):
"""Adds custom data sets to SampleData.
:param category: Section title of data set in SampleData module GUI.
:param sampleName: Displayed name of data set in SampleData module GUI.
:param thumbnailFileName: Displayed thumbnail of data set in SampleData module GUI,
:param uris: Download URL(s).
:param fileNames: File name(s) that will be loaded.
:param nodeNames: Node name(s) in the scene.
:param customDownloader: Custom function for downloading.
:param loadFileTypes: file format name(s) (if not specified then the default file reader will be used).
:param loadFiles: Boolean indicating if file(s) should be loaded. By default, the function decides.
:param loadFileProperties: custom properties passed to the IO plugin.
:param checksums: Checksum(s) formatted as ``<algo>:<digest>`` to verify the downloaded file(s). For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
:param loadFileType: deprecated, use ``loadFileTypes`` instead.
"""
# For backward compatibility (allow using "loadFileType" instead of "loadFileTypes")
if (loadFileType is not None) and (loadFileTypes is not None):
raise ValueError("loadFileType and loadFileTypes cannot be specified at the same time")
if (loadFileType is not None) and (loadFileTypes is None):
loadFileTypes = loadFileType
try:
slicer.modules.sampleDataSources
except AttributeError:
slicer.modules.sampleDataSources = {}
if category not in slicer.modules.sampleDataSources:
slicer.modules.sampleDataSources[category] = []
dataSource = SampleDataSource(
sampleName=sampleName,
uris=uris,
fileNames=fileNames,
nodeNames=nodeNames,
thumbnailFileName=thumbnailFileName,
loadFileTypes=loadFileTypes,
loadFiles=loadFiles,
loadFileProperties=loadFileProperties,
checksums=checksums,
customDownloader=customDownloader,
)
if SampleDataLogic.isSampleDataSourceRegistered(category, dataSource):
return
slicer.modules.sampleDataSources[category].append(dataSource)
@staticmethod
def sampleDataSourcesByCategory(category=None):
"""Return the registered SampleDataSources for with the given category.
If no category is specified, returns all registered SampleDataSources.
"""
try:
slicer.modules.sampleDataSources
except AttributeError:
slicer.modules.sampleDataSources = {}
if category is None:
return slicer.modules.sampleDataSources
else:
return slicer.modules.sampleDataSources.get(category, [])
@staticmethod
def isSampleDataSourceRegistered(category, sampleDataSource):
"""Returns True if the sampleDataSource is registered with the category."""
try:
slicer.modules.sampleDataSources
except AttributeError:
slicer.modules.sampleDataSources = {}
if not isinstance(sampleDataSource, SampleDataSource):
raise TypeError(f"unsupported sampleDataSource type '{type(sampleDataSource)}': '{str(SampleDataSource)}' is expected")
return sampleDataSource in slicer.modules.sampleDataSources.get(category, [])
@staticmethod
def registerUrlRewriteRule(name, rewriteFunction):
"""Adds a URL rewrite rule for custom data sets in SampleData.
:param name: Name of the rewrite rule.
:param rewriteFunction: Function that implements the rewrite logic.
"""
if not callable(rewriteFunction):
raise TypeError(f"Expected 'rewriteFunction' to be callable, got {type(rewriteFunction)}")
try:
slicer.modules.sampleDataUrlRewriteRules
except AttributeError:
slicer.modules.sampleDataUrlRewriteRules = {}
slicer.modules.sampleDataUrlRewriteRules[name] = rewriteFunction
def __init__(self, logMessage=None):
if logMessage:
self.logMessage = logMessage
self.builtInCategoryName = _("General")
self.developmentCategoryName = _("Development")
self.registerBuiltInSampleDataSources()
self.registerDevelopmentSampleDataSources()
if slicer.app.testingEnabled():
self.registerTestingDataSources()
self.registerBuiltInUrlRewriteRules()
self.downloadPercent = 0
def registerBuiltInSampleDataSources(self):
"""Fills in the pre-define sample data sources"""
# Arguments:
# sampleName=None, sampleDescription=None,
# uris=None,
# fileNames=None, nodeNames=None,
# checksums=None,
# loadFiles=None, customDownloader=None, thumbnailFileName=None, loadFileTypes=None, loadFileProperties=None
sourceArguments = (
("MRHead", None, TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
"MR-head.nrrd", "MRHead", "SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93"),
("CTChest", None, TESTING_DATA_URL + "SHA256/4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e",
"CT-chest.nrrd", "CTChest", "SHA256:4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e"),
("CTACardio", None, TESTING_DATA_URL + "SHA256/3b0d4eb1a7d8ebb0c5a89cc0504640f76a030b4e869e33ff34c564c3d3b88ad2",
"CTA-cardio.nrrd", "CTACardio", "SHA256:3b0d4eb1a7d8ebb0c5a89cc0504640f76a030b4e869e33ff34c564c3d3b88ad2"),
("DTIBrain", None, TESTING_DATA_URL + "SHA256/5c78d00c86ae8d968caa7a49b870ef8e1c04525b1abc53845751d8bce1f0b91a",
"DTI-Brain.nrrd", "DTIBrain", "SHA256:5c78d00c86ae8d968caa7a49b870ef8e1c04525b1abc53845751d8bce1f0b91a"),
("MRBrainTumor1", None, TESTING_DATA_URL + "SHA256/998cb522173839c78657f4bc0ea907cea09fd04e44601f17c82ea27927937b95",
"RegLib_C01_1.nrrd", "MRBrainTumor1", "SHA256:998cb522173839c78657f4bc0ea907cea09fd04e44601f17c82ea27927937b95"),
("MRBrainTumor2", None, TESTING_DATA_URL + "SHA256/1a64f3f422eb3d1c9b093d1a18da354b13bcf307907c66317e2463ee530b7a97",
"RegLib_C01_2.nrrd", "MRBrainTumor2", "SHA256:1a64f3f422eb3d1c9b093d1a18da354b13bcf307907c66317e2463ee530b7a97"),
("BaselineVolume", None, TESTING_DATA_URL + "SHA256/dff28a7711d20b6e16d5416535f6010eb99fd0c8468aaa39be4e39da78e93ec2",
"BaselineVolume.nrrd", "BaselineVolume", "SHA256:dff28a7711d20b6e16d5416535f6010eb99fd0c8468aaa39be4e39da78e93ec2"),
("DTIVolume", None,
(TESTING_DATA_URL + "SHA256/d785837276758ddd9d21d76a3694e7fd866505a05bc305793517774c117cb38d",
TESTING_DATA_URL + "SHA256/67564aa42c7e2eec5c3fd68afb5a910e9eab837b61da780933716a3b922e50fe" ),
("DTIVolume.raw.gz", "DTIVolume.nhdr"), (None, "DTIVolume"),
("SHA256:d785837276758ddd9d21d76a3694e7fd866505a05bc305793517774c117cb38d",
"SHA256:67564aa42c7e2eec5c3fd68afb5a910e9eab837b61da780933716a3b922e50fe")),
("DWIVolume", None,
(TESTING_DATA_URL + "SHA256/cf03fd53583dc05120d3314d0a82bdf5946799b1f72f2a7f08963f3fd24ca692",
TESTING_DATA_URL + "SHA256/7666d83bc205382e418444ea60ab7df6dba6a0bd684933df8809da6b476b0fed"),
("dwi.raw.gz", "dwi.nhdr"), (None, "dwi"),
("SHA256:cf03fd53583dc05120d3314d0a82bdf5946799b1f72f2a7f08963f3fd24ca692",
"SHA256:7666d83bc205382e418444ea60ab7df6dba6a0bd684933df8809da6b476b0fed")),
("CTAAbdomenPanoramix", "CTA abdomen\n(Panoramix)", TESTING_DATA_URL + "SHA256/146af87511520c500a3706b7b2bfb545f40d5d04dd180be3a7a2c6940e447433",
"Panoramix-cropped.nrrd", "Panoramix-cropped", "SHA256:146af87511520c500a3706b7b2bfb545f40d5d04dd180be3a7a2c6940e447433"),
("CBCTDentalSurgery", None,
(TESTING_DATA_URL + "SHA256/7bfa16945629c319a439f414cfb7edddd2a97ba97753e12eede3b56a0eb09968",
TESTING_DATA_URL + "SHA256/4cdc3dc35519bb57daeef4e5df89c00849750e778809e94971d3876f95cc7bbd"),
("PreDentalSurgery.gipl.gz", "PostDentalSurgery.gipl.gz"), ("PreDentalSurgery", "PostDentalSurgery"),
("SHA256:7bfa16945629c319a439f414cfb7edddd2a97ba97753e12eede3b56a0eb09968",
"SHA256:4cdc3dc35519bb57daeef4e5df89c00849750e778809e94971d3876f95cc7bbd")),
("MRUSProstate", "MR-US Prostate",
(TESTING_DATA_URL + "SHA256/4843cdc9ea5d7bcce61650d1492ce01035727c892019339dca726380496896aa",
TESTING_DATA_URL + "SHA256/34decf58b1e6794069acbe947b460252262fe95b6858c5e320aeab03bc82ebb2"),
("Case10-MR.nrrd", "case10_US_resampled.nrrd"), ("MRProstate", "USProstate"),
("SHA256:4843cdc9ea5d7bcce61650d1492ce01035727c892019339dca726380496896aa",
"SHA256:34decf58b1e6794069acbe947b460252262fe95b6858c5e320aeab03bc82ebb2")),
("CTMRBrain", "CT-MR Brain",
(TESTING_DATA_URL + "SHA256/6a5b6caccb76576a863beb095e3bfb910c50ca78f4c9bf043aa42f976cfa53d1",
TESTING_DATA_URL + "SHA256/2da3f655ed20356ee8cdf32aa0f8f9420385de4b6e407d28e67f9974d7ce1593",
TESTING_DATA_URL + "SHA256/fa1fe5910a69182f2b03c0150d8151ac6c75df986449fb5a6c5ae67141e0f5e7"),
("CT-brain.nrrd", "MR-brain-T1.nrrd", "MR-brain-T2.nrrd"),
("CTBrain", "MRBrainT1", "MRBrainT2"),
("SHA256:6a5b6caccb76576a863beb095e3bfb910c50ca78f4c9bf043aa42f976cfa53d1",
"SHA256:2da3f655ed20356ee8cdf32aa0f8f9420385de4b6e407d28e67f9974d7ce1593",
"SHA256:fa1fe5910a69182f2b03c0150d8151ac6c75df986449fb5a6c5ae67141e0f5e7")),
("CBCTMRHead", "CBCT-MR Head",
(TESTING_DATA_URL + "SHA256/4ce7aa75278b5a7b757ed0c8d7a6b3caccfc3e2973b020532456dbc8f3def7db",
TESTING_DATA_URL + "SHA256/b5e9f8afac58d6eb0e0d63d059616c25a98e0beb80f3108410b15260a6817842"),
("DZ-CBCT.nrrd", "DZ-MR.nrrd"),
("DZ-CBCT", "DZ-MR"),
("SHA256:4ce7aa75278b5a7b757ed0c8d7a6b3caccfc3e2973b020532456dbc8f3def7db",
"SHA256:b5e9f8afac58d6eb0e0d63d059616c25a98e0beb80f3108410b15260a6817842")),
("CTLiver", None, TESTING_DATA_URL + "SHA256/e16eae0ae6fefa858c5c11e58f0f1bb81834d81b7102e021571056324ef6f37e",
"CTLiver.nrrd", "CTLiver", "SHA256:e16eae0ae6fefa858c5c11e58f0f1bb81834d81b7102e021571056324ef6f37e"),
("CTPCardioSeq", "CTP Cardio Sequence",
"https://github.com/Slicer/SlicerDataStore/releases/download/SHA256/7fbb6ad0aed9c00820d66e143c2f037568025ed63db0a8db05ae7f26affeb1c2",
"CTP-cardio.seq.nrrd", "CTPCardioSeq",
"SHA256:7fbb6ad0aed9c00820d66e143c2f037568025ed63db0a8db05ae7f26affeb1c2",
None, None, None, "SequenceFile"),
("CTCardioSeq", "CT Cardio Sequence",
"https://github.com/Slicer/SlicerDataStore/releases/download/SHA256/d1a1119969acead6c39c7c3ec69223fa2957edc561bc5bf384a203e2284dbc93",
"CT-cardio.seq.nrrd", "CTCardioSeq",
"SHA256:d1a1119969acead6c39c7c3ec69223fa2957edc561bc5bf384a203e2284dbc93",
None, None, None, "SequenceFile"),
)
if self.builtInCategoryName not in slicer.modules.sampleDataSources:
slicer.modules.sampleDataSources[self.builtInCategoryName] = []
for sourceArgument in sourceArguments:
dataSource = SampleDataSource(*sourceArgument)
if SampleDataLogic.isSampleDataSourceRegistered(self.builtInCategoryName, dataSource):
continue
slicer.modules.sampleDataSources[self.builtInCategoryName].append(dataSource)
def registerDevelopmentSampleDataSources(self):
"""Fills in the sample data sources displayed only if developer mode is enabled."""
iconPath = os.path.join(os.path.dirname(__file__).replace("\\", "/"), "Resources", "Icons")
self.registerCustomSampleDataSource(
category=self.developmentCategoryName, sampleName="TinyPatient",
uris=[TESTING_DATA_URL + "SHA256/c0743772587e2dd4c97d4e894f5486f7a9a202049c8575e032114c0a5c935c3b",
TESTING_DATA_URL + "SHA256/3243b62bde36b1db1cdbfe204785bd4bc1fbb772558d5f8cac964cda8385d470"],
fileNames=["TinyPatient_CT.nrrd", "TinyPatient_Structures.seg.nrrd"],
nodeNames=["TinyPatient_CT", "TinyPatient_Segments"],
thumbnailFileName=os.path.join(iconPath, "TinyPatient.png"),
loadFileTypes=["VolumeFile", "SegmentationFile"],
checksums=["SHA256:c0743772587e2dd4c97d4e894f5486f7a9a202049c8575e032114c0a5c935c3b", "SHA256:3243b62bde36b1db1cdbfe204785bd4bc1fbb772558d5f8cac964cda8385d470"],
)
def registerTestingDataSources(self):
"""Register sample data sources used by SampleData self-test to test module functionalities."""
self.registerCustomSampleDataSource(**SampleDataTest.CustomDownloaderDataSource)
def registerBuiltInUrlRewriteRules(self):
"""Register built-in URL rewrite rules."""
def githubFileWebpageUrlRewriteRule(url):
"""Rewrite GitHub file webpage URL to download URL.
Example:
https://github.com/SlicerMorph/terms-and-colors/blob/main/JaimiGrayTetrapodSkulls.csv
is automatically converted to
https://raw.githubusercontent.com/SlicerMorph/terms-and-colors/refs/heads/main/JaimiGrayTetrapodSkulls.csv
"""
import urllib.parse as urlparse
parsedUrl = urlparse.urlparse(url)
if parsedUrl.scheme != "https" or parsedUrl.netloc != "github.com":
return url
pathParts = parsedUrl.path.split("/")
if len(pathParts) <= 4:
return url
organization = pathParts[1]
repository = pathParts[2]
rootFolder = pathParts[3]
filePath = "/".join(pathParts[4:])
if rootFolder != "blob":
return url
parsedUrl = parsedUrl._replace(netloc="raw.githubusercontent.com")
parsedUrl = parsedUrl._replace(path="/".join([organization, repository, "refs", "heads"]) + "/" + filePath)
return parsedUrl.geturl()
def dropboxDownloadUrlRewriteRule(url):
"""Rewrite Dropbox shared link to direct download link.
Example:
https://www.dropbox.com/s/abcd1234efgh5678/myfile.nrrd
is automatically converted to
https://dl.dropbox.com/s/abcd1234efgh5678/myfile.nrrd
"""
import urllib.parse as urlparse
parsedUrl = urlparse.urlparse(url)
if parsedUrl.scheme != "https" or parsedUrl.netloc != "www.dropbox.com":
return url
parsedUrl = parsedUrl._replace(netloc="dl.dropbox.com")
return parsedUrl.geturl()
self.registerUrlRewriteRule("GitHubFileWebpage", githubFileWebpageUrlRewriteRule)
self.registerUrlRewriteRule("DropboxDownloadLink", dropboxDownloadUrlRewriteRule)
def downloadFileIntoCache(self, uri, name, checksum=None, forceDownload=False):
"""Given a uri and a filename, download the data into
a file of the given name in the scene's cache.
If checksum is provided then it is used to decide if the file has to be downloaded again.
If checksum is not provided then the file is not downloaded again unless forceDownload is True.
"""
destFolderPath = slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory()
if not os.access(destFolderPath, os.W_OK):
try:
os.makedirs(destFolderPath, exist_ok=True)
except:
self.logMessage(_("Failed to create cache folder {path}").format(path=destFolderPath), logging.ERROR)
if not os.access(destFolderPath, os.W_OK):
self.logMessage(_("Cache folder {path} is not writable").format(path=destFolderPath), logging.ERROR)
return self.downloadFile(uri, destFolderPath, name, checksum, forceDownload=forceDownload)
def downloadSourceIntoCache(self, source):
"""Download all files for the given source and return a
list of file paths for the results
"""
filePaths = []
for uri, fileName, checksum in zip(source.uris, source.fileNames, source.checksums, strict=False):
filePaths.append(self.downloadFileIntoCache(uri, fileName, checksum))
return filePaths
def downloadFromSource(self, source, maximumAttemptsCount=3, forceDownload=False):
"""Given an instance of SampleDataSource, downloads the associated data and
load them into Slicer if it applies.
The function always returns a list.
Based on the fileType(s), nodeName(s) and loadFile(s) associated with
the source, different values may be appended to the returned list:
- if nodeName is specified, appends loaded nodes but if ``loadFile`` is False appends downloaded filepath
- if fileType is ``SceneFile``, appends downloaded filepath
- if fileType is ``ZipFile``, appends directory of extracted archive but if ``loadFile`` is False appends downloaded filepath
If no ``nodeNames`` and no ``fileTypes`` are specified or if ``loadFiles`` are all False,
returns the list of all downloaded filepaths.
If ``forceDownload`` is True then existing files are downloaded again (if no hash is provided).
"""
# Input may contain urls without associated node names, which correspond to additional data files
# (e.g., .raw file for a .nhdr header file). Therefore we collect nodes and file paths separately
# and we only return file paths if no node names have been provided.
resultNodes = []
resultFilePaths = []
# If some node names are defined and some are left empty then we assume that it is intentional
# (e.g., you only want to load the image.nhdr file and not the image.raw file).
# In this case default node names are not generated.
generateDefaultNodeNames = all(n is None for n in source.nodeNames)
for uri, fileName, nodeName, checksum, loadFile, loadFileType in zip(
source.uris, source.fileNames, source.nodeNames, source.checksums, source.loadFiles, source.loadFileTypes, strict=False):
if nodeName is None or fileName is None:
# Determine file basename and extension from URL or path
import urllib
import uuid
if fileName is not None:
basename, ext = os.path.splitext(os.path.basename(fileName))
else:
p = urllib.parse.urlparse(uri)
basename, ext = os.path.splitext(os.path.basename(p.path))
# Generate default node name (we only need this if we want to load the file into the scene and no node name is provided)
if (nodeName is None) and (loadFile is not False) and generateDefaultNodeNames:
nodeName = basename
# Generate default file name (we always need this, even for just downloading)
if fileName is None:
# Generate a unique filename to avoid overwriting existing file with the same name
fileName = f"{nodeName if nodeName else basename}-{uuid.uuid4().hex}{ext}"
current_source = SampleDataSource(
uris=uri,
fileNames=fileName,
nodeNames=nodeName,
checksums=checksum,
loadFiles=loadFile,
loadFileTypes=loadFileType,
loadFileProperties=source.loadFileProperties)
for attemptsCount in range(maximumAttemptsCount):
# Download
try:
filePath = self.downloadFileIntoCache(uri, fileName, checksum, forceDownload=forceDownload)
except ValueError:
self.logMessage(_("Download failed (attempt {current} of {total})...").format(
current=attemptsCount + 1, total=maximumAttemptsCount), logging.ERROR)
continue
# Special behavior (how `loadFileType` is used and what is returned in `resultNodes` ) is implemented
# for scene and zip file loading, for preserving backward compatible behavior.
# - ZipFile: If `loadFile` is explicitly set to `False` then the zip file is just downloaded. Otherwise, the zip file is extracted.
# By default `loadFile` is set to `None`, so by default the zip file is extracted.
# Nodes are not loaded from the .zip file in either case. To load a scene from a .zip file, `loadFileType` has to be set explicitly to `SceneFile`.
# Path is returned in `resultNodes`.
# - SceneFile: If `loadFile` is not explicitly set or it is set to `False` then the scene is just downloaded (not loaded).
# Path is returned in `resultNodes`.
if (loadFileType is None) and (nodeName is None):
ext = os.path.splitext(fileName.lower())[1]
if ext in [".mrml", ".mrb"]:
loadFileType = "SceneFile"
elif ext in [".zip"]:
loadFileType = "ZipFile"
if loadFileType == "ZipFile":
if loadFile is False:
resultNodes.append(filePath)
break
outputDir = slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory() + "/" + os.path.splitext(os.path.basename(filePath))[0]
qt.QDir().mkpath(outputDir)
if slicer.util.extractArchive(filePath, outputDir):
# Success
resultNodes.append(outputDir)
break
elif loadFileType == "SceneFile":
if not loadFile:
resultNodes.append(filePath)
break
if self.loadScene(filePath, source.loadFileProperties.copy()):
# Success
resultNodes.append(filePath)
break
elif nodeName:
if loadFile is False:
resultNodes.append(filePath)
break
loadedNode = self.loadNode(filePath, nodeName, loadFileType, source.loadFileProperties.copy())
if loadedNode:
# Success
resultNodes.append(loadedNode)
break
else:
# no need to load node
resultFilePaths.append(filePath)
break
# Failed. Clean up downloaded file (it might have been a partial download)
file = qt.QFile(filePath)
if file.exists() and not file.remove():
self.logMessage(_("Load failed (attempt {current} of {total}). Unable to delete and try again loading {path}").format(
current=attemptsCount + 1, total=maximumAttemptsCount, path=filePath), logging.ERROR)
resultNodes.append(loadedNode)
break
self.logMessage(_("Load failed (attempt {current} of {total})...").format(
current=attemptsCount + 1, total=maximumAttemptsCount), logging.ERROR)
if resultNodes:
return resultNodes
else:
return resultFilePaths
def sourceForSampleName(self, sampleName):
"""For a given sample name this will search the available sources.
Returns SampleDataSource instance.
"""
for category in slicer.modules.sampleDataSources.keys():
for source in slicer.modules.sampleDataSources[category]:
if sampleName == source.sampleName:
return source
return None
def categoryForSource(self, a_source):
"""For a given SampleDataSource return the associated category name."""
for category in slicer.modules.sampleDataSources.keys():
for source in slicer.modules.sampleDataSources[category]:
if a_source == source:
return category
return None
def downloadFromURL(self, uris=None, fileNames=None, nodeNames=None, checksums=None, loadFiles=None,
customDownloader=None, loadFileTypes=None, loadFileProperties={}, forceDownload=False):
"""Download and optionally load data into the application.
:param uris: Download URL(s).
:param fileNames: File name(s) that will be downloaded (and loaded).
:param nodeNames: Node name(s) in the scene.
:param checksums: Checksum(s) formatted as ``<algo>:<digest>`` to verify the downloaded file(s). For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
:param loadFiles: Boolean indicating if file(s) should be loaded. By default, the function decides.
:param customDownloader: Custom function for downloading.
:param loadFileTypes: file format name(s) (if not specified then the default file reader will be used).
:param loadFileProperties: custom properties passed to the IO plugin.
If the given ``fileNames`` are not found in the application cache directory, they
are downloaded using the associated URIs.
See ``slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory()``
If not explicitly provided or if set to ``None``, the ``loadFileTypes`` are
guessed based on the corresponding filename extensions and file content.
Special behavior for scene and archive files:
- If a ``fileName`` has the ``.mrb`` or ``.mrml`` extension, it will **not** be loaded
by default. To ensure the file is loaded, ``loadFiles`` must be set.
- If a ``fileName`` has the ``.zip`` extension and ``loadFiles`` is set to ``True`` (or left at default value)
then the archive is extracted and the folder that contains the files is returned.
If ``loadFiles`` is set to ``False`` then the archive is not extracted and the filepath of the downloaded
zip file is returned.
The ``loadFileProperties`` are common for all files. If different properties
need to be associated with files of different types, downloadFromURL must
be called for each.
If ``forceDownload`` is True then existing files are downloaded again (if no hash is provided).
"""
source = SampleDataSource(
uris=uris, fileNames=fileNames, nodeNames=nodeNames, loadFiles=loadFiles,
loadFileTypes=loadFileTypes, loadFileProperties=loadFileProperties, checksums=checksums,
customDownloader=customDownloader)
return self.downloadFromSource(source, forceDownload=forceDownload)
def downloadSample(self, sampleName):
"""For a given sample name this will search the available sources
and load it if it is available. Returns the first loaded node.
"""
return self.downloadSamples(sampleName)[0]
def downloadSamples(self, sampleName):
"""For a given sample name this will search the available sources
and load it if it is available. Returns the loaded nodes.
"""
source = self.sourceForSampleName(sampleName)
nodes = []
if source:
nodes = self.downloadFromSource(source)
return nodes
def logMessage(self, message, logLevel=logging.DEBUG):
logging.log(logLevel, message)
"""Utility methods for backwards compatibility"""
def downloadMRHead(self):
return self.downloadSample("MRHead")
def downloadCTChest(self):
return self.downloadSample("CTChest")
def downloadCTACardio(self):
return self.downloadSample("CTACardio")
def downloadDTIBrain(self):
return self.downloadSample("DTIBrain")
def downloadMRBrainTumor1(self):
return self.downloadSample("MRBrainTumor1")
def downloadMRBrainTumor2(self):
return self.downloadSample("MRBrainTumor2")
def downloadWhiteMatterExplorationBaselineVolume(self):
return self.downloadSample("BaselineVolume")
def downloadWhiteMatterExplorationDTIVolume(self):
return self.downloadSample("DTIVolume")
def downloadDiffusionMRIDWIVolume(self):
return self.downloadSample("DWIVolume")
def downloadAbdominalCTVolume(self):
return self.downloadSample("CTAAbdomenPanoramix")
def downloadDentalSurgery(self):
# returns list since that's what earlier method did
return self.downloadSamples("CBCTDentalSurgery")
def downloadMRUSPostate(self):
# returns list since that's what earlier method did
return self.downloadSamples("MRUSProstate")
def humanFormatSize(self, size):
"""from https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size"""
for x in ["bytes", "KB", "MB", "GB"]:
if size < 1024.0 and size > -1024.0:
return f"{size:3.1f} {x}"
size /= 1024.0
return "{:3.1f} {}".format(size, "TB")
def reportHook(self, blocksSoFar, blockSize, totalSize):
# we clamp to 100% because the blockSize might be larger than the file itself
percent = min(int((100.0 * blocksSoFar * blockSize) / totalSize), 100)
if percent == 100 or (percent - self.downloadPercent >= 10):
# we clamp to totalSize when blockSize is larger than totalSize
humanSizeSoFar = self.humanFormatSize(min(blocksSoFar * blockSize, totalSize))
humanSizeTotal = self.humanFormatSize(totalSize)
self.logMessage("<i>" + _("Downloaded {sizeCompleted} ({percentCompleted}% of {sizeTotal})...").format(
sizeCompleted=humanSizeSoFar, percentCompleted=percent, sizeTotal=humanSizeTotal) + "</i>")
self.downloadPercent = percent
@staticmethod
def rewriteUrl(url):
"""Apply all registered URL rewrite rules to the given URL."""
try:
slicer.modules.sampleDataUrlRewriteRules
except AttributeError:
# No rules are registered
return url
for rule in slicer.modules.sampleDataUrlRewriteRules.values():
url = rule(url)
return url
def downloadFile(self, uri, destFolderPath, name, checksum=None, forceDownload=False):
"""
:param uri: Download URL. It is rewritten using registered URL rewrite rules before download.
:param destFolderPath: Folder to download the file into.
:param name: File name that will be downloaded.
:param checksum: Checksum formatted as ``<algo>:<digest>`` to verify the downloaded file. For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``.
:param forceDownload: If True then the file is always downloaded even if it already exists in the destination folder.
"""
uri = self.rewriteUrl(uri)
self.downloadPercent = 0
filePath = destFolderPath + "/" + name
(algo, digest) = extractAlgoAndDigest(checksum)
if forceDownload or not os.path.exists(filePath) or os.stat(filePath).st_size == 0:
import urllib.request, urllib.parse, urllib.error
self.logMessage(_("Requesting download {name} from {uri} ...").format(name=name, uri=uri))
try:
urllib.request.urlretrieve(uri, filePath, self.reportHook)
self.logMessage(_("Download finished"))
except OSError as e:
self.logMessage("\t" + _("Download failed: {errorMessage}").format(errorMessage=e), logging.ERROR)
raise ValueError(_("Failed to download {uri} to {filePath}").format(uri=uri, filePath=filePath))
if algo is not None:
self.logMessage(_("Verifying checksum"))
current_digest = computeChecksum(algo, filePath)
if current_digest != digest:
self.logMessage(
_("Checksum verification failed. Computed checksum {currentChecksum} different from expected checksum {expectedChecksum}").format(
currentChecksum=current_digest, expectedChecksum=digest))
qt.QFile(filePath).remove()
else:
self.downloadPercent = 100
self.logMessage(_("Checksum OK"))
else:
if algo is not None:
self.logMessage(_("Verifying checksum"))
current_digest = computeChecksum(algo, filePath)
if current_digest != digest:
self.logMessage(_("File already exists in cache but checksum is different - re-downloading it."))
qt.QFile(filePath).remove()
return self.downloadFile(uri, destFolderPath, name, checksum)
else:
self.downloadPercent = 100
self.logMessage(_("File already exists and checksum is OK - reusing it."))
else:
self.downloadPercent = 100
self.logMessage(_("File already exists in cache - reusing it."))
return filePath
def loadScene(self, uri, fileProperties={}):
"""Returns True is scene loading was successful, False if failed."""
loadedNode = self.loadNode(uri, None, "SceneFile", fileProperties)
success = loadedNode is not None
return success
def loadNode(self, uri, name, fileType=None, fileProperties={}):
"""Returns the first loaded node (or the scene if the reader did not provide a specific node) on success.
Returns None if failed.
"""
self.logMessage("<b>" + _("Requesting load {name} from {uri} ...").format(name=name, uri=uri) + "</b>")
fileProperties["fileName"] = uri
if name:
fileProperties["name"] = name
if not fileType:
fileType = slicer.app.coreIOManager().fileType(fileProperties["fileName"])
firstLoadedNode = None
loadedNodes = vtk.vtkCollection()
success = slicer.app.coreIOManager().loadNodes(fileType, fileProperties, loadedNodes)
if not success:
if loadedNodes.GetNumberOfItems() < 1:
self.logMessage("\t" + _("Load failed!"), logging.ERROR)
return None
else:
# Loading did not fail, because some nodes were loaded, proceed with a warning
self.logMessage(_("Error was reported while loading {count} nodes from {path}").format(
count=loadedNodes.GetNumberOfItems(), path=uri), logging.WARNING)
self.logMessage("<b>" + _("Load finished") + "</b><p></p>")
# since nodes were read from a temp directory remove the storage nodes
for i in range(loadedNodes.GetNumberOfItems()):
loadedNode = loadedNodes.GetItemAsObject(i)
if not loadedNode.IsA("vtkMRMLStorableNode"):
continue
storageNode = loadedNode.GetStorageNode()
if not storageNode:
continue
slicer.mrmlScene.RemoveNode(storageNode)
loadedNode.SetAndObserveStorageNodeID(None)
firstLoadedNode = loadedNodes.GetItemAsObject(0)
if firstLoadedNode:
return firstLoadedNode
else:
# If a reader does not report loading of any specific node (it may happen for example with a scene reader)
# then return the scene to distinguish from a load error.
return slicer.mrmlScene
class SampleDataTest(ScriptedLoadableModuleTest):
"""
This is the test case for your scripted module.
Uses ScriptedLoadableModuleTest base class, available at:
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""
customDownloads = []
def setUp(self):
slicer.mrmlScene.Clear(0)
SampleDataTest.customDownloads = []
def runTest(self):
for test in [
self.test_downloadFromSource_downloadFiles,
self.test_downloadFromSource_downloadZipFile,
self.test_downloadFromSource_loadMRBFile,
self.test_downloadFromSource_loadMRMLFile,
self.test_downloadFromSource_downloadMRBFile,
self.test_downloadFromSource_downloadMRMLFile,
self.test_downloadFromSource_loadNode,
self.test_downloadFromSource_loadNodeFromMultipleFiles,
self.test_downloadFromSource_loadNodes,
self.test_downloadFromSource_loadNodesWithLoadFileFalse,
self.test_sampleDataSourcesByCategory,
self.test_categoryVisibility,
self.test_setCategoriesFromSampleDataSources,
self.test_isSampleDataSourceRegistered,
self.test_defaultFileType,
self.test_customDownloader,
self.test_categoryForSource,
]:
self.setUp()
test()
@staticmethod
def path2uri(path):
"""Gets a URI from a local file path.
Typically it prefixes the received path by file:// or file:///.
"""
import urllib.parse, urllib.request, urllib.parse, urllib.error
return urllib.parse.urljoin("file:", urllib.request.pathname2url(path))
def test_downloadFromSource_downloadFiles(self):
"""Specifying URIs and fileNames without nodeNames is expected to download the files
without loading into Slicer.
"""
logic = SampleDataLogic()
sceneMTime = slicer.mrmlScene.GetMTime()
filePaths = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
fileNames="MR-head.nrrd", loadFiles=False))
self.assertEqual(len(filePaths), 1)
self.assertTrue(os.path.exists(filePaths[0]))
self.assertTrue(os.path.isfile(filePaths[0]))
self.assertEqual(sceneMTime, slicer.mrmlScene.GetMTime())
sceneMTime = slicer.mrmlScene.GetMTime()
filePaths = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
TESTING_DATA_URL + "SHA256/4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e"],
fileNames=["MR-head.nrrd", "CT-chest.nrrd"],
loadFiles=[False, False]))
self.assertEqual(len(filePaths), 2)
self.assertTrue(os.path.exists(filePaths[0]))
self.assertTrue(os.path.isfile(filePaths[0]))
self.assertTrue(os.path.exists(filePaths[1]))
self.assertTrue(os.path.isfile(filePaths[1]))
self.assertEqual(sceneMTime, slicer.mrmlScene.GetMTime())
def test_downloadFromSource_downloadZipFile(self):
logic = SampleDataLogic()
sceneMTime = slicer.mrmlScene.GetMTime()
filePaths = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "SHA256/b902f635ef2059cd3b4ba854c000b388e4a9e817a651f28be05c22511a317ec7",
fileNames="TinyPatient_Seg.zip",
loadFileTypes="ZipFile"))
self.assertEqual(len(filePaths), 1)
self.assertTrue(os.path.exists(filePaths[0]))
self.assertTrue(os.path.isdir(filePaths[0]))
self.assertEqual(sceneMTime, slicer.mrmlScene.GetMTime())
def test_downloadFromSource_loadMRBFile(self):
logic = SampleDataLogic()
sceneMTime = slicer.mrmlScene.GetMTime()
nodes = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "SHA256/5a1c78c3347f77970b1a29e718bfa10e5376214692d55a7320af94b9d8d592b8",
loadFiles=True, fileNames="slicer4minute.mrb"))
self.assertEqual(len(nodes), 1)
self.assertIsInstance(nodes[0], slicer.vtkMRMLCameraNode)
self.assertTrue(sceneMTime < slicer.mrmlScene.GetMTime())
def test_downloadFromSource_loadMRMLFile(self):
logic = SampleDataLogic()
tempFile = qt.QTemporaryFile(slicer.app.temporaryPath + "/SampleDataTest-loadSceneFile-XXXXXX.mrml")
tempFile.open()
tempFile.write(textwrap.dedent("""
<?xml version="1.0" encoding="UTF-8"?>
<MRML version="Slicer4.4.0" userTags="">
</MRML>
""").strip())
tempFile.close()
sceneMTime = slicer.mrmlScene.GetMTime()
nodes = logic.downloadFromSource(SampleDataSource(
uris=self.path2uri(tempFile.fileName()), loadFiles=True, fileNames="scene.mrml"))
self.assertEqual(len(nodes), 1)
self.assertIsInstance(nodes[0], slicer.vtkMRMLScene)
self.assertTrue(sceneMTime < slicer.mrmlScene.GetMTime())
def test_downloadFromSource_downloadMRBFile(self):
logic = SampleDataLogic()
sceneMTime = slicer.mrmlScene.GetMTime()
filePaths = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "SHA256/5a1c78c3347f77970b1a29e718bfa10e5376214692d55a7320af94b9d8d592b8",
fileNames="slicer4minute.mrb",
loadFileTypes="SceneFile"))
self.assertEqual(len(filePaths), 1)
self.assertTrue(os.path.exists(filePaths[0]))
self.assertTrue(os.path.isfile(filePaths[0]))
self.assertEqual(sceneMTime, slicer.mrmlScene.GetMTime())
def test_downloadFromSource_downloadMRMLFile(self):
logic = SampleDataLogic()
tempFile = qt.QTemporaryFile(slicer.app.temporaryPath + "/SampleDataTest-loadSceneFile-XXXXXX.mrml")
tempFile.open()
tempFile.write(textwrap.dedent("""
<?xml version="1.0" encoding="UTF-8"?>
<MRML version="Slicer4.4.0" userTags="">
</MRML>
""").strip())
tempFile.close()
sceneMTime = slicer.mrmlScene.GetMTime()
filePaths = logic.downloadFromSource(SampleDataSource(
uris=self.path2uri(tempFile.fileName()), fileNames="scene.mrml", loadFileTypes="SceneFile"))
self.assertEqual(len(filePaths), 1)
self.assertTrue(os.path.exists(filePaths[0]))
self.assertTrue(os.path.isfile(filePaths[0]))
self.assertEqual(sceneMTime, slicer.mrmlScene.GetMTime())
def test_downloadFromSource_loadNode(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "MD5/39b01631b7b38232a220007230624c8e",
fileNames="MR-head.nrrd", nodeNames="MRHead"))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0], slicer.mrmlScene.GetFirstNodeByName("MRHead"))
def test_downloadFromSource_loadNodeWithoutNodeName(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=TESTING_DATA_URL + "MD5/39b01631b7b38232a220007230624c8e",
fileNames="MR-head.nrrd"))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0], slicer.mrmlScene.GetFirstNodeByName("MR-head"))
def test_downloadFromSource_loadNodeFromMultipleFiles(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/bbfd8dd1914e9d7af09df3f6e254374064f6993a37552cc587581623a520a11f",
TESTING_DATA_URL + "SHA256/33825585b01a506e532934581a7bddd9de9e7b898e24adfed5454ffc6dfe48ea"],
fileNames=["MRHeadResampled.raw.gz", "MRHeadResampled.nhdr"],
nodeNames=[None, "MRHeadResampled"],
loadFiles=[False, True]))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0], slicer.mrmlScene.GetFirstNodeByName("MRHeadResampled"))
def test_downloadFromSource_loadNodesWithLoadFileFalse(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
TESTING_DATA_URL + "SHA256/4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e"],
fileNames=["MR-head.nrrd", "CT-chest.nrrd"],
nodeNames=["MRHead", "CTChest"],
loadFiles=[False, True]))
self.assertEqual(len(nodes), 2)
self.assertTrue(os.path.exists(nodes[0]))
self.assertTrue(os.path.isfile(nodes[0]))
self.assertEqual(nodes[1], slicer.mrmlScene.GetFirstNodeByName("CTChest"))
def test_downloadFromSource_loadNodes(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
TESTING_DATA_URL + "SHA256/4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e"],
fileNames=["MR-head.nrrd", "CT-chest.nrrd"],
nodeNames=["MRHead", "CTChest"]))
self.assertEqual(len(nodes), 2)
self.assertEqual(nodes[0], slicer.mrmlScene.GetFirstNodeByName("MRHead"))
self.assertEqual(nodes[1], slicer.mrmlScene.GetFirstNodeByName("CTChest"))
def test_downloadFromSource_loadNodesWithoutNodeNames(self):
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
TESTING_DATA_URL + "SHA256/4507b664690840abb6cb9af2d919377ffc4ef75b167cb6fd0f747befdb12e38e"],
fileNames=["MR-head.nrrd", "CT-chest.nrrd"]))
self.assertEqual(len(nodes), 2)
self.assertEqual(nodes[0], slicer.mrmlScene.GetFirstNodeByName("MR-head"))
self.assertEqual(nodes[1], slicer.mrmlScene.GetFirstNodeByName("CT-chest"))
def test_sampleDataSourcesByCategory(self):
self.assertTrue(len(SampleDataLogic.sampleDataSourcesByCategory()) > 0)
self.assertTrue(len(SampleDataLogic.sampleDataSourcesByCategory("General")) > 0)
self.assertTrue(len(SampleDataLogic.sampleDataSourcesByCategory("Not_A_Registered_Category")) == 0)
def test_categoryVisibility(self):
slicer.util.selectModule("SampleData")
widget = slicer.modules.SampleDataWidget
widget.setCategoryVisible("General", False)
self.assertFalse(widget.isCategoryVisible("General"))
widget.setCategoryVisible("General", True)
self.assertTrue(widget.isCategoryVisible("General"))
def test_setCategoriesFromSampleDataSources(self):
slicer.util.selectModule("SampleData")
widget = slicer.modules.SampleDataWidget
self.assertGreater(widget.categoryLayout.count(), 0)
SampleDataWidget.removeCategories(widget.categoryLayout)
self.assertEqual(widget.categoryLayout.count(), 0)
SampleDataWidget.setCategoriesFromSampleDataSources(widget.categoryLayout, slicer.modules.sampleDataSources, widget.logic)
self.assertGreater(widget.categoryLayout.count(), 0)
def test_isSampleDataSourceRegistered(self):
if not slicer.app.testingEnabled():
return
sourceArguments = {
"sampleName": "isSampleDataSourceRegistered",
"uris": "https://slicer.org",
"fileNames": "volume.nrrd",
"loadFileTypes": "VolumeFile",
}
self.assertFalse(SampleDataLogic.isSampleDataSourceRegistered("Testing", SampleDataSource(**sourceArguments)))
SampleDataLogic.registerCustomSampleDataSource(**sourceArguments, category="Testing")
self.assertTrue(SampleDataLogic.isSampleDataSourceRegistered("Testing", SampleDataSource(**sourceArguments)))
self.assertFalse(SampleDataLogic.isSampleDataSourceRegistered("Other", SampleDataSource(**sourceArguments)))
def test_defaultFileType(self):
"""Test that file type is guessed correctly when not specified."""
logic = SampleDataLogic()
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "MD5/958737f8621437b18e4d8ab16eb65ad7"],
fileNames=["brainMesh.vtk"],
nodeNames=["brainMesh"]))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0].GetClassName(), "vtkMRMLModelNode")
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/3243b62bde36b1db1cdbfe204785bd4bc1fbb772558d5f8cac964cda8385d470"],
fileNames=["TinyPatient_Structures.seg.nrrd"],
nodeNames=["TinyPatient_Segments"]))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0].GetClassName(), "vtkMRMLSegmentationNode")
nodes = logic.downloadFromSource(SampleDataSource(
uris=[TESTING_DATA_URL + "SHA256/72fbc8c8e0e4fc7c3f628d833e4a6fb7adbe15b0bac2f8669f296e052414578c"],
fileNames=["FastNonrigidBSplineregistrationTransform.tfm"],
nodeNames=["FastNonrigidBSplineregistrationTransform"]))
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0].GetClassName(), "vtkMRMLBSplineTransformNode")
class CustomDownloader:
def __call__(self, source):
SampleDataTest.customDownloads.append(source)
CustomDownloaderDataSource = {
"category": "Testing",
"sampleName": "customDownloader",
"uris": "http://down.load/test",
"fileNames": "cust.om",
"customDownloader": CustomDownloader(),
}
def test_customDownloader(self):
if not slicer.app.testingEnabled():
return
slicer.util.selectModule("SampleData")
widget = slicer.modules.SampleDataWidget
button = slicer.util.findChild(widget.parent, "customDownloaderPushButton")
self.assertEqual(self.customDownloads, [])
button.click()
self.assertEqual(len(self.customDownloads), 1)
self.assertEqual(self.customDownloads[0].sampleName, "customDownloader")
def test_categoryForSource(self):
logic = SampleDataLogic()
source = slicer.modules.sampleDataSources[logic.builtInCategoryName][0]
self.assertEqual(logic.categoryForSource(source), logic.builtInCategoryName)
|