Commit a1c7bad3 authored by Caillat Michel's avatar Caillat Michel
Browse files

Working on contours measurements

parent 600e5228
......@@ -27,6 +27,8 @@ import re
from functools import reduce
from matplotlib import pyplot as pp
import cv2
def cmap2palette (palette_name):
x = pp.get_cmap(palette_name)
......@@ -695,22 +697,6 @@ class DataBlock:
def getDimensions(self):
return {"status": True, "message": "", "result": self.__data.shape}
def getSlice(self, iFREQ, step=1):
self.__logger.debug("getSlice: entering")
x = None
numDimensions = len(self.__data.shape)
if numDimensions == 2:
x = self.__data[0:self.__data.shape[0]:step, 0:self.__data.shape[1]:step]
elif numDimensions == 3:
if iFREQ == None:
iFREQ = self.__data.shape[0] / 2
x = self.__data[iFREQ, 0:self.__data.shape[1]:step, 0:self.__data.shape[2]:step]
self.__logger.debug("getSlice: exiting")
return x
def __getSlice(self, iFREQ, step=1):
self.__logger.debug("getSlice: entering")
......@@ -1106,7 +1092,33 @@ class DataBlock:
self.__logger.debug("getContours: exiting")
return result
def measureContour(self, iFREQ, contour):
self.__logger.debug("measureContour : entering")
self.__logger.debug("type of contour %r" % type(contour))
slice = self.__getSlice(iFREQ)
shape = slice.shape
theMask = np.empty(shape, dtype=bool)
numpix = 0
for j in np.arange(shape[0]):
for i in np.arange(shape[1]):
theMask[j][i] = cv2.pointPolygonTest(contour, (j, i), False) > 0
if theMask[j][i] :
numpix += 1
result = {}
result["sum"] = np.nansum(slice[theMask])
result["min"] = np.nanmin(slice[theMask])
result["max"] = np.nanmax(slice[theMask])
result["mean"] = np.nanmean(slice[theMask])
result["var"] = np.nanvar(slice[theMask])
result["numpix"] = numpix
result["percent"] = numpix / (shape[0]*shape[1]) * 100
result["boudingRect"] = cv2.boudingRect(contour)
result = {"status" : True, "message": "", "result" : result}
self.__logger.debug("measureContour : exiting")
return result
#
# End of DataBlock class definition.
#
......@@ -255,12 +255,20 @@ class DataManagerImpl :
def getContours(self, relFITSFilePath, iFREQ, iDEC0, iDEC1, iRA0, iRA1, **kwargs):
self.__logger.debug("getContours : entering.")
result = self.__checkPresence(relFITSFilePath)
self.__logger.debug("kwargs = %r" % kwargs)
if result :
if result["status"] :
result = self.__dataBlocks[relFITSFilePath].getContours(iFREQ, iDEC0, iDEC1, iRA0, iRA1, **kwargs)
self.__logger.debug("getContours : exiting.")
return result
def measureContour(self, relFITSFilePath, iFREQ, contour):
self.__logger.debug("measureContour - dispatcher : entering")
result = self.__checkPresence(relFITSFilePath)
if result["status"] :
result = self.__dataBlocks[relFITSFilePath].measureContour(iFREQ, contour)
self.__logger.debug("measureContour - dispatcher : exiting")
return result
#
# create fits file containing a spectrum at iRA, iDEC
# The use case is interoperability via SAMP
......
......@@ -3,3 +3,4 @@ matplotlib
pillow
pypng
Autologging
opencv-python
......@@ -70,6 +70,7 @@ import os
import traceback
import collections
import numpy as np
import cv2
import json
import errno
......@@ -858,6 +859,29 @@ def getContours():
return json.dumps(result)
@route( baseUrl+'/measureContour', name='measureContour', method='POST')
@enable_cors
def measureContour():
logger.debug("measureContour - wrapper : entering")
try:
body = byteify(json.loads(request.body.read()))
relFITSFilePath = rebuildFilename(body['relFITSFilePath'])
iFREQ = getIntValue(body['iFREQ'])
contour = np.asarray(json.loads(body['contour']))
result = dm.measureContour(relFITSFilePath, iFREQ, contour)
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
message = "%s ! %s" % (type(e), "".join(traceback.format_list(traceback.extract_tb(exc_traceback))))
logger.debug(message)
result = {"status": False, "message": message}
response.content_type = "application/json; charset=utf-8"
logger.debug("measureContour - wrapper : exiting")
return json.dumps(result)
"""
Given the values of a spectrum 'ydata' passed with the corresponding frequency values 'xdata'
at an (x,y) given position ( integer indexes ) in the RAxDEC plan of a cube, generate a FITS file containing that
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment