Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hidra
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
steinbac
hidra
Commits
d7375b58
Commit
d7375b58
authored
9 years ago
by
Manuela Kuhn
Browse files
Options
Downloads
Patches
Plain Diff
Added communication with receiver infrastructure to live viewer
parent
9ba0529e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LiveViewer.py
+166
-118
166 additions, 118 deletions
LiveViewer.py
with
166 additions
and
118 deletions
LiveViewer.py
+
166
−
118
View file @
d7375b58
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import
os
import
os
import
time
import
time
from
dectris
import
albula
#from dectris import albula
from
PyQt4
import
QtCore
from
PyQt4
import
QtCore
from
PyQt4.QtCore
import
SIGNAL
,
QThread
,
QMutex
from
PyQt4.QtCore
import
SIGNAL
,
QThread
,
QMutex
import
zmq
class
LiveView
(
QThread
):
FILETYPE_CBF
=
0
class
LiveView
(
QThread
):
FILETYPE_TIF
=
1
FILETYPE_CBF
=
0
FILETYPE_HDF5
=
2
FILETYPE_TIF
=
1
FILETYPE_HDF5
=
2
alive
=
False
path
=
""
alive
=
False
filetype
=
0
path
=
""
interval
=
0.5
#s
filetype
=
0
stoptimer
=
-
1.0
interval
=
0.5
#s
stoptimer
=
-
1.0
viewer
=
None
subframe
=
None
viewer
=
None
mutex
=
None
subframe
=
None
mutex
=
None
def
__init__
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
,
parent
=
None
):
QThread
.
__init__
(
self
,
parent
)
zmqIp
=
"
127.0.0.1
"
if
path
is
not
None
:
zmqPort
=
"
6071
"
self
.
path
=
path
zmqContext
=
None
if
filetype
is
not
None
:
zmqSocket
=
None
self
.
filetype
=
filetype
if
interval
is
not
None
:
def
__init__
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
,
parent
=
None
):
self
.
interval
=
interval
QThread
.
__init__
(
self
,
parent
)
self
.
mutex
=
QMutex
()
if
path
is
not
None
:
self
.
path
=
path
if
filetype
is
not
None
:
def
start
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
):
self
.
filetype
=
filetype
if
path
is
not
None
:
if
interval
is
not
None
:
self
.
path
=
path
self
.
interval
=
interval
if
filetype
is
not
None
:
self
.
zmqContext
,
self
.
zmqSocket
=
createZmqSocket
(
self
.
zmqIp
,
self
.
zmqPort
)
self
.
filetype
=
filetype
self
.
mutex
=
QMutex
()
if
interval
is
not
None
:
self
.
interval
=
interval
QThread
.
start
(
self
)
def
start
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
):
if
path
is
not
None
:
def
stop
(
self
,
interval
=
0.0
):
self
.
path
=
path
if
self
.
stoptimer
<
0.0
and
interval
>
0.0
:
if
filetype
is
not
None
:
print
"
Live view thread: Stopping in %d seconds
"
%
interval
self
.
filetype
=
filetype
self
.
stoptimer
=
interval
if
interval
is
not
None
:
return
self
.
interval
=
interval
print
"
Live view thread: Stopping thread
"
QThread
.
start
(
self
)
self
.
alive
=
False
self
.
wait
()
# waits until run stops on his own
def
stop
(
self
,
interval
=
0.0
):
if
self
.
stoptimer
<
0.0
and
interval
>
0.0
:
def
run
(
self
):
print
"
Live view thread: Stopping in %d seconds
"
%
interval
self
.
alive
=
True
self
.
stoptimer
=
interval
print
"
Live view thread: started
"
return
suffix
=
[
"
.cbf
"
,
"
.tif
"
,
"
.hdf5
"
]
print
"
Live view thread: Stopping thread
"
self
.
alive
=
False
if
self
.
filetype
in
[
LiveView
.
FILETYPE_CBF
,
LiveView
.
FILETYPE_TIF
]:
# open viewer
# close ZeroMQ socket and destroy ZeroMQ context
while
self
.
alive
:
stopZmq
(
self
.
zmqSocket
,
self
.
zmqContext
)
# find latest image
self
.
mutex
.
lock
()
self
.
wait
()
# waits until run stops on his own
files
=
[(
os
.
path
.
getmtime
(
self
.
path
+
"
/
"
+
fn
),
fn
)
for
fn
in
os
.
listdir
(
self
.
path
)
if
fn
.
lower
().
endswith
(
suffix
[
self
.
filetype
])]
def
run
(
self
):
files
.
sort
()
self
.
alive
=
True
files
.
reverse
()
print
"
Live view thread: started
"
if
len
(
files
)
>
0
:
suffix
=
[
"
.cbf
"
,
"
.tif
"
,
"
.hdf5
"
]
# display image
# wait to make sure the image is copied completely before displaying it
if
self
.
filetype
in
[
LiveView
.
FILETYPE_CBF
,
LiveView
.
FILETYPE_TIF
]:
time
.
sleep
(
0.1
)
# open viewer
try
:
while
self
.
alive
:
self
.
subframe
.
loadFile
(
self
.
path
+
"
/
"
+
files
[
0
][
1
])
print
"
self.alive
"
,
self
.
alive
# viewer or subframe has been closed by the user
# find latest image
except
:
self
.
mutex
.
lock
()
self
.
mutex
.
unlock
()
time
.
sleep
(
0.1
)
# get latest file from reveiver
try
:
try
:
self
.
subframe
=
self
.
viewer
.
openSubFrame
()
received_file
=
communicateWithReceiver
(
self
.
zmqSocket
)
except
:
except
zmq
.
error
.
ZMQError
:
self
.
viewer
=
albula
.
openMainFrame
()
print
"
ZMQError
"
self
.
subframe
=
self
.
viewer
.
openSubFrame
()
break
continue
self
.
mutex
.
unlock
()
# display image
# wait interval
# try:
interval
=
0.0
# self.subframe.loadFile(receiived_file)
while
interval
<
self
.
interval
and
self
.
alive
:
# viewer or subframe has been closed by the user
if
self
.
stoptimer
>
0.0
:
# except:
self
.
stoptimer
-=
0.05
# self.mutex.unlock()
if
self
.
stoptimer
<
0.0
:
# time.sleep(0.1)
self
.
stoptimer
=
-
1.0
# try:
self
.
alive
=
False
# self.subframe = self.viewer.openSubFrame()
time
.
sleep
(
0.05
)
# except:
interval
+=
0.05
# self.viewer = albula.openMainFrame()
elif
self
.
filetype
==
LiveView
.
FILETYPE_HDF5
:
# self.subframe = self.viewer.openSubFrame()
print
"
Live view thread: HDF5 not supported yet
"
# continue
self
.
mutex
.
unlock
()
print
"
Live view thread: Thread for Live view died
"
# wait interval
self
.
alive
=
False
interval
=
0.0
while
interval
<
self
.
interval
and
self
.
alive
:
def
setPath
(
self
,
path
=
None
):
if
self
.
stoptimer
>
0.0
:
self
.
mutex
.
lock
()
self
.
stoptimer
-=
0.05
if
path
is
not
None
:
if
self
.
stoptimer
<
0.0
:
self
.
path
=
path
self
.
stoptimer
=
-
1.0
self
.
mutex
.
unlock
()
self
.
alive
=
False
time
.
sleep
(
0.05
)
def
setFiletype
(
self
,
filetype
=
None
):
interval
+=
0.05
restart
=
False
elif
self
.
filetype
==
LiveView
.
FILETYPE_HDF5
:
if
self
.
alive
:
print
"
Live view thread: HDF5 not supported yet
"
restart
=
True
self
.
stop
()
print
"
Live view thread: Thread for Live view died
"
if
filetype
is
not
None
:
self
.
alive
=
False
self
.
filetype
=
filetype
if
restart
:
def
setPath
(
self
,
path
=
None
):
self
.
start
()
self
.
mutex
.
lock
()
if
path
is
not
None
:
def
setInterval
(
self
,
interval
=
None
):
self
.
path
=
path
if
interval
is
not
None
:
self
.
mutex
.
unlock
()
self
.
interval
=
interval
def
setFiletype
(
self
,
filetype
=
None
):
restart
=
False
if
self
.
alive
:
restart
=
True
self
.
stop
()
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
restart
:
self
.
start
()
def
setInterval
(
self
,
interval
=
None
):
if
interval
is
not
None
:
self
.
interval
=
interval
def
createZmqSocket
(
zmqIp
,
zmqPort
):
context
=
zmq
.
Context
()
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
socket
=
context
.
socket
(
zmq
.
REQ
)
connectionStrSocket
=
"
tcp://{ip}:{port}
"
.
format
(
ip
=
zmqIp
,
port
=
zmqPort
)
socket
.
connect
(
connectionStrSocket
)
return
context
,
socket
def
communicateWithReceiver
(
socket
):
print
"
Asking for next file
"
socket
.
send
(
"
NextFile
"
)
# Get the reply.
message
=
socket
.
recv
()
print
"
Next file:
"
,
message
def
stopZmq
(
zmqSocket
,
zmqContext
):
try
:
print
"
closing zmqSocket...
"
zmqSocket
.
close
(
linger
=
0
)
print
"
closing zmqSocket...done.
"
except
Exception
as
e
:
print
"
closing zmqSocket...failed.
"
print
e
try
:
print
"
closing zmqContext...
"
zmqContext
.
destroy
()
"
closing zmqContext...done.
"
except
Exception
as
e
:
print
"
closing zmqContext...failed.
"
print
e
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment