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

Updated Python Unit Tests and Added Log Functionality to ParseDig #338

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 2 additions & 1 deletion contrib/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
.idea

*.csv
*.dig
2 changes: 1 addition & 1 deletion contrib/python/DumpGraphAndScreenCoordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
for row in curve:
xGraph = row [0]
yGraph = row [1]
(xScreen, yScreen) = parseDig.transformGraphToScreen (xGraph, yGraph)
(xScreen, yScreen) = row [2], row [3] #parseDig.transformGraphToScreen (xGraph, yGraph)
print ("{}{}{}{}{}{}{}" . format (xGraph, exportDelimiter,
yGraph, exportDelimiter,
xScreen, exportDelimiter,
Expand Down
111 changes: 111 additions & 0 deletions contrib/python/DumpGraphAndScreenCoordinates_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Script that outputs the graph and screen coordinates of each point in a DIG file.
# The format is:
# 1) "xGraph yGraph xScreen yScreen" on each line
# 2) Between each field is the export delimiter selected in the DIG file
#
# Requirements:
# 1) python3 (versus python2)
# 2) numpy
# 3) DefaultListOrderedDict.py from the Engauge scripts directory
# 4) ParseDig.py from the Engauge scripts directory

from ParseDig import ParseDig
import testDigGenerator as TDG
import pandas as pd
import numpy as np
import subprocess

ENGAUGE_EXECUTABLE = "C:/Program Files/Engauge Digitizer/engauge.exe"
def parseDigFile(fileName):
parseDig = ParseDig (fileName)
curveNames = parseDig.curveNames()
exportDelimiter = parseDig.exportDelimiter()
try:
f = open(fileName[:-4]+'Parsed'+'.csv', 'w')
except Exception as e:
f = open(fileName[:-4]+'Parsed'+'.csv', 'w+')
for curveName in curveNames:

header = ("# {}" . format (curveName))
f.write(header+exportDelimiter+exportDelimiter+exportDelimiter+'\n')
curve = parseDig.curve (curveName)

for row in curve:
xGraph = row [0]
yGraph = row [1]
(xScreen, yScreen) = parseDig.transformGraphToScreen (xGraph, yGraph)
dataLine = ("{}{}{}{}{}{}{}" . format (xGraph, exportDelimiter,
yGraph, exportDelimiter,
xScreen, exportDelimiter,
yScreen))
f.write(dataLine+'\n')
f.close()
return pd.read_csv(fileName[:-4]+'Parsed'+'.csv')

def test_infSlope():
xScreen = [45, 587, 45]
yScreen = [327, 171, 15]
xGraph = [0, 14, 0]
yGraph = [-1.5, 0, 1.5]
xPoints = 55
yPoints = 96
title = 'infSlopeTest.dig'
TDG.createTestCase(np.array([xScreen, xGraph]),
np.array([yScreen, yGraph]),
xPoints, yPoints,
'Linear','Linear', title)
subprocess.call([ENGAUGE_EXECUTABLE,'-exportonly',title])
engaugeOutput = pd.read_csv(title[:-3]+'csv')
parsedData = np.array(parseDigFile(title).iloc[:,:2])[0]
testData = np.array(engaugeOutput.iloc[:,:2])[0]
for i in range(len(testData)):
decimalIndex = str(testData[i]).find('.')
decimals = len(str(testData[i])) - decimalIndex - 1
parsedData[i] = np.round(parsedData[i], decimals)
assert (parsedData == testData).all()


def test_randomSlope():
xScreen = [np.random.randint(100, 150), 587, 45]
yScreen = [327, 171, 15]
xGraph = [0, 14, 0]
yGraph = [-1.5, 0, 1.5]
xPoints = 55
yPoints = 96
title = 'randSlopeTest.dig'
TDG.createTestCase(np.array([xScreen, xGraph]),
np.array([yScreen, yGraph]),
xPoints, yPoints,
'Linear','Linear', title)
print(np.array([xScreen, xGraph])[1])
subprocess.call([ENGAUGE_EXECUTABLE,'-exportonly',title])
engaugeOutput = pd.read_csv(title[:-3]+'csv')
parsedData = np.array(parseDigFile(title).iloc[:,:2])[0]
testData = np.array(engaugeOutput.iloc[:,:2])[0]
for i in range(len(testData)):
decimalIndex = str(testData[i]).find('.')
decimals = len(str(testData[i])) - decimalIndex - 1
parsedData[i] = np.round(parsedData[i], decimals)
assert (parsedData == testData).all()

def test_fourAxesInfSlope():
xScreen = [45, 587, 45, 45]
yScreen = [171, 171, 15, 327]
xGraph = [0, 14, 0, 0]
yGraph = [0, 0, 1.5, -1.5]
xPoints = 55
yPoints = 96
title = 'fourAxesInfSlope.dig'
TDG.createTestCase(np.array([xScreen, xGraph]),
np.array([yScreen, yGraph]),
xPoints, yPoints,
'Linear','Linear', title)
subprocess.call([ENGAUGE_EXECUTABLE,'-exportonly',title])
engaugeOutput = pd.read_csv(title[:-3]+'csv')
parsedData = np.array(parseDigFile(title).iloc[:,:2])[0]
testData = np.array(engaugeOutput.iloc[:,:2])[0]
for i in range(len(testData)):
decimalIndex = str(testData[i]).find('.')
decimals = len(str(testData[i])) - decimalIndex - 1
parsedData[i] = np.round(parsedData[i], decimals)
assert (parsedData == testData).all()
71 changes: 57 additions & 14 deletions contrib/python/ParseDig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
# linux /usr/bin/engauge
# osx /Applications/Engauge\ Digitizer.app/Contents/MacOS/Engauge\ Digitizer
# windows C:/Program Files/Engauge Digitizer/engauge.exe
ENGAUGE_EXECUTABLE = "/usr/bin/engauge"
ENGAUGE_EXECUTABLE = "C:/Program Files/Engauge Digitizer/engauge.exe"

NO_EXE_ERROR = 'Execution error. You may need to modify ENGAUGE_EXECUTABLE to find the Engauge executable. ' + \
'Version 11.3 or newer is required'

class ParseDig:
def __init__(self, digFile):
# Hash table of curve name to lists, with each list consisting of graph points
# Hash table of curve name to lists, with each list consisting of graph points
# and screen coordinates
self._curves = DefaultListOrderedDict()

if not os.path.exists (ENGAUGE_EXECUTABLE):
print (NO_EXE_ERROR)
sys.exit (0)
Expand Down Expand Up @@ -119,20 +119,33 @@ def getScreenAndGraph(self, node):
yAxesScreenY = screenY[IsXOnly == 'False']
#The below section calculates the screen coords of intersections of
#lines formed by the x and y axes
m1 = (yAxesScreenY[1] - yAxesScreenY[0]) / (yAxesScreenX[1] - yAxesScreenX[0])
if ((yAxesScreenX[1] - yAxesScreenX[0]) != 0):
m1 = (yAxesScreenY[1] - yAxesScreenY[0]) / (yAxesScreenX[1] - yAxesScreenX[0])
else:
m1 = np.inf
m3 = (xAxesScreenY[1] - xAxesScreenY[0]) / (xAxesScreenX[1] - xAxesScreenX[0])
m2 = m1
m4 = m3
b1 = xAxesScreenY[0] - m1 * xAxesScreenX[0]
b2 = xAxesScreenY[1] - m2 * xAxesScreenX[1]
b3 = yAxesScreenY[0] - m3 * yAxesScreenX[0]
b4 = yAxesScreenY[1] - m4 * yAxesScreenX[1]
screenXNew.append((b1 - b3) / (m3 - m1))
screenYNew.append(m3 * screenXNew[0] + b3)
screenXNew.append((b2 - b3) / (m3 - m2))
screenYNew.append(m3 * screenXNew[1] + b3)
screenXNew.append((b1 - b4) / (m4 - m1))
screenYNew.append(m4 * screenXNew[2] + b4)
#Use a slightly different procedure if the y-axis
#has an infinite slope
if ((m1 == np.inf) or (m1 == -np.inf)):
screenXNew.append(xAxesScreenX[0])
screenYNew.append(m3 * screenXNew[0] + b3)
screenXNew.append(xAxesScreenX[1])
screenYNew.append(m3 * screenXNew[1] + b3)
screenXNew.append(xAxesScreenX[0])
screenYNew.append(m4 * screenXNew[2] + b4)
else:
screenXNew.append((b1 - b3) / (m3 - m1))
screenYNew.append(m3 * screenXNew[0] + b3)
screenXNew.append((b2 - b3) / (m3 - m2))
screenYNew.append(m3 * screenXNew[1] + b3)
screenXNew.append((b1 - b4) / (m4 - m1))
screenYNew.append(m4 * screenXNew[2] + b4)
return np.array([screenXNew, screenYNew, np.ones(3)]).T, graphX, graphY, np.ones(3), rowsGraph, rowsScreen

def curve (self, curveName):
Expand Down Expand Up @@ -162,17 +175,37 @@ def parseXml(self, tree):
# screen = (xS1 yS1 1)
# (xS2 yS2 1)
# (xS3 yS3 1)
self.LogPlot = False
self._screenToGraph = np.array([])
self._delimiter = ','
for node in tree.iter():
# print (node.tag, '<->', node.attrib)
if (node.tag == 'Export'):
if (node.tag == 'Coords'):
self.xAxisType = node.attrib.get('ScaleXThetaString')
self.yAxisType = node.attrib.get('ScaleYRadiusString')
if ((self.xAxisType == 'Log') or (self.yAxisType == 'Log')):
self.LogPlot = True
#sys.exit (1)
elif (node.tag == 'Export'):
delimiterEnum = node.attrib.get('Delimiter')
delimiter = self.delimiterEnumToDelimiter(delimiterEnum)
elif (node.tag == 'Curve'):
curveName = node.attrib.get('CurveName')
if (curveName == 'Axes'):
screen, graphX, graphY, graph1, rowsGraph, rowsScreen = self.getScreenAndGraph(node)
self.xScreenMax = screen[:, 0].max()
self.yScreenMax = screen[:, 1].max()
self.xScreenMin = screen[:, 0].min()
self.yScreenMin = screen[:, 1].min()
self.xMin = min(graphX)
self.yMin = min(graphY)
self.xMax = max(graphX)
self.yMax = max(graphY)
print(self.yScreenMin)
print(self.yScreenMax)
print(self.yMin)
print(self.yMax)
print(screen)
if rowsGraph < 3 or rowsScreen < 3:
print ('This script requires three axes points. Quitting')
sys.exit (1)
Expand All @@ -188,10 +221,20 @@ def parseXml(self, tree):
vecScreen = np.array([x, y, 1])
vecGraph = np.dot(self._screenToGraph,
vecScreen)
x = vecGraph[0]
y = vecGraph[1]
xGraph = vecGraph[0]
yGraph = vecGraph[1]
if (self.xAxisType == 'Log'):
xGraph = np.exp( (xGraph - self.xMin) \
* (np.log(self.xMax) - np.log(self.xMin)) \
/ (self.xMax - self.xMin) \
+ np.log(self.xMin) )[0]
if (self.yAxisType == 'Log'):
yGraph = np.exp( (yGraph - self.yMin) \
* (np.log(self.yMax) - np.log(self.yMin)) \
/ (self.yMax - self.yMin) \
+ np.log(self.yMin) )[0]
# print ('Computed positionGraph', x, y)
self._curves[curveName].append([x, y])
self._curves[curveName].append([xGraph, yGraph, x, y])

def transformGraphToScreen (self, xGraph, yGraph):
graphToScreen = np.linalg.inv (self._screenToGraph)
Expand Down
Loading