diff --git a/kadmos/external/XDSM_writer/XDSM.py b/kadmos/external/XDSM_writer/XDSM.py index 9834cc9932d839f6f849fa010bfe89e34e76abfe..1879202372e802d73a86e72a3499d387266cd883 100644 --- a/kadmos/external/XDSM_writer/XDSM.py +++ b/kadmos/external/XDSM_writer/XDSM.py @@ -15,6 +15,8 @@ import shutil import subprocess import unicodedata +from kadmos.utilities.general import format_string_for_latex + class XDSM(object): @@ -53,8 +55,7 @@ class XDSM(object): """ if name[0:5] != 'EMPTY': name = unicodedata.normalize('NFKD', name.decode('unicode-escape')).encode('hex') - string = string.encode('unicode-escape').replace('_', '\_').replace('&','\&').replace('%','\%')\ - .replace('$','\$').replace('#','\#').replace('{','\{').replace('}','\}') + string = format_string_for_latex(string) self.inds[name] = len(self.comps) self.comps.append([name, style, string, stack]) @@ -76,8 +77,7 @@ class XDSM(object): out = unicodedata.normalize('NFKD', out.decode('unicode-escape')).encode('hex') if inp[0:5] != 'EMPTY': inp = unicodedata.normalize('NFKD', inp.decode('unicode-escape')).encode('hex') - string = string.encode('unicode-escape').replace('_', '\_').replace('&','\&').replace('%','\%')\ - .replace('$','\$').replace('#','\#').replace('{','\{').replace('}','\}') + string = format_string_for_latex(string) self.deps.append([out, inp, style, string, stack]) def addChain(self, chain_list): diff --git a/kadmos/graph/graph_process.py b/kadmos/graph/graph_process.py index 7193bbb6690ff2520e5568dc37fb2f9e901648ed..f9a2fcc7ada968e0ca6f844153d04a0804952893 100644 --- a/kadmos/graph/graph_process.py +++ b/kadmos/graph/graph_process.py @@ -3,7 +3,7 @@ import logging import networkx as nx -from ..utilities.general import format_string_for_d3js, remove_if_exists +from ..utilities.general import format_string_for_vistoms, remove_if_exists from ..utilities.testing import check from ..utilities.xml import Element @@ -525,21 +525,21 @@ class MdaoProcessGraph(ProcessGraph): # In case of regular process steps, determine their list positions for step_node in process_step_nodes: if use_d3js_node_ids: - node_name = format_string_for_d3js(step_node, prefix='id_') + node_name = format_string_for_vistoms(step_node, prefix='id_') else: node_name = step_node process_list[step]['process_step_blocks'].append(node_name) for step_node in converger_step_nodes: if use_d3js_node_ids: - node_name = format_string_for_d3js(step_node, prefix='id_') + node_name = format_string_for_vistoms(step_node, prefix='id_') else: node_name = step_node process_list[step]['converger_step_blocks'].append(node_name) for edge in self.edges_iter(data=True): if edge[2]['process_step'] == step: if use_d3js_node_ids: - edge0_name = format_string_for_d3js(edge[0], prefix='id_') - edge1_name = format_string_for_d3js(edge[1], prefix='id_') + edge0_name = format_string_for_vistoms(edge[0], prefix='id_') + edge1_name = format_string_for_vistoms(edge[1], prefix='id_') else: edge0_name = edge[0] edge1_name = edge[1] diff --git a/kadmos/graph/mixin_vispack.py b/kadmos/graph/mixin_vispack.py index 4b082493fe8d0cbf5873f75ff7e09f1b314bf9b3..ae96ece7db571e8b752d91700df3d3a7a3c3f64c 100644 --- a/kadmos/graph/mixin_vispack.py +++ b/kadmos/graph/mixin_vispack.py @@ -15,7 +15,7 @@ import kadmos.vispack as vispack from lxml import etree from ..utilities.xml import get_element_details, recursively_unique_attribute, merge -from ..utilities.general import make_camel_case, get_list_entries, format_string_for_d3js, get_element_dict +from ..utilities.general import make_camel_case, get_list_entries, format_string_for_vistoms, get_element_dict # Settings for the logger @@ -468,17 +468,17 @@ class VistomsMixin(object): # Extend tools_data with tool outputs for input_tool in input_tools: if input_tool not in tools_data: - tools_data[input_tool] = dict(name=format_string_for_d3js(input_tool), - input=[], - output=[key]) + tools_data[input_tool] = dict(name=format_string_for_vistoms(input_tool), + input=[], + output=[key]) else: tools_data[input_tool]['output'].append(key) # Create edge_bundles for tool in full_graph['attributes']['tools']: if key in full_graph[tool]: if tool not in edge_bundles: - edge_bundles[tool] = dict(name=format_string_for_d3js(tool), input=[], - pipeline_data=dict()) + edge_bundles[tool] = dict(name=format_string_for_vistoms(tool), input=[], + pipeline_data=dict()) # Add input tools edge_bundles[tool]['input'].extend(input_tools) for input_tool in input_tools: @@ -489,9 +489,9 @@ class VistomsMixin(object): # Check if variable is also input to the coordinator if key in full_graph[coordinator_str]: if coordinator_str not in edge_bundles: - edge_bundles[coordinator_str] = dict(name=format_string_for_d3js(coordinator_str), - input=[], - pipeline_data=dict()) + edge_bundles[coordinator_str] = dict(name=format_string_for_vistoms(coordinator_str), + input=[], + pipeline_data=dict()) # Add input tools to coordinator edge_bundles[coordinator_str]['input'].extend(input_tools) for input_tool in input_tools: @@ -504,14 +504,14 @@ class VistomsMixin(object): if key is not 'attributes': if key is coordinator_str: if key not in tools_data: - tools_data[key] = dict(name=format_string_for_d3js(key), input=full_graph[key], - output=[]) + tools_data[key] = dict(name=format_string_for_vistoms(key), input=full_graph[key], + output=[]) else: tools_data[key]['input'] = full_graph[key] elif self.node[key]['category'] == 'function': if key not in tools_data: - tools_data[key] = dict(name=format_string_for_d3js(key), input=full_graph[key], - output=[]) + tools_data[key] = dict(name=format_string_for_vistoms(key), input=full_graph[key], + output=[]) else: tools_data[key]['input'] = full_graph[key] if logging.getLogger().getEffectiveLevel() in [logging.DEBUG, logging.INFO] and n_keys > 10000: @@ -614,24 +614,24 @@ class VistomsMixin(object): {'creator': 'Imco van Gent'}] # noinspection PyUnboundLocalVariable xdsm_dict['nodes'].append(dict(type=block_type, - id=format_string_for_d3js(block, prefix='id_'), - name=format_string_for_d3js(block), + id=format_string_for_vistoms(block, prefix='id_'), + name=format_string_for_vistoms(block), metadata=block_metadata)) # Add edges between blocks for item in edge_bundles_list: name_keyword = ' couplings' if item['name'] is coordinator_str: - to_node_id = format_string_for_d3js(coordinator_str, prefix='id_') + to_node_id = format_string_for_vistoms(coordinator_str, prefix='id_') name_keyword = ' outputs' else: - to_node_id = format_string_for_d3js(item['name'], prefix='id_') + to_node_id = format_string_for_vistoms(item['name'], prefix='id_') for from_node in item['input']: if from_node is coordinator_str: - from_node_id = format_string_for_d3js(coordinator_str, prefix='id_') + from_node_id = format_string_for_vistoms(coordinator_str, prefix='id_') name_keyword = ' inputs' else: - from_node_id = format_string_for_d3js(from_node, prefix='id_') + from_node_id = format_string_for_vistoms(from_node, prefix='id_') if not to_node_id == from_node_id: # check to avoid showing circular couplings on top of the diagonal xdsm_dict['edges'].append({"to": to_node_id, "from": from_node_id, diff --git a/kadmos/utilities/general.py b/kadmos/utilities/general.py index 5129deda4521fbef68c70b3c18f71f30c7bacb1f..7d09de4ed4c11c9d9896cf1e0b66891b43095961 100644 --- a/kadmos/utilities/general.py +++ b/kadmos/utilities/general.py @@ -409,9 +409,9 @@ def unmake_camel_case(string, separator='_'): return string -def format_string_for_d3js(string, prefix='', suffix=''): +def format_string_for_vistoms(string, prefix='', suffix=''): """ - Function to format a string such that it can be used in the dynamic visualization package. + Function to format a string such that it can be used in VISTOMS. :param string: string to be formatted :type string: str @@ -422,7 +422,30 @@ def format_string_for_d3js(string, prefix='', suffix=''): :return: formatted string :rtype: basestring """ - return str(prefix) + string.replace(' ', '').replace('_', '').replace('[', '').replace(']', '') + str(suffix) + replacement_list = ((' ', ''), ('_', ''), ('[', ''), (']', '')) + for repl in replacement_list: + string = string.replace(repl[0], repl[1]) + return str(prefix) + string + str(suffix) + + +def format_string_for_latex(string, prefix='', suffix=''): + """ + Function to format a string such that it can be used in LaTeX. + + :param string: string to be formatted + :type string: str + :param prefix: prefix to be placed in front of the string + :type prefix: basestring + :param suffix: suffix to be appended to the string + :type suffix: basestring + :return: formatted string + :rtype: basestring + """ + replacement_list = (('_', '\_'), ('&', '\&'), ('%', '\%'), ('$', '\$'), ('#', '\#'), ('{', '\{'), ('}', '\}')) + string.encode('unicode-escape') + for repl in replacement_list: + string = string.replace(repl[0], repl[1]) + return str(prefix) + string + str(suffix) def get_list_entries(*args):