Skip to content

Commit

Permalink
Merge pull request #349 from jGaboardi/format_change
Browse files Browse the repository at this point in the history
order analysis.py classes/functions alphabetically
  • Loading branch information
jGaboardi committed Dec 29, 2019
2 parents 2b566be + f7c0f38 commit 97c698c
Showing 1 changed file with 105 additions and 104 deletions.
209 changes: 105 additions & 104 deletions spaghetti/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class NetworkBase(object):
----------
ntw : spaghetti.Network
spaghetti Network object.
spaghetti network object.
pointpattern : spaghetti.network.PointPattern
pointpattern : spaghetti.PointPattern
A spaghetti point pattern object.
nsteps : int
Expand All @@ -30,11 +30,11 @@ class NetworkBase(object):
Either ``"uniform"`` or ``"poisson"``.
lowerbound : float
The lower bound at which the G-function is computed.
The lower bound at which the function is computed.
Default 0.
upperbound : float
The upper bound at which the G-function is computed.
The upper bound at which the function is computed.
Defaults to the maximum observed nearest neighbor distance.
Attributes
Expand Down Expand Up @@ -99,7 +99,7 @@ def validatedistribution(self):
valid_distributions = ["uniform", "poisson"]
assert (
self.distribution in valid_distributions
), "Distribution not in {}".format(valid_distributions)
), "Distribution not in %s" % valid_distributions

def computeenvelope(self):
"""compute upper and lower bounds of envelope
Expand All @@ -121,23 +121,40 @@ def setbounds(self, nearest):
self.upperbound = np.nanmax(nearest)


class NetworkG(NetworkBase):
"""Compute a network constrained G statistic. This requires the
class NetworkF(NetworkBase):
"""Compute a network constrained F statistic. This requires the
capability to compute a distance matrix between two point patterns.
In this case one will be observed and one will be simulated.
Attributes
----------
fsim : spaghetti.SimulatedPointPattern
simulated point pattern of ``self.nptsv`` points
"""

def computeobserved(self):
"""compute the observed nearest
"""compute the observed nearest and simulated nearest
"""

# find nearest point that is not NaN
nearest = np.nanmin(self.ntw.allneighbordistances(self.pointpattern), axis=1)
# create an initial simulated point pattern
self.fsim = self.ntw.simulate_observations(self.npts)

# find nearest neighbor distances from
# the simulated to the observed
nearest = np.nanmin(
self.ntw.allneighbordistances(self.fsim, self.pointpattern), axis=1
)
self.setbounds(nearest)

# compute a G-Function
observedx, observedy = gfunction(
nearest, self.lowerbound, self.upperbound, nsteps=self.nsteps
# compute an F-function
observedx, observedy = ffunction(
nearest,
self.lowerbound,
self.upperbound,
nsteps=self.nsteps,
npts=self.npts,
)

# set observed values
Expand All @@ -157,49 +174,34 @@ def computepermutations(self):
)

# find nearest observation
nearest = np.nanmin(self.ntw.allneighbordistances(sim), axis=1)
nearest = np.nanmin(self.ntw.allneighbordistances(sim, self.fsim), axis=1)

# compute a G-Function
simx, simy = gfunction(
nearest, self.lowerbound, self.upperbound, nsteps=self.nsteps
# compute an F-function
simx, simy = ffunction(
nearest, self.lowerbound, self.upperbound, self.npts, nsteps=self.nsteps
)

# label the permutation
self.sim[p] = simy


class NetworkK(NetworkBase):
"""Compute a network constrained K statistic. This requires the
class NetworkG(NetworkBase):
"""Compute a network constrained G statistic. This requires the
capability to compute a distance matrix between two point patterns.
In this case one will be observed and one will be simulated.
Attributes
----------
lam : float
``lambda`` value
Notes
-----
Based on :cite:`Okabe2001`.
"""

def computeobserved(self):
"""compute the observed nearest
"""

# find nearest point that is not NaN
nearest = self.ntw.allneighbordistances(self.pointpattern)
nearest = np.nanmin(self.ntw.allneighbordistances(self.pointpattern), axis=1)
self.setbounds(nearest)

# set the intensity (lambda)
self.lam = self.npts / sum(self.ntw.arc_lengths.values())

# compute a K-Function
observedx, observedy = kfunction(
nearest, self.upperbound, self.lam, nsteps=self.nsteps
# compute a G-Function
observedx, observedy = gfunction(
nearest, self.lowerbound, self.upperbound, nsteps=self.nsteps
)

# set observed values
Expand All @@ -219,51 +221,49 @@ def computepermutations(self):
)

# find nearest observation
nearest = self.ntw.allneighbordistances(sim)
nearest = np.nanmin(self.ntw.allneighbordistances(sim), axis=1)

# compute a K-Function
simx, simy = kfunction(
nearest, self.upperbound, self.lam, nsteps=self.nsteps
# compute a G-Function
simx, simy = gfunction(
nearest, self.lowerbound, self.upperbound, nsteps=self.nsteps
)

# label the permutation
self.sim[p] = simy


class NetworkF(NetworkBase):
"""Compute a network constrained F statistic. This requires the
class NetworkK(NetworkBase):
"""Compute a network constrained K statistic. This requires the
capability to compute a distance matrix between two point patterns.
In this case one will be observed and one will be simulated.
Attributes
----------
fsim : spaghetti.network.SimulatedPointPattern
simulated point pattern of ``self.nptsv points
lam : float
``lambda`` value
Notes
-----
Based on :cite:`Okabe2001`.
"""

def computeobserved(self):
"""compute the observed nearest and simulated nearest
"""compute the observed nearest
"""

# create an initial simulated point pattern
self.fsim = self.ntw.simulate_observations(self.npts)

# find nearest neighbor distances from
# the simulated to the observed
nearest = np.nanmin(
self.ntw.allneighbordistances(self.fsim, self.pointpattern), axis=1
)
# find nearest point that is not NaN
nearest = self.ntw.allneighbordistances(self.pointpattern)
self.setbounds(nearest)

# compute an F-function
observedx, observedy = ffunction(
nearest,
self.lowerbound,
self.upperbound,
nsteps=self.nsteps,
npts=self.npts,
# set the intensity (lambda)
self.lam = self.npts / sum(self.ntw.arc_lengths.values())

# compute a K-Function
observedx, observedy = kfunction(
nearest, self.upperbound, self.lam, nsteps=self.nsteps
)

# set observed values
Expand All @@ -283,19 +283,19 @@ def computepermutations(self):
)

# find nearest observation
nearest = np.nanmin(self.ntw.allneighbordistances(sim, self.fsim), axis=1)
nearest = self.ntw.allneighbordistances(sim)

# compute an F-function
simx, simy = ffunction(
nearest, self.lowerbound, self.upperbound, self.npts, nsteps=self.nsteps
# compute a K-Function
simx, simy = kfunction(
nearest, self.upperbound, self.lam, nsteps=self.nsteps
)

# label the permutation
self.sim[p] = simy


def gfunction(nearest, lowerbound, upperbound, nsteps=10):
"""Compute a G-Function
def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
"""Compute an F-Function
Parameters
----------
Expand All @@ -309,6 +309,9 @@ def gfunction(nearest, lowerbound, upperbound, nsteps=10):
upperbound : int or float
The end value of the sequence.
npts : int
pointpattern.npoints
nsteps : int
The number of distance bands. Default is 10. Must be
non-negative.
Expand Down Expand Up @@ -342,34 +345,34 @@ def gfunction(nearest, lowerbound, upperbound, nsteps=10):
# slice out and count neighbors within radius
cnt = len(nearest[nearest <= r])

# if there is one or more neighbors compute `g`
# if there is one or more neighbors compute `f`
if cnt > 0:
g = cnt / float(nobs)
# otherwise set `g` to zero
f = cnt / float(npts)
# otherwise set `f` to zero
else:
g = 0
f = 0

# label `g` on the y-axis
y[i] = g
# label `f` on the y-axis
y[i] = f

return x, y


def kfunction(nearest, upperbound, intensity, nsteps=10):
"""Compute a K-Function
def gfunction(nearest, lowerbound, upperbound, nsteps=10):
"""Compute a G-Function
Parameters
----------
nearest : numpy.ndarray
A vector of nearest neighbor distances.
lowerbound : int or float
The starting value of the sequence.
upperbound : int or float
The end value of the sequence.
intensity : float
lambda value
nsteps : int
The number of distance bands. Default is 10. Must be
non-negative.
Expand All @@ -389,7 +392,10 @@ def kfunction(nearest, upperbound, intensity, nsteps=10):
nobs = len(nearest)

# create interval for x-axis
x = np.linspace(0, upperbound, nsteps)
x = np.linspace(lowerbound, upperbound, nsteps)

# sort nearest neighbor distances
nearest = np.sort(nearest)

# create empty y-axis vector
y = np.empty(len(x))
Expand All @@ -398,31 +404,35 @@ def kfunction(nearest, upperbound, intensity, nsteps=10):
for i, r in enumerate(x):

# slice out and count neighbors within radius
y[i] = len(nearest[nearest <= r])
cnt = len(nearest[nearest <= r])

# compute k for y-axis vector
y *= intensity ** -1
# if there is one or more neighbors compute `g`
if cnt > 0:
g = cnt / float(nobs)
# otherwise set `g` to zero
else:
g = 0

# label `g` on the y-axis
y[i] = g

return x, y


def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
"""Compute an F-Function
def kfunction(nearest, upperbound, intensity, nsteps=10):
"""Compute a K-Function
Parameters
----------
nearest : numpy.ndarray
A vector of nearest neighbor distances.
lowerbound : int or float
The starting value of the sequence.
upperbound : int or float
The end value of the sequence.
npts : int
pointpattern.npoints
intensity : float
lambda value
nsteps : int
The number of distance bands. Default is 10. Must be
Expand All @@ -443,10 +453,7 @@ def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
nobs = len(nearest)

# create interval for x-axis
x = np.linspace(lowerbound, upperbound, nsteps)

# sort nearest neighbor distances
nearest = np.sort(nearest)
x = np.linspace(0, upperbound, nsteps)

# create empty y-axis vector
y = np.empty(len(x))
Expand All @@ -455,16 +462,10 @@ def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
for i, r in enumerate(x):

# slice out and count neighbors within radius
cnt = len(nearest[nearest <= r])

# if there is one or more neighbors compute `f`
if cnt > 0:
f = cnt / float(npts)
# otherwise set `f` to zero
else:
f = 0
y[i] = len(nearest[nearest <= r])

# label `f` on the y-axis
y[i] = f
# compute k for y-axis vector
y *= intensity ** -1

return x, y

0 comments on commit 97c698c

Please sign in to comment.