Surface NMR processing and inversion GUI
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

akvoGUI.py 59KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. #/usr/bin/env python
  2. import sys
  3. #import readline
  4. try:
  5. from akvo.gui.main_ui import Ui_MainWindow
  6. uicerr = False
  7. except: # Fallback
  8. from akvo.gui.mainui import Ui_MainWindow
  9. uicerr = """
  10. USING THE DEFAULT GUI FILES, AKVO MAY NOT WORK CORRECTLY!
  11. See INSTALL.txt for details regarding GUI configuration
  12. if you are encountering problems.
  13. Clicking ignore will prevent this warning from showing
  14. each time you launch Akvo.
  15. """
  16. import matplotlib
  17. #matplotlib.use("QT4Agg")
  18. from PyQt5 import QtCore, QtGui, QtWidgets
  19. import numpy as np
  20. import time
  21. import os
  22. from copy import deepcopy
  23. from matplotlib.backends.backend_qt4 import NavigationToolbar2QT #as NavigationToolbar
  24. import datetime, time
  25. from akvo.tressel import mrsurvey
  26. import pkg_resources # part of setuptools
  27. version = pkg_resources.require("Akvo")[0].version
  28. import yaml
  29. # Writes out numpy arrays into Eigen vectors as serialized by Lemma
  30. class MatrixXr(yaml.YAMLObject):
  31. yaml_tag = u'MatrixXr'
  32. def __init__(self, rows, cols, data):
  33. self.rows = rows
  34. self.cols = cols
  35. self.data = np.zeros((rows,cols))
  36. def __repr__(self):
  37. return "%s(rows=%r, cols=%r, data=%r)" % (self.__class__.__name__, self.rows, self.cols, self.data)
  38. class VectorXr(yaml.YAMLObject):
  39. yaml_tag = r'VectorXr'
  40. def __init__(self, array):
  41. self.size = np.shape(array)[0]
  42. self.data = array.tolist()
  43. def __repr__(self):
  44. # Converts to numpy array on import
  45. return "np.array(%r)" % (self.data)
  46. from collections import OrderedDict
  47. #def represent_ordereddict(dumper, data):
  48. # print("representing IN DA HOUSE!!!!!!!!!!!!!!!!!!!!!")
  49. # value = []
  50. # for item_key, item_value in data.items():
  51. # node_key = dumper.represent_data(item_key)
  52. # node_value = dumper.represent_data(item_value)
  53. # value.append((node_key, node_value))
  54. # return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)
  55. #yaml.add_representer(OrderedDict, represent_ordereddict)
  56. def setup_yaml():
  57. """ https://stackoverflow.com/a/8661021 """
  58. represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', data.items())
  59. yaml.add_representer(OrderedDict, represent_dict_order)
  60. setup_yaml()
  61. class AkvoYamlNode(yaml.YAMLObject):
  62. yaml_tag = u'AkvoData'
  63. def __init__(self):
  64. self.Akvo_VERSION = version
  65. self.Import = OrderedDict() # {}
  66. self.Processing = OrderedDict()
  67. #def __init__(self, node):
  68. # self.Akvo_VERSION = node["version"]
  69. # self.Import = OrderedDict( node["Import"] ) # {}
  70. # self.Processing = OrderedDict( node["Processing"] )
  71. def __repr__(self):
  72. return "%s(name=%r, Akvo_VESION=%r, Import=%r, Processing=%r)" % (
  73. #self.__class__.__name__, self.Akvo_VERSION, self.Import, self.Processing )
  74. self.__class__.__name__, self.Akvo_VERSION, self.Import, OrderedDict(self.Processing) )
  75. #self.__class__.__name__, self.Akvo_VERSION, self.Import, OrderedDict(self.Processing))
  76. try:
  77. import thread
  78. except ImportError:
  79. import _thread as thread #Py3K compatibility
  80. class MyPopup(QtWidgets.QWidget):
  81. def __init__(self, name):
  82. super().__init__()
  83. self.name = name
  84. self.initUI()
  85. def initUI(self):
  86. lblName = QtWidgets.QLabel(self.name, self)
  87. class ApplicationWindow(QtWidgets.QMainWindow):
  88. def __init__(self):
  89. QtWidgets.QMainWindow.__init__(self)
  90. self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  91. akvohome = os.path.expanduser("~") + "/.akvo"
  92. if not os.path.exists(akvohome):
  93. os.makedirs(akvohome)
  94. self.ui = Ui_MainWindow()
  95. self.ui.setupUi(self)
  96. if uicerr != False and not os.path.exists(akvohome+"/pyuic-warned"):
  97. reply = QtGui.QMessageBox.warning(self, 'Warning', uicerr, QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ignore)
  98. if reply == 1024: # "0x400" in hex
  99. pass
  100. elif reply == 1048576: # "0x100000" in hex
  101. warn = open( akvohome+"/pyuic-warned" ,"w" )
  102. warn.write("Gui files were not compiled locally using pyuic! Further warnings have been supressed")
  103. warn.close()
  104. self.RAWDataProc = None
  105. self.YamlNode = AkvoYamlNode()
  106. # initialise some stuff
  107. self.ui.lcdNumberTauPulse2.setEnabled(0)
  108. self.ui.lcdNumberTauPulse1.setEnabled(0)
  109. self.ui.lcdNumberNuTx.setEnabled(0)
  110. self.ui.lcdNumberTuneuF.setEnabled(0)
  111. self.ui.lcdNumberSampFreq.setEnabled(0)
  112. self.ui.lcdNumberTauDelay.setEnabled(0)
  113. self.ui.lcdNumberNQ.setEnabled(0)
  114. #MAK 20170126: add in a list to hold processing steps
  115. self.logText = []
  116. ####################
  117. # Make connections #
  118. ####################
  119. # Menu items
  120. self.ui.actionOpen_GMR.triggered.connect(self.openGMRRAWDataset)
  121. self.ui.actionSave_Preprocessed_Dataset.triggered.connect(self.SavePreprocess)
  122. self.ui.actionExport_Preprocessed_Dataset.triggered.connect(self.ExportPreprocess)
  123. self.ui.actionExport_Preprocessed_Dataset.setEnabled(False)
  124. self.ui.actionOpen_Preprocessed_Dataset.triggered.connect(self.OpenPreprocess)
  125. self.ui.actionAboutAkvo.triggered.connect(self.about)
  126. # Buttons
  127. # #QtCore.QObject.connect(self.ui.fullWorkflowPushButton, QtCore.SIGNAL("clicked()"), self.preprocess )
  128. self.ui.loadDataPushButton.pressed.connect(self.loadRAW)
  129. self.ui.sumDataGO.pressed.connect( self.sumDataChans )
  130. self.ui.bandPassGO.pressed.connect( self.bandPassFilter )
  131. self.ui.filterDesignPushButton.pressed.connect( self.designFilter )
  132. self.ui.fdDesignPushButton.pressed.connect( self.designFDFilter )
  133. self.ui.downSampleGO.pressed.connect( self.downsample )
  134. self.ui.windowFilterGO.pressed.connect( self.windowFilter )
  135. # self.ui.despikeGO.pressed.connect( self.despikeFilter ) # use smart stack instead
  136. self.ui.adaptGO.pressed.connect( self.adaptFilter )
  137. self.ui.adaptFDGO.pressed.connect( self.adaptFilterFD )
  138. self.ui.qdGO.pressed.connect( self.quadDet )
  139. self.ui.gateIntegrateGO.pressed.connect( self.gateIntegrate )
  140. self.ui.calcQGO.pressed.connect( self.calcQ )
  141. self.ui.FDSmartStackGO.pressed.connect( self.FDSmartStack )
  142. self.ui.plotQD.setEnabled(False)
  143. self.ui.plotQD.pressed.connect( self.plotQD )
  144. self.ui.plotGI.setEnabled(False)
  145. self.ui.plotGI.pressed.connect( self.plotGI )
  146. # hide header info box
  147. #self.ui.headerFileBox.setVisible(False)
  148. self.ui.headerFileBox.clicked.connect( self.headerBoxShrink )
  149. self.ui.headerBox2.setVisible(False)
  150. # Clean up the tab widget
  151. self.ui.actionPreprocessing.triggered.connect(self.addPreProc)
  152. self.ui.actionModelling.triggered.connect(self.addModelling)
  153. self.ui.actionInversion.triggered.connect(self.addInversion)
  154. # tabs
  155. #self.ui.ProcTabs.tabCloseRequested.connect( self.closeTabs )
  156. #self.ui.ProcTabs.tabBar().setTabButton(7, QtWidgets.QTabBar.RightSide,None)
  157. self.ui.ProcTabs.removeTab(4)
  158. self.ui.ProcTabs.removeTab(4)
  159. self.ui.ProcTabs.removeTab(4)
  160. self.ui.ProcTabs.removeTab(4)
  161. #self.ui.LoadTab.close( )
  162. # Add progressbar to statusbar
  163. self.ui.barProgress = QtWidgets.QProgressBar()
  164. self.ui.statusbar.addPermanentWidget(self.ui.barProgress, 0);
  165. self.ui.barProgress.setMaximumSize(100, 16777215);
  166. self.ui.barProgress.hide();
  167. self.ui.mplwidget_navigator.setCanvas(self.ui.mplwidget)
  168. #self.ui.mplwidget_navigator_2.setCanvas(self.ui.mplwidget)
  169. ##########################################################################
  170. # Loop Table
  171. self.ui.loopTableWidget.setRowCount(80)
  172. self.ui.loopTableWidget.setColumnCount(6)
  173. self.ui.loopTableWidget.setHorizontalHeaderLabels( ["ch. tag", \
  174. "Northing [m]","Easting [m]","Height [m]", "Radius","Tx"] )
  175. for ir in range(0, self.ui.loopTableWidget.rowCount() ):
  176. for ic in range(1, self.ui.loopTableWidget.columnCount() ):
  177. pCell = QtWidgets.QTableWidgetItem()
  178. #pCell.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
  179. pCell.setFlags(QtCore.Qt.NoItemFlags) # not selectable
  180. pCell.setBackground( QtGui.QColor("lightgrey").lighter(110) )
  181. self.ui.loopTableWidget.setItem(ir, ic, pCell)
  182. self.ui.loopTableWidget.cellChanged.connect(self.loopCellChanged)
  183. #self.ui.loopTableWidget.cellPressed.connect(self.loopCellChanged)
  184. self.ui.loopTableWidget.itemClicked.connect(self.loopCellClicked)
  185. #self.ui.loopTableWidget.cellPressed.connect(self.loopCellClicked)
  186. self.ui.loopTableWidget.setDragDropOverwriteMode(False)
  187. self.ui.loopTableWidget.setDragEnabled(False)
  188. #self.ui.loopTableWidget.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
  189. self.loops = {}
  190. ##########################################################################
  191. # layer Table
  192. self.ui.layerTableWidget.setRowCount(80)
  193. self.ui.layerTableWidget.setColumnCount(3)
  194. self.ui.layerTableWidget.setHorizontalHeaderLabels( [r"top [m]", r"bottom [m]", "σ [ Ωm]" ] )
  195. # do we want this
  196. self.ui.layerTableWidget.setDragDropOverwriteMode(False)
  197. self.ui.layerTableWidget.setDragEnabled(True)
  198. self.ui.layerTableWidget.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
  199. pCell = QtWidgets.QTableWidgetItem()
  200. pCell.setFlags(QtCore.Qt.NoItemFlags) # not selectable
  201. pCell.setBackground( QtGui.QColor("lightgrey").lighter(110) )
  202. self.ui.layerTableWidget.setItem(0, 1, pCell)
  203. for ir in range(1, self.ui.layerTableWidget.rowCount() ):
  204. for ic in range(0, self.ui.layerTableWidget.columnCount() ):
  205. pCell = QtWidgets.QTableWidgetItem()
  206. #pCell.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
  207. pCell.setFlags(QtCore.Qt.NoItemFlags) # not selectable
  208. pCell.setBackground( QtGui.QColor("lightgrey").lighter(110) )
  209. self.ui.layerTableWidget.setItem(ir, ic, pCell)
  210. self.ui.layerTableWidget.cellChanged.connect(self.sigmaCellChanged)
  211. def closeTabs(self):
  212. #self.ui.ProcTabs.removeTab(idx)
  213. self.ui.ProcTabs.clear( )
  214. def addPreProc(self):
  215. if self.ui.actionPreprocessing.isChecked():
  216. self.ui.actionModelling.setChecked(False)
  217. self.ui.actionInversion.setChecked(False)
  218. self.ui.ProcTabs.clear( )
  219. self.ui.ProcTabs.insertTab( 0, self.ui.LoadTab, "Load" )
  220. self.ui.ProcTabs.insertTab( 1, self.ui.NCTab, "NC" )
  221. self.ui.ProcTabs.insertTab( 2, self.ui.QCTab, "QC" )
  222. self.ui.ProcTabs.insertTab( 3, self.ui.METATab, "META" )
  223. self.ui.ProcTabs.insertTab( 4, self.ui.LogTab, "Log" )
  224. else:
  225. self.ui.ProcTabs.removeTab(0)
  226. self.ui.ProcTabs.removeTab(0)
  227. self.ui.ProcTabs.removeTab(0)
  228. self.ui.ProcTabs.removeTab(0)
  229. def addModelling(self):
  230. if self.ui.actionModelling.isChecked():
  231. self.ui.actionPreprocessing.setChecked(False)
  232. self.ui.actionInversion.setChecked(False)
  233. self.ui.ProcTabs.clear( )
  234. self.ui.ProcTabs.insertTab( 0, self.ui.KernTab, "Kernel" )
  235. self.ui.ProcTabs.insertTab( 1, self.ui.ModelTab, "Modelling" )
  236. self.ui.ProcTabs.insertTab( 2, self.ui.LogTab, "Log" )
  237. else:
  238. self.ui.ProcTabs.removeTab(0)
  239. self.ui.ProcTabs.removeTab(0)
  240. def addInversion(self, idx):
  241. if self.ui.actionInversion.isChecked():
  242. self.ui.actionPreprocessing.setChecked(False)
  243. self.ui.actionModelling.setChecked(False)
  244. self.ui.ProcTabs.clear( )
  245. self.ui.ProcTabs.insertTab( 0, self.ui.InvertTab, "Inversion" )
  246. self.ui.ProcTabs.insertTab( 1, self.ui.AppraiseTab, "Appraisal" )
  247. self.ui.ProcTabs.insertTab( 2, self.ui.LogTab, "Log" )
  248. else:
  249. self.ui.ProcTabs.removeTab(0)
  250. self.ui.ProcTabs.removeTab(0)
  251. def headerBoxShrink(self):
  252. #self.ui.headerFileBox.setVisible(False)
  253. if self.ui.headerFileBox.isChecked( ):
  254. #self.ui.headerFileBox.setMinimumSize(460,250)
  255. self.ui.headerBox2.setVisible(True)
  256. else:
  257. #self.ui.headerFileBox.setMinimumSize(460,50)
  258. self.ui.headerBox2.setVisible(False)
  259. def sigmaCellChanged(self):
  260. self.ui.layerTableWidget.cellChanged.disconnect(self.sigmaCellChanged)
  261. # TODO consider building the model whenever this is called. Would be nice to be able to
  262. # do that. Would require instead dist of T2 I guess.
  263. jj = self.ui.layerTableWidget.currentColumn()
  264. ii = self.ui.layerTableWidget.currentRow()
  265. val = "class 'NoneType'>"
  266. try:
  267. val = eval (str( self.ui.layerTableWidget.item(ii, jj).text() ))
  268. except:
  269. #if jj != 0:
  270. # Error = QtWidgets.QMessageBox()
  271. # Error.setWindowTitle("Error!")
  272. # Error.setText("Non-numeric value encountered")
  273. self.ui.layerTableWidget.cellChanged.connect(self.sigmaCellChanged)
  274. return
  275. if jj == 1:
  276. #item.setFlags(QtCore.Qt.ItemIsEnabled)
  277. pCell = self.ui.layerTableWidget.item(ii, jj)
  278. pCell.setBackground( QtGui.QColor("white"))
  279. pCell = self.ui.layerTableWidget.item(ii+1, jj-1)
  280. if str(type(pCell)) == "<class 'NoneType'>":
  281. pCell = QtWidgets.QTableWidgetItem()
  282. pCell.setFlags(QtCore.Qt.ItemIsEnabled)
  283. self.ui.layerTableWidget.setItem(ii+1, jj-1, pCell)
  284. if ii == 0:
  285. pCell.setText(str(val))
  286. #pCell3 = self.ui.layerTableWidget.item(ii+1, jj)
  287. #print ("setting", ii, jj, type(pCell3))
  288. #print ( "setting", ii, jj, type(pCell3))
  289. #pCell3.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled )
  290. #pCell3.setFlags( QtCore.Qt.ItemIsEditable )
  291. elif ii > 0:
  292. val2 = eval (str( self.ui.layerTableWidget.item(ii-1, jj).text() ))
  293. #print ("val2", val2, val, type(val))
  294. #if str(type(pCell)) == "<class 'NoneType'>":
  295. if type(val) == str or val > val2:
  296. pCell.setText(str(val))
  297. else:
  298. Error = QtWidgets.QMessageBox()
  299. Error.setWindowTitle("Error!")
  300. Error.setText("Non-increasing layer detected")
  301. Error.setDetailedText("Each layer interface must be below the one above it.")
  302. Error.exec_()
  303. pCell2 = self.ui.layerTableWidget.item(ii, jj)
  304. pCell2.setText(str(""))
  305. self.ui.layerTableWidget.cellChanged.connect(self.sigmaCellChanged)
  306. return
  307. # enable next layer
  308. pCell4 = self.ui.layerTableWidget.item(ii+1, jj)
  309. pCell4.setBackground( QtGui.QColor("lightblue") ) #.lighter(110))
  310. pCell4.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled )
  311. pCell5 = self.ui.layerTableWidget.item(ii+1, jj+1)
  312. pCell5.setBackground( QtGui.QColor("white"))
  313. pCell5.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled )
  314. print("ii", ii, "jj", jj)
  315. if ii == 0 and jj == 0:
  316. pCell = self.ui.layerTableWidget.item(0, 1)
  317. pCell.setBackground( QtGui.QColor("lightblue")) #.lighter(110) )
  318. pCell.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled )
  319. self.ui.layerTableWidget.cellChanged.connect(self.sigmaCellChanged)
  320. def loopCellClicked(self, item):
  321. print("checkstate", item.checkState(),item.row())
  322. #self.ui.loopTableWidget.itemClicked.disconnect(self.loopCellClicked)
  323. jj = item.column()
  324. ii = item.row()
  325. tp = type(self.ui.loopTableWidget.item(ii, 0))
  326. print("tp", tp, ii, jj)
  327. if str(tp) == "<class 'NoneType'>":
  328. return
  329. #print("Clicked", ii, jj)
  330. if jj == 5 and self.ui.loopTableWidget.item(ii, 0).text() in self.loops.keys():
  331. #print("jj=5")
  332. self.loops[ self.ui.loopTableWidget.item(ii, 0).text() ]["Tx"] = self.ui.loopTableWidget.item(ii, 5).checkState()
  333. # update surrogates
  334. print("updating surrogates")
  335. for point in self.loops[ self.ui.loopTableWidget.item(ii, 0).text() ]["points"][1:]:
  336. pCell = self.ui.loopTableWidget.item(point, 5)
  337. if self.ui.loopTableWidget.item(ii, 5).checkState():
  338. pCell.setCheckState(QtCore.Qt.Checked);
  339. else:
  340. pCell.setCheckState(QtCore.Qt.Unchecked);
  341. #print( "loops", self.loops[ self.ui.loopTableWidget.item(ii, 0).text() ]["Tx"])
  342. #self.ui.loopTableWidget.itemClicked.connect(self.loopCellClicked)
  343. def loopCellChanged(self):
  344. self.ui.loopTableWidget.cellChanged.disconnect(self.loopCellChanged)
  345. jj = self.ui.loopTableWidget.currentColumn()
  346. ii = self.ui.loopTableWidget.currentRow()
  347. if jj == 0 and len( self.ui.loopTableWidget.item(ii, jj).text().strip()) == 0:
  348. for jjj in range(jj+1,jj+6):
  349. pCell = self.ui.loopTableWidget.item(ii, jjj)
  350. pCell.setBackground( QtGui.QColor("white") )
  351. pCell.setFlags( QtCore.Qt.NoItemFlags | QtCore.Qt.ItemIsUserCheckable ) # not selectable
  352. elif jj == 0 and len( self.ui.loopTableWidget.item(ii, jj).text().strip() ): # ch. tag modified
  353. for jjj in range(jj+1,jj+5):
  354. pCell = self.ui.loopTableWidget.item(ii, jjj)
  355. pCell.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled )
  356. pCell.setBackground( QtGui.QColor("lightblue") )
  357. if self.ui.loopTableWidget.item(ii, jj).text() not in self.loops.keys():
  358. # This is a new loop ID
  359. self.loops[ self.ui.loopTableWidget.item(ii, jj).text() ] = {}
  360. self.loops[ self.ui.loopTableWidget.item(ii, jj).text() ]["Tx"] = self.ui.loopTableWidget.item(ii, 5).checkState()
  361. self.loops[ self.ui.loopTableWidget.item(ii, jj).text() ]["points"] = [ii]
  362. # Transmitter cell
  363. pCell = self.ui.loopTableWidget.item(ii, jj+5)
  364. pCell.setCheckState(QtCore.Qt.Unchecked)
  365. pCell.setFlags( QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled )
  366. pCell.setBackground( QtGui.QColor("lightblue") )
  367. else:
  368. # This is an existing loop ID
  369. self.loops[ self.ui.loopTableWidget.item(ii, jj).text() ]["points"].append( ii )
  370. pCell = self.ui.loopTableWidget.item(ii, jj+5)
  371. pCell.setFlags(QtCore.Qt.NoItemFlags) # not selectable
  372. if self.loops[ self.ui.loopTableWidget.item(ii, 0).text() ]["Tx"]:
  373. pCell.setCheckState(QtCore.Qt.Checked);
  374. else:
  375. pCell.setCheckState(QtCore.Qt.Unchecked);
  376. #pCell.setFlags( )
  377. pCell.setBackground( QtGui.QColor("lightblue") )
  378. self.plotLoops()
  379. self.ui.loopTableWidget.cellChanged.connect(self.loopCellChanged)
  380. def plotLoops(self):
  381. print("Plotting loopz")
  382. self.ui.mplwidget.reAxH(1)
  383. #self.ui.mplwidget.ax1.clear()
  384. #self.ui.mplwidget.ax2.clear()
  385. nor = dict()
  386. eas = dict()
  387. dep = dict()
  388. for ii in range( self.ui.loopTableWidget.rowCount() ):
  389. for jj in range( self.ui.loopTableWidget.columnCount() ):
  390. tp = type(self.ui.loopTableWidget.item(ii, jj))
  391. if str(tp) == "<class 'NoneType'>":
  392. pass
  393. elif not len(self.ui.loopTableWidget.item(ii, jj).text()):
  394. pass
  395. else:
  396. if jj == 0:
  397. idx = self.ui.loopTableWidget.item(ii, 0).text()
  398. if idx not in nor.keys():
  399. nor[idx] = list()
  400. eas[idx] = list()
  401. dep[idx] = list()
  402. if jj == 1:
  403. nor[idx].append( eval(self.ui.loopTableWidget.item(ii, 1).text()) )
  404. elif jj == 2:
  405. eas[idx].append( eval(self.ui.loopTableWidget.item(ii, 2).text()) )
  406. elif jj == 3:
  407. dep[idx].append( eval(self.ui.loopTableWidget.item(ii, 3).text()) )
  408. for ii in nor.keys():
  409. try:
  410. self.ui.mplwidget.ax1.plot( np.array(nor[ii]), np.array(eas[ii]) )
  411. except:
  412. pass
  413. #self.ui.mplwidget.figure.axes().set
  414. self.ui.mplwidget.ax1.set_aspect('equal') #, adjustable='box')
  415. self.ui.mplwidget.draw()
  416. def about(self):
  417. # TODO proper popup with info
  418. #self.w = MyPopup("""About Akvo \n
  419. # Akvo is an open source project developed primarily by Trevor Irons.
  420. #""")
  421. #self.w.setGeometry(100, 100, 400, 200)
  422. #self.w.show()
  423. # Just a splash screen for now
  424. logo = pkg_resources.resource_filename(__name__, 'akvo_about.png')
  425. pixmap = QtGui.QPixmap(logo)
  426. self.splash = QtWidgets.QSplashScreen(pixmap, QtCore.Qt.WindowStaysOnTopHint)
  427. self.splash.show()
  428. def connectGMRDataProcessor(self):
  429. self.RAWDataProc = mrsurvey.GMRDataProcessor()
  430. self.RAWDataProc.progressTrigger.connect(self.updateProgressBar)
  431. self.RAWDataProc.enableDSPTrigger.connect(self.enableDSP)
  432. self.RAWDataProc.doneTrigger.connect(self.doneStatus)
  433. self.RAWDataProc.updateProcTrigger.connect(self.updateProc)
  434. def openGMRRAWDataset(self):
  435. """ Opens a GMR header file
  436. """
  437. try:
  438. with open('.gmr.last.path') as f:
  439. fpath = f.readline()
  440. pass
  441. except IOError as e:
  442. fpath = '.'
  443. self.headerstr = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', fpath)[0] # arg2 = File Type 'All Files (*)'
  444. self.ui.headerFileTextBrowser.clear()
  445. self.ui.headerFileTextBrowser.append(self.headerstr)
  446. if len(self.headerstr) == 0:
  447. return
  448. # clear the processing log
  449. self.ui.logTextBrowser.clear()
  450. self.logText = [] #MAK 20170126
  451. path,filen=os.path.split(str(self.headerstr))
  452. f = open('.gmr.last.path', 'w')
  453. f.write( str(self.headerstr) ) # prompt last file
  454. self.connectGMRDataProcessor()
  455. self.RAWDataProc.readHeaderFile(str(self.headerstr))
  456. # If we got this far, enable all the widgets
  457. self.ui.lcdNumberTauPulse1.setEnabled(True)
  458. self.ui.lcdNumberNuTx.setEnabled(True)
  459. self.ui.lcdNumberTuneuF.setEnabled(True)
  460. self.ui.lcdNumberSampFreq.setEnabled(True)
  461. self.ui.lcdNumberNQ.setEnabled(True)
  462. self.ui.headerFileBox.setEnabled(True)
  463. self.ui.headerFileBox.setChecked( True )
  464. self.ui.headerBox2.setVisible(True)
  465. self.ui.inputRAWParametersBox.setEnabled(True)
  466. self.ui.loadDataPushButton.setEnabled(True)
  467. # make plots as you import the dataset
  468. self.ui.plotImportCheckBox.setEnabled(True)
  469. self.ui.plotImportCheckBox.setChecked(True)
  470. # Update info from the header into the GUI
  471. self.ui.pulseTypeTextBrowser.clear()
  472. self.ui.pulseTypeTextBrowser.append(self.RAWDataProc.pulseType)
  473. self.ui.lcdNumberNuTx.display(self.RAWDataProc.transFreq)
  474. self.ui.lcdNumberTauPulse1.display(1e3*self.RAWDataProc.pulseLength[0])
  475. self.ui.lcdNumberTuneuF.display(self.RAWDataProc.TuneCapacitance)
  476. self.ui.lcdNumberSampFreq.display(self.RAWDataProc.samp)
  477. self.ui.lcdNumberNQ.display(self.RAWDataProc.nPulseMoments)
  478. self.ui.DeadTimeSpinBox.setValue(1e3*self.RAWDataProc.deadTime)
  479. self.ui.CentralVSpinBox.setValue( self.RAWDataProc.transFreq )
  480. if self.RAWDataProc.pulseType != "FID":
  481. self.ui.lcdNumberTauPulse2.setEnabled(1)
  482. self.ui.lcdNumberTauPulse2.display(1e3*self.RAWDataProc.pulseLength[1])
  483. self.ui.lcdNumberTauDelay.setEnabled(1)
  484. self.ui.lcdNumberTauDelay.display(1e3*self.RAWDataProc.interpulseDelay)
  485. self.ui.FIDProcComboBox.clear()
  486. if self.RAWDataProc.pulseType == "4PhaseT1":
  487. self.ui.FIDProcComboBox.insertItem(0, "Pulse 1")
  488. self.ui.FIDProcComboBox.insertItem(1, "Pulse 2")
  489. self.ui.FIDProcComboBox.insertItem(2, "Both")
  490. self.ui.FIDProcComboBox.setCurrentIndex (1)
  491. elif self.RAWDataProc.pulseType == "FID":
  492. self.ui.FIDProcComboBox.insertItem(0, "Pulse 1")
  493. self.ui.FIDProcComboBox.setCurrentIndex (0)
  494. def ExportPreprocess(self):
  495. """ This method export to YAML
  496. """
  497. try:
  498. with open('.akvo.last.yaml.path') as f:
  499. fpath = f.readline()
  500. pass
  501. except IOError as e:
  502. fpath = '.'
  503. fdir = os.path.dirname(fpath)
  504. # Pickle the preprocessed data dictionary
  505. SaveStr = QtWidgets.QFileDialog.getSaveFileName(self, "Save as", fdir, r"Processed data (*.yaml)")[0]
  506. spath,filen=os.path.split(str(SaveStr))
  507. f = open('.akvo.last.yaml.path', 'w')
  508. f.write( str(spath) ) # prompt last file
  509. INFO = {}
  510. INFO["headerstr"] = str(self.headerstr)
  511. INFO["pulseType"] = self.RAWDataProc.pulseType
  512. INFO["transFreq"] = self.RAWDataProc.transFreq.tolist()
  513. INFO["pulseLength"] = self.RAWDataProc.pulseLength.tolist()
  514. INFO["TuneCapacitance"] = self.RAWDataProc.TuneCapacitance.tolist()
  515. #INFO["samp"] = self.RAWDataProc.samp
  516. INFO["nPulseMoments"] = self.RAWDataProc.nPulseMoments
  517. #INFO["deadTime"] = self.RAWDataProc.deadTime
  518. INFO["processed"] = "Akvo v. 1.0, on " + time.strftime("%d/%m/%Y")
  519. # Pulse current info
  520. ip = 0
  521. INFO["Pulses"] = {}
  522. for pulse in self.RAWDataProc.DATADICT["PULSES"]:
  523. qq = []
  524. qv = []
  525. for ipm in range(self.RAWDataProc.DATADICT["nPulseMoments"]):
  526. #for istack in self.RAWDataProc.DATADICT["stacks"]:
  527. # print ("stack q", self.RAWDataProc.DATADICT[pulse]["Q"][ipm,istack-1])
  528. qq.append(np.mean( self.RAWDataProc.DATADICT[pulse]["Q"][ipm,:]) )
  529. qv.append(np.std( self.RAWDataProc.DATADICT[pulse]["Q"][ipm,:]/self.RAWDataProc.pulseLength[ip] ))
  530. INFO["Pulses"][pulse] = {}
  531. INFO["Pulses"][pulse]["units"] = "A"
  532. INFO["Pulses"][pulse]["current"] = VectorXr(np.array(qq)/self.RAWDataProc.pulseLength[ip])
  533. INFO["Pulses"][pulse]["variance"] = VectorXr(np.array(qv))
  534. ip += 1
  535. # Data
  536. if self.RAWDataProc.gated == True:
  537. INFO["Gated"] = {}
  538. INFO["Gated"]["abscissa units"] = "ms"
  539. INFO["Gated"]["data units"] = "nT"
  540. for pulse in self.RAWDataProc.DATADICT["PULSES"]:
  541. INFO["Gated"][pulse] = {}
  542. INFO["Gated"][pulse]["abscissa"] = VectorXr( self.RAWDataProc.GATEDABSCISSA )
  543. INFO["Gated"][pulse]["windows"] = VectorXr( self.RAWDataProc.GATEDWINDOW )
  544. for ichan in self.RAWDataProc.DATADICT[pulse]["chan"]:
  545. INFO["Gated"][pulse]["Chan. " + str(ichan)] = {}
  546. INFO["Gated"][pulse]["Chan. " + str(ichan)]["STD"] = VectorXr( np.std(self.RAWDataProc.GATED[ichan]["NR"], axis=0) )
  547. for ipm in range(self.RAWDataProc.DATADICT["nPulseMoments"]):
  548. INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " CA"] = VectorXr(self.RAWDataProc.GATED[ichan]["CA"][ipm])
  549. INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " RE"] = VectorXr(self.RAWDataProc.GATED[ichan]["RE"][ipm])
  550. INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " IM"] = VectorXr(self.RAWDataProc.GATED[ichan]["IM"][ipm])
  551. #INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " IP"] = VectorXr(self.RAWDataProc.GATED[ichan]["IP"][ipm])
  552. #INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " NR"] = VectorXr(self.RAWDataProc.GATED[ichan]["NR"][ipm])
  553. #INFO["Gated"][pulse]["Chan. " + str(ichan)]["Q-"+str(ipm) + " STD" ] = VectorXr(self.RAWDataProc.GATED[ichan]["SIGMA"][ipm])
  554. # we have gated data
  555. # Window edges
  556. # Window centres
  557. with open(SaveStr, 'w') as outfile:
  558. #for line in self.logText:
  559. # outfile.write(line+"\n")
  560. yaml.dump(self.YamlNode, outfile)
  561. yaml.dump(INFO, outfile, default_flow_style=False)
  562. def SavePreprocess(self):
  563. #if "Saved" not in self.YamlNode.Processing.keys():
  564. # self.YamlNode.Processing["Saved"] = []
  565. #self.YamlNode.Processing["Saved"].append(datetime.datetime.now().isoformat())
  566. #self.Log()
  567. import pickle, os
  568. try:
  569. with open('.akvo.last.path') as f:
  570. fpath = f.readline()
  571. pass
  572. except IOError as e:
  573. fpath = '.'
  574. fdir = os.path.dirname(fpath)
  575. # Pickle the preprocessed data dictionary
  576. SaveStr = QtWidgets.QFileDialog.getSaveFileName(self, "Save as", fdir, r"Pickle (*.dmp)")
  577. print(SaveStr)
  578. spath,filen=os.path.split(str(SaveStr[0]))
  579. f = open('.akvo.last.path', 'w')
  580. f.write( str(spath) ) # prompt last file
  581. save = open(SaveStr[0], 'wb')
  582. # Add some extra info
  583. INFO = {}
  584. INFO["pulseType"] = self.RAWDataProc.pulseType
  585. INFO["transFreq"] = self.RAWDataProc.transFreq
  586. INFO["pulseLength"] = self.RAWDataProc.pulseLength
  587. INFO["TuneCapacitance"] = self.RAWDataProc.TuneCapacitance
  588. INFO["samp"] = self.RAWDataProc.samp
  589. INFO["nPulseMoments"] = self.RAWDataProc.nPulseMoments
  590. INFO["deadTime"] = self.RAWDataProc.deadTime
  591. INFO["transFreq"] = self.RAWDataProc.transFreq
  592. INFO["headerstr"] = str(self.headerstr)
  593. INFO["log"] = yaml.dump( self.YamlNode ) #self.logText #MAK 20170127
  594. self.RAWDataProc.DATADICT["INFO"] = INFO
  595. pickle.dump(self.RAWDataProc.DATADICT, save)
  596. save.close()
  597. # Export XML file suitable for USGS ScienceBase Data Release
  598. def ExportXML(self):
  599. """ This is a filler function for use by USGS collaborators
  600. """
  601. return 42
  602. def OpenPreprocess(self):
  603. import pickle
  604. try:
  605. with open('.akvo.last.path') as f:
  606. fpath = f.readline()
  607. pass
  608. except IOError as e:
  609. fpath = '.'
  610. #filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', '.')
  611. fpath = QtWidgets.QFileDialog.getOpenFileName(self, 'Open preprocessed file', fpath, r"Pickle Files (*.dmp)")[0]
  612. f = open('.akvo.last.path', 'w')
  613. f.write( str(fpath) ) # prompt last file
  614. self.ui.logTextBrowser.clear()
  615. self.logText = []
  616. if len(fpath) == 0:
  617. return
  618. pfile = open(fpath,'rb')
  619. unpickle = pickle.Unpickler(pfile)
  620. self.connectGMRDataProcessor()
  621. self.RAWDataProc.DATADICT = unpickle.load()
  622. self.RAWDataProc.readHeaderFile(self.RAWDataProc.DATADICT["INFO"]["headerstr"])
  623. self.headerstr = self.RAWDataProc.DATADICT["INFO"]["headerstr"]
  624. self.RAWDataProc.pulseType = self.RAWDataProc.DATADICT["INFO"]["pulseType"]
  625. self.RAWDataProc.transFreq = self.RAWDataProc.DATADICT["INFO"]["transFreq"]
  626. self.RAWDataProc.pulseLength = self.RAWDataProc.DATADICT["INFO"]["pulseLength"]
  627. self.RAWDataProc.TuneCapacitance = self.RAWDataProc.DATADICT["INFO"]["TuneCapacitance"]
  628. self.RAWDataProc.samp = self.RAWDataProc.DATADICT["INFO"]["samp"]
  629. self.RAWDataProc.nPulseMoments = self.RAWDataProc.DATADICT["INFO"]["nPulseMoments"]
  630. self.RAWDataProc.deadTime = self.RAWDataProc.DATADICT["INFO"]["deadTime"]
  631. self.RAWDataProc.transFreq = self.RAWDataProc.DATADICT["INFO"]["transFreq"]
  632. self.RAWDataProc.dt = 1./self.RAWDataProc.samp
  633. self.dataChan = self.RAWDataProc.DATADICT[ self.RAWDataProc.DATADICT["PULSES"][0] ]["chan"]
  634. # Keep backwards compatibility with prior saved pickles???
  635. #self.ui.logTextBrowser.clear()
  636. #self.ui.logTextBrowser.append( yaml.dump(self.YamlNode)) #, default_flow_style=False) )
  637. #for a in self.logText:
  638. # self.ui.logTextBrowser.append(str(a))
  639. #self.ui.logTextBrowser
  640. #self.ui.logTextBrowser.clear()
  641. #print ( self.RAWDataProc.DATADICT["INFO"]["log"] )
  642. self.logText = self.RAWDataProc.DATADICT["INFO"]["log"] # YAML
  643. self.YamlNode = AkvoYamlNode( ) #self.logText )
  644. self.YamlNode.Akvo_VERSION = (yaml.load( self.logText )).Akvo_VERSION
  645. self.YamlNode.Import = OrderedDict((yaml.load( self.logText )).Import)
  646. self.YamlNode.Processing = OrderedDict((yaml.load( self.logText )).Processing)
  647. #self.YamlNode.Akvo_VERSION = 2 #yaml.load( self.logText )["Akvo_VERSION"] #, Loader=yaml.RoundTripLoader) # offending line!
  648. #self.YamlNode = AkvoYamlNode( self.logText ) # offending line!
  649. #print("import type", type( self.YamlNode.Processing ))
  650. self.Log()
  651. #self.ui.logTextBrowser.append( yaml.dump(self.YamlNode)) #, default_flow_style=False) )
  652. #except KeyError:
  653. # pass
  654. # Remove "Saved" and "Loaded" from processing flow
  655. #if "Loaded" not in self.YamlNode.Processing.keys():
  656. # self.YamlNode.Processing["Loaded"] = []
  657. #self.YamlNode.Processing["Loaded"].append(datetime.datetime.now().isoformat())
  658. #self.Log()
  659. # If we got this far, enable all the widgets
  660. self.ui.lcdNumberTauPulse1.setEnabled(True)
  661. self.ui.lcdNumberNuTx.setEnabled(True)
  662. self.ui.lcdNumberTuneuF.setEnabled(True)
  663. self.ui.lcdNumberSampFreq.setEnabled(True)
  664. self.ui.lcdNumberNQ.setEnabled(True)
  665. self.ui.headerFileBox.setEnabled(True)
  666. self.ui.headerFileBox.setChecked( False )
  667. #self.ui.headerBox2.setVisible(True)
  668. self.ui.inputRAWParametersBox.setEnabled(True)
  669. self.ui.loadDataPushButton.setEnabled(True)
  670. # make plots as you import the dataset
  671. self.ui.plotImportCheckBox.setEnabled(True)
  672. self.ui.plotImportCheckBox.setChecked(True)
  673. # Update info from the header into the GUI
  674. self.ui.pulseTypeTextBrowser.clear()
  675. self.ui.pulseTypeTextBrowser.append(self.RAWDataProc.pulseType)
  676. self.ui.lcdNumberNuTx.display(self.RAWDataProc.transFreq)
  677. self.ui.lcdNumberTauPulse1.display(1e3*self.RAWDataProc.pulseLength[0])
  678. self.ui.lcdNumberTuneuF.display(self.RAWDataProc.TuneCapacitance)
  679. self.ui.lcdNumberSampFreq.display(self.RAWDataProc.samp)
  680. self.ui.lcdNumberNQ.display(self.RAWDataProc.nPulseMoments)
  681. self.ui.DeadTimeSpinBox.setValue(1e3*self.RAWDataProc.deadTime)
  682. self.ui.CentralVSpinBox.setValue( self.RAWDataProc.transFreq )
  683. if self.RAWDataProc.pulseType != "FID":
  684. self.ui.lcdNumberTauPulse2.setEnabled(1)
  685. self.ui.lcdNumberTauPulse2.display(1e3*self.RAWDataProc.pulseLength[1])
  686. self.ui.lcdNumberTauDelay.setEnabled(1)
  687. self.ui.lcdNumberTauDelay.display(1e3*self.RAWDataProc.interpulseDelay)
  688. self.ui.FIDProcComboBox.clear()
  689. if self.RAWDataProc.pulseType == "4PhaseT1":
  690. self.ui.FIDProcComboBox.insertItem(0, "Pulse 1") #, const QVariant & userData = QVariant() )
  691. self.ui.FIDProcComboBox.insertItem(1, "Pulse 2") #, const QVariant & userData = QVariant() )
  692. self.ui.FIDProcComboBox.insertItem(2, "Both") #, const QVariant & userData = QVariant() )
  693. if len( self.RAWDataProc.DATADICT["PULSES"]) == 2:
  694. self.ui.FIDProcComboBox.setCurrentIndex (2)
  695. elif self.RAWDataProc.DATADICT["PULSES"][0] == "Pulse 1":
  696. self.ui.FIDProcComboBox.setCurrentIndex (0)
  697. else:
  698. self.ui.FIDProcComboBox.setCurrentIndex (1)
  699. elif self.RAWDataProc.pulseType == "FID":
  700. self.ui.FIDProcComboBox.insertItem(0, "Pulse 1") #, const QVariant & userData = QVariant() )
  701. self.ui.FIDProcComboBox.setCurrentIndex (0)
  702. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("updateProgress(int)"), self.updateProgressBar)
  703. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("enableDSP()"), self.enableDSP)
  704. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("doneStatus()"), self.doneStatus)
  705. self.RAWDataProc.progressTrigger.connect(self.updateProgressBar)
  706. self.RAWDataProc.enableDSPTrigger.connect(self.enableDSP)
  707. self.RAWDataProc.doneTrigger.connect(self.doneStatus)
  708. self.enableAll()
  709. def loadRAW(self):
  710. #################################################
  711. # Check to make sure we are ready to process
  712. # Header
  713. if self.RAWDataProc == None:
  714. err_msg = "You need to load a header first."
  715. reply = QtGui.QMessageBox.critical(self, 'Error',
  716. err_msg) #, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
  717. return
  718. # Stacks
  719. try:
  720. self.procStacks = np.array(eval(str("np.r_["+self.ui.stacksLineEdit.text())+"]"))
  721. except:
  722. err_msg = "You need to set your stacks correctly.\n" + \
  723. "This should be a Python Numpy interpretable list\n" + \
  724. "of stack indices. For example 1:24 or 1:4,8:24"
  725. QtGui.QMessageBox.critical(self, 'Error', err_msg)
  726. return
  727. # Data Channels
  728. #Chan = np.arange(0,9,1)
  729. try:
  730. self.dataChan = np.array(eval(str("np.r_["+self.ui.dataChanLineEdit.text())+"]"))
  731. except:
  732. #QMessageBox messageBox;
  733. #messageBox.critical(0,"Error","An error has occured !");
  734. #messageBox.setFixedSize(500,200);
  735. #quit_msg = "Are you sure you want to exit the program?"
  736. #reply = QtGui.QMessageBox.question(self, 'Message',
  737. # quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
  738. err_msg = "You need to set your data channels correctly.\n" + \
  739. "This should be a Python Numpy interpretable list\n" + \
  740. "of indices. For example 1 or 1:3 or 1:3 5\n\n" + \
  741. "valid GMR data channels fall between 1 and 8. Note that\n" +\
  742. "1:3 is not inclusive of 3 and is the same as 1,2 "
  743. reply = QtGui.QMessageBox.critical(self, 'Error',
  744. err_msg) #, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
  745. return
  746. #############################
  747. # Reference Channels
  748. # TODO make sure no overlap between data and ref channels
  749. self.refChan = np.array( () )
  750. if str(self.ui.refChanLineEdit.text()): # != "none":
  751. try:
  752. self.refChan = np.array(eval(str("np.r_["+self.ui.refChanLineEdit.text())+"]"))
  753. except:
  754. err_msg = "You need to set your reference channels correctly.\n" + \
  755. "This should be a Python Numpy interpretable list\n" + \
  756. "of indices. For example 1 or 1:3 or 1:3 5\n\n" + \
  757. "valid GMR data channels fall between 1 and 8. Note that\n" +\
  758. "1:3 is not inclusive of 3 and is the same as 1,2 "
  759. QtGui.QMessageBox.critical(self, 'Error', err_msg)
  760. return
  761. #####################################################
  762. # Load data
  763. self.lock("loading RAW GMR dataset")
  764. if self.RAWDataProc.pulseType == "FID":
  765. self.procThread = thread.start_new_thread(self.RAWDataProc.loadFIDData, \
  766. (str(self.headerstr), self.procStacks, self.dataChan, self.refChan, \
  767. str(self.ui.FIDProcComboBox.currentText()), self.ui.mplwidget, \
  768. 1e-3 * self.ui.DeadTimeSpinBox.value( ), self.ui.plotImportCheckBox.isChecked() )) #, self))
  769. elif self.RAWDataProc.pulseType == "4PhaseT1":
  770. self.procThread = thread.start_new_thread(self.RAWDataProc.load4PhaseT1Data, \
  771. (str(self.headerstr), self.procStacks, self.dataChan, self.refChan, \
  772. str(self.ui.FIDProcComboBox.currentText()), self.ui.mplwidget, \
  773. 1e-3 * self.ui.DeadTimeSpinBox.value( ), self.ui.plotImportCheckBox.isChecked() )) #, self))
  774. self.YamlNode.Import["GMR Header"] = self.headerstr
  775. self.YamlNode.Import["opened"] = datetime.datetime.now().isoformat()
  776. self.YamlNode.Import["pulse Type"] = str(self.RAWDataProc.pulseType)
  777. self.YamlNode.Import["stacks"] = self.procStacks.tolist()
  778. self.YamlNode.Import["data channels"] = self.dataChan.tolist()
  779. self.YamlNode.Import["reference channels"] = self.refChan.tolist()
  780. self.YamlNode.Import["pulse records"] = str(self.ui.FIDProcComboBox.currentText())
  781. self.YamlNode.Import["instrument dead time"] = (1e-3 * self.ui.DeadTimeSpinBox.value( ))
  782. self.Log ( )
  783. # should be already done
  784. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("updateProgress(int)"), self.updateProgressBar)
  785. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("enableDSP()"), self.enableDSP)
  786. # QtCore.QObject.connect(self.RAWDataProc, QtCore.SIGNAL("doneStatus()"), self.doneStatus)
  787. #self.ui.ProcessedBox.setEnabled(True)
  788. self.ui.lcdNumberFID1Length.setEnabled(1)
  789. self.ui.lcdNumberFID2Length.setEnabled(1)
  790. self.ui.lcdNumberResampFreq.setEnabled(1)
  791. self.ui.lcdTotalDeadTime.setEnabled(1)
  792. self.ui.lcdTotalDeadTime.display( self.ui.DeadTimeSpinBox.value( ) )
  793. #self.ui.lcdNumberFID1Length.display(0)
  794. #self.ui.lcdNumberFID2Length.display(0)
  795. #self.ui.lcdNumberResampFreq.display( self.RAWDataProc.samp )
  796. self.mpl_toolbar = NavigationToolbar2QT(self.ui.mplwidget, self.ui.mplwidget)
  797. self.ui.mplwidget.draw()
  798. def Log(self):
  799. #for line in yaml.dump(self.YamlNode, default_flow_style=False):
  800. #for line in nlogText:
  801. # self.ui.logTextBrowser.append( line )
  802. # self.logText.append( line )
  803. self.ui.logTextBrowser.clear()
  804. self.ui.logTextBrowser.append( yaml.dump(self.YamlNode)) #, default_flow_style=False) )
  805. def disable(self):
  806. self.ui.BandPassBox.setEnabled(False)
  807. self.ui.downSampleGroupBox.setEnabled(False)
  808. self.ui.windowFilterGroupBox.setEnabled(False)
  809. # self.ui.despikeGroupBox.setEnabled(False)
  810. self.ui.adaptBox.setEnabled(False)
  811. self.ui.adaptFDBox.setEnabled(False)
  812. self.ui.qCalcGroupBox.setEnabled(False)
  813. self.ui.FDSmartStackGroupBox.setEnabled(False)
  814. self.ui.sumDataBox.setEnabled(False)
  815. self.ui.qdGroupBox.setEnabled(False)
  816. self.ui.gateBox.setEnabled(False)
  817. def enableAll(self):
  818. self.enableDSP()
  819. self.enableQC()
  820. def enableDSP(self):
  821. # Bandpass filter
  822. self.ui.BandPassBox.setEnabled(True)
  823. self.ui.BandPassBox.setChecked(True)
  824. self.ui.bandPassGO.setEnabled(False) # need to design first
  825. self.ui.plotBP.setEnabled(True)
  826. self.ui.plotBP.setChecked(True)
  827. # downsample
  828. self.ui.downSampleGroupBox.setEnabled(True)
  829. self.ui.downSampleGroupBox.setChecked(True)
  830. # window
  831. self.ui.windowFilterGroupBox.setEnabled(True)
  832. self.ui.windowFilterGroupBox.setChecked(True)
  833. # Despike
  834. # self.ui.despikeGroupBox.setEnabled(True)
  835. # self.ui.despikeGroupBox.setChecked(False)
  836. # Adaptive filtering
  837. self.ui.adaptBox.setEnabled(True)
  838. self.ui.adaptBox.setChecked(True)
  839. # FD Adaptive filtering
  840. self.ui.adaptFDBox.setEnabled(True)
  841. self.ui.adaptFDBox.setChecked(True)
  842. # sum group box
  843. try:
  844. if len(self.dataChan) > 1:
  845. self.ui.sumDataBox.setEnabled(True)
  846. self.ui.sumDataBox.setChecked(True)
  847. except:
  848. pass
  849. # Quadrature Detect
  850. self.ui.qdGroupBox.setEnabled(True)
  851. self.ui.qdGroupBox.setChecked(True)
  852. self.enableQC()
  853. def enableQC(self):
  854. # Q calc
  855. self.ui.qCalcGroupBox.setEnabled(True)
  856. self.ui.qCalcGroupBox.setChecked(True)
  857. # FD SmartStack
  858. self.ui.FDSmartStackGroupBox.setEnabled(True)
  859. self.ui.FDSmartStackGroupBox.setChecked(True)
  860. # Quadrature detect
  861. try:
  862. for pulse in self.RAWDataProc.DATADICT["PULSES"]:
  863. np.shape(self.RAWDataProc.DATADICT[pulse]["Q"])
  864. self.RAWDataProc.DATADICT["stack"]
  865. self.ui.qdGroupBox.setEnabled(True)
  866. self.ui.qdGroupBox.setChecked(True)
  867. except:
  868. self.ui.qdGroupBox.setEnabled(False)
  869. self.ui.qdGroupBox.setChecked(False)
  870. # Gating
  871. try:
  872. self.RAWDataProc.DATADICT["CA"]
  873. self.ui.gateBox.setEnabled(True)
  874. self.ui.gateBox.setChecked(True)
  875. except:
  876. self.ui.gateBox.setEnabled(False)
  877. self.ui.gateBox.setChecked(False)
  878. def despikeFilter(self):
  879. self.lock("despike filter")
  880. thread.start_new_thread(self.RAWDataProc.despike, \
  881. (self.ui.windowSpinBox.value(), \
  882. self.ui.thresholdSpinBox.value(), \
  883. str(self.ui.replComboBox.currentText()), \
  884. self.ui.rollOnSpinBox.value(), \
  885. self.ui.despikeInterpWinSpinBox.value(),
  886. self.ui.mplwidget))
  887. def calcQ(self):
  888. if "Calc Q" not in self.YamlNode.Processing.keys():
  889. #print("In CalcQ", yaml.dump(self.YamlNode.Processing) )
  890. self.YamlNode.Processing["Calc Q"] = True
  891. #print( yaml.dump(self.YamlNode.Processing) )
  892. self.Log()
  893. else:
  894. err_msg = "Q values have already been calculated"
  895. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  896. err_msg)
  897. return
  898. self.lock("pulse moment calculation")
  899. thread.start_new_thread(self.RAWDataProc.effectivePulseMoment, \
  900. (self.ui.CentralVSpinBox.value(), \
  901. self.ui.mplwidget))
  902. def FDSmartStack(self):
  903. if "TD stack" not in self.YamlNode.Processing.keys():
  904. self.YamlNode.Processing["TD stack"] = {}
  905. self.YamlNode.Processing["TD stack"]["outlier"] = str( self.ui.outlierTestCB.currentText() )
  906. self.YamlNode.Processing["TD stack"]["cutoff"] = str( self.ui.MADCutoff.value() )
  907. self.Log()
  908. else:
  909. err_msg = "TD noise cancellation has already been applied!"
  910. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  911. err_msg)
  912. return
  913. self.lock("time-domain smart stack")
  914. thread.start_new_thread(self.RAWDataProc.TDSmartStack, \
  915. (str(self.ui.outlierTestCB.currentText()), \
  916. self.ui.MADCutoff.value(),
  917. self.ui.mplwidget))
  918. def adaptFilter(self):
  919. if "TD noise cancellation" not in self.YamlNode.Processing.keys():
  920. self.YamlNode.Processing["TD noise cancellation"] = {}
  921. self.YamlNode.Processing["TD noise cancellation"]["n_Taps"] = str(self.ui.MTapsSpinBox.value())
  922. self.YamlNode.Processing["TD noise cancellation"]["lambda"] = str(self.ui.adaptLambdaSpinBox.value())
  923. self.YamlNode.Processing["TD noise cancellation"]["truncate"] = str(self.ui.adaptTruncateSpinBox.value())
  924. self.YamlNode.Processing["TD noise cancellation"]["mu"] = str(self.ui.adaptMuSpinBox.value())
  925. self.YamlNode.Processing["TD noise cancellation"]["PCA"] = str(self.ui.PCAComboBox.currentText())
  926. self.Log()
  927. else:
  928. err_msg = "TD noise cancellation has already been applied!"
  929. reply =QtWidgets.QMessageBox.critical(self, 'Error', err_msg)
  930. #return
  931. self.lock("TD noise cancellation filter")
  932. thread.start_new_thread(self.RAWDataProc.adaptiveFilter, \
  933. (self.ui.MTapsSpinBox.value(), \
  934. self.ui.adaptLambdaSpinBox.value(), \
  935. self.ui.adaptTruncateSpinBox.value(), \
  936. self.ui.adaptMuSpinBox.value(), \
  937. str(self.ui.PCAComboBox.currentText()), \
  938. self.ui.mplwidget))
  939. def sumDataChans(self):
  940. if "Data sum" not in self.YamlNode.Processing.keys():
  941. self.YamlNode.Processing["Data sum"] = True
  942. self.Log()
  943. else:
  944. err_msg = "Data channels have already been summed!"
  945. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  946. err_msg)
  947. return
  948. self.lock("Summing data channels")
  949. self.dataChan = [self.dataChan[0]]
  950. self.ui.sumDataBox.setEnabled(False)
  951. thread.start_new_thread( self.RAWDataProc.sumData, ( self.ui.mplwidget, 7 ) )
  952. def adaptFilterFD(self):
  953. self.lock("FD noise cancellation filter")
  954. thread.start_new_thread(self.RAWDataProc.adaptiveFilterFD, \
  955. (str(self.ui.windowTypeComboBox.currentText()), \
  956. self.ui.windowBandwidthSpinBox.value(), \
  957. self.ui.CentralVSpinBox.value(), \
  958. self.ui.mplwidget))
  959. def bandPassFilter(self):
  960. if "Bandpass filter" not in self.YamlNode.Processing.keys():
  961. self.YamlNode.Processing["Bandpass filter"] = {}
  962. self.YamlNode.Processing["Bandpass filter"]["central_nu"] = str(self.ui.CentralVSpinBox.value())
  963. self.YamlNode.Processing["Bandpass filter"]["passband"] = str(self.ui.passBandSpinBox.value())
  964. self.YamlNode.Processing["Bandpass filter"]["stopband"] = str(self.ui.stopBandSpinBox.value())
  965. self.YamlNode.Processing["Bandpass filter"]["gpass"] = str(self.ui.gpassSpinBox.value())
  966. self.YamlNode.Processing["Bandpass filter"]["gstop"] = str(self.ui.gstopSpinBox.value())
  967. self.YamlNode.Processing["Bandpass filter"]["type"] = str(self.ui.fTypeComboBox.currentText())
  968. self.Log()
  969. else:
  970. err_msg = "Bandpass filter has already been applied!"
  971. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  972. err_msg)
  973. return
  974. self.lock("bandpass filter")
  975. nv = self.ui.lcdTotalDeadTime.value( ) + self.ui.lcdNumberFTauDead.value()
  976. self.ui.lcdTotalDeadTime.display( nv )
  977. thread.start_new_thread(self.RAWDataProc.bandpassFilter, \
  978. (self.ui.mplwidget, 0, self.ui.plotBP.isChecked() ))
  979. def downsample(self):
  980. self.lock("resampling")
  981. if "Resample" not in self.YamlNode.Processing.keys():
  982. self.YamlNode.Processing["Resample"] = {}
  983. self.YamlNode.Processing["Resample"]["downsample factor"] = []
  984. self.YamlNode.Processing["Resample"]["truncate length"] = []
  985. self.YamlNode.Processing["Resample"]["downsample factor"].append( str(self.ui.downSampleSpinBox.value() ) )
  986. self.YamlNode.Processing["Resample"]["truncate length"].append( str( self.ui.truncateSpinBox.value() ) )
  987. self.Log( )
  988. thread.start_new_thread(self.RAWDataProc.downsample, \
  989. (self.ui.truncateSpinBox.value(), \
  990. self.ui.downSampleSpinBox.value(),
  991. self.ui.mplwidget))
  992. def quadDet(self):
  993. if "Quadrature detection" not in self.YamlNode.Processing.keys():
  994. self.YamlNode.Processing["Quadrature detection"] = {}
  995. self.YamlNode.Processing["Quadrature detection"]["trim"] = str( self.ui.trimSpin.value() )
  996. self.Log()
  997. else:
  998. err_msg = "Quadrature detection has already been done!"
  999. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  1000. err_msg)
  1001. return
  1002. self.lock("quadrature detection")
  1003. thread.start_new_thread(self.RAWDataProc.quadDet, \
  1004. (self.ui.trimSpin.value(), int(self.ui.QDType.currentIndex()), self.ui.mplwidget))
  1005. self.ui.plotQD.setEnabled(True)
  1006. def plotQD(self):
  1007. self.lock("plot QD")
  1008. thread.start_new_thread(self.RAWDataProc.plotQuadDet, \
  1009. (self.ui.trimSpin.value(), int(self.ui.QDType.currentIndex()), self.ui.mplwidget))
  1010. def gateIntegrate(self):
  1011. if "Gate integrate" not in self.YamlNode.Processing.keys():
  1012. self.YamlNode.Processing["Gate integrate"] = {}
  1013. self.YamlNode.Processing["Gate integrate"]["gpd"] = str(self.ui.GPDspinBox.value( ) )
  1014. self.Log()
  1015. self.lock("gate integration")
  1016. thread.start_new_thread(self.RAWDataProc.gateIntegrate, \
  1017. (self.ui.GPDspinBox.value(), self.ui.trimSpin.value(), self.ui.mplwidget))
  1018. self.ui.actionExport_Preprocessed_Dataset.setEnabled(True)
  1019. self.ui.plotGI.setEnabled(True)
  1020. def plotGI(self):
  1021. self.lock("plot gate integrate")
  1022. thread.start_new_thread(self.RAWDataProc.plotGateIntegrate, \
  1023. (self.ui.GPDspinBox.value(), self.ui.trimSpin.value(), \
  1024. self.ui.QDType_2.currentIndex(), self.ui.mplwidget))
  1025. def designFilter(self):
  1026. [bord, fe] = self.RAWDataProc.designFilter( \
  1027. self.ui.CentralVSpinBox.value(), \
  1028. self.ui.passBandSpinBox.value(), \
  1029. self.ui.stopBandSpinBox.value(), \
  1030. self.ui.gpassSpinBox.value(), \
  1031. self.ui.gstopSpinBox.value(), \
  1032. str(self.ui.fTypeComboBox.currentText()),
  1033. self.ui.mplwidget)
  1034. self.ui.lcdNumberFilterOrder.display(bord)
  1035. self.ui.lcdNumberFTauDead.display(1e3*fe)
  1036. #self.ui.lcdNumberFilterOrder.display(bord)
  1037. self.ui.bandPassGO.setEnabled(1)
  1038. # self.ui.lcdNumberTauPulse2.display(1e3*self.RAWDataProc.pulseLength[1])
  1039. def windowFilter(self):
  1040. if "Window filter" not in self.YamlNode.Processing.keys():
  1041. self.YamlNode.Processing["Window filter"] = {}
  1042. self.YamlNode.Processing["Window filter"]["type"] = str(self.ui.windowTypeComboBox.currentText())
  1043. self.YamlNode.Processing["Window filter"]["width"] = str(self.ui.windowBandwidthSpinBox.value())
  1044. self.YamlNode.Processing["Window filter"]["centre"] = str(self.ui.CentralVSpinBox.value() )
  1045. self.Log()
  1046. else:
  1047. err_msg = "FD window has already been applied!"
  1048. reply =QtWidgets.QMessageBox.critical(self, 'Error',
  1049. err_msg)
  1050. return
  1051. self.lock("window filter")
  1052. thread.start_new_thread(self.RAWDataProc.windowFilter, \
  1053. (str(self.ui.windowTypeComboBox.currentText()), \
  1054. self.ui.windowBandwidthSpinBox.value(), \
  1055. self.ui.CentralVSpinBox.value(), \
  1056. self.ui.mplwidget))
  1057. def designFDFilter(self):
  1058. # thread.start_new_thread(self.RAWDataProc.computeWindow, ( \
  1059. # "Pulse 1",
  1060. # self.ui.windowBandwidthSpinBox.value(), \
  1061. # self.ui.CentralVSpinBox.value(), \
  1062. # str(self.ui.windowTypeComboBox.currentText()), \
  1063. # self.ui.mplwidget ))
  1064. a,b,c,d,dead = self.RAWDataProc.computeWindow( \
  1065. "Pulse 1",
  1066. self.ui.windowBandwidthSpinBox.value(), \
  1067. self.ui.CentralVSpinBox.value(), \
  1068. str(self.ui.windowTypeComboBox.currentText()), \
  1069. self.ui.mplwidget )
  1070. self.ui.lcdWinDead.display(dead)
  1071. def updateProgressBar(self, percent):
  1072. self.ui.barProgress.setValue(percent)
  1073. def updateProc(self):
  1074. if str(self.ui.FIDProcComboBox.currentText()) == "Pulse 1":
  1075. self.ui.lcdNumberFID1Length.display(self.RAWDataProc.DATADICT["Pulse 1"]["TIMES"][-1]- self.RAWDataProc.DATADICT["Pulse 1"]["TIMES"][0])
  1076. elif str(self.ui.FIDProcComboBox.currentText()) == "Pulse 2":
  1077. self.ui.lcdNumberFID2Length.display(self.RAWDataProc.DATADICT["Pulse 2"]["TIMES"][-1]- self.RAWDataProc.DATADICT["Pulse 2"]["TIMES"][0])
  1078. else:
  1079. self.ui.lcdNumberFID1Length.display(self.RAWDataProc.DATADICT["Pulse 1"]["TIMES"][-1]- self.RAWDataProc.DATADICT["Pulse 1"]["TIMES"][0])
  1080. self.ui.lcdNumberFID2Length.display(self.RAWDataProc.DATADICT["Pulse 2"]["TIMES"][-1]- self.RAWDataProc.DATADICT["Pulse 2"]["TIMES"][0])
  1081. self.ui.lcdNumberResampFreq.display( self.RAWDataProc.samp )
  1082. def doneStatus(self): # unlocks GUI
  1083. self.ui.statusbar.clearMessage ( )
  1084. self.ui.barProgress.hide()
  1085. self.updateProc()
  1086. self.enableAll()
  1087. def lock(self, string):
  1088. self.ui.statusbar.showMessage ( string )
  1089. self.ui.barProgress.show()
  1090. self.ui.barProgress.setValue(0)
  1091. self.disable()
  1092. def unlock(self):
  1093. self.ui.statusbar.clearMessage ( )
  1094. self.ui.barProgress.hide()
  1095. self.enableAll()
  1096. def done(self):
  1097. self.ui.statusbar.showMessage ( "" )
  1098. ################################################################
  1099. ################################################################
  1100. # Boiler plate main function
  1101. import pkg_resources
  1102. from pkg_resources import resource_string
  1103. import matplotlib.image as mpimg
  1104. import matplotlib.pyplot as plt
  1105. def main():
  1106. # splash screen logo
  1107. logo = pkg_resources.resource_filename(__name__, 'akvo.png')
  1108. logo2 = pkg_resources.resource_filename(__name__, 'akvo2.png')
  1109. qApp = QtWidgets.QApplication(sys.argv)
  1110. ssplash = False
  1111. if ssplash:
  1112. pixmap = QtGui.QPixmap(logo)
  1113. splash = QtWidgets.QSplashScreen(pixmap, QtCore.Qt.WindowStaysOnTopHint)
  1114. splash.show()
  1115. aw = ApplicationWindow()
  1116. img=mpimg.imread(logo)
  1117. for ax in [ aw.ui.mplwidget ]:
  1118. ax.fig.clear()
  1119. subplot = ax.fig.add_subplot(111)
  1120. #ax.fig.patch.set_facecolor( None )
  1121. #ax.fig.patch.set_alpha( .0 )
  1122. subplot.imshow(img)
  1123. subplot.xaxis.set_major_locator(plt.NullLocator())
  1124. subplot.yaxis.set_major_locator(plt.NullLocator())
  1125. ax.draw()
  1126. if ssplash:
  1127. splash.showMessage("Loading modules")
  1128. splash.finish(aw)
  1129. #time.sleep(1)
  1130. aw.setWindowTitle("Akvo v"+str(version))
  1131. aw.show()
  1132. qApp.setWindowIcon(QtGui.QIcon(logo2))
  1133. sys.exit(qApp.exec_())
  1134. if __name__ == "__main__":
  1135. main()