BrainLoader.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. brain.brain_yaml = dict_brain
  35. # create list of Synapse
  36. synapses = list()
  37. for synapses_dict in dict_brain:
  38. if "includes" not in synapses_dict: # we don't need to check includes as it's not a synapse
  39. if ConfigurationChecker().check_synape_dict(synapses_dict):
  40. # print "synapses_dict ok"
  41. name = synapses_dict["name"]
  42. neurons = cls._get_neurons(synapses_dict["neurons"])
  43. signals = cls._get_signals(synapses_dict["signals"])
  44. new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
  45. synapses.append(new_synapse)
  46. brain.synapses = synapses
  47. if file_path is None:
  48. brain.brain_file = cls._get_root_brain_path()
  49. else:
  50. brain.brain_file = file_path
  51. # check that no synapse have the same name than another
  52. if ConfigurationChecker().check_synapes(synapses):
  53. return brain
  54. return None
  55. @staticmethod
  56. def _get_neurons(neurons_dict):
  57. """
  58. Get a list of Neuron object from a neuron dict
  59. :param neurons_dict:
  60. :return:
  61. """
  62. neurons = list()
  63. for neuron_dict in neurons_dict:
  64. if isinstance(neuron_dict, dict):
  65. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  66. # print "Neurons dict ok"
  67. for neuron_name in neuron_dict:
  68. name = neuron_name
  69. parameters = neuron_dict[name]
  70. # print parameters
  71. new_neuron = Neuron(name=name, parameters=parameters)
  72. neurons.append(new_neuron)
  73. else:
  74. # the neuron does not have parameter
  75. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  76. new_neuron = Neuron(name=neuron_dict)
  77. neurons.append(new_neuron)
  78. return neurons
  79. @classmethod
  80. def _get_signals(cls, signals_dict):
  81. # print signals_dict
  82. signals = list()
  83. for signal_dict in signals_dict:
  84. if ConfigurationChecker().check_signal_dict(signal_dict):
  85. # print "Signals dict ok"
  86. event_or_order = cls._get_event_or_order_from_dict(signal_dict)
  87. signals.append(event_or_order)
  88. return signals
  89. @staticmethod
  90. def _get_event_or_order_from_dict(signal_or_event_dict):
  91. if 'event' in signal_or_event_dict:
  92. # print "is event"
  93. event = signal_or_event_dict["event"]
  94. if ConfigurationChecker.check_event_dict(event):
  95. return Event(period=event)
  96. if 'order' in signal_or_event_dict:
  97. order = signal_or_event_dict["order"]
  98. if ConfigurationChecker.check_order_dict(order):
  99. return Order(sentence=order)
  100. @staticmethod
  101. def _get_root_brain_path():
  102. """
  103. Return the full path of the default brain file
  104. :return:
  105. """
  106. # get current script directory path. We are in /an/unknown/path/kalliope/core/ConfigurationManager
  107. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  108. # get parent dir. Now we are in /an/unknown/path/kalliope
  109. parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir + os.sep + os.pardir)
  110. brain_path = parent_dir + os.sep + "brain.yml"
  111. logger.debug("Real brain.yml path: %s" % brain_path)
  112. if os.path.isfile(brain_path):
  113. return brain_path
  114. raise IOError("Default brain.yml file not found")