-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacyConvert.py
More file actions
143 lines (110 loc) · 5.06 KB
/
legacyConvert.py
File metadata and controls
143 lines (110 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
HSI Classifier
Copyright (C) 2021 Josef Brandt, University of Gothenburg <josef.brandt@gu.se>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program, see COPYING.
If not, see <https://www.gnu.org/licenses/>.
"""
from typing import TYPE_CHECKING, List, Union
import difflib
from collections import Counter
from spectraObject import SpectraObject
from logger import getLogger
from particles import ParticleHandler
from gui.nodegraph.nodegraph import NodeGraph
from gui.nodegraph.nodes import nodeTypes
if TYPE_CHECKING:
from logging import Logger
from dataObjects import Sample, View
from gui.nodegraph.nodecore import BaseNode
currentSampleVersion = 2
currentViewVersion = 2
logger: 'Logger' = getLogger("LegacyConvert")
def assertUpToDateSample(sampleData: 'Sample') -> 'Sample':
"""Convenience method for converting from older versions"""
if not hasattr(sampleData, "specObj"):
sampleData = _updateSampleToVersion0(sampleData)
if not hasattr(sampleData, "version"):
sampleData = _updateSampleToVersion1(sampleData)
if sampleData.version == 1:
sampleData = _updateSampleToVersion2(sampleData)
return sampleData
def assertUpToDateView(view: 'View') -> 'View':
"""Convenience method for converting from older versions"""
if not hasattr(view, "version"):
view = _updateViewToVersion1(view)
if view.version == 1:
view = _updateViewToVersion2(view)
for sample in view.samples:
sample = assertUpToDateSample(sample)
return view
def _updateSampleToVersion0(sampledata: 'Sample') -> 'Sample':
logger.info(f"Converting sample {sampledata.name} to Version 0")
sampledata.specObj = SpectraObject()
sampledata.specObj._wavenumbers = None
return sampledata
def _updateSampleToVersion1(sampleData: 'Sample') -> 'Sample':
logger.info(f"Converting sample {sampleData.name} to Version 1")
sampleData.specObj._wavelengths = sampleData.specObj._wavenumbers
del sampleData.specObj._wavenumbers
sampleData.version = 1
return sampleData
def _updateSampleToVersion2(sampleData: 'Sample') -> 'Sample':
logger.info(f"converting sample {sampleData.name} to Version 2")
sampleData.batchResult = None
if not hasattr(sampleData, "particleHandler"):
sampleData.particleHandler = ParticleHandler()
for id, particle in sampleData.particleHandler._particles.items():
if type(particle._result) == Counter:
particle._result = None
return sampleData
def _updateViewToVersion1(view: 'View') -> 'View':
logger.info(f"Converting view {view.title} to Version 1")
from dataObjects import Sample # import statement down here to avoid cyclic reference...
samples: List['Sample'] = []
for sample in view.samples:
newSample: Sample = Sample()
sample = assertUpToDateSample(sample)
newSample.__dict__.update(sample.__dict__)
samples.append(newSample)
view.samples = samples
view.version = 1
return view
def _updateViewToVersion2(view: 'View') -> 'View':
logger.info(f"Converting view {view.title} to Version 2")
# Recreate nodegraph from just the preprocessorNames.
if len(view.processStack) > 0:
nodeClasses: list = _preprocNames2NodeClasses(view.processStack)
nodegraph: NodeGraph = NodeGraph()
nodegraph._deleteAllNodesAndConnections()
lastNode: Union[None, 'BaseNode'] = None
for i, cls in enumerate(nodeClasses):
newNode: 'BaseNode' = nodegraph._addNode(cls)
if i == 0:
nodegraph._addConnection(newNode._inputs[0], nodegraph._inputNode._outputs[0]) # connect to input
else:
nodegraph._addConnection(newNode._inputs[0], lastNode._outputs[0])
lastNode: 'BaseNode' = newNode
nodegraph._addConnection(nodegraph._nodeClf._inputs[0], lastNode._outputs[0]) # connect lastNode to Classifier Node
view.processingGraph = nodegraph.getGraphConfig()
# del view.processStack # I leave it there for the moment. Could be uncommented after some good testing in practice.
return view
def _preprocNames2NodeClasses(preprocNames: List[str]) -> list:
"""
Takes a List of preprocessorName and finds the available preprocessor NodeTypes that are closest to that.
:param preprocNames: List of Names of Preprocessors
:return: List of NodeTypes corresponding to the procList.
"""
types: list = []
for name in preprocNames:
closestName = difflib.get_close_matches(name, nodeTypes.keys(), cutoff=0)[0]
types.append(nodeTypes[closestName])
return types