-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
import random | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_sample_granules( | ||
granules: list, | ||
sample_size: int, | ||
max_granule_size: int | float, | ||
round_ndigits: int = None, | ||
): | ||
"""Return a list of randomly-sampled granules and their size in MB. | ||
Attempt to find only granules smaller or equal to max_granule_size. May return a | ||
sample smaller than sample_size. | ||
""" | ||
sample = [] | ||
total_size = 0 | ||
max_tries = sample_size * 2 | ||
tries = 0 | ||
|
||
while tries <= max_tries: | ||
g = random.sample(granules, 1)[0] | ||
if g.size() > max_granule_size: | ||
logger.debug( | ||
f"Granule {g['meta']['concept-id']} exceded max size: {g.size()}." | ||
"Trying another random sample." | ||
) | ||
tries += 1 | ||
continue | ||
else: | ||
logger.debug( | ||
f"Adding granule to random sample: {g['meta']['concept-id']} size: {g.size()}" | ||
) | ||
sample.append(g) | ||
total_size += g.size() | ||
if len(sample) >= sample_size: | ||
break | ||
|
||
return sample, round(total_size, round_ndigits) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters