Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to plasma graph #382

Merged
merged 10 commits into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion tardis/data/tardis_config_definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ plasma:
help: sets all beta_sobolevs to 1



model:
structure:
property_type : container-property
Expand Down
1 change: 1 addition & 0 deletions tardis/io/config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ def from_config_dict(cls, config_dict, atom_data=None, test_parser=False,




##### NLTE subsection of Plasma start
nlte_validated_config_dict = {}
nlte_species = []
Expand Down
44 changes: 36 additions & 8 deletions tardis/plasma/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import networkx as nx
from tardis.plasma.exceptions import PlasmaMissingModule, NotInitializedModule
from tardis.plasma.properties.base import HiddenPlasmaProperty

import tempfile
import fileinput
Expand All @@ -18,7 +19,7 @@ def __init__(self, plasma_properties, **kwargs):
self.plasma_properties = self._init_properties(plasma_properties,
**kwargs)
self._build_graph()
# self.write_to_tex('Plasma_Graph', 'Plasma_Formulae')
self.write_to_tex('Plasma_Graph', 'Plasma_Formulae')
self.update(**kwargs)

def __getattr__(self, item):
Expand Down Expand Up @@ -186,12 +187,12 @@ def write_to_dot(self, fname, latex_label=True):

try:
import pygraphviz
except ImportError:
raise ImportError('pygraphviz is needed for method '
'\'write_to_dot\'')
except:
logger.warn('pygraphviz missing. Plasma graph will not be '
'generated.')
return
print_graph = self.graph.copy()
print_graph.remove_node('LinesUpperLevelIndex')
print_graph.remove_node('LinesLowerLevelIndex')
print_graph = self.remove_hidden_properties(print_graph)
for node in print_graph:
print_graph.node[str(node)]['label'] = node
if hasattr(self.plasma_properties_dict[node],
Expand All @@ -209,8 +210,10 @@ def write_to_dot(self, fname, latex_label=True):
def write_to_tex(self, fname_graph, fname_formulae):
try:
import dot2tex
except ImportError:
raise ImportError('dot2tex is needed for method\'write_to_tex\'')
except:
logger.warn('dot2tex missing. Plasma graph will not be '
'generated.')
return

temp_fname = tempfile.NamedTemporaryFile().name

Expand All @@ -228,6 +231,31 @@ def write_to_tex(self, fname_graph, fname_formulae):
for line in fileinput.input(fname_graph, inplace = 1):
print line.replace('\enlargethispage{100cm}', ''),

def remove_hidden_properties(self, print_graph):
for item in self.plasma_properties_dict.values():
module = self.plasma_properties_dict[item.name].__class__
if (issubclass(module, HiddenPlasmaProperty)):
output = module.outputs[0]
inputs = self.plasma_properties_dict[item.name].inputs
for value in self.plasma_properties_dict.keys():
if output in getattr(self.plasma_properties_dict[value],
'inputs', []):
for input in self.plasma_properties_dict[
item.name].inputs:
try:
position = self.outputs_dict[
input].outputs.index(input)
label = self.outputs_dict[
input].latex_name[position]
label = '$' + label + '$'
label = label.replace('\\', '\\\\')
except:
label = input.replace('_', '-')
self.graph.add_edge(self.outputs_dict[input].name,
value, label = label)
print_graph.remove_node(str(item.name))
return print_graph

class StandardPlasma(BasePlasma):

def __init__(self, number_densities, atom_data, time_explosion,
Expand Down
8 changes: 4 additions & 4 deletions tardis/plasma/properties/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from collections import Counter as counter
import logging

from tardis.plasma.properties.base import ProcessingPlasmaProperty
from tardis.plasma.properties.base import (ProcessingPlasmaProperty,
HiddenPlasmaProperty)
from tardis.plasma.exceptions import IncompleteAtomicData

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,15 +89,14 @@ def _set_index(self, lines):
lines = reindexed.dropna(subset=['atomic_number'])
return lines, lines['nu'], lines['f_lu'], lines['wavelength_cm']

class LinesLowerLevelIndex(ProcessingPlasmaProperty):
class LinesLowerLevelIndex(HiddenPlasmaProperty):
"""
Outputs:
lines_lower_level_index : One-dimensional Numpy Array
Levels data for lower levels of particular lines
Usage: levels.ix[lines_lower_level_index]
"""
outputs = ('lines_lower_level_index',)

def calculate(self, levels, lines):
levels_index = pd.Series(np.arange(len(levels), dtype=np.int64),
index=levels)
Expand All @@ -105,7 +105,7 @@ def calculate(self, levels, lines):
'level_number_lower']).index
return np.array(levels_index.ix[lines_index])

class LinesUpperLevelIndex(ProcessingPlasmaProperty):
class LinesUpperLevelIndex(HiddenPlasmaProperty):
"""
Outputs:
lines_upper_level_index : One-dimensional Numpy Array
Expand Down
6 changes: 6 additions & 0 deletions tardis/plasma/properties/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ def update(self):
def calculate(self, *args, **kwargs):
raise NotImplementedError('This method needs to be implemented by '
'processing plasma modules')

class HiddenPlasmaProperty(ProcessingPlasmaProperty):
__metaclass__ = ABCMeta

def __init__(self, plasma_parent):
super(HiddenPlasmaProperty, self).__init__(plasma_parent)
3 changes: 2 additions & 1 deletion tardis/plasma/properties/nlte.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ class PreviousElectronDensities(PreviousIterationProperty):
outputs = ('previous_electron_densities',)

class PreviousBetaSobolevs(PreviousIterationProperty):
outputs = ('previous_beta_sobolevs',)
outputs = ('previous_beta_sobolevs',)

2 changes: 1 addition & 1 deletion tardis/plasma/properties/property_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class PlasmaPropertyCollection(list):
LevelBoltzmannFactorDiluteLTE])
non_nlte_properties = PlasmaPropertyCollection([LevelBoltzmannFactorNoNLTE])
nlte_properties = PlasmaPropertyCollection([
LevelBoltzmannFactorNLTE, NLTEData, NLTESpecies, LTEJBlues])
LevelBoltzmannFactorNLTE, NLTEData, NLTESpecies, LTEJBlues])