diff options
author | Onderwaater <onderwaa@esrf.fr> | 2015-09-29 00:16:23 +0200 |
---|---|---|
committer | Onderwaater <onderwaa@esrf.fr> | 2015-09-29 00:16:23 +0200 |
commit | d02fad265965718e1877aad6fe6aea75886bb9cd (patch) | |
tree | d9155474fa96aac5787a48fc5a7e35084324bae3 | |
parent | 411f072242bf7d767eaf19eea0873a2889a6fb3b (diff) |
server for online number crunching, axis slicing fix + id03 backend fix
-rw-r--r-- | BINoculars/backends/id03.py | 22 | ||||
-rw-r--r-- | BINoculars/space.py | 4 | ||||
-rw-r--r-- | server.py | 70 |
3 files changed, 91 insertions, 5 deletions
diff --git a/BINoculars/backends/id03.py b/BINoculars/backends/id03.py index ad96e86..6687258 100644 --- a/BINoculars/backends/id03.py +++ b/BINoculars/backends/id03.py @@ -446,7 +446,7 @@ class EH1(ID03Input): if mon == 0: raise errors.BackendError('Monitor is zero, this results in empty output. Scannumber = {0}, pointnumber = {1}. Did you forget to open the shutter?'.format(self.dbg_scanno, self.dbg_pointno)) - print '{4}| gamma: {0}, delta: {1}, theta: {2}, mu: {3}'.format(gamma, delta, theta, mu, time.ctime(time.time())) + util.status('{4}| gamma: {0}, delta: {1}, theta: {2}, mu: {3}'.format(gamma, delta, theta, mu, time.ctime(time.time()))) # pixels to angles pixelsize = numpy.array(self.config.pixelsize) @@ -554,7 +554,7 @@ class EH2(ID03Input): if mon == 0: raise errors.BackendError('Monitor is zero, this results in empty output. Scannumber = {0}, pointnumber = {1}. Did you forget to open the shutter?'.format(self.dbg_scanno, self.dbg_pointno)) - print '{4}| gamma: {0}, delta: {1}, theta: {2}, mu: {3}'.format(gamma, delta, theta, mu, time.ctime(time.time())) + util.status('{4}| gamma: {0}, delta: {1}, theta: {2}, mu: {3}'.format(gamma, delta, theta, mu, time.ctime(time.time()))) # area correction sdd = self.config.sdd / numpy.cos(gamma * numpy.pi / 180) @@ -652,7 +652,7 @@ class GisaxsDetector(ID03Input): if mon == 0: raise errors.BackendError('Monitor is zero, this results in empty output. Scannumber = {0}, pointnumber = {1}. Did you forget to open the shutter?'.format(self.dbg_scanno, self.dbg_pointno)) - print '{4}| ccdy: {0}, ccdz: {1}, theta: {2}, mu: {3}'.format(ccdy, ccdz, theta, mu, time.ctime(time.time())) + util.status('{4}| ccdy: {0}, ccdz: {1}, theta: {2}, mu: {3}'.format(ccdy, ccdz, theta, mu, time.ctime(time.time()))) # pixels to angles pixelsize = numpy.array(self.config.pixelsize) @@ -699,3 +699,19 @@ class GisaxsDetector(ID03Input): return params +def load_matrix(filename): + if filename == None: + return None + if os.path.exists(filename): + ext = os.path.splitext(filename)[-1] + if ext == '.txt': + return numpy.loadtxt(filename) + elif ext == '.npy': + return numpy.load(filename) + elif ext == '.edf': + return EdfFile.EdfFile(filename).getData(0) + else: + raise ValueError('unknown extension {0}, unable to load matrix!\n'.format(ext)) + else: + raise IOError('filename: {0} does not exist. Can not load matrix'.format(filename)) + diff --git a/BINoculars/space.py b/BINoculars/space.py index b74c4a1..f69aff7 100644 --- a/BINoculars/space.py +++ b/BINoculars/space.py @@ -99,7 +99,7 @@ class Axis(object): if value.stop is None: stop = None else: - stop = self.get_index(value.stop) + 1 + stop = self.get_index(value.stop) if start is not None and stop is not None and start > stop: start, stop = stop, start return slice(start, stop) @@ -589,7 +589,7 @@ class Space(object): mask = intensity.mask intensity = intensity.data else: - mask = numpy.ones_like(intensity) + mask = numpy.zeros_like(intensity, dtype = numpy.bool) valid = numpy.bitwise_and(numpy.isfinite(intensity), ~mask) intensity = intensity[valid] diff --git a/server.py b/server.py new file mode 100644 index 0000000..39838b2 --- /dev/null +++ b/server.py @@ -0,0 +1,70 @@ +import socket +import threading +import SocketServer +import json +import time +import sys +import BINoculars.util, BINoculars.main +import traceback +import re + +queue = list() + +class MyTCPHandler(SocketServer.BaseRequestHandler): + def handle(self): + input = self.request.recv(1024) + try: + job = json.loads(input) + queue.append(job) + print 'Recieved command: {0}. Job is added to queue.\nJobs left in queue: {1}'.format(job['command'], ','.join(tuple(j['command'] for j in queue))) + response = 'Job added to queue' + except: + print 'Could not parse the job: {0}'.format(input) + response = 'Error: Job could not be added to queue' + finally: + self.request.sendall(response) + +def process(run_event): + while run_event.is_set(): + if len(queue) == 0: + time.sleep(1) + else: + job = queue.pop() + command = job['command'] + print 'Start processing: {0}'.format(command) + try: + if 'overrides' in job: + overrides = tuple(re.split('[:=]', ovr) for ovr in job['overrides']) + else: + overrides = [] + configobj = BINoculars.util.ConfigFile.fromtxtfile(job['configfilename'], overrides = overrides) + BINoculars.main.Main(configobj, [command]) + print 'Succesfully finished processing: {0}.'.format(command) + except Exception as exc: + errorfilename = 'error_{0}.txt'.format(command) + print 'An error occured for scan {0}. For more information see {1}'.format(command, errorfilename) + with open(errorfilename, 'w') as fp: + traceback.print_exc(file = fp) + finally: + print 'Jobs left in queue: {0}'.format(','.join(tuple(j['command'] for j in queue))) + +if __name__ == "__main__": + HOST, PORT = socket.gethostbyname(socket.gethostname()), 0 + + run_event = threading.Event() + run_event.set() + + process_thread = threading.Thread(target=process, args = (run_event,)) + process_thread.start() + + server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) + ip, port = server.server_address + + # Activate the server; this will keep running until you + # interrupt the program with Ctrl-C + print 'Server started running at ip {0} and port {1}. Interrupt server with Ctrl-C'.format(ip,port) + try: + server.serve_forever() + except KeyboardInterrupt: + run_event.clear() + process_thread.join() |