From 27ff9f4031fe2fca41564d7e4676567386d70664 Mon Sep 17 00:00:00 2001 From: Sebastian Steinlechner Date: Tue, 9 Aug 2016 13:47:38 +0100 Subject: [PATCH 1/5] Nodegraph plotting function Added functionality to produce a graph of the node network. Relies on pygraphviz. --- pykat/__init__.py | 8 +++++++ pykat/finesse.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pykat/__init__.py b/pykat/__init__.py index 1474e20..8a0e7e0 100644 --- a/pykat/__init__.py +++ b/pykat/__init__.py @@ -8,6 +8,7 @@ __version__ = "1.0.8" # This flag is used to switch on the gui features in pkat at import time USE_GUI = False HAS_OPTIVIS = False +HAS_GRAPHVIZ = False import imp @@ -17,6 +18,13 @@ try: except ImportError: HAS_OPTIVIS = False +try: + imp.find_module('pygraphviz') + HAS_GRAPHVIZ = True +except ImportError: + HAS_GRAPHVIZ = False + + import pykat.exceptions as pkex NoGUIException = pkex.BasePyKatException("No PyQt4 module was found so cannot open a GUI") diff --git a/pykat/finesse.py b/pykat/finesse.py index cdb5a2a..db2bbec 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -91,7 +91,7 @@ import pykat.external.six as six import pykat.exceptions as pkex -from pykat import USE_GUI, HAS_OPTIVIS, NoGUIException +from pykat import USE_GUI, HAS_OPTIVIS, HAS_GRAPHVIZ, NoGUIException if HAS_OPTIVIS: @@ -2089,6 +2089,57 @@ class kat(object): return out + def node_graph(self, output): + """ + node_graph(output) + + Generate node visualisation of pykat node network using the + pygraphviz package. The generated graphics is saved in output, where the + output format is determined from the file extension. The exact formats + available depend on how graphviz was built, but generally .pdf and .png + should work. + """ + if not HAS_GRAPHVIZ: + print('pygraphviz is not installed.') + return + + import pygraphviz as pgv + + def add_kat_node(G, from_id, node): + color = 'black' if node.name == 'dump' else 'red' + G.add_node(node.id, label='', xlabel=node.name, width=0.2, + shape='point', fontsize=11, color=color) + G.add_edge(from_id, node.id) + + G = pgv.AGraph(strict=False) + G.graph_attr['label'] = 'Created by PyKat' + G.graph_attr['pencolor'] = 'black' + G.node_attr['style'] = 'filled' + G.node_attr['fontname'] = 'helvetica' + + # add components and associated nodes + for comp in self.components.itervalues(): + if isinstance(comp, pykat.components.laser): + attr = {'fillcolor': 'Orange', 'shape': 'box'} + elif isinstance(comp, pykat.components.space): + attr = {'fillcolor': 'MediumSlateBlue', 'shape': 'diamond'} + else: + attr = {'fillcolor': 'LightSkyBlue', 'shape': 'box'} + G.add_node(comp.name, **attr) + for node in comp.nodes: + add_kat_node(G, comp.name, node) + + # add detectors and associated nodes + for det in self.detectors.itervalues(): + # slightly ugly, but unfortunately detectors don't have the .nodes + # property like components do. + if len(det._nodes) > 0: + G.add_node(det.name, fillcolor='YellowGreen', shape='ellipse') + for node in det._nodes: + add_kat_node(G, det.name, node) + + G.draw(output, prog='neato') + def optivis(self): if not HAS_OPTIVIS: print("Optivis is not installed") -- GitLab From 260ed16d5a0fc937d572e3acf9d43f3377e6ce90 Mon Sep 17 00:00:00 2001 From: Sebastian Steinlechner Date: Fri, 12 Aug 2016 16:33:00 +0100 Subject: [PATCH 2/5] renamed node_graph() to nodeGraph() to be compatible with the naming convention --- pykat/finesse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pykat/finesse.py b/pykat/finesse.py index db2bbec..2d27822 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -2089,9 +2089,9 @@ class kat(object): return out - def node_graph(self, output): + def nodeGraph(self, output): """ - node_graph(output) + nodeGraph(output) Generate node visualisation of pykat node network using the pygraphviz package. The generated graphics is saved in output, where the -- GitLab From 3e5ac3e3da638e28e2dca09ddca9cce21205ed28 Mon Sep 17 00:00:00 2001 From: Sebastian Steinlechner Date: Fri, 12 Aug 2016 16:41:03 +0100 Subject: [PATCH 3/5] katRun.info() updated to give x location of min/max values --- pykat/finesse.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pykat/finesse.py b/pykat/finesse.py index 2d27822..c1ea4e7 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -277,14 +277,22 @@ class katRun(object): import numpy as np - maxs = np.max(self.y, 0) - mins = np.min(self.y, 0) - - maxlbl = max([len(lbl) for lbl in self.ylabels]) - + imaxs = np.argmax(self.y, 0) + imins = np.argmin(self.y, 0) + + maxs = self.y[imaxs, np.arange(self.y.shape[1])] + mins = self.y[imins, np.arange(self.y.shape[1])] + if not kat.noxaxis: + xmaxs = self.x[imaxs] + xmins = self.x[imins] + else: + xmaxs = np.zeros(self.y.size) + xmins = xmaxs + + maxlbl = max([len(lbl) for lbl in self.ylabels]) for i, lbl in enumerate(self.ylabels): - a = "{0:" + str(maxlbl) + "} : min = {1:.15e} max = {2:.15e}" - print(a.format(lbl, mins[i], maxs[i])) + a = "{0:" + str(maxlbl) + "}: min = {1:.15e} @ {2:.15e},\n\tmax = {3:.15e} @ {4:.15e}" + print(a.format(lbl, mins[i], xmins[i], maxs[i], xmaxs[i])) def plot(self, detectors=None, filename=None, show=True, -- GitLab From 4e3ed73aa6844b6f17e1889730a9d4b683255444 Mon Sep 17 00:00:00 2001 From: Sebastian Steinlechner Date: Fri, 12 Aug 2016 16:47:16 +0100 Subject: [PATCH 4/5] removed build pipeline for this repo --- .gitlab-ci.yml => .gitlab-ci.yml.old | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .gitlab-ci.yml => .gitlab-ci.yml.old (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml.old similarity index 100% rename from .gitlab-ci.yml rename to .gitlab-ci.yml.old -- GitLab From 6c633651a158629ac108ced4a3ce0687c09c0fa3 Mon Sep 17 00:00:00 2001 From: Sebastian Steinlechner Date: Mon, 12 Sep 2016 14:22:32 +0200 Subject: [PATCH 5/5] fixed getTraceData for non-default wavelengths --- pykat/finesse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pykat/finesse.py b/pykat/finesse.py index 83bf3b3..959a7f7 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -1687,7 +1687,8 @@ class kat(object): qx = spqx[0].split("=")[1].replace('i','j').replace(' ','') qy = spqy[0].split("=")[1].replace('i','j').replace(' ','') - traceData[-1][node_name] = (pykat.beam_param(q=complex(qx)), pykat.beam_param(q=complex(qy))) + traceData[-1][node_name] = (pykat.beam_param(q=complex(qx),wavelength=self.lambda0), + pykat.beam_param(q=complex(qy),wavelength=self.lambda0)) finally: ifile.close() -- GitLab