/usr/share/pyshared/csa/_misc.py is in python-csa 0.1.0-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | #
# This file is part of the Connection-Set Algebra (CSA).
# Copyright (C) 2010,2011,2012 Mikael Djurfeldt
#
# CSA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# CSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import math
import random
import copy
import connset as cs
import valueset as vs
import _elementary
from csaobject import *
class Random (cs.Operator):
def __mul__ (self, valueSet):
return ValueSetRandomMask (valueSet)
def __call__ (self, p = None, N = None, fanIn = None, fanOut = None):
if p != None:
assert N == None and fanIn == None and fanOut == None, \
'inconsistent parameters'
return _elementary.ConstantRandomMask (p)
elif N != None:
assert fanIn == None and fanOut == None, \
'inconsistent parameters'
return _elementary.SampleNRandomOperator (N)
elif fanIn != None:
assert fanOut == None, \
'inconsistent parameters'
return _elementary.FanInRandomOperator (fanIn)
elif fanOut != None:
return _elementary.FanOutRandomOperator (fanOut)
assert False, 'inconsistent parameters'
class ValueSetRandomMask (cs.Mask):
def __init__ (self, valueSet):
cs.Mask.__init__ (self)
self.valueSet = valueSet
self.state = random.getstate ()
def startIteration (self, state):
random.setstate (self.state)
return self
def iterator (self, low0, high0, low1, high1, state):
for j in xrange (low1, high1):
for i in xrange (low0, high0):
if random.random () < self.valueSet (i, j):
yield (i, j)
def _to_xml (self):
return CSAObject.apply ('times', 'random', self.valueSet._to_xml ())
class Disc (cs.Operator):
def __init__ (self, r):
self.r = r
def __mul__ (self, metric):
return DiscMask (self.r, metric)
class DiscMask (cs.Mask):
def __init__ (self, r, metric):
cs.Mask.__init__ (self)
self.r = r
self.metric = metric
def iterator (self, low0, high0, low1, high1, state):
for j in xrange (low1, high1):
for i in xrange (low0, high0):
if self.metric (i, j) < self.r:
yield (i, j)
class Rectangle (cs.Operator):
def __init__ (self, width, height):
self.width = width
self.height = height
def __mul__ (self, gFunction):
if isinstance (gFunction, tuple):
return RectangleMask (self.width, self.height,
gFunction[0], gFunction[1])
else:
return RectangleMask (self.width, self.height, gFunction, gFunction)
class RectangleMask (cs.Mask):
def __init__ (self, width, height, g0, g1):
cs.Mask.__init__ (self)
self.hwidth = width / 2.0
self.hheight = height / 2.0
self.g0 = g0
self.g1 = g1
def iterator (self, low0, high0, low1, high1, state):
for j in xrange (low1, high1):
for i in xrange (low0, high0):
p0 = self.g0 (i)
p1 = self.g1 (j)
dx = p0[0] - p1[0]
dy = p0[1] - p1[1]
if abs (dx) < self.hwidth and abs (dy) < self.hheight:
yield (i, j)
class Gaussian (cs.Operator):
def __init__ (self, sigma, cutoff):
self.sigma = sigma
self.cutoff = cutoff
def __mul__ (self, metric):
return GaussianValueSet (self, metric)
class GaussianValueSet (OpExprValue, vs.ValueSet):
def __init__ (self, operator, metric):
OpExprValue.__init__ (self, operator, metric)
self.sigma22 = 2 * operator.sigma * operator.sigma
self.cutoff = operator.cutoff
self.metric = metric
def __call__ (self, i, j):
d = self.metric (i, j)
return math.exp (- d * d / self.sigma22) if d < self.cutoff else 0.0
class Block (cs.Operator):
def __init__ (self, M, N):
self.M = M
self.N = N
def __mul__ (self, other):
c = cs.coerceCSet (other)
if isinstance (c, cs.Mask):
return BlockMask (self.M, self.N, c)
else:
return cs.ConnectionSet (BlockCSet (self.M, self.N, c))
class BlockMask (cs.Mask):
def __init__ (self, M, N, mask):
cs.Mask.__init__ (self)
self.M = M
self.N = N
self.m = mask
def iterator (self, low0, high0, low1, high1, state):
maskIter = self.m.iterator (low0 / self.M,
(high0 + self.M - 1) / self.M,
low1 / self.N,
(high1 + self.N - 1) / self.N,
state)
try:
pre = []
(i, j) = maskIter.next ()
while True:
# collect connections in one connection matrix column
post = j
while j == post:
pre.append (i)
(i, j) = maskIter.next ()
# generate blocks for the column
for jj in xrange (max (self.N * post, low1),
min (self.N * (post + 1), high1)):
for k in pre:
for ii in xrange (max (self.M * k, low0),
min (self.M * (k + 1), high0)):
yield (ii, jj)
pre = []
except StopIteration:
if pre:
# generate blocks for the last column
for jj in xrange (max (self.N * post, low1),
min (self.N * (post + 1), high1)):
for k in pre:
for ii in xrange (max (self.M * k, low0),
min (self.M * (k + 1), high0)):
yield (ii, jj)
class Transpose (cs.Operator):
def __mul__ (self, other):
c = cs.coerceCSet (other)
if isinstance (c, cs.Mask):
return other.transpose ()
else:
return cs.ConnectionSet (other.transpose ())
class Shift (cs.Operator):
def __init__ (self, M, N):
self.M = M
self.N = N
def __mul__ (self, other):
c = cs.coerceCSet (other)
if isinstance (c, cs.Mask):
return other.shift (self.M, self.N)
else:
return cs.ConnectionSet (other.shift (self.M, self.N))
class Fix (cs.Operator):
def __mul__ (self, other):
c = cs.coerceCSet (other)
if isinstance (c, cs.Mask):
return FixedMask (other)
else:
return cs.ConnectionSet (FixedCSet (other))
class FixedMask (cs.FiniteMask):
def __init__ (self, mask):
cs.FiniteMask.__init__ (self)
ls = []
for c in mask:
ls.append (c)
self.connections = ls
targets = map (cs.target, ls)
self.low0 = min (ls)[0]
self.high0 = max (ls)[0] + 1
self.low1 = min (targets)
self.high1 = max (targets) + 1
def iterator (self, low0, high0, low1, high1, state):
if not self.isBoundedBy (low0, high0, low1, high1):
return iter (self.connections)
else:
return self.boundedIterator (low0, high0, low1, high1)
def boundedIterator (self, low0, high0, low1, high1):
for c in self.connections:
if low0 <= c[0] and c[0] < high0 \
and low1 <= c[1] and c[1] < high1:
yield c
|