Skip to content

Commit ca5167c

Browse files
headriMartinStarmanMartin
authored
feat: column metadata calculation (#147)
* added operation type metadata_value * added operation type header_value * Enabled regex without line * Added handler for ignore_missing_values operation option Added Tag in jcamp if data has been modified * added CALCULATION_APPLIED_Y to test results if necessary. * added comment box to jcamp with data operations descriptions --------- Co-authored-by: Martin <m.starman@live.comdd> Co-authored-by: Martin Starman <martin.starman@kit.edu>
1 parent 5368305 commit ca5167c

47 files changed

Lines changed: 148 additions & 57 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

converter_app/converters.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
logger = logging.getLogger(__name__)
1010

1111

12+
class CalculationError(Exception):
13+
pass
14+
15+
1216
class Converter:
1317
"""
1418
Converter object checks if profile matches to filedata and runs the converting process
@@ -273,33 +277,74 @@ def process(self):
273277
table_index == operation.get('column', {}).get('tableIndex') and \
274278
column_index == operation.get('column', {}).get('columnIndex'):
275279
operation['rows'].append(self.get_value(row, column_index))
280+
applied_operators = {
281+
"applied_x_operator": False,
282+
"applied_y_operator": False,
283+
"applied_operator_failed": False,
284+
"x_operations_description": output_table.get('table', {}).get('xOperationsDescription'),
285+
"y_operations_description": output_table.get('table', {}).get('yOperationsDescription')
286+
}
287+
try:
288+
for operation in x_operations:
289+
applied_operators["applied_x_operator"] |= self._run_operation(x_rows, operation)
290+
for operation in y_operations:
291+
applied_operators["applied_y_operator"] |= self._run_operation(y_rows, operation)
292+
except CalculationError:
293+
applied_operators['applied_x_operator'] = applied_operators['applied_y_operator'] = False
294+
applied_operators['applied_operator_failed'] = True
295+
x_rows = []
296+
y_rows = []
276297

277-
for operation in x_operations:
278-
x_rows = self._run_operation(x_rows, operation)
279-
for operation in y_operations:
280-
y_rows = self._run_operation(y_rows, operation)
281298

282299
self.tables.append({
283300
'header': header,
284301
'x': x_rows,
285302
'y': y_rows
286-
})
303+
} | applied_operators)
287304

288305
def _run_operation(self, rows, operation):
289306
for i, row in enumerate(rows):
290-
op_value = None
307+
str_value = None
291308
if operation.get('type') == 'column':
292309
try:
293-
op_value = operation['rows'][i]
310+
str_value = operation['rows'][i]
294311
except IndexError:
295312
pass
296313
elif operation.get('type') == 'value':
297-
op_value = operation.get('value')
314+
str_value = operation.get('value')
315+
elif operation.get('type') == 'metadata_value':
316+
str_value = self.input_tables[int(operation.get('table'))]['metadata'].get(operation.get('value'))
317+
elif operation.get('type') == 'header_value':
318+
table = 0
319+
if operation.get('table') is not None:
320+
table = int(operation.get('table'))
321+
try:
322+
line_number = int(operation.get('line', ''))
323+
header = self.input_tables[table]['header'][line_number - 1].rstrip()
324+
except (TypeError, ValueError, IndexError):
325+
header = os.linesep.join(self.input_tables[table]['header']).rstrip()
326+
pattern = operation.get('regex')
327+
if header is not None and pattern is not None:
328+
str_value = header
329+
match = re.search(pattern, str_value)
330+
if match is not None:
331+
if len(match.regs) > 1:
332+
str_value = match[1]
333+
else:
334+
str_value = match[0]
335+
else:
336+
raise ValueError(f"Unknown operation type: {operation.get('type')}")
337+
try:
338+
op_value = float(str_value)
339+
except (TypeError, ValueError):
340+
if not operation.get('ignore_missing_values'):
341+
raise CalculationError("Calculation could not be executed")
342+
return False
298343

299344
if op_value:
300345
rows[i] = str(self.apply_operation(row, op_value, operation.get('operator')))
301346

302-
return rows
347+
return True
303348

304349
def _run_identifier_operation(self, value, operation):
305350
op_value = operation.get('value')

converter_app/readers/dta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def check(self):
1616
"""
1717
:return: True if it fits
1818
"""
19-
return self.file.encoding != 'binary' and self.file.suffix.lower() == '.dta' and self.file.mime_type == 'text/plain'
19+
return self.file.encoding != 'binary' and self.file.suffix.lower() == '.dta'
2020

2121
def prepare_tables(self):
2222
tables = []

converter_app/writers/jcamp.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os
33
import sys
44

5+
from .base import Writer
56
from .. import __title__, __version__
67
from ..options import DATA_TYPES, DATA_CLASSES, XUNITS, YUNITS
7-
from .base import Writer
88

99

1010
class JcampWriter(Writer):
@@ -17,6 +17,7 @@ def __init__(self, converter):
1717
self.table = converter.tables[0]
1818
self.buffer = io.StringIO()
1919

20+
2021
def process(self):
2122
self.process_table(self.table)
2223

@@ -31,12 +32,29 @@ def process_table(self, table):
3132
'ORIGIN': header.get('ORIGIN', ''),
3233
'OWNER': header.get('OWNER', '')
3334
}
35+
36+
if table.get('applied_x_operator'):
37+
jcamp_header['CALCULATION_APPLIED_X'] = True
38+
if table.get('x_operations_description'):
39+
self.write_comment_header(['X operations description:'] + table.get('x_operations_description'))
40+
41+
if table.get('applied_y_operator'):
42+
jcamp_header['CALCULATION_APPLIED_Y'] = True
43+
if table.get('y_operations_description'):
44+
self.write_comment_header(['Y operations description:'] + table.get('y_operations_description'))
45+
46+
if table.get('applied_operator_failed'):
47+
jcamp_header['CALCULATION_FAILED'] = True
48+
3449
for key in header:
3550
key_upper = key.upper()
3651
if key_upper not in jcamp_header:
3752
jcamp_header[key_upper] = header[key]
3853
self.write_header(jcamp_header)
3954

55+
if not table.get('y') or not table.get('x'):
56+
return
57+
4058
data_class = header.get('DATA CLASS', DATA_CLASSES[0])
4159
if data_class == 'XYDATA':
4260
self.process_xydata(header, table.get('y'))
@@ -214,6 +232,14 @@ def write_header(self, header):
214232
if value is not None:
215233
self.buffer.write('##{}={}'.format(key, value) + os.linesep)
216234

235+
def write_comment_header(self, header):
236+
self.buffer.write('$$ ' + '-' * 20 + '\n')
237+
for value in header:
238+
if value is not None:
239+
for line in value.split('\n'):
240+
self.buffer.write(f'$$ {line}{os.linesep}')
241+
self.buffer.write('$$ ' + '-' * 20 + '\n')
242+
217243
def write_xydata(self, y, npoints, firstx, deltax, max_decimal):
218244
for i in range(0, npoints, self.nline):
219245
x = float(firstx) + i * float(deltax)

test_manager/profile_results/CV/PalmSens/PSTrace-pssession/File053.pssession.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Binary file not shown.

test_manager/profile_results/CV/PalmSens/PSTrace-pssession/File053.pssession/data/table_01.jdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##DATA CLASS=XYPOINTS
55
##ORIGIN=
66
##OWNER=
7+
##CALCULATION_APPLIED_Y=True
78
##XUNITS=Voltage in V
89
##YUNITS=Current in A
910
##CYCLES=1

test_manager/profile_results/CV/PalmSens/PSTrace-pssession/File053.pssession/data/table_02.jdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##DATA CLASS=XYPOINTS
55
##ORIGIN=
66
##OWNER=
7+
##CALCULATION_APPLIED_Y=True
78
##XUNITS=Voltage in V
89
##YUNITS=Current in A
910
##CYCLES=1

test_manager/profile_results/CV/PalmSens/PSTrace-pssession/File053.pssession/data/table_03.jdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##DATA CLASS=XYPOINTS
55
##ORIGIN=
66
##OWNER=
7+
##CALCULATION_APPLIED_Y=True
78
##XUNITS=Voltage in V
89
##YUNITS=Current in A
910
##CYCLES=1
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
f8ef9b2b69e1ae61e712ebc6d5faf860afdde3dd095062426576c288fb0dcd1e data/table_01.jdx
2-
b8e5436b9c021b914848c3abf05e387aa4d17ba08ac74af8beccdbf247afbb4a data/table_02.jdx
3-
cdb920d1aaa6ecf5e5efa15282a4c845e5cc670660ad152305750dadd2451e67 data/table_03.jdx
1+
4a4ac6759e69a734b4955fdc58bc5469610bee68f5a911d734851fd12572bef0 data/table_01.jdx
2+
573f634f7901a8484554987b6d72e82df8a097eeaa66a95d1915121824601d9d data/table_02.jdx
3+
e200adb008c57b899c12b3a078e04ddb8926f047dc85fd3c6c3fdb6c6cd86101 data/table_03.jdx
44
d8f2e3d5c7eb1d956d2d1148d81d8ebc18865d62378d87de9d7507b644f05d6c metadata/converter.json
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1a0a9d5b3dc0438d709dbbd714200d8eb5aaba9a45f3bb76965109daa902dbcb089591571064695087a50ebba38db2ed9c46f35b8fa773fb426778fb9de13242 data/table_01.jdx
2-
a03b6d2818b55baa65aae9cf2473a004021fa782b563f8be83c377a85bbe984f1b5cb74a35e51b2b66691481b53f5bafcd4af4b9bddbb65a43856365d7144a7a data/table_02.jdx
3-
0629f98ce4b0e39c4b77f67cd401b48657194dd366ffc0870c203a735787fece455312dffd39cda50484c2d959b2bf85cff6f2f0be6ebe770de1665f50519115 data/table_03.jdx
1+
a472000bfb52a945034ce9153bf95da4d848ddfaccd81dc85ca5b76ecd7f440fb5845a16eeb96a48b439ad0fe5e93998ffc1a2de629c6949ff095fd9b4a68f21 data/table_01.jdx
2+
07d91df667562b38d1a7e2d3fc4bc636119262ac73a2d36569c154806132212cd9ea61f1b92d11b8ae7378b6dd9920790c08b7d1fac49b1d0f2eb8b2cf91b1aa data/table_02.jdx
3+
16a384b4fc6c8fa7d79a749a6bca440e3b0c0793c0eada28a4a594aa36e474d295fbbf506292e6130e08cb5fc6c159776d534c35818776f480d63cced976c6c4 data/table_03.jdx
44
0eaba47a8dea7784066cb51fd2456528af7b801b4fd1c18f845810c9d0de3c7e227333aa470b1c9d133668af9cd166ad796b7251dc72b7e56a9329c49278d3f5 metadata/converter.json

0 commit comments

Comments
 (0)