1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/visualization.py

70 lines
2.0 KiB
Python
Raw Normal View History

2017-12-15 16:59:50 +00:00
import os
import networkx as nx
from server import VisualizationElement
from soil.simulation import SoilSimulation
from xml.etree import ElementTree
class Model():
2018-02-01 18:37:10 +00:00
def __init__(self, dump=True, dir_path='output'):
self.name = 'soil'
self.dump = dump
self.dir_path = dir_path
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
def run(self, config):
name = config['name']
print('Using config(s): {name}'.format(name=name))
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
sim = SoilSimulation(**config)
sim.dir_path = os.path.join(self.dir_path, name)
sim.dump = self.dump
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
print('Dumping results to {} : {}'.format(sim.dir_path, sim.dump))
sim.run_simulation()
2017-12-15 16:59:50 +00:00
2017-12-20 17:10:14 +00:00
2018-02-01 18:37:10 +00:00
def get_trial(self, name, trial):
graph = nx.read_gexf(os.path.join(self.dir_path, name, '{}_trial_{}.gexf'.format(name, trial)))
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
attributes = self.get_attributes(os.path.join(self.dir_path, name, '{}_trial_{}.gexf'.format(name, trial)))
json = {}
json['graph'] = nx.node_link_data(graph)
json['models'] = attributes
return json
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
def reset(self):
pass
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
def get_attributes(self, path_gexf):
attributes = {}
tree = ElementTree.parse(path_gexf)
root = tree.getroot()
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
ns = { 'gexf': 'http://www.gexf.net/1.2draft' }
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
for mode in root[0].findall('gexf:attributes', ns):
attributes[mode.attrib['mode']] = []
for attribute in mode:
values = {
'id' : attribute.attrib['id'],
'title' : attribute.attrib['title'],
'type' : attribute.attrib['type']
}
attributes[mode.attrib['mode']].append(values)
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
return attributes
2017-12-15 16:59:50 +00:00
class GraphVisualization(VisualizationElement):
2018-02-01 18:37:10 +00:00
package_includes = []
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
# TODO: esta por definir todos los ajustes de simulacion
def __init__(self, params=None):
new_element = ("new funcion()")
self.js_code = "elements.push(" + new_element + ");"
2017-12-15 16:59:50 +00:00
2018-02-01 18:37:10 +00:00
def render(self, model):
pass