11import pytest
22import os , sys , shutil
33from cellpose import utils
4+ import zipfile
45
56from pathlib import Path
67
@@ -12,58 +13,16 @@ def image_names():
1213 ]
1314 return image_names
1415
16+ def extract_zip (cached_file , url , data_path ):
17+ utils .download_url_to_file (url , cached_file )
18+ with zipfile .ZipFile (cached_file ,"r" ) as zip_ref :
19+ zip_ref .extractall (data_path )
1520
1621@pytest .fixture ()
1722def data_dir (image_names ):
1823 cp_dir = Path .home ().joinpath (".cellpose" )
1924 cp_dir .mkdir (exist_ok = True )
25+ extract_zip (cp_dir .joinpath ("data.zip" ), "https://osf.io/download/67f022eb033d25194f82a4ee/" , cp_dir )
2026 data_dir = cp_dir .joinpath ("data" )
21- data_dir .mkdir (exist_ok = True )
22- data_dir_2D = data_dir .joinpath ("2D" )
23- data_dir_2D .mkdir (exist_ok = True )
24- data_dir_3D = data_dir .joinpath ("3D" )
25- data_dir_3D .mkdir (exist_ok = True )
26-
27- for i , image_name in enumerate (image_names ):
28- url = "https://www.cellpose.org/static/data/" + image_name
29- if i < 3 :
30- cached_file = str (data_dir_2D .joinpath (image_name ))
31- ext = ".png"
32- elif i < 5 :
33- cached_file = str (data_dir_3D .joinpath (image_name ))
34- ext = ".tif"
35- else :
36- cached_file = str (data_dir_distributed .joinpath (image_name ))
37- ext = ".tif"
38- if not os .path .exists (cached_file ):
39- print (url )
40- utils .download_url_to_file (url , cached_file )
41-
42- # check if mask downloaded (and clear potential previous test data)
43- if i < 2 :
44- train_dir = data_dir_2D .joinpath ("train" )
45- train_dir .mkdir (exist_ok = True )
46- shutil .copyfile (cached_file , train_dir .joinpath (image_name ))
47-
48- name = os .path .splitext (cached_file )[0 ]
49- mask_file = name + "_cp_masks" + ext
50- if os .path .exists (mask_file ):
51- os .remove (mask_file )
52- if i == 1 or i == 4 :
53- cached_mask_files = [
54- name + "_cyto_masks" + ext , name + "_nuclei_masks" + ext
55- ]
56- else :
57- cached_mask_files = [name + "_cyto_masks" + ext ]
58- for c , cached_mask_file in enumerate (cached_mask_files ):
59- url = "https://www.cellpose.org/static/data/" + os .path .split (
60- cached_mask_file )[- 1 ]
61- if not os .path .exists (cached_mask_file ):
62- print (cached_mask_file )
63- utils .download_url_to_file (url , cached_mask_file , progress = True )
64- if i < 2 and c == 0 :
65- shutil .copyfile (
66- cached_mask_file ,
67- train_dir .joinpath (
68- os .path .splitext (image_name )[0 ] + "_cyto_masks" + ext ))
6927 return data_dir
28+
0 commit comments