1#!/usr/bin/python3
2
3# Script that outputs the graph and screen coordinates of each point in a DIG file.
4# The format is:
5# 1) "xGraph yGraph xScreen yScreen" on each line
6# 2) Between each field is the export delimiter selected in the DIG file
7#
8# Requirements:
9# 1) python3 (versus python2)
10# 2) numpy and pandas
11# 3) DefaultListOrderedDict.py from the Engauge scripts directory
12# 4) ParseDig.py from the Engauge scripts directory
13
14import faulthandler
15import inspect
16import numpy as np
17import pandas as pd
18import subprocess
19import sys
20import testDigGenerator as TDG
21
22sys.path.insert (0, '../contrib/python')
23from ParseDig import ParseDig
24
25DEBUG_ENABLE = True
26
27def parseDigFile (fileName):
28    parseDig = ParseDig (fileName)
29    curveNames = parseDig.curveNames ()
30    exportDelimiter = parseDig.exportDelimiter ()
31    try:
32        f = open (fileName[:-4] + 'Parsed.csv', 'w')
33    except Exception as e:
34        f = open (fileName[:-4] + 'Parsed.csv', 'w+')
35    for curveName in curveNames:
36
37        header = ("# {}" . format (curveName))
38        f.write (header + exportDelimiter + exportDelimiter + exportDelimiter + '\n')
39        curve = parseDig.curve (curveName)
40
41        for row in curve:
42            xGraph = row [0]
43            yGraph = row [1]
44            (xScreen, yScreen) = parseDig.transformGraphToScreen (xGraph, yGraph)
45            dataLine = ("{}{}{}{}{}{}{}" . format (xGraph, exportDelimiter,
46                                                   yGraph, exportDelimiter,
47                                                   xScreen, exportDelimiter,
48                                                   yScreen))
49            f.write(dataLine + '\n')
50    f.close()
51    return pd.read_csv(fileName[:-4] + 'Parsed.csv')
52
53def showResults (testName, expected, actual):
54    GREEN = '\033[32m'
55    RED = '\033[31m'
56    ENDCOLOR = '\033[0m'
57    expected = np.array ([round (elem, 3) for elem in expected]) # Remove issues due to roundoff
58    actual = np.array ([round (elem, 3) for elem in actual])
59    result = (expected == actual).all ()
60    passFail = (GREEN + "PASS" + ENDCOLOR) if result else (RED + "FAIL" + ENDCOLOR)
61    print ('{}   : {}' . format (passFail, testName))
62
63def TestFourAxesInfiniteSlope ():
64    xScreen = [45, 587, 45, 45]
65    yScreen = [171, 171, 15, 327]
66    xGraph = [0, 14, 0, 0]
67    yGraph = [0, 0, 1.5, -1.5]
68    xPoints = 55
69    yPoints = 96
70    title = '.TestFourAxesInfiniteSlope.dig'
71    TDG.createTestCase(np.array ([xScreen, xGraph]),
72                       np.array ([yScreen, yGraph]),
73                       xPoints, yPoints,
74                       'Linear', 'Linear', title)
75    ParseDig.callEngauge (['-exportonly', title], DEBUG_ENABLE)
76    engaugeOutput = pd.read_csv (title [:-3] + 'csv')
77    parsedData = np.array (parseDigFile (title).iloc [:, :2])[0]
78    testData = np.array (engaugeOutput.iloc [:, :2]) [0]
79    for i in range (len (testData)):
80        decimalIndex = str (testData [i]).find ('.')
81        decimals = len (str (testData [i])) - decimalIndex - 1
82        parsedData[i] = np.round (parsedData [i], decimals)
83    showResults (inspect.stack () [0] [3],
84                 testData,
85                 parsedData)
86
87def TestInfiniteSlope():
88    xScreen = [45, 587, 45]
89    yScreen = [327, 171, 15]
90    xGraph = [0, 14, 0]
91    yGraph = [-1.5, 0, 1.5]
92    xPoints = 55
93    yPoints = 96
94    title = '.TestInfiniteSlope.dig'
95    TDG.createTestCase (np.array ([xScreen, xGraph]),
96                        np.array ([yScreen, yGraph]),
97                        xPoints, yPoints,
98                        'Linear', 'Linear', title)
99    ParseDig.callEngauge (['-exportonly', title], DEBUG_ENABLE)
100    engaugeOutput = pd.read_csv (title [:-3] + 'csv')
101    parsedData = np.array (parseDigFile (title).iloc [:, :2]) [0]
102    testData = np.array (engaugeOutput.iloc [:, :2]) [0]
103    for i in range (len (testData)):
104        decimalIndex = str (testData[i]).find ('.')
105        decimals = len (str (testData [i])) - decimalIndex - 1
106        parsedData[i] = np.round (parsedData [i], decimals)
107    showResults (inspect.stack () [0] [3],
108                 testData,
109                 parsedData)
110
111def TestRandomSlope ():
112    xScreen = [np.random.randint(100, 150), 587, 45]
113    yScreen = [327, 171, 15]
114    xGraph = [0, 14, 0]
115    yGraph = [-1.5, 0, 1.5]
116    xPoints = 55
117    yPoints = 96
118    title = '.TestRandomSlope.dig'
119    TDG.createTestCase (np.array ([xScreen, xGraph]),
120                        np.array ([yScreen, yGraph]),
121                        xPoints, yPoints,
122                        'Linear', 'Linear', title)
123    ParseDig.callEngauge (['-exportonly', title], DEBUG_ENABLE)
124    engaugeOutput = pd.read_csv (title [:-3] + 'csv')
125    parsedData = np.array (parseDigFile (title).iloc [:, :2]) [0]
126    testData = np.array (engaugeOutput.iloc [:, :2]) [0]
127    for i in range (len (testData)):
128        decimalIndex = str (testData[i]).find ('.')
129        decimals = len(str (testData[i])) - decimalIndex - 1
130        parsedData [i] = np.round (parsedData [i], decimals)
131    showResults (inspect.stack () [0] [3],
132                 testData,
133                 parsedData)
134
135faulthandler.enable ()
136TestFourAxesInfiniteSlope ()
137TestInfiniteSlope ()
138TestRandomSlope ()
139