-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-assets-ranges.py
executable file
·50 lines (40 loc) · 1.35 KB
/
generate-assets-ranges.py
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
#!/usr/bin/env python
import random
import requests
def generate():
es_docs = 20
es_url = 'https://search-api.stage.hubmapconsortium.org/portal/search'
assets_url = 'https://assets.stage.hubmapconsortium.org'
rows_per_file = 20
chunk_size = 2**16
response = requests.post(
es_url,
json={
"post_filter": {
"exists": {"field": "files"}
},
"size": es_docs,
"_source": ["uuid", "files"],
"sort": {"uuid.keyword": "asc"}
},
headers={
'Content-Type': 'application/json'
}).json()
file_sizes = {}
for hit in response['hits']['hits']:
uuid = hit['_source']['uuid']
for file_info in hit['_source']['files']:
if file_info['rel_path'].endswith('.ome.tiff'):
full_url = f'{assets_url}/{uuid}/{file_info["rel_path"]}'
file_sizes[full_url] = file_info['size']
for (url, size) in file_sizes.items():
chunk_count = size // chunk_size
if chunk_count < rows_per_file:
continue
chunks = random.sample(range(chunk_count), rows_per_file)
for i in chunks:
bottom = i * chunk_size
top = (i + 1) * chunk_size - 1
print(f'{size},{bottom}-{top},{url}')
if __name__ == "__main__":
generate()