Skip to content

Commit 8cd7152

Browse files
committed
Sawbridge modified and pylint verified
1 parent d87ce22 commit 8cd7152

File tree

5 files changed

+189
-249
lines changed

5 files changed

+189
-249
lines changed

models/toy_sources/circle.py

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
1+
"""Circle process."""
2+
13
import tensorflow as tf
24
import tensorflow_probability as tfp
35
import numpy as np
6+
47
class Circle(tfp.distributions.Distribution):
5-
"""The "circle": [cos x, sin x] where x is uniform over [0,2pi]"""
6-
def __init__(self,width=0.,dtype=tf.float32,validate_args=False,allow_nan_stats=True,name='circle'):
7-
"""
8-
Args:
9-
width: Float in [0,1]. Allows for realizations to be uniformly distributed in a band between radius 1-width and 1+width.
8+
"""The "circle": [cos x, sin x] where x is uniform over [0,2pi]"""
9+
def __init__(self,width=0.,dtype=tf.float32,validate_args=False,
10+
allow_nan_stats=True,name='circle'):
11+
"""
12+
Args:
13+
width: Float in [0,1]. Allows for realizations to be uniformly
14+
distributed in a band between radius 1-width and 1+width.
1015
dtype: Data type of returned realization. Defaults to tf.float32.
1116
validate_args: required by tensoflow Distribution class but unused.
1217
allow_nan_stats: required by tensorflow Distribution class but unused.
1318
name: String. Name of the created object.
14-
"""
15-
parameters = dict(locals())
16-
with tf.name_scope(name) as name:
17-
self._width=width
18-
super().__init__(dtype=dtype,
19+
"""
20+
parameters = dict(locals())
21+
with tf.name_scope(name) as name:
22+
self._width=width
23+
super().__init__(dtype=dtype,
1924
reparameterization_type=tfp.distributions.NOT_REPARAMETERIZED,
2025
validate_args = validate_args,
2126
allow_nan_stats = allow_nan_stats,
2227
parameters = parameters,
2328
name=name)
24-
@property
25-
def width(self):
26-
return self._width
29+
@property
30+
def width(self):
31+
return self._width
2732

28-
def _batch_shape_tensor(self):
29-
return tf.constant([],dtype=tf.int32)
30-
def _batch_shape(self):
31-
return tf.TensorShape([])
32-
def _event_shape(self):
33-
smp = tf.linspace(0.,1.,2)
34-
return smp.shape
35-
def _event_shape_tensor(self):
36-
smp = tf.linspace(0.,1.,2)
37-
return tf.shape(smp)
38-
def _sample_n(self,n,seed=None):
39-
angles = tf.random.uniform((1,n),minval = 0, maxval = 2*tf.constant(np.pi),seed=seed,dtype=self.dtype)
40-
sample = tf.convert_to_tensor([tf.math.cos(angles),tf.math.sin(angles)])
41-
if self.width>0:
42-
dev = tf.random.uniform((1,n),minval = -(self.width)/2.0, maxval = (self.width)/2.0, seed=seed, dtype=self.dtype)
43-
sample = sample + dev*sample
44-
sample = tf.transpose(sample,[2,1,0])
45-
sample = tf.reshape(sample,[n,2])
46-
return sample
33+
def _batch_shape_tensor(self):
34+
return tf.constant([],dtype=tf.int32)
35+
def _batch_shape(self):
36+
return tf.TensorShape([])
37+
def _event_shape(self):
38+
smp = tf.linspace(0.,1.,2)
39+
return smp.shape
40+
def _event_shape_tensor(self):
41+
smp = tf.linspace(0.,1.,2)
42+
return tf.shape(smp)
43+
def _sample_n(self,n,seed=None):
44+
angles = tf.random.uniform((1,n),minval = 0,maxval = 2*tf.constant(np.pi)
45+
,seed=seed,dtype=self.dtype)
46+
sample = tf.convert_to_tensor([tf.math.cos(angles),tf.math.sin(angles)])
47+
if self.width>0:
48+
dev = tf.random.uniform((1,n),minval = -(self.width)/2.0,
49+
maxval = (self.width)/2.0, seed=seed,
50+
dtype=self.dtype)
51+
sample = sample + dev*sample
52+
sample = tf.transpose(sample,[2,1,0])
53+
sample = tf.reshape(sample,[n,2])
54+
return sample
4755

48-
def _prob(self, x):
49-
new_x = tf.reshape(x,[-1,tf.shape(x)[-1]])
50-
flat_norm = tf.norm(new_x,axis=1)
51-
if self.width>0:
52-
prob = tf.where((flat_norm >= 1-self.width)&(flat_norm <= 1+self.width),
53-
1.0/(2*tf.convert_to_tensor(np.pi)*self.width),
54-
0.
55-
)
56-
else:
57-
prob = tf.where(flat_norm==1,
58-
1.0/(2*tf.convert_to_tensor(np.pi)),
59-
0.
60-
)
61-
return tf.reshape(prob,tf.shape(x)[:-1])
56+
def _prob(self, x):
57+
new_x = tf.reshape(x,[-1,tf.shape(x)[-1]])
58+
flat_norm = tf.norm(new_x,axis=1)
59+
if self.width>0:
60+
prob = tf.where((flat_norm >= 1-self.width)&(flat_norm <= 1+self.width),
61+
1.0/(2*tf.convert_to_tensor(np.pi)*self.width),0.)
62+
else:
63+
prob = tf.where(flat_norm==1,1.0/(2*tf.convert_to_tensor(np.pi)),0.)
64+
return tf.reshape(prob,tf.shape(x)[:-1])

models/toy_sources/ramp.py

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
1+
"""Ramp process."""
2+
13
import tensorflow as tf
24
import tensorflow_probability as tfp
35

46
class Ramp(tfp.distributions.Distribution):
5-
"""The "ramp": Y(t) = (t+V)mod1 - 0.5, where V is uniform over [0,1]. """
6-
def __init__(self,index_points,phase=None,dtype=tf.float32,validate_args=False,allow_nan_stats=True,name='ramp'):
7-
"""
8-
Args:
9-
index_points: 1-D Tensor representing the locations at which to evaluate the process.
10-
phase: Float in [0,1]. Specifies a realization of V.
11-
dtype: Data type of the returned realization. Defaults to tf.flaot32.
12-
validate_args: required by tensorflow Distribution class but not used.
13-
allow_nan_stats: required by tensorflow Distribution class but not used.
14-
name: String. Name of the created object.
15-
"""
16-
parameters = dict(locals())
17-
with tf.name_scope(name) as name:
18-
self._index_points = tf.convert_to_tensor(index_points,dtype_hint= dtype,name='index_points')
19-
self._phase = phase
20-
super().__init__(
7+
"""The "ramp": Y(t) = (t+V)mod1 - 0.5, where V is uniform over [0,1]. """
8+
def __init__(self,index_points,phase=None,dtype=tf.float32,
9+
validate_args=False,allow_nan_stats=True,name='ramp'):
10+
"""
11+
Args:
12+
index_points: 1-D Tensor representing the locations at which to evaluate
13+
the process.
14+
phase: Float in [0,1]. Specifies a realization of V.
15+
dtype: Data type of the returned realization. Defaults to tf.flaot32.
16+
validate_args: required by tensorflow Distribution class but not used.
17+
allow_nan_stats: required by tensorflow Distribution class but not used.
18+
name: String. Name of the created object.
19+
"""
20+
parameters = dict(locals())
21+
with tf.name_scope(name) as name:
22+
self._index_points = tf.convert_to_tensor(index_points,dtype_hint= dtype,
23+
name='index_points')
24+
self._phase = phase
25+
super().__init__(
2126
dtype=dtype,
2227
reparameterization_type = tfp.distributions.NOT_REPARAMETERIZED,
2328
validate_args = validate_args,
2429
allow_nan_stats = allow_nan_stats,
2530
parameters = parameters,
2631
name=name,)
27-
@property
28-
def index_points(self):
29-
return self._index_points
30-
31-
@property
32-
def phase(self):
33-
return self._phase
34-
35-
def _batch_shape_tensor(self):
36-
return tf.constant([],dtype=tf.int32)
37-
38-
def _batch_shape(self):
39-
return tf.TensorShape([])
40-
41-
def _event_shape(self):
42-
return self.index_points.shape
43-
44-
def _event_shape_tensor(self):
45-
return tf.shape(self.index_points)
46-
47-
def _sample_n(self,n,seed=None):
48-
ind = self.index_points
49-
if self._phase is None:
50-
ind += tf.random.uniform ((n,1),seed=seed,dtype=self.dtype)
51-
ind %= 1
52-
else:
53-
phase = tf.repeat(self._phase,repeats=n,axis=0)
54-
phase = tf.reshape(phase,[n,1])
55-
ind += phase
56-
ind %= 1
57-
return ind - 0.5
32+
@property
33+
def index_points(self):
34+
return self._index_points
35+
36+
@property
37+
def phase(self):
38+
return self._phase
39+
40+
def _batch_shape_tensor(self):
41+
return tf.constant([],dtype=tf.int32)
42+
43+
def _batch_shape(self):
44+
return tf.TensorShape([])
45+
46+
def _event_shape(self):
47+
return self.index_points.shape
48+
49+
def _event_shape_tensor(self):
50+
return tf.shape(self.index_points)
51+
52+
def _sample_n(self,n,seed=None):
53+
ind = self.index_points
54+
if self._phase is None:
55+
ind += tf.random.uniform ((n,1),seed=seed,dtype=self.dtype)
56+
ind %= 1
57+
else:
58+
phase = tf.repeat(self._phase,repeats=n,axis=0)
59+
phase = tf.reshape(phase,[n,1])
60+
ind += phase
61+
ind %= 1
62+
return ind - 0.5

models/toy_sources/sawbridge.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55

66

77
class Sawbridge(tfp.distributions.Distribution):
8-
"""The "sawbridge": B(t) = t - 1(t > Z), where Z is uniform over [0,1]."""
8+
"""The "sawbridge": B(t) = t - 1(t > Z), where Z is uniform over [0,1].
9+
The stationary sawbridge is B(t+V mod 1) where V is uniform over [0,1]
10+
and Z,V are independent."""
911

10-
def __init__(self, index_points, stationary=True, order=1,
11-
dtype=tf.float32, validate_args=False, allow_nan_stats=True,
12-
name="sawbridge"):
12+
def __init__(self, index_points, phase=None, drop = None, stationary=True,
13+
order=1,dtype=tf.float32, validate_args=False,
14+
allow_nan_stats=True,name="sawbridge"):
1315
"""Initializer.
1416
1517
Args:
1618
index_points: 1-D `Tensor` representing the locations at which to evaluate
1719
the process. The intent is that all locations are in [0,1], but the
1820
process has a natural extrapolation outside this range so no error is
1921
thrown.
22+
phase: Float in [0,1]. Specifies realization of V.
23+
drop: Float in [0,1]. Specifies realization of Z.
2024
stationary: Boolean. Whether or not to "scramble" phase.
2125
order: Integer >= 1. The resulting process is a linear combination of
2226
`order` sawbridges.
@@ -32,6 +36,8 @@ def __init__(self, index_points, stationary=True, order=1,
3236
index_points, dtype_hint=dtype, name="index_points")
3337
self._stationary = bool(stationary)
3438
self._order = int(order)
39+
self._phase = phase
40+
self._drop = drop
3541
super().__init__(
3642
dtype=dtype,
3743
reparameterization_type=tfp.distributions.NOT_REPARAMETERIZED,
@@ -53,6 +59,14 @@ def stationary(self):
5359
def order(self):
5460
return self._order
5561

62+
@property
63+
def phase(self):
64+
return self._phase
65+
66+
@property
67+
def drop(self):
68+
return self._drop
69+
5670
def _batch_shape_tensor(self):
5771
return tf.constant([], dtype=tf.int32)
5872

@@ -66,13 +80,22 @@ def _event_shape(self):
6680
return self.index_points.shape
6781

6882
def _sample_n(self, n, seed=None):
69-
uniform = tf.random.uniform((self.order, n), seed=seed, dtype=self.dtype)
7083
ind = self.index_points
84+
if self._drop is None:
85+
uniform = tf.random.uniform((self.order,n),seed=seed,dtype = self.dtype)
86+
else:
87+
uniform = tf.repeat(self._drop,repeats = n*self.order)
88+
uniform = tf.reshape(uniform,[self.order,n])
7189
# ind shape: (time)
7290
uniform = tf.expand_dims(uniform, -1)
7391
# uniform shape: (order, n, 1)
7492
if self.stationary:
75-
ind += tf.random.uniform((n, 1), seed=seed, dtype=self.dtype)
93+
if self._phase is None:
94+
phase = tf.random.uniform((n, 1), seed=seed, dtype=self.dtype)
95+
else:
96+
phase = tf.repeat(self._phase,repeats=n,axis=0)
97+
phase = tf.reshape(phase, [n,1])
98+
ind += phase
7699
ind %= 1.
77100
less = tf.less(uniform, ind)
78101
# less shape: (order, n, time)

0 commit comments

Comments
 (0)