util.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pickle
  2. def save_obj(obj, name):
  3. with open(name + '.pkl', 'wb') as f:
  4. pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
  5. def load_obj(name):
  6. with open(name + '.pkl', 'rb') as f:
  7. return pickle.load(f)
  8. def save_dict(dic, filename):
  9. with open(filename, 'wb') as f:
  10. pickle.dump(dic, f)
  11. def load_dict(filename):
  12. with open(filename, 'rb') as f:
  13. dic = pickle.load(f)
  14. return dic
  15. def iid_divide(l, g):
  16. """
  17. divide list l among g groups
  18. each group has either int(len(l)/g) or int(len(l)/g)+1 elements
  19. returns a list of groups
  20. """
  21. num_elems = len(l)
  22. group_size = int(len(l) / g)
  23. num_big_groups = num_elems - g * group_size
  24. num_small_groups = g - num_big_groups
  25. glist = []
  26. for i in range(num_small_groups):
  27. glist.append(l[group_size * i: group_size * (i + 1)])
  28. bi = group_size * num_small_groups
  29. group_size += 1
  30. for i in range(num_big_groups):
  31. glist.append(l[bi + group_size * i:bi + group_size * (i + 1)])
  32. return glist