torch-em 0.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (352) hide show
  1. torch_em-0.9.0/LICENSE +21 -0
  2. torch_em-0.9.0/PKG-INFO +27 -0
  3. torch_em-0.9.0/README.md +80 -0
  4. torch_em-0.9.0/pyproject.toml +3 -0
  5. torch_em-0.9.0/setup.cfg +4 -0
  6. torch_em-0.9.0/setup.py +45 -0
  7. torch_em-0.9.0/test/data/__init__.py +0 -0
  8. torch_em-0.9.0/test/data/test_dataset_wrapper.py +36 -0
  9. torch_em-0.9.0/test/data/test_image_collection_dataset.py +108 -0
  10. torch_em-0.9.0/test/data/test_raw_dataset.py +173 -0
  11. torch_em-0.9.0/test/data/test_sampler.py +35 -0
  12. torch_em-0.9.0/test/data/test_segmentation_dataset.py +210 -0
  13. torch_em-0.9.0/test/data/test_tensor_dataset.py +68 -0
  14. torch_em-0.9.0/test/loss/__init__.py +0 -0
  15. torch_em-0.9.0/test/loss/test_cldice.py +68 -0
  16. torch_em-0.9.0/test/loss/test_contrastive.py +126 -0
  17. torch_em-0.9.0/test/loss/test_contrastive_impl.py +134 -0
  18. torch_em-0.9.0/test/loss/test_dice.py +69 -0
  19. torch_em-0.9.0/test/loss/test_loss_wrapper.py +154 -0
  20. torch_em-0.9.0/test/loss/test_spoco.py +67 -0
  21. torch_em-0.9.0/test/metric/__init__.py +0 -0
  22. torch_em-0.9.0/test/metric/test_cldice.py +47 -0
  23. torch_em-0.9.0/test/metric/test_metric.py +149 -0
  24. torch_em-0.9.0/test/model/__init__.py +0 -0
  25. torch_em-0.9.0/test/model/test_punet.py +56 -0
  26. torch_em-0.9.0/test/model/test_resnet3d.py +27 -0
  27. torch_em-0.9.0/test/model/test_unet.py +82 -0
  28. torch_em-0.9.0/test/model/test_unetr.py +94 -0
  29. torch_em-0.9.0/test/self_training/__init__.py +0 -0
  30. torch_em-0.9.0/test/self_training/test_fix_match.py +127 -0
  31. torch_em-0.9.0/test/self_training/test_mean_teacher.py +128 -0
  32. torch_em-0.9.0/test/test_classification.py +85 -0
  33. torch_em-0.9.0/test/test_cli.py +94 -0
  34. torch_em-0.9.0/test/test_segmentation.py +231 -0
  35. torch_em-0.9.0/test/trainer/__init__.py +0 -0
  36. torch_em-0.9.0/test/trainer/test_default_trainer.py +162 -0
  37. torch_em-0.9.0/test/trainer/test_spoco_trainer.py +123 -0
  38. torch_em-0.9.0/test/transform/__init__.py +0 -0
  39. torch_em-0.9.0/test/transform/test_augmentations.py +29 -0
  40. torch_em-0.9.0/test/transform/test_generic.py +54 -0
  41. torch_em-0.9.0/test/transform/test_invertible_augmentations.py +138 -0
  42. torch_em-0.9.0/test/transform/test_label_transforms.py +229 -0
  43. torch_em-0.9.0/test/transform/test_raw.py +103 -0
  44. torch_em-0.9.0/test/util/__init__.py +0 -0
  45. torch_em-0.9.0/test/util/test_imageio.py +29 -0
  46. torch_em-0.9.0/test/util/test_modelzoo.py +119 -0
  47. torch_em-0.9.0/test/util/test_prediction.py +90 -0
  48. torch_em-0.9.0/test/util/test_segmentation.py +82 -0
  49. torch_em-0.9.0/test/util/test_util.py +64 -0
  50. torch_em-0.9.0/torch_em/__init__.py +11 -0
  51. torch_em-0.9.0/torch_em/__version__.py +1 -0
  52. torch_em-0.9.0/torch_em/classification/__init__.py +6 -0
  53. torch_em-0.9.0/torch_em/classification/classification.py +126 -0
  54. torch_em-0.9.0/torch_em/classification/classification_dataset.py +65 -0
  55. torch_em-0.9.0/torch_em/classification/classification_logger.py +137 -0
  56. torch_em-0.9.0/torch_em/classification/classification_trainer.py +42 -0
  57. torch_em-0.9.0/torch_em/cli.py +413 -0
  58. torch_em-0.9.0/torch_em/data/__init__.py +18 -0
  59. torch_em-0.9.0/torch_em/data/concat_dataset.py +39 -0
  60. torch_em-0.9.0/torch_em/data/dataset_wrapper.py +23 -0
  61. torch_em-0.9.0/torch_em/data/datasets/__init__.py +13 -0
  62. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/__init__.py +48 -0
  63. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/aimseg.py +159 -0
  64. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/asem.py +256 -0
  65. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/astih.py +340 -0
  66. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/axondeepseg.py +277 -0
  67. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/axonem.py +150 -0
  68. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/betaseg.py +166 -0
  69. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/cellmap.py +550 -0
  70. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/cem.py +382 -0
  71. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/cremi.py +251 -0
  72. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/deepict.py +178 -0
  73. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/densecell.py +197 -0
  74. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/emneuron.py +153 -0
  75. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/emps.py +185 -0
  76. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/fafb.py +241 -0
  77. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/fafb_nuclei.py +116 -0
  78. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/fib25.py +215 -0
  79. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/hemibrain.py +243 -0
  80. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/human_cortex_h01.py +242 -0
  81. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/human_organoids.py +179 -0
  82. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/hydra_vulgaris.py +299 -0
  83. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/isbi2012.py +121 -0
  84. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/kasthuri.py +173 -0
  85. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/lucchi.py +174 -0
  86. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/malecns.py +214 -0
  87. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/manc.py +197 -0
  88. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/microns.py +527 -0
  89. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/microns_nuclei.py +119 -0
  90. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/mitoem.py +274 -0
  91. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/mitoemv2.py +309 -0
  92. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/mousecc.py +180 -0
  93. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/nisb.py +283 -0
  94. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/nuc_mm.py +159 -0
  95. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/platynereis.py +448 -0
  96. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/probtem.py +179 -0
  97. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/snemi.py +135 -0
  98. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/sponge_em.py +137 -0
  99. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/synapseweb_hippocampus.py +200 -0
  100. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/uro_cell.py +226 -0
  101. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/vnc.py +163 -0
  102. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/waenet.py +293 -0
  103. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/wildenberg.py +384 -0
  104. torch_em-0.9.0/torch_em/data/datasets/electron_microscopy/zebrafinch.py +312 -0
  105. torch_em-0.9.0/torch_em/data/datasets/histopathology/__init__.py +23 -0
  106. torch_em-0.9.0/torch_em/data/datasets/histopathology/bcss.py +243 -0
  107. torch_em-0.9.0/torch_em/data/datasets/histopathology/conic.py +234 -0
  108. torch_em-0.9.0/torch_em/data/datasets/histopathology/consep.py +209 -0
  109. torch_em-0.9.0/torch_em/data/datasets/histopathology/cpm.py +221 -0
  110. torch_em-0.9.0/torch_em/data/datasets/histopathology/cryonuseg.py +186 -0
  111. torch_em-0.9.0/torch_em/data/datasets/histopathology/cytodark0.py +211 -0
  112. torch_em-0.9.0/torch_em/data/datasets/histopathology/glas.py +172 -0
  113. torch_em-0.9.0/torch_em/data/datasets/histopathology/glysac.py +182 -0
  114. torch_em-0.9.0/torch_em/data/datasets/histopathology/hest.py +735 -0
  115. torch_em-0.9.0/torch_em/data/datasets/histopathology/janowczyk.py +231 -0
  116. torch_em-0.9.0/torch_em/data/datasets/histopathology/lizard.py +194 -0
  117. torch_em-0.9.0/torch_em/data/datasets/histopathology/lynsec.py +222 -0
  118. torch_em-0.9.0/torch_em/data/datasets/histopathology/monusac.py +294 -0
  119. torch_em-0.9.0/torch_em/data/datasets/histopathology/monuseg.py +248 -0
  120. torch_em-0.9.0/torch_em/data/datasets/histopathology/nuclick.py +149 -0
  121. torch_em-0.9.0/torch_em/data/datasets/histopathology/nuinsseg.py +128 -0
  122. torch_em-0.9.0/torch_em/data/datasets/histopathology/orion_crc.py +526 -0
  123. torch_em-0.9.0/torch_em/data/datasets/histopathology/pannuke.py +277 -0
  124. torch_em-0.9.0/torch_em/data/datasets/histopathology/panoptils.py +195 -0
  125. torch_em-0.9.0/torch_em/data/datasets/histopathology/pcns.py +325 -0
  126. torch_em-0.9.0/torch_em/data/datasets/histopathology/puma.py +345 -0
  127. torch_em-0.9.0/torch_em/data/datasets/histopathology/segpath.py +336 -0
  128. torch_em-0.9.0/torch_em/data/datasets/histopathology/srsanet.py +167 -0
  129. torch_em-0.9.0/torch_em/data/datasets/histopathology/tnbc.py +200 -0
  130. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/__init__.py +83 -0
  131. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/aisegcell.py +210 -0
  132. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/arvidsson.py +179 -0
  133. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bac_mother.py +136 -0
  134. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/balf.py +268 -0
  135. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bbbc030.py +214 -0
  136. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bbbc034.py +155 -0
  137. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bccd.py +206 -0
  138. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bitdepth_nucseg.py +156 -0
  139. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/blastospim.py +139 -0
  140. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/bmgd.py +236 -0
  141. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/brain_organoids.py +131 -0
  142. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/brifiseg.py +166 -0
  143. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cartocell.py +145 -0
  144. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/celegans_atlas.py +134 -0
  145. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cellbindb.py +166 -0
  146. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cellpose.py +176 -0
  147. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cellseg_3d.py +126 -0
  148. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cisd.py +278 -0
  149. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/covid_if.py +166 -0
  150. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cshaper.py +231 -0
  151. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/ctc.py +226 -0
  152. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/cvz_fluo.py +172 -0
  153. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/deepbacs.py +213 -0
  154. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/deepseas.py +142 -0
  155. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/dic_hepg2.py +190 -0
  156. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/dsb.py +248 -0
  157. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/dynamicnuclearnet.py +170 -0
  158. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/e11bio.py +259 -0
  159. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/embedseg_data.py +147 -0
  160. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/enseg.py +234 -0
  161. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/evican.py +335 -0
  162. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/glioma_c6.py +234 -0
  163. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/gonuclear.py +245 -0
  164. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/hpa.py +453 -0
  165. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/idr0095.py +274 -0
  166. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/ifnuclei.py +107 -0
  167. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/liconn.py +271 -0
  168. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/livecell.py +305 -0
  169. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/lpc_nucseg.py +201 -0
  170. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/lsm_mouse_embryo.py +210 -0
  171. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/medussa.py +228 -0
  172. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/microbeseg.py +174 -0
  173. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/mndino.py +230 -0
  174. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/morphonet.py +247 -0
  175. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/morphoseg.py +216 -0
  176. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/mouse_embryo.py +153 -0
  177. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/mucic.py +484 -0
  178. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/neurips_cell_seg.py +361 -0
  179. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/neurosphere.py +176 -0
  180. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/nis3d.py +156 -0
  181. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/nisnet3d.py +191 -0
  182. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/nuc_morph.py +238 -0
  183. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/omnipose.py +171 -0
  184. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/oocyteseg.py +234 -0
  185. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/orgaextractor.py +160 -0
  186. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/organoid.py +270 -0
  187. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/organoidnet.py +148 -0
  188. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/orgasegment.py +144 -0
  189. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/orgline.py +282 -0
  190. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/parhyale_regen.py +140 -0
  191. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/phmamm.py +154 -0
  192. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/plantseg.py +244 -0
  193. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/pnas_arabidopsis.py +140 -0
  194. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/scaffold_a549.py +171 -0
  195. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/segpc.py +175 -0
  196. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/slimia.py +235 -0
  197. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/spheroids_hepg2.py +139 -0
  198. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/synthmt.py +191 -0
  199. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/tissuenet.py +192 -0
  200. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/toiam.py +111 -0
  201. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/u20s.py +159 -0
  202. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/usiigaci.py +124 -0
  203. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/vgg_hela.py +160 -0
  204. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/vibrio_cholerae.py +137 -0
  205. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/vicar.py +152 -0
  206. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/wing_disc.py +198 -0
  207. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/xpress.py +149 -0
  208. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/yeastcellseg.py +187 -0
  209. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/yeastms.py +196 -0
  210. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/yeastsam.py +131 -0
  211. torch_em-0.9.0/torch_em/data/datasets/light_microscopy/yeaz.py +181 -0
  212. torch_em-0.9.0/torch_em/data/datasets/medical/__init__.py +60 -0
  213. torch_em-0.9.0/torch_em/data/datasets/medical/abus.py +165 -0
  214. torch_em-0.9.0/torch_em/data/datasets/medical/acdc.py +162 -0
  215. torch_em-0.9.0/torch_em/data/datasets/medical/acouslic_ai.py +125 -0
  216. torch_em-0.9.0/torch_em/data/datasets/medical/amd_sd.py +190 -0
  217. torch_em-0.9.0/torch_em/data/datasets/medical/amos.py +179 -0
  218. torch_em-0.9.0/torch_em/data/datasets/medical/autopet.py +152 -0
  219. torch_em-0.9.0/torch_em/data/datasets/medical/btcv.py +229 -0
  220. torch_em-0.9.0/torch_em/data/datasets/medical/busi.py +142 -0
  221. torch_em-0.9.0/torch_em/data/datasets/medical/camus.py +141 -0
  222. torch_em-0.9.0/torch_em/data/datasets/medical/cbis_ddsm.py +195 -0
  223. torch_em-0.9.0/torch_em/data/datasets/medical/chaos.py +236 -0
  224. torch_em-0.9.0/torch_em/data/datasets/medical/cholecseg8k.py +203 -0
  225. torch_em-0.9.0/torch_em/data/datasets/medical/covid19_seg.py +171 -0
  226. torch_em-0.9.0/torch_em/data/datasets/medical/covid_qu_ex.py +190 -0
  227. torch_em-0.9.0/torch_em/data/datasets/medical/ct_cadaiver.py +131 -0
  228. torch_em-0.9.0/torch_em/data/datasets/medical/curvas.py +194 -0
  229. torch_em-0.9.0/torch_em/data/datasets/medical/dca1.py +149 -0
  230. torch_em-0.9.0/torch_em/data/datasets/medical/drive.py +176 -0
  231. torch_em-0.9.0/torch_em/data/datasets/medical/dsad.py +154 -0
  232. torch_em-0.9.0/torch_em/data/datasets/medical/duke_liver.py +208 -0
  233. torch_em-0.9.0/torch_em/data/datasets/medical/feta24.py +144 -0
  234. torch_em-0.9.0/torch_em/data/datasets/medical/fundus_avseg.py +180 -0
  235. torch_em-0.9.0/torch_em/data/datasets/medical/han_seg.py +166 -0
  236. torch_em-0.9.0/torch_em/data/datasets/medical/hil_toothseg.py +174 -0
  237. torch_em-0.9.0/torch_em/data/datasets/medical/idrid.py +164 -0
  238. torch_em-0.9.0/torch_em/data/datasets/medical/ircadb.py +201 -0
  239. torch_em-0.9.0/torch_em/data/datasets/medical/isic.py +190 -0
  240. torch_em-0.9.0/torch_em/data/datasets/medical/isles.py +141 -0
  241. torch_em-0.9.0/torch_em/data/datasets/medical/jnuifm.py +122 -0
  242. torch_em-0.9.0/torch_em/data/datasets/medical/jsrt.py +179 -0
  243. torch_em-0.9.0/torch_em/data/datasets/medical/kits.py +303 -0
  244. torch_em-0.9.0/torch_em/data/datasets/medical/kvasir.py +147 -0
  245. torch_em-0.9.0/torch_em/data/datasets/medical/leg_3d_us.py +190 -0
  246. torch_em-0.9.0/torch_em/data/datasets/medical/lgg_mri.py +183 -0
  247. torch_em-0.9.0/torch_em/data/datasets/medical/m2caiseg.py +213 -0
  248. torch_em-0.9.0/torch_em/data/datasets/medical/mbh_seg.py +125 -0
  249. torch_em-0.9.0/torch_em/data/datasets/medical/mice_tumseg.py +157 -0
  250. torch_em-0.9.0/torch_em/data/datasets/medical/micro_usp.py +141 -0
  251. torch_em-0.9.0/torch_em/data/datasets/medical/montgomery.py +155 -0
  252. torch_em-0.9.0/torch_em/data/datasets/medical/motum.py +161 -0
  253. torch_em-0.9.0/torch_em/data/datasets/medical/msd.py +180 -0
  254. torch_em-0.9.0/torch_em/data/datasets/medical/oasis.py +162 -0
  255. torch_em-0.9.0/torch_em/data/datasets/medical/oimhs.py +205 -0
  256. torch_em-0.9.0/torch_em/data/datasets/medical/osic_pulmofib.py +234 -0
  257. torch_em-0.9.0/torch_em/data/datasets/medical/palm.py +174 -0
  258. torch_em-0.9.0/torch_em/data/datasets/medical/panorama.py +193 -0
  259. torch_em-0.9.0/torch_em/data/datasets/medical/papila.py +205 -0
  260. torch_em-0.9.0/torch_em/data/datasets/medical/pengwin.py +168 -0
  261. torch_em-0.9.0/torch_em/data/datasets/medical/piccolo.py +149 -0
  262. torch_em-0.9.0/torch_em/data/datasets/medical/plethora.py +238 -0
  263. torch_em-0.9.0/torch_em/data/datasets/medical/psfhs.py +148 -0
  264. torch_em-0.9.0/torch_em/data/datasets/medical/ravir.py +133 -0
  265. torch_em-0.9.0/torch_em/data/datasets/medical/sa_med2d.py +431 -0
  266. torch_em-0.9.0/torch_em/data/datasets/medical/sega.py +201 -0
  267. torch_em-0.9.0/torch_em/data/datasets/medical/segthy.py +204 -0
  268. torch_em-0.9.0/torch_em/data/datasets/medical/siim_acr.py +179 -0
  269. torch_em-0.9.0/torch_em/data/datasets/medical/spider.py +142 -0
  270. torch_em-0.9.0/torch_em/data/datasets/medical/toothfairy.py +223 -0
  271. torch_em-0.9.0/torch_em/data/datasets/medical/uwaterloo_skin.py +161 -0
  272. torch_em-0.9.0/torch_em/data/datasets/medical/verse.py +142 -0
  273. torch_em-0.9.0/torch_em/data/datasets/util.py +593 -0
  274. torch_em-0.9.0/torch_em/data/image_collection_dataset.py +264 -0
  275. torch_em-0.9.0/torch_em/data/pseudo_label_dataset.py +86 -0
  276. torch_em-0.9.0/torch_em/data/raw_dataset.py +383 -0
  277. torch_em-0.9.0/torch_em/data/raw_image_collection_dataset.py +187 -0
  278. torch_em-0.9.0/torch_em/data/sampler.py +233 -0
  279. torch_em-0.9.0/torch_em/data/segmentation_dataset.py +287 -0
  280. torch_em-0.9.0/torch_em/data/tensor_dataset.py +90 -0
  281. torch_em-0.9.0/torch_em/loss/__init__.py +15 -0
  282. torch_em-0.9.0/torch_em/loss/affinity_side_loss.py +172 -0
  283. torch_em-0.9.0/torch_em/loss/cldice.py +216 -0
  284. torch_em-0.9.0/torch_em/loss/combined_loss.py +38 -0
  285. torch_em-0.9.0/torch_em/loss/contrastive.py +169 -0
  286. torch_em-0.9.0/torch_em/loss/contrastive_impl.py +272 -0
  287. torch_em-0.9.0/torch_em/loss/dice.py +256 -0
  288. torch_em-0.9.0/torch_em/loss/distance_based.py +69 -0
  289. torch_em-0.9.0/torch_em/loss/spoco_loss.py +643 -0
  290. torch_em-0.9.0/torch_em/loss/wrapper.py +183 -0
  291. torch_em-0.9.0/torch_em/metric/__init__.py +8 -0
  292. torch_em-0.9.0/torch_em/metric/cldice.py +63 -0
  293. torch_em-0.9.0/torch_em/metric/instance_segmentation_metric.py +483 -0
  294. torch_em-0.9.0/torch_em/model/__init__.py +8 -0
  295. torch_em-0.9.0/torch_em/model/probabilistic_unet.py +475 -0
  296. torch_em-0.9.0/torch_em/model/resnet3d.py +518 -0
  297. torch_em-0.9.0/torch_em/model/unet.py +728 -0
  298. torch_em-0.9.0/torch_em/model/unetr.py +1018 -0
  299. torch_em-0.9.0/torch_em/model/vim.py +267 -0
  300. torch_em-0.9.0/torch_em/model/vit.py +1017 -0
  301. torch_em-0.9.0/torch_em/multi_gpu_training.py +190 -0
  302. torch_em-0.9.0/torch_em/segmentation.py +574 -0
  303. torch_em-0.9.0/torch_em/self_training/__init__.py +13 -0
  304. torch_em-0.9.0/torch_em/self_training/augmentations.py +4 -0
  305. torch_em-0.9.0/torch_em/self_training/fix_match.py +689 -0
  306. torch_em-0.9.0/torch_em/self_training/logger.py +404 -0
  307. torch_em-0.9.0/torch_em/self_training/loss.py +406 -0
  308. torch_em-0.9.0/torch_em/self_training/mean_teacher.py +723 -0
  309. torch_em-0.9.0/torch_em/self_training/probabilistic_unet_trainer.py +117 -0
  310. torch_em-0.9.0/torch_em/self_training/pseudo_labeling.py +397 -0
  311. torch_em-0.9.0/torch_em/self_training/uni_match_v2.py +435 -0
  312. torch_em-0.9.0/torch_em/shallow2deep/__init__.py +8 -0
  313. torch_em-0.9.0/torch_em/shallow2deep/prepare_shallow2deep.py +880 -0
  314. torch_em-0.9.0/torch_em/shallow2deep/pseudolabel_training.py +162 -0
  315. torch_em-0.9.0/torch_em/shallow2deep/shallow2deep_dataset.py +403 -0
  316. torch_em-0.9.0/torch_em/shallow2deep/shallow2deep_eval.py +231 -0
  317. torch_em-0.9.0/torch_em/shallow2deep/shallow2deep_model.py +205 -0
  318. torch_em-0.9.0/torch_em/shallow2deep/transform.py +86 -0
  319. torch_em-0.9.0/torch_em/trainer/__init__.py +3 -0
  320. torch_em-0.9.0/torch_em/trainer/default_trainer.py +852 -0
  321. torch_em-0.9.0/torch_em/trainer/flashoptim_trainer.py +111 -0
  322. torch_em-0.9.0/torch_em/trainer/logger_base.py +18 -0
  323. torch_em-0.9.0/torch_em/trainer/spoco_trainer.py +152 -0
  324. torch_em-0.9.0/torch_em/trainer/tensorboard_logger.py +162 -0
  325. torch_em-0.9.0/torch_em/trainer/wandb_logger.py +118 -0
  326. torch_em-0.9.0/torch_em/transform/__init__.py +8 -0
  327. torch_em-0.9.0/torch_em/transform/augmentation.py +302 -0
  328. torch_em-0.9.0/torch_em/transform/defect.py +245 -0
  329. torch_em-0.9.0/torch_em/transform/generic.py +235 -0
  330. torch_em-0.9.0/torch_em/transform/invertible_augmentations.py +199 -0
  331. torch_em-0.9.0/torch_em/transform/label.py +633 -0
  332. torch_em-0.9.0/torch_em/transform/nnunet_raw.py +96 -0
  333. torch_em-0.9.0/torch_em/transform/raw.py +389 -0
  334. torch_em-0.9.0/torch_em/util/__init__.py +21 -0
  335. torch_em-0.9.0/torch_em/util/debug.py +178 -0
  336. torch_em-0.9.0/torch_em/util/grid_search.py +232 -0
  337. torch_em-0.9.0/torch_em/util/image.py +115 -0
  338. torch_em-0.9.0/torch_em/util/modelzoo.py +893 -0
  339. torch_em-0.9.0/torch_em/util/prediction.py +320 -0
  340. torch_em-0.9.0/torch_em/util/reporting.py +77 -0
  341. torch_em-0.9.0/torch_em/util/segmentation.py +233 -0
  342. torch_em-0.9.0/torch_em/util/submit_slurm.py +103 -0
  343. torch_em-0.9.0/torch_em/util/test.py +59 -0
  344. torch_em-0.9.0/torch_em/util/training.py +19 -0
  345. torch_em-0.9.0/torch_em/util/util.py +486 -0
  346. torch_em-0.9.0/torch_em/util/validation.py +265 -0
  347. torch_em-0.9.0/torch_em.egg-info/PKG-INFO +27 -0
  348. torch_em-0.9.0/torch_em.egg-info/SOURCES.txt +350 -0
  349. torch_em-0.9.0/torch_em.egg-info/dependency_links.txt +1 -0
  350. torch_em-0.9.0/torch_em.egg-info/entry_points.txt +7 -0
  351. torch_em-0.9.0/torch_em.egg-info/requires.txt +15 -0
  352. torch_em-0.9.0/torch_em.egg-info/top_level.txt +2 -0
torch_em-0.9.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Constantin Pape
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,27 @@
1
+ Metadata-Version: 2.4
2
+ Name: torch_em
3
+ Version: 0.9.0
4
+ Home-page: https://github.com/constantinpape/torch-em
5
+ Author: Constantin Pape
6
+ License: MIT
7
+ License-File: LICENSE
8
+ Requires-Dist: bioimage-cpp
9
+ Requires-Dist: bioimageio.core
10
+ Requires-Dist: h5py
11
+ Requires-Dist: imageio
12
+ Requires-Dist: kornia
13
+ Requires-Dist: mrcfile
14
+ Requires-Dist: natsort
15
+ Requires-Dist: python-elf>=0.9.1
16
+ Requires-Dist: requests
17
+ Requires-Dist: scikit-image
18
+ Requires-Dist: tensorboard
19
+ Requires-Dist: tifffile
20
+ Requires-Dist: torch
21
+ Requires-Dist: torchvision
22
+ Requires-Dist: tqdm
23
+ Dynamic: author
24
+ Dynamic: home-page
25
+ Dynamic: license
26
+ Dynamic: license-file
27
+ Dynamic: requires-dist
@@ -0,0 +1,80 @@
1
+ [![DOC](https://shields.mitmproxy.org/badge/docs-pdoc.dev-brightgreen.svg)](https://constantinpape.github.io/torch-em/torch_em.html)
2
+ [![Build Status](https://github.com/constantinpape/torch-em/workflows/test/badge.svg)](https://github.com/constantinpape/torch-em/actions)
3
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5108853.svg)](https://doi.org/10.5281/zenodo.5108853)
4
+ [![Anaconda-Server Badge](https://anaconda.org/conda-forge/torch_em/badges/version.svg)](https://anaconda.org/conda-forge/torch_em)
5
+
6
+ # torch-em
7
+
8
+ Deep-learning based semantic and instance segmentation for 3D Electron Microscopy and other bioimage analysis problems based on PyTorch. Any feedback is highly appreciated, just open an issue!
9
+
10
+ **Important:** We recently migrated `torch_em` to [bioimage-cpp](https://github.com/computational-cell-analytics/bioimage-cpp). This enables installing `torch_em` via pip (in addition to conda) but changes dependencies and may still lead to some bugs. Please report any problems you encounter in an issue; you can pin `torch_em < 0.9` to install a version based on previous dependencies.
11
+
12
+ Highlights:
13
+ - Functional API with sensible defaults to train a state-of-the-art segmentation model with a few lines of code.
14
+ - Differentiable augmentations on GPU and CPU thanks to [kornia](https://github.com/kornia/kornia).
15
+ - Off-the-shelf logging with [tensorboard](https://www.tensorflow.org/tensorboard) or [wandb](https://wandb.ai/site).
16
+ - Export trained models to [bioimage.io](https://bioimage.io/#/) model format with one function call to deploy them in [ilastik](https://www.ilastik.org/documentation/nn/nn) or [deepimageJ](https://deepimagej.github.io/deepimagej/).
17
+
18
+ Design:
19
+ - All parameters are specified in code, no configuration files.
20
+ - No callback logic; to extend the core functionality inherit from `torch_em.trainer.DefaultTrainer` instead.
21
+ - All data-loading is lazy to support training on large datasets.
22
+
23
+ `torch_em` can be installed via conda: `conda install -c conda-forge torch_em`.
24
+ Find an example script for how to train a 2D U-Net with it below and check out the [documentation](https://constantinpape.github.io/torch-em/torch_em.html) for more details.
25
+
26
+ ```python
27
+ # Train a 2d U-Net for foreground and boundary segmentation of nuclei, using data from
28
+ # https://github.com/mpicbg-csbd/stardist/releases/download/0.1.0/dsb2018.zip
29
+
30
+ import torch_em
31
+ from torch_em.model import UNet2d
32
+ from torch_em.data.datasets import get_dsb_loader
33
+
34
+ model = UNet2d(in_channels=1, out_channels=2)
35
+
36
+ # Transform to convert from instance segmentation labels to foreground and boundary probabilties.
37
+ label_transform = torch_em.transform.BoundaryTransform(add_binary_target=True, ndim=2)
38
+
39
+ # Create the training and validation data loader.
40
+ data_path = "./dsb" # The training data will be downloaded and saved here.
41
+ train_loader = get_dsb_loader(
42
+ data_path,
43
+ patch_shape=(1, 256, 256),
44
+ batch_size=8,
45
+ split="train",
46
+ download=True,
47
+ label_transform=label_transform,
48
+ )
49
+ val_loader = get_dsb_loader(
50
+ data_path,
51
+ patch_shape=(1, 256, 256),
52
+ batch_size=8,
53
+ split="test",
54
+ label_transform=label_transform,
55
+ )
56
+
57
+ # The trainer handles the details of the training process.
58
+ # It will save checkpoints in "checkpoints/dsb-boundary-model"
59
+ # and the tensorboard logs in "logs/dsb-boundary-model".
60
+ trainer = torch_em.default_segmentation_trainer(
61
+ name="dsb-boundary-model",
62
+ model=model,
63
+ train_loader=train_loader,
64
+ val_loader=val_loader,
65
+ learning_rate=1e-4,
66
+ )
67
+ trainer.fit(iterations=5000) # Fit for 5000 iterations.
68
+
69
+ # Export the trained model to the bioimage.io model format.
70
+ from glob import glob
71
+ import imageio
72
+ from torch_em.util import export_bioimageio_model
73
+
74
+ # Load one of the images to use as reference image.
75
+ # Crop it to a shape that is guaranteed to fit the network.
76
+ test_im = imageio.imread(glob(f"{data_path}/test/images/*.tif")[0])[:256, :256]
77
+
78
+ # Export the model.
79
+ export_bioimageio_model("./checkpoints/dsb-boundary-model", "./bioimageio-model", test_im)
80
+ ```
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,45 @@
1
+ import runpy
2
+ from setuptools import setup, find_packages
3
+
4
+ __version__ = runpy.run_path("torch_em/__version__.py")["__version__"]
5
+
6
+
7
+ requires = [
8
+ "bioimage-cpp",
9
+ "bioimageio.core",
10
+ "h5py",
11
+ "imageio",
12
+ "kornia",
13
+ "mrcfile",
14
+ "natsort",
15
+ "python-elf >=0.9.1",
16
+ "requests",
17
+ "scikit-image",
18
+ "tensorboard",
19
+ "tifffile",
20
+ "torch",
21
+ "torchvision",
22
+ "tqdm",
23
+ ]
24
+
25
+
26
+ setup(
27
+ name="torch_em",
28
+ packages=find_packages(exclude=["test"]),
29
+ version=__version__,
30
+ author="Constantin Pape",
31
+ install_requires=requires,
32
+ url="https://github.com/constantinpape/torch-em",
33
+ license="MIT",
34
+ entry_points={
35
+ "console_scripts": [
36
+ "torch_em.train_2d_unet = torch_em.cli:train_2d_unet",
37
+ "torch_em.train_3d_unet = torch_em.cli:train_3d_unet",
38
+ "torch_em.predict = torch_em.cli:predict",
39
+ "torch_em.predict_with_tiling = torch_em.cli:predict_with_tiling",
40
+ "torch_em.export_bioimageio_model = torch_em.util.modelzoo:main",
41
+ "torch_em.validate_checkpoint = torch_em.util.validation:main",
42
+ # "torch_em.submit_slurm = torch_em.util.submit_slurm:main",
43
+ ]
44
+ }
45
+ )
File without changes
@@ -0,0 +1,36 @@
1
+ import os
2
+ import unittest
3
+
4
+ from torch_em.data import SegmentationDataset
5
+ from torch_em.util.test import create_segmentation_test_data
6
+
7
+
8
+ class TestDatasetWrapper(unittest.TestCase):
9
+ path = "./data.h5"
10
+ raw_key = "raw"
11
+ label_key = "labels"
12
+ shape = (32,) * 3
13
+
14
+ def setUp(self):
15
+ create_segmentation_test_data(self.path, self.raw_key, self.label_key, shape=self.shape, chunks=(32,) * 3)
16
+
17
+ def tearDown(self):
18
+ os.remove(self.path)
19
+
20
+ def test_wrap_dataset(self):
21
+ from torch_em.data import DatasetWrapper
22
+
23
+ patch_shape = (32, 32, 32)
24
+ ds = SegmentationDataset(self.path, self.raw_key, self.path, self.label_key, patch_shape=patch_shape)
25
+ wrapped_ds = DatasetWrapper(ds, lambda xy: (xy[0][:, :10, :10, :10], xy[1][:, :20, :20, :20]))
26
+
27
+ expected_shape_x = (1, 10, 10, 10)
28
+ expected_shape_y = (1, 20, 20, 20)
29
+ for i in range(3):
30
+ x, y = wrapped_ds[i]
31
+ self.assertEqual(x.shape, expected_shape_x)
32
+ self.assertEqual(y.shape, expected_shape_y)
33
+
34
+
35
+ if __name__ == "__main__":
36
+ unittest.main()
@@ -0,0 +1,108 @@
1
+ import os
2
+ import tempfile
3
+ import unittest
4
+ from glob import glob
5
+ from shutil import rmtree
6
+
7
+ import tifffile
8
+ import numpy as np
9
+ from torch_em.util.test import create_image_collection_test_data
10
+
11
+
12
+ class TestImageCollectionDataset(unittest.TestCase):
13
+ folder = "./data"
14
+ n_images = 10
15
+
16
+ def setUp(self):
17
+ create_image_collection_test_data(self.folder,
18
+ n_images=self.n_images,
19
+ min_shape=(256, 256),
20
+ max_shape=(512, 512))
21
+
22
+ def tearDown(self):
23
+ rmtree(self.folder, ignore_errors=True)
24
+
25
+ def test_dataset(self):
26
+ from torch_em.data import ImageCollectionDataset
27
+ patch_shape = (256, 256)
28
+
29
+ raw_paths = glob(os.path.join(self.folder, "images", "*.tif"))
30
+ label_paths = glob(os.path.join(self.folder, "labels", "*.tif"))
31
+ ds = ImageCollectionDataset(raw_paths, label_paths,
32
+ patch_shape=patch_shape)
33
+ self.assertEqual(len(ds), self.n_images)
34
+ self.assertEqual(ds._ndim, 2)
35
+
36
+ expected_shape = (1,) + patch_shape
37
+ for i in range(10):
38
+ x, y = ds[i]
39
+ self.assertEqual(x.shape, expected_shape)
40
+ self.assertEqual(y.shape, expected_shape)
41
+
42
+ def test_dataset_with_sampler(self):
43
+ from torch_em.data import ImageCollectionDataset
44
+ from torch_em.data.sampler import MinIntensitySampler
45
+ patch_shape = (256, 256)
46
+
47
+ raw_paths = glob(os.path.join(self.folder, "images", "*.tif"))
48
+ label_paths = glob(os.path.join(self.folder, "labels", "*.tif"))
49
+ sampler = MinIntensitySampler(min_intensity=0.51, p_reject=0.9)
50
+ ds = ImageCollectionDataset(raw_paths, label_paths,
51
+ patch_shape=patch_shape,
52
+ sampler=sampler)
53
+ self.assertEqual(len(ds), self.n_images)
54
+ self.assertEqual(ds._ndim, 2)
55
+
56
+ expected_shape = (1,) + patch_shape
57
+ for i in range(10):
58
+ x, y = ds[i]
59
+ self.assertEqual(x.shape, expected_shape)
60
+ self.assertEqual(y.shape, expected_shape)
61
+
62
+
63
+ def generate_sample_data(folder, n_images, image_shape, label_shape):
64
+ im_folder = os.path.join(folder, "images")
65
+ label_folder = os.path.join(folder, "labels")
66
+ os.makedirs(im_folder)
67
+ os.makedirs(label_folder)
68
+ for i in range(n_images):
69
+ raw = np.empty(image_shape, dtype=np.uint8)
70
+ label = np.ones(label_shape, dtype=np.float32)
71
+ tifffile.imwrite(os.path.join(im_folder, f"test_{i}.tif"), raw)
72
+ tifffile.imwrite(os.path.join(label_folder, f"test_{i}.tif"), label)
73
+
74
+
75
+ class TestImageCollectionDatasetWithChannels(unittest.TestCase):
76
+ def test_channel_end(self):
77
+ from torch_em.data import ImageCollectionDataset
78
+
79
+ patch_shape = (256, 256)
80
+
81
+ with tempfile.TemporaryDirectory() as td:
82
+ generate_sample_data(td, 10, (256, 256, 2), (256, 256))
83
+ raw_paths = glob(os.path.join(td, "images", "*.tif"))
84
+ label_paths = glob(os.path.join(td, "labels", "*.tif"))
85
+
86
+ ds = ImageCollectionDataset(raw_paths, label_paths,
87
+ patch_shape=patch_shape)
88
+ self.assertEqual(len(ds), 10)
89
+ self.assertEqual(ds._get_sample(0)[0].shape[0], 2)
90
+
91
+ def test_channel_begin(self):
92
+ from torch_em.data import ImageCollectionDataset
93
+
94
+ patch_shape = (256, 256)
95
+
96
+ with tempfile.TemporaryDirectory() as td:
97
+ generate_sample_data(td, 10, (2, 256, 256), (256, 256))
98
+ raw_paths = glob(os.path.join(td, "images", "*.tif"))
99
+ label_paths = glob(os.path.join(td, "labels", "*.tif"))
100
+
101
+ ds = ImageCollectionDataset(raw_paths, label_paths,
102
+ patch_shape=patch_shape)
103
+ self.assertEqual(len(ds), 10)
104
+ self.assertEqual(ds._get_sample(0)[0].shape[0], 2)
105
+
106
+
107
+ if __name__ == "__main__":
108
+ unittest.main()
@@ -0,0 +1,173 @@
1
+ import os
2
+ import unittest
3
+
4
+ import h5py
5
+ import numpy as np
6
+
7
+ from torch_em.data.raw_dataset import RawDataset, RawDatasetWithMasks
8
+
9
+
10
+ class TestRawDatasetBase:
11
+ """Base class to hold shared tests for RawDataset and subclasses."""
12
+
13
+ path = "./data.h5"
14
+ dataset_class = None
15
+
16
+ def tearDown(self):
17
+ os.remove(self.path)
18
+
19
+ def create_default_data(self, key, shape=None, chunks=None):
20
+ shape = (128,) * 3 if shape is None else shape
21
+ chunks = tuple(min(32, sh) for sh in shape) if chunks is None else chunks
22
+ with h5py.File(self.path, "a") as f:
23
+ f.create_dataset(key, data=np.random.rand(*shape), chunks=chunks)
24
+ return shape, chunks
25
+
26
+ def test_dataset_3d(self):
27
+ raw_key = "raw"
28
+ shape, chunks = self.create_default_data(raw_key)
29
+ patch_shape = chunks
30
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape)
31
+ self.assertEqual(ds.raw.shape, shape)
32
+ self.assertEqual(ds._ndim, 3)
33
+ expected_shape = (1,) + patch_shape
34
+ for i in range(10):
35
+ self.assertEqual(ds[i].shape, expected_shape)
36
+
37
+ def test_dataset_2d(self):
38
+ raw_key = "raw"
39
+ shape, chunks = self.create_default_data(raw_key)
40
+ patch_shape = (1, 32, 32)
41
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape, ndim=2)
42
+ self.assertEqual(ds.raw.shape, shape)
43
+ self.assertEqual(ds._ndim, 2)
44
+ expected_shape = patch_shape
45
+ for i in range(10):
46
+ self.assertEqual(ds[i].shape, expected_shape)
47
+
48
+ def test_dataset_4d(self):
49
+ raw_key = "raw"
50
+ shape = (12, 64, 64, 64)
51
+ self.create_default_data(raw_key, shape=shape)
52
+ patch_shape = (3, 32, 32, 32)
53
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape)
54
+ self.assertEqual(ds.raw.shape, shape)
55
+ self.assertEqual(ds._ndim, 4)
56
+ expected_shape = patch_shape
57
+ for i in range(10):
58
+ self.assertEqual(ds[i].shape, expected_shape)
59
+
60
+ def test_roi(self):
61
+ raw_key = "raw"
62
+ self.create_default_data(raw_key)
63
+ patch_shape = (32, 32, 32)
64
+ roi = np.s_[32:96, 32:96, 32:96]
65
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape, roi=roi)
66
+ roi_shape = 3 * (64,)
67
+ self.assertEqual(ds.raw.shape, roi_shape)
68
+ expected_shape = (1,) + patch_shape
69
+ for i in range(10):
70
+ self.assertEqual(tuple(ds[i].shape), expected_shape)
71
+
72
+ def test_with_channels(self):
73
+ raw_key = "raw"
74
+ self.create_default_data(raw_key, shape=(3, 128, 128, 128), chunks=(1, 32, 32, 32))
75
+ patch_shape = (32, 32, 32)
76
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape, ndim=3, with_channels=True)
77
+ self.assertEqual(ds._ndim, 3)
78
+ expected_raw_shape = (3,) + patch_shape
79
+ for i in range(10):
80
+ self.assertEqual(ds[i].shape, expected_raw_shape)
81
+
82
+
83
+ class TestRawDataset(TestRawDatasetBase, unittest.TestCase):
84
+ dataset_class = RawDataset
85
+
86
+
87
+ class TestRawDatasetWithMasks(TestRawDatasetBase, unittest.TestCase):
88
+ dataset_class = RawDatasetWithMasks
89
+ mrc_path = "./mask.mrc"
90
+
91
+ def tearDown(self):
92
+ super().tearDown()
93
+
94
+ if os.path.exists(self.mrc_path):
95
+ os.remove(self.mrc_path)
96
+
97
+ def create_default_mrc_data(self, data):
98
+ import mrcfile
99
+ with mrcfile.new(self.mrc_path, overwrite=True) as f:
100
+ f.set_data(data.astype(np.float32))
101
+
102
+ # subclass-specific tests, designed for a 3d dataset
103
+ def test_sample_mask(self):
104
+ from torch_em.data.sampler import MinForegroundSampler
105
+
106
+ raw_key, mask_key = "raw", "mask"
107
+ shape = (128, 128, 128)
108
+ chunks = (32, 32, 32)
109
+
110
+ # define central z slices 40:80 as the sample ROI, above and below is empty
111
+ sample_mask = np.zeros(shape, dtype=np.float32)
112
+ sample_mask[40:80, :, :] = 1.0
113
+
114
+ with h5py.File(self.path, "a") as f:
115
+ f.create_dataset(raw_key, data=sample_mask, chunks=chunks)
116
+ f.create_dataset(mask_key, data=sample_mask, chunks=chunks)
117
+
118
+ patch_shape = chunks
119
+
120
+ min_fraction = 0.95
121
+ sampler = MinForegroundSampler(min_fraction=min_fraction)
122
+
123
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape, sampler=sampler,
124
+ sample_mask_path=self.path, sample_mask_key=mask_key)
125
+
126
+ self.assertEqual(ds.raw.shape, shape)
127
+ self.assertEqual(ds.sample_mask.shape, shape)
128
+ self.assertEqual(ds._ndim, 3)
129
+
130
+ expected_shape = (1,) + patch_shape
131
+ for i in range(10):
132
+ patch = ds[i]
133
+ self.assertEqual(patch.shape, expected_shape)
134
+
135
+ # check that patches are sampled inside the ROI defined by sample mask
136
+ # note that the raw data and the sample mask are identical, so this checks if the
137
+ # positive samples from the sample mask are correct
138
+ self.assertGreaterEqual(patch.mean().item(), min_fraction)
139
+
140
+ def test_bg_mask(self):
141
+ raw_key, mask_key = "raw", "mask"
142
+
143
+ shape, chunks = self.create_default_data(raw_key)
144
+ self.create_default_data(mask_key)
145
+
146
+ patch_shape = chunks
147
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape,
148
+ bg_mask_path=self.path, bg_mask_key=mask_key)
149
+
150
+ self.assertEqual(ds.raw.shape, shape)
151
+ self.assertEqual(ds.bg_mask.shape, shape)
152
+ self.assertEqual(ds._ndim, 3)
153
+ expected_shape = (2,) + patch_shape
154
+
155
+ for i in range(10):
156
+
157
+ patch = ds[i]
158
+ self.assertEqual(patch.shape, expected_shape)
159
+
160
+ def test_bg_mask_mrc(self):
161
+ raw_key, mask_key = "raw", None
162
+
163
+ shape, chunks = self.create_default_data(raw_key)
164
+ self.create_default_mrc_data(np.zeros(shape))
165
+
166
+ patch_shape = chunks
167
+ ds = self.dataset_class(self.path, raw_key, patch_shape=patch_shape,
168
+ bg_mask_path=self.mrc_path, bg_mask_key=None)
169
+
170
+ self.assertEqual(ds.bg_mask.shape, shape)
171
+
172
+ if __name__ == "__main__":
173
+ unittest.main()
@@ -0,0 +1,35 @@
1
+ import unittest
2
+ import numpy as np
3
+
4
+
5
+ class TestSampler(unittest.TestCase):
6
+ def test_min_foreground_sampler(self):
7
+ from torch_em.data import MinForegroundSampler
8
+ threshold = 0.5
9
+ sampler = MinForegroundSampler(min_fraction=threshold)
10
+
11
+ shape = (32, 32)
12
+ n_samples = 100
13
+ for _ in range(n_samples):
14
+ data = np.random.rand(*shape)
15
+ labels = (np.random.rand(*shape) > 0.5).astype("float32")
16
+ accept = sampler(data, labels)
17
+ expected = (labels.sum() / labels.size) > threshold
18
+ self.assertEqual(accept, expected)
19
+
20
+ def test_min_intensity_sampler(self):
21
+ from torch_em.data import MinIntensitySampler
22
+ threshold = 0.5
23
+ sampler = MinIntensitySampler(min_intensity=threshold)
24
+ shape = (32, 32)
25
+
26
+ n_samples = 100
27
+ for _ in range(n_samples):
28
+ data = np.random.rand(*shape)
29
+ accept = sampler(data)
30
+ expected = np.median(data) > threshold
31
+ self.assertEqual(accept, expected)
32
+
33
+
34
+ if __name__ == "__main__":
35
+ unittest.main()