Skip to content

Commit 7c91259

Browse files
committed
Changes during analysis to accomodate more data
types
1 parent 2089320 commit 7c91259

File tree

4 files changed

+116
-94
lines changed

4 files changed

+116
-94
lines changed

NNio.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Input/Ouput module for data generated using the network_watchdog approach for adaptive temporal
33
sampling on the iSIM
44
'''
5+
# There are mixed naming styles here unfortunately
6+
# pylint: disable=C0103
57

68
import contextlib
79
import glob
@@ -37,7 +39,6 @@ def loadiSIMmetadata(folder):
3739
new_name = file.split("Timing_")[0] + "Timing_z" + file.split("Timing_")[1]
3840
os.rename(file, new_name)
3941

40-
4142
delay = []
4243
fileList = sorted(glob.glob(folder + '/iSIMmetadata*.txt'))
4344
for name in fileList:
@@ -54,13 +55,15 @@ def loadiSIMmetadata(folder):
5455
except ValueError:
5556
# This means that delay was > 240 and an empty frame was added
5657
delay[-1] = delay[-1] + 240
58+
5759
return delay
5860

5961

6062
def loadElapsedTime(folder, progress=None, app=None):
6163
""" get the Elapsed time for all .tif files in folder or from a stack """
6264

6365
elapsed = []
66+
step = None
6467
# Check for folder or stack mode
6568
# if not re.match(r'*.tif*', folder) is None:
6669
# # get microManager elapsed times if available
@@ -72,9 +75,11 @@ def loadElapsedTime(folder, progress=None, app=None):
7275
if os.path.isfile(folder + '/img_channel001_position000_time000000000_z000.tif'):
7376
fileList = sorted(glob.glob(folder + '/img_channel001*'))
7477
numFrames = len(fileList)
78+
step = 1
7579
else:
76-
fileList = glob.glob(folder + '/img_*[0-9].tif')
80+
fileList = sorted(glob.glob(folder + '/img_*[0-9].tif'))
7781
numFrames = int(len(fileList)/2)
82+
step = 2
7883

7984
if progress is not None:
8085
progress.setRange(0, numFrames*2)
@@ -99,7 +104,7 @@ def loadElapsedTime(folder, progress=None, app=None):
99104
progress.setValue(i)
100105
i = i + 1
101106

102-
return elapsed
107+
return elapsed[0::step]
103108

104109

105110
def loadNNData(folder):
@@ -468,13 +473,13 @@ def calculateNNforStack(file, model=None, nnPath=None, img_range=None):
468473
""" calculate neural network output for all frames in a stack and write to new stack """
469474
# dataOrder = 1 # 0 for drp/foci first, 1 for mito/structure first
470475
if model is None:
471-
from tensorflow import keras
476+
from tensorflow import keras # pylint: disable=C0415
472477
modelPath = '//lebnas1.epfl.ch/microsc125/Watchdog/GUI/model_Dora.h5'
473478
modelPath = '//lebnas1.epfl.ch/microsc125/Watchdog/Model/paramSweep5/f32_c07_b08.h5'
474479
model = keras.models.load_model(modelPath, compile=True)
475480
elif isinstance(model, str):
476481
if os.path.isfile(model):
477-
from tensorflow import keras
482+
from tensorflow import keras # pylint: disable=C0415
478483
model = keras.models.load_model(model, compile=True)
479484
else:
480485
raise FileNotFoundError
@@ -489,7 +494,8 @@ def calculateNNforStack(file, model=None, nnPath=None, img_range=None):
489494
mdInfo = tif.ome_metadata.encode(encoding='UTF-8', errors='strict')
490495
# extract only the planes that was written to
491496
mdInfoDict = xmltodict.parse(mdInfo)
492-
mdInfoDict['OME']['Image']['Pixels']['Plane'] = mdInfoDict['OME']['Image']['Pixels']['Plane'][0::2]
497+
intermediateInfo = mdInfoDict['OME']['Image']['Pixels']['Plane'][0::2]
498+
mdInfoDict['OME']['Image']['Pixels']['Plane'] = intermediateInfo
493499

494500
dataOrder = int(mdInfoDict['OME']['Image']['Description']['@dataOrder'])
495501
mdInfo = xmltodict.unparse(mdInfoDict).encode(encoding='UTF-8', errors='strict')
@@ -526,14 +532,15 @@ def calculateNNforStack(file, model=None, nnPath=None, img_range=None):
526532

527533

528534
def calculateNNforFolder(folder, model=None):
535+
""" Recalculate all the neural network frames for a folder with peak/structure frames """
529536
if model is None:
530-
from tensorflow import keras
537+
from tensorflow import keras # pylint: disable=C0415
531538
modelPath = '//lebnas1.epfl.ch/microsc125/Watchdog/GUI/model_Dora.h5'
532539
# modelPath = '//lebnas1.epfl.ch/microsc125/Watchdog/Model/paramSweep5/f32_c07_b08.h5'
533540
model = keras.models.load_model(modelPath, compile=True)
534541
elif isinstance(model, str):
535542
if os.path.isfile(model):
536-
from tensorflow import keras
543+
from tensorflow import keras # pylint: disable=C0415
537544
model = keras.models.load_model(model, compile=True)
538545
else:
539546
raise FileNotFoundError
@@ -546,7 +553,7 @@ def calculateNNforFolder(folder, model=None):
546553
filelist = sorted(glob.glob(folder + '/img_*.tif'))
547554
re_odd = re.compile(r'.*time\d*[13579]_.*')
548555
bact_filelist = [file for file in filelist if re_odd.match(file)]
549-
re_even = re.compile(".*time\d*[02468]_.*")
556+
re_even = re.compile(r'.*time\d*[02468]_.*')
550557
ftsz_filelist = [file for file in filelist if re_even.match(file)]
551558

552559
for index, bact_file in tqdm(enumerate(bact_filelist)):
@@ -707,13 +714,14 @@ def on_select(*_):
707714
minspanx=5, minspany=5, spancoords='pixels', rectprops=rectprops,
708715
interactive=True, state_modifier_keys={'square': 'shift'})
709716
plt.show()
710-
rectProp = rectHandle._rect_bbox
717+
# There might be different properties there that could give the same information
718+
# https://matplotlib.org/stable/api/widgets_api.html#matplotlib.widgets.RectangleSelector
719+
rectProp = rectHandle._rect_bbox # pylint: disable=W0212
711720
rectProp = tuple(int(x) for x in rectProp)
712721
print(rectProp)
713722
return rectProp
714723

715724

716-
717725
def main():
718726
""" Main method calculating a nn stack for a set of old Mito/drp stacks """
719727

@@ -735,7 +743,6 @@ def main():
735743
# makePrepImages(folder, model)
736744
# loadiSIMmetadata(folder)
737745

738-
739746
# allFiles = glob.glob('//lebnas1.epfl.ch/microsc125/iSIMstorage/Users/Willi/'
740747
# '180420_DRP_mito_Dora/**/*MMStack*lzw.ome.tif', recursive=True)
741748
# mainPath = 'W:/iSIMstorage/Users/Willi/160622_caulobacter/160622_CB15N_WT/SIM images'

QtImageViewerSeries.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import pyqtgraph as pg
1111
from PyQt5 import QtGui
1212
from PyQt5.QtCore import QRectF, QTimer, pyqtSignal
13-
from PyQt5.QtWidgets import QApplication, QGridLayout, QInputDialog, QWidget
13+
from PyQt5.QtWidgets import (QApplication, QFileDialog, QGridLayout,
14+
QInputDialog, QWidget)
1415
from skimage import io
1516

1617
from SmartMicro.QtImageViewerMerge import QtImageViewerMerge
@@ -20,7 +21,7 @@ def main():
2021
""" Method to test the Viewer in a QBoxLayout with 1 and 2 channels"""
2122
app = QApplication(sys.argv)
2223
viewer = QtImageViewerSeries()
23-
fname = ('//lebnas1.epfl.ch/microsc125/Watchdog/Model/Drp1.h5')
24+
fname = ('//lebnas1.epfl.ch/microsc125/Watchdog/Model/Mito.h5')
2425
# fname = ('W:/Watchdog/Model/Proc.h5')
2526
# fname = ('C:/Users/stepp/Documents/02_Raw/SmartMito/__short.tif')
2627
viewer.loadSeries(fname)
@@ -55,12 +56,22 @@ def __init__(self):
5556
self.timer.setInterval(20)
5657

5758
self.series = None
59+
self.lastKey = None
5860

5961
def newSlider(self, event):
6062
""" ??? """
6163
print(self.region.getRegion())
6264
print(event)
6365

66+
def keyPressEvent(self, keyEvent: QtGui.QKeyEvent) -> None:
67+
""" Save the current frame that is displayed if Ctrl+Save is pressed """
68+
if self.lastKey == 16777249 and keyEvent.key() == 83:
69+
fname, *_ = QFileDialog.getSaveFileName(QWidget(), 'Save file')
70+
frame = int(self.slider.position)
71+
io.imsave(fname, self.series[frame, :, :])
72+
self.lastKey = keyEvent.key()
73+
return super().keyPressEvent(keyEvent)
74+
6475
def onTimer(self, i=None):
6576
"""Set the current frame if timer is running. Also accesible programmatically to set a
6677
certain frame by setting the i input
@@ -71,14 +82,14 @@ def onTimer(self, i=None):
7182
i = int(np.round(i))
7283
print(i)
7384
if self.series.ndim == 4:
74-
num_channels = self.series.shape[3]
85+
numChannels = self.series.shape[3]
7586
else:
76-
num_channels = 1
87+
numChannels = 1
7788

78-
if num_channels == 1:
89+
if numChannels == 1:
7990
self.viewer.setImage(self.series[i, :, :], 0)
8091
else:
81-
for channel in range(num_channels):
92+
for channel in range(numChannels):
8293
self.viewer.setImage(self.series[i, :, :, channel-1], channel-1)
8394

8495
def loadSeries(self, filePath):
@@ -112,7 +123,6 @@ def loadSeries(self, filePath):
112123
self.slider.setSliderRange([-1, self.series.shape[0]-1])
113124
self.viewer.resetZoom()
114125

115-
116126
def startTimer(self):
117127
""" start Timer when slider is pressed """
118128
self.timer.start()
@@ -304,6 +314,5 @@ def mouseReleaseEvent(self, _):
304314
self.buttonReleased.emit()
305315

306316

307-
308317
if __name__ == '__main__':
309318
main()

0 commit comments

Comments
 (0)