utils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. # -----------------------------------------------------------------------------
  4. # File: utils.py (as part of project URUMETRICS)
  5. # Created: 01/06/2022 16:36
  6. # Last Modified: 01/06/2022 16:36
  7. # -----------------------------------------------------------------------------
  8. # Author: William N. Havard
  9. # Postdoctoral Researcher
  10. #
  11. # Mail : william.havard@ens.fr / william.havard@gmail.com
  12. #
  13. # Institution: ENS / Laboratoire de Sciences Cognitives et Psycholinguistique
  14. #
  15. # ------------------------------------------------------------------------------
  16. # Description:
  17. # •
  18. # -----------------------------------------------------------------------------
  19. import os
  20. import numpy as np
  21. from functools import wraps
  22. def staticvariable(**assignments):
  23. def decorate(func):
  24. for var, val in assignments.items():
  25. setattr(func, var, val)
  26. @wraps(func)
  27. def wrapper(*args, **kwargs):
  28. return func(*args, **kwargs)
  29. return wrapper
  30. return decorate
  31. def vectorise(func):
  32. """
  33. To be used as decorator to vectorise a function using Numpy. Contrary to Numpy's vectorize function, this function
  34. preserves the name of the original function
  35. :param func: function
  36. :type func: callable
  37. :return: decorated function
  38. :rtype: callable
  39. """
  40. @wraps(func)
  41. def wrapper(args, **kwargs):
  42. return np.vectorize(func)(args, **kwargs)
  43. return wrapper