Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

conftest.py 3.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. import pytest
  2. import suite2p
  3. import os
  4. import zipfile
  5. import tempfile
  6. import shutil
  7. from tqdm import tqdm
  8. from pathlib import Path
  9. from urllib.request import urlopen
  10. from tenacity import retry
  11. @pytest.fixture()
  12. def data_dir():
  13. """
  14. Initializes data input directory for tests. Downloads test_inputs and test_outputs if not present.
  15. """
  16. data_path = Path('data/')
  17. data_path.mkdir(exist_ok=True)
  18. download_cached_inputs(data_path)
  19. cached_outputs = data_path.joinpath('test_outputs')
  20. cached_outputs_url = 'https://www.suite2p.org/static/test_data/test_outputs.zip'
  21. if not os.path.exists(cached_outputs):
  22. extract_zip(data_path.joinpath('test_outputs.zip'), cached_outputs_url, data_path)
  23. return data_path
  24. @pytest.fixture()
  25. def test_ops(tmpdir, data_dir):
  26. """Initializes ops to be used for tests. Also, uses tmpdir fixture to create a unique temporary dir for each test."""
  27. return initialize_ops(tmpdir, data_dir)
  28. def download_cached_inputs(data_path):
  29. """ Downloads test_input data if not present on machine. This function was created so it can also be used by scripts/generate_test_data.py."""
  30. cached_inputs = data_path.joinpath('test_inputs')
  31. cached_inputs_url = 'https://www.suite2p.org/static/test_data/test_inputs.zip'
  32. if not os.path.exists(cached_inputs):
  33. extract_zip(data_path.joinpath('test_inputs.zip'), cached_inputs_url, data_path)
  34. def initialize_ops(tmpdir, data_dir):
  35. """Initializes ops. Used for both the test_ops function above and for generate_test_data script. This function was made to accomodate creation of ops for both pytest and non-pytest settings."""
  36. ops = suite2p.default_ops()
  37. ops.update(
  38. {
  39. 'use_builtin_classifier': True,
  40. 'data_path': [Path(data_dir).joinpath('test_inputs')],
  41. 'save_path0': str(tmpdir),
  42. 'norm_frames': False,
  43. 'denoise': False,
  44. 'soma_crop': False
  45. }
  46. )
  47. return ops
  48. def extract_zip(cached_file, url, data_path):
  49. download_url_to_file(url, cached_file)
  50. with zipfile.ZipFile(cached_file,"r") as zip_ref:
  51. zip_ref.extractall(data_path)
  52. @retry
  53. def download_url_to_file(url, dst, progress=True):
  54. r"""Download object at the given URL to a local path.
  55. Thanks to torch, slightly modified
  56. Args:
  57. url (string): URL of the object to download
  58. dst (string): Full path where object will be saved, e.g. `/tmp/temporary_file`
  59. progress (bool, optional): whether or not to display a progress bar to stderr
  60. Default: True
  61. """
  62. file_size = None
  63. import ssl
  64. ssl._create_default_https_context = ssl._create_unverified_context
  65. u = urlopen(url)
  66. meta = u.info()
  67. if hasattr(meta, 'getheaders'):
  68. content_length = meta.getheaders("Content-Length")
  69. else:
  70. content_length = meta.get_all("Content-Length")
  71. if content_length is not None and len(content_length) > 0:
  72. file_size = int(content_length[0])
  73. # We deliberately save it in a temp file and move it after
  74. dst = os.path.expanduser(dst)
  75. dst_dir = os.path.dirname(dst)
  76. f = tempfile.NamedTemporaryFile(delete=False, dir=dst_dir)
  77. print(f"\nDownloading: {url}")
  78. try:
  79. with tqdm(total=file_size, disable=not progress,
  80. unit='B', unit_scale=True, unit_divisor=1024) as pbar:
  81. while True:
  82. buffer = u.read(8192)
  83. if len(buffer) == 0:
  84. break
  85. f.write(buffer)
  86. pbar.update(len(buffer))
  87. f.close()
  88. shutil.move(f.name, dst)
  89. finally:
  90. f.close()
  91. if os.path.exists(f.name):
  92. os.remove(f.name)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...