__init__.py 869 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import numpy as np
  2. def random_idxs(idxs, rnd=None, n_parts=None):
  3. if rnd is None or isinstance(rnd, int):
  4. rnd = np.random.RandomState(rnd)
  5. else:
  6. assert isinstance(rnd, np.random.RandomState), \
  7. "'rnd' should be either a random seed or a RandomState instance!"
  8. n_parts = n_parts or rnd.randint(1, len(idxs))
  9. res = rnd.choice(idxs, n_parts, replace=False)
  10. res.sort()
  11. return res
  12. class attr_dict(dict):
  13. def __getattr__(self, name):
  14. if name in self:
  15. return self[name]
  16. else:
  17. return super(attr_dict, self).get(name)
  18. def __getitem__(self, key):
  19. res = super(attr_dict, self).__getitem__(key)
  20. if isinstance(res, dict):
  21. return attr_dict(res)
  22. return res
  23. class _MetaInfo(object):
  24. def __init__(self, **kwargs):
  25. for name, value in kwargs.items():
  26. setattr(self, name, value)
  27. self.structure = []
  28. from .image import asarray, dimensions