BrainLoader.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import inspect
  2. import logging
  3. import os
  4. from YAMLLoader import YAMLLoader
  5. from core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
  6. from core.Models.Brain import Brain
  7. from core.Models.Event import Event
  8. from core.Models.Neuron import Neuron
  9. from core.Models.Order import Order
  10. from core.Models.Synapse import Synapse
  11. logging.basicConfig()
  12. logger = logging.getLogger("kalliope")
  13. class BrainLoader(object):
  14. def __init__(self):
  15. pass
  16. @classmethod
  17. def get_yaml_config(cls, file_path=None):
  18. if file_path is None:
  19. brain_file_path = cls._get_root_brain_path()
  20. else:
  21. brain_file_path = file_path
  22. return YAMLLoader.get_config(brain_file_path)
  23. @classmethod
  24. def get_brain(cls, file_path=None):
  25. """
  26. return a brain object from YAML settings
  27. :return: Brain object
  28. :rtype: Brain
  29. """
  30. # get the brain with dict
  31. dict_brain = cls.get_yaml_config(file_path)
  32. # create a new brain
  33. brain = Brain()
  34. # create list of Synapse
  35. synapses = list()
  36. for synapses_dict in dict_brain:
  37. if "includes" not in synapses_dict: # we don't need to check includes as it's not a synapse
  38. if ConfigurationChecker().check_synape_dict(synapses_dict):
  39. # print "synapses_dict ok"
  40. name = synapses_dict["name"]
  41. neurons = cls._get_neurons(synapses_dict["neurons"])
  42. signals = cls._get_signals(synapses_dict["signals"])
  43. new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
  44. synapses.append(new_synapse)
  45. brain.synapses = synapses
  46. if file_path is None:
  47. brain.brain_file = cls._get_root_brain_path()
  48. else:
  49. brain.brain_file = file_path
  50. # check that no synapse have the same name than another
  51. if ConfigurationChecker().check_synapes(synapses):
  52. return brain
  53. return None
  54. @staticmethod
  55. def _get_neurons(neurons_dict):
  56. """
  57. Get a list of Neuron object from a neuron dict
  58. :param neurons_dict:
  59. :return:
  60. """
  61. neurons = list()
  62. for neuron_dict in neurons_dict:
  63. if isinstance(neuron_dict, dict):
  64. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  65. # print "Neurons dict ok"
  66. for neuron_name in neuron_dict:
  67. name = neuron_name
  68. parameters = neuron_dict[name]
  69. # print parameters
  70. new_neuron = Neuron(name=name, parameters=parameters)
  71. neurons.append(new_neuron)
  72. else:
  73. # the neuron does not have parameter
  74. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  75. new_neuron = Neuron(name=neuron_dict)
  76. neurons.append(new_neuron)
  77. return neurons
  78. @classmethod
  79. def _get_signals(cls, signals_dict):
  80. # print signals_dict
  81. signals = list()
  82. for signal_dict in signals_dict:
  83. if ConfigurationChecker().check_signal_dict(signal_dict):
  84. # print "Signals dict ok"
  85. event_or_order = cls._get_event_or_order_from_dict(signal_dict)
  86. signals.append(event_or_order)
  87. return signals
  88. @staticmethod
  89. def _get_event_or_order_from_dict(signal_or_event_dict):
  90. if 'event' in signal_or_event_dict:
  91. # print "is event"
  92. event = signal_or_event_dict["event"]
  93. if ConfigurationChecker.check_event_dict(event):
  94. return Event(period=event)
  95. if 'order' in signal_or_event_dict:
  96. order = signal_or_event_dict["order"]
  97. if ConfigurationChecker.check_order_dict(order):
  98. return Order(sentence=order)
  99. @staticmethod
  100. def _get_root_brain_path():
  101. """
  102. Return the full path of the default brain file
  103. :return:
  104. """
  105. # get current script directory path. We are in /an/unknown/path/kalliope/core/ConfigurationManager
  106. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  107. # get parent dir. Now we are in /an/unknown/path/kalliope
  108. parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir + os.sep + os.pardir)
  109. brain_path = parent_dir + os.sep + "brain.yml"
  110. logger.debug("Real brain.yml path: %s" % brain_path)
  111. if os.path.isfile(brain_path):
  112. return brain_path
  113. raise IOError("Default brain.yml file not found")