[GRASS-SVN] r66008 - in sandbox/krejcmat/src: . cairs cairs/wx.cairs

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Aug 24 11:49:30 PDT 2015


Author: krejcmat
Date: 2015-08-24 11:49:30 -0700 (Mon, 24 Aug 2015)
New Revision: 66008

Added:
   sandbox/krejcmat/src/cairs/
   sandbox/krejcmat/src/cairs/wx.cairs/
   sandbox/krejcmat/src/cairs/wx.cairs/__init__.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_calib.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_coor.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_core.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_factory.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_gauss.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_helpers.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_predict.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_prediction.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_prior_definition.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_sensor.py
   sandbox/krejcmat/src/cairs/wx.cairs/cairs_signal.py
Log:
wx.cairs sandbox: beginning

Added: sandbox/krejcmat/src/cairs/wx.cairs/__init__.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/__init__.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/__init__.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,6 @@
+from datetime import date
+
+#dependency
+#https://github.com/saullocastro/cubature
+
+REF_TIME = date(1990, 6, 30)
\ No newline at end of file

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_calib.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_calib.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_calib.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1 @@
+#def make_function_dict()
\ No newline at end of file

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_coor.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_coor.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_coor.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,70 @@
+from abc import ABCMeta
+from math import sin, cos
+
+
+class Location:
+    __metaclass__ = ABCMeta
+
+
+class Coor(Location):
+    def __init__(self, x, y, time=0.0):
+        self.x = x
+        self.y = y
+        self.time = time
+
+    def __str__(self):
+        return "({0},{1},{2})".format(self.x, self.y, self.time)
+
+    def __add__(self, other):
+        # addition of coordinates
+        x = self.x + other.x
+        y = self.y + other.y
+        time = self.time + other.time
+
+        return Coor(x, y, time)
+
+    def IsZero(self):
+        if self.x == 0 and self.y == 0 and self.time == 0:
+            return True
+        else:
+            return False
+
+    def __sub__(self, other):
+        # subtraction of coordinates
+        x = self.x - other.x
+        y = self.y - other.y
+        time = self.time - other.time
+
+        return Coor(x, y, time)
+
+    @staticmethod
+    def rotate( coor, origin, angle):
+        """ rotate coordinates around 'origin' (in spatial dimensions only)
+            angle: rotation angle in rad"""
+        if angle == 0.0:
+            return coor
+
+        x_trans = coor.x - origin.x
+        y_trans = coor.y - origin.y
+
+        return Coor(cos(angle) * x_trans - sin(angle) * y_trans + origin.x,
+                    sin(angle) * x_trans + cos(angle) * y_trans + origin.y,
+                    coor.time)
+
+
+class Domain(Location):
+    """An imutable class definning type Domain"""
+
+    __slots__ = ["position", "extend", "angle"]
+
+    def __init__(self, position, extend, angle):
+        """Constructor"""
+        super(Domain, self).__setattr__("position", position)
+        super(Domain, self).__setattr__("extend", extend)
+        super(Domain, self).__setattr__("angle", angle)
+
+    def __setattr__(self, name, value):
+        """"""
+        msg = "'%s' has no attribute %s" % (self.__class__,
+                                            name)
+        raise AttributeError(msg)
\ No newline at end of file

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_core.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_core.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_core.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1 @@
+__author__ = 'matt'

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_factory.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_factory.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_factory.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,35 @@
+__author__ = 'matt'
+import csv
+from datetime import datetime
+from cairs_signal import Signal
+from cairs_coor import Coor
+
+class MySignals(object):
+    def __init__(self):
+        self.signals=[]
+
+    def add_signal(self,filecsv,sensor,position,date_format,angle=0.0,delim=','):
+        new_signal=None
+        with open(filecsv, 'rb') as csvfile:
+            data = csv.reader(csvfile, delimiter=delim)
+            for date,val in data:
+                time = datetime.strptime(date, '%b %d %Y %I:%M%p')
+                new_signal = Signal(data, sensor, Coor(position.x, position.y, time), angle)
+
+        if new_signal:
+            self.signals.append(new_signal)
+
+    def remove_signal(self,sensor=None,coor=None,angle=None):
+        if sensor:
+            for i,signal in self.signals:
+                if signal.sensor is sensor:
+                    del self.signals[i]
+                    return
+        else:
+            for i,signal in self.signals:
+                if signal.coor == coor and signal.angle == angle:
+                    del self.signals[i]
+                    return
+
+
+

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_gauss.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_gauss.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_gauss.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1 @@
+__author__ = 'matt'

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_helpers.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_helpers.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_helpers.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1 @@
+__author__ = 'matt'

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_predict.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_predict.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_predict.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,25 @@
+__author__ = 'matt'
+
+
+class CAIRS(object):
+    """
+    Predict (integrated) intensities condition on all relevant signals
+    :param loc_pred:       Vector or array of 'Location's for predictions
+    :param signals:        Vector or array of 'Signal's
+    :param prior_mean:     mean function of Prior, f(c::Coor)
+    :param prior_cov:      covariance function of Prior, f(c1::Coor, c2::Coor)
+    # - optional: -
+    :param n_sample_calib: number of MCMC samples for calibration
+    :param burn_in:        number of calibration samples that are discarded
+    :param n_sample_pred:  number of samples for prediction
+    :param delta:          maximal time distance of signals to prediction location in Milli seconds
+    """
+    def __init__(self):
+
+
+        self.mean_GP=None
+        self.cov_GP=None
+        self.loc_pred=[]
+        self.signals=[]
+        self.prior_mean=None
+        self.n_sample_calib=
\ No newline at end of file

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_prediction.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_prediction.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_prediction.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1 @@
+__author__ = 'matt'

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_prior_definition.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_prior_definition.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_prior_definition.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,101 @@
+__author__ = 'matt'
+import sys
+from math import sqrt,exp
+from cairs_coor import Coor
+
+class MeanConstant(object):
+    def __init__(self,mean=0.0):
+        self.mean=mean
+        print('Prior has a constant mean of %s'%mean)
+        self.extend_v=None
+        self.pos_v=None
+        self.d=None
+
+    def f_mu(self,d):
+        """
+        #vector of domain extend
+        mean function
+        :param d: domain
+        :type d: Domain
+        """
+
+        self.extend_v = [d.extend.x,
+                    d.extend.y,
+                    d.extend.time]
+        # vector of positions
+        self.pos_v = [d.position.x,
+                 d.position.y,
+                 d.position.time]
+        self.d=d
+        lower=[]
+        for i,val in enumerate(self.pos_v):
+            if self.extend_v[i] != 0.0:
+                lower.append(val)
+        upper = []
+
+        for i,val in enumerate(self.pos_v):
+            if (self.extend_v[i] + val) != 0.0:
+                upper.append(val)
+
+        TODO cubature
+
+
+
+    def f_int(self,v):
+        """
+        :brief : construct function to integrate over
+        :param v: numbers
+        :type v: list
+        :return:
+        :rtype:
+        """
+        kk=1
+        for i in range(0,2,1):
+            if self.extend_v[i] != 0.0:
+                self.pos_v[i] = v[kk]
+                kk += 1
+        coorTMP=Coor(self.pos_v[1],self.pos_v[2],self.pos_v[3])
+        self.f_mu(Coor.rotate(coorTMP,self.d.position,self.d.angle))
+
+
+class CovExponentional(object):
+    def __init__(self,sigma=10.0,l_spatial=3000.0,l_temporal=600*1000,gamma=1.0):
+        """
+         separable gamma-exponential
+
+        ## Equation (4.18) in Rasmussen, C. E. and Williams, C. K. I. (2006) Gaussian processes
+        ## for machine learning, Massachusett, MIT Press.
+        @:param sigma -standard deviation of GP
+        @:param l_spatioal -spatial correlation length
+        @:param l_temporal -temporal correlation length [milliseconds]
+        @:param gamma -exponent for smoothness in [0, 2]
+        """
+        self.sigma=sigma
+        self.l_spatial=l_spatial
+        self.temporal=l_temporal
+        self.gamma=gamma
+        if gamma<0 or gamma>2:
+            sys.exit("Gamma must bi in [0,2]")
+
+        #TODO check if time is in milliseconds
+        print("- Prior has a separable gamma-exponential covariance function with:")
+        print("   standard deviation: %s"%sigma)
+        print("   spatial correlation length: %s"%l_spatial)
+        print("   temporal correlation length [s]: %s"%(l_temporal/1000))
+        print("   gamma: %s"%gamma)
+
+    def f_cov(self,c1,c2):
+        """
+        :param c1: coordination
+        :type c1: Coord
+        :param c2: coordination
+        :type c2: Coord
+        :return: covariance between two points
+        :rtype: number
+        """
+        #covariance
+        var=self.sigma * self.sigma
+        dist_spatial = sqrt((c1.x-c2.x)**2 + (c1.y-c2.y)**2)
+        dist_temporal = abs(c1.time - c2.time)
+        cov=var* exp(- (dist_spatial/self.l_spatial)**self.gamma - (dist_temporal/self.l_spatial)**self.gamma)
+        return cov

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_sensor.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_sensor.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_sensor.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,37 @@
+__author__ = 'matt'
+import sys
+from cairs_coor import Coor
+
+class Sensor(object):
+    """
+    function that computes log p(S| [R_1 ,... R_n], I)
+    must take a signal S and an array of *not transformed* rain
+    """
+    def __init__(self, log_p, delta_coor=None, domain_extent=None):
+        """@:param log_p - methods for computing
+           @:param delta_coor - list of Coor
+           @:param domain_extent - cube of integration, relative to sensor position
+        """
+        self.log_p = log_p
+        if not delta_coor and not domain_extent:
+            coor=Coor(0.0,0.0,0.0)
+            self.delta_coor = [coor]
+            self.domain_extent = coor
+        else:
+            if delta_coor:
+                self.delta_coor = delta_coor
+                self.domain_extent = Coor(0.0, 0.0, 0.0)
+            else:
+                self.domain_extent = domain_extent
+                self.delta_coor=[]
+
+    def __str__(self):
+        if len(self.delta_coor) > 0:
+            print "- log likelihood: %s"%self.log_p
+            for coor in self.delta_coor:
+                print coor
+
+        if not self.domain_extent.IsZero()  :
+            print( "- integration domain: %s"%self.domain_extent)
+        else:
+            print( "- no integration")

Added: sandbox/krejcmat/src/cairs/wx.cairs/cairs_signal.py
===================================================================
--- sandbox/krejcmat/src/cairs/wx.cairs/cairs_signal.py	                        (rev 0)
+++ sandbox/krejcmat/src/cairs/wx.cairs/cairs_signal.py	2015-08-24 18:49:30 UTC (rev 66008)
@@ -0,0 +1,56 @@
+from cairs_coor import *
+
+
+class Signal():
+    def __init__(self, signal, sensor, coor, angle=0.0):
+        """
+        :param signal:  signal obtained by sensor
+        :param sensor: sensor
+        :param coor: coordinates of the sensor
+        :param angle: direction in rad, rotations centre is 'coor'
+        """
+        self.signal = signal
+        self.sensor = sensor
+        self.coor = coor
+        self.angle = angle
+
+    def __str__(self):
+        print("Signal:")
+        print("- position: %s" % self.coor)
+        print("- angle: %s ", self.angle)
+        print("- measured value: %s " % self.signal)
+        print("- signal type: %s" % type)
+        print("- sensor: %s" % str(self.sensor))
+
+
+def find_near_signals(loc_pred, signals, delta):
+    # Find the extrem time-coordiantes of 'loc_pred'
+    tmin = float("inf")
+    tmax = -float("inf")
+    for loc in loc_pred:
+        if type(loc) == type(Domain):
+            t1 = loc.position.time
+            t2 = (loc.position + loc.extend).time
+            tmin = min(tmin, t1, t2)
+            tmax = max(tmax, t1, t2)
+        else:
+            tmin = min(tmin, loc.time)
+            tmax = max(tmax, loc.time)
+
+    tmin -= delta
+    tmax += delta
+
+    # Find signals that are within (tmin, tmax)
+    signals_near = []
+    for sig in signals:
+        t = [sig.position.time,
+             (sig.position + sig.sensor.domain_extent).time,
+             [(sig.position + i).time for i in sig.sensor.delta_coor]]
+
+        # if any(t .< tmax) && any(t .> tmin)
+        #    push!(signals_near, sig)
+        if all(i < tmax for i in t) and all(i > tmin for i in t):  #TODO check it
+            signals_near.append(sig)
+
+    return signals_near
+



More information about the grass-commit mailing list