-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
124 additions
and
13 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .file_loader import FileLoader | ||
from .viz import make_gif | ||
from .types import event_dtype | ||
from .types import event_dtype | ||
from .hdf5tools import HDF5FileIterator |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ============================================================================= | ||
|
||
__author__ = "Rafael Mosca" | ||
__email__ = "rafael.mosca@mail.polimi.it" | ||
__copyright__ = "Copyright 2020 - Rafael Mosca" | ||
__license__ = "MIT" | ||
__version__ = "1.0" | ||
|
||
# ============================================================================= | ||
import numpy as np | ||
import random | ||
import click | ||
import h5py | ||
|
||
from .types import Sample, event_dtype | ||
# ============================================================================= | ||
|
||
class HDF5FileIterator: | ||
|
||
def __init__(self, filename, groups='all', n_samples='all', rand=-1): | ||
""" | ||
Returns an iterator over an HDF5 file | ||
Suggested usage is: | ||
``` | ||
iterator = HDF5FileIterator(..) | ||
for elem in iterator: | ||
# do something ... | ||
``` | ||
Params | ||
------ | ||
:param groups: the groups in the HDF5 that will be considered | ||
by default all groups | ||
:param n_samples: the number of samples that will be considered | ||
by default every sample in the group | ||
:param rand: if greater than zero it specifies the seed for the | ||
random selection, if negative it is sequential | ||
Returns | ||
------- | ||
nothing | ||
""" | ||
dataset = h5py.File(filename, 'r') | ||
self.dataset = dataset | ||
|
||
if groups == 'all': groups = list(dataset.keys()) | ||
|
||
samples = [] | ||
for group in groups: | ||
|
||
group_samples = list(dataset[group].keys()) | ||
|
||
if n_samples == 'all': | ||
n_samples = len(group_samples) | ||
|
||
elif len(group_samples) < n_samples: | ||
err_msg = f'There are insufficient samples in group {group}' | ||
click.secho(err_msg, bg='yellow') | ||
n_samples = group_samples | ||
|
||
random.seed(rand) | ||
indices = random.sample(range(0, len(group_samples)), n_samples) | ||
|
||
for i in indices: | ||
samples.append((group_samples[i], group)) | ||
|
||
if rand > 0: | ||
random.Random(rand).shuffle(samples) | ||
|
||
self.samples = samples | ||
self.index = 0 | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
|
||
while self.index < len(self.samples): | ||
sample, group = self.samples[self.index] | ||
data = self.dataset[group][sample] | ||
events_np = np.array(data, dtype=event_dtype) | ||
self.index += 1 | ||
|
||
return Sample(sample, group, events_np) | ||
|
||
else: | ||
self.dataset.close() | ||
raise StopIteration | ||
|
||
def reset(self): | ||
self.index = 0 |
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
File renamed without changes.
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 |
---|---|---|
|
@@ -130,4 +130,5 @@ dmypy.json | |
|
||
# Mac OS | ||
.Ds_Store | ||
.DS_store | ||
.DS_Store |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
matplotlib==3.2.2 | ||
click_shell==2.0 | ||
tqdm==4.46.1 | ||
h5py==2.10.0 | ||
gif==1.0.4 | ||
numpy==1.18.5 | ||
click_shell==2.0 | ||
click==7.1.2 | ||
numpy==1.18.5 | ||
h5py==2.10.0 | ||
matplotlib==3.2.2 |
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