Skip to content

Commit

Permalink
Merge pull request #350 from pysal/revert-349-format_change
Browse files Browse the repository at this point in the history
Revert "order analysis.py classes/functions alphabetically"
  • Loading branch information
jGaboardi committed Dec 29, 2019
2 parents 97c698c + 7eafc98 commit 3421d64
Showing 1 changed file with 104 additions and 105 deletions.
209 changes: 104 additions & 105 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.PointPattern
pointpattern : spaghetti.network.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 function is computed.
The lower bound at which the G-function is computed.
Default 0.
upperbound : float
The upper bound at which the function is computed.
The upper bound at which the G-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 %s" % valid_distributions
), "Distribution not in {}".format(valid_distributions)

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


class NetworkF(NetworkBase):
"""Compute a network constrained F 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
----------
fsim : spaghetti.SimulatedPointPattern
simulated point pattern of ``self.nptsv`` points
"""

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 = np.nanmin(self.ntw.allneighbordistances(self.pointpattern), axis=1)
self.setbounds(nearest)

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

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

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

# compute an F-function
simx, simy = ffunction(
nearest, self.lowerbound, self.upperbound, self.npts, 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 NetworkG(NetworkBase):
"""Compute a network constrained G 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
----------
lam : float
``lambda`` value
Notes
-----
Based on :cite:`Okabe2001`.
"""

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

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

# compute a G-Function
observedx, observedy = gfunction(
nearest, self.lowerbound, self.upperbound, nsteps=self.nsteps
# 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 @@ -221,49 +219,51 @@ def computepermutations(self):
)

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

# compute a G-Function
simx, simy = gfunction(
nearest, self.lowerbound, self.upperbound, 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


class NetworkK(NetworkBase):
"""Compute a network constrained K 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
----------
lam : float
``lambda`` value
Notes
-----
Based on :cite:`Okabe2001`.
fsim : spaghetti.network.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 = self.ntw.allneighbordistances(self.pointpattern)
self.setbounds(nearest)
# create an initial simulated point pattern
self.fsim = self.ntw.simulate_observations(self.npts)

# set the intensity (lambda)
self.lam = self.npts / sum(self.ntw.arc_lengths.values())
# 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 K-Function
observedx, observedy = kfunction(
nearest, self.upperbound, self.lam, 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 @@ -283,19 +283,19 @@ def computepermutations(self):
)

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

# compute a K-Function
simx, simy = kfunction(
nearest, self.upperbound, self.lam, 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


def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
"""Compute an F-Function
def gfunction(nearest, lowerbound, upperbound, nsteps=10):
"""Compute a G-Function
Parameters
----------
Expand All @@ -309,9 +309,6 @@ def ffunction(nearest, lowerbound, upperbound, npts, 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 @@ -345,34 +342,34 @@ def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
# slice out and count neighbors within radius
cnt = len(nearest[nearest <= r])

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

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

return x, y


def gfunction(nearest, lowerbound, upperbound, nsteps=10):
"""Compute a G-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.
intensity : float
lambda value
nsteps : int
The number of distance bands. Default is 10. Must be
non-negative.
Expand All @@ -392,10 +389,7 @@ def gfunction(nearest, lowerbound, upperbound, 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 @@ -404,35 +398,31 @@ def gfunction(nearest, lowerbound, upperbound, 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 `g`
if cnt > 0:
g = cnt / float(nobs)
# otherwise set `g` to zero
else:
g = 0
y[i] = len(nearest[nearest <= r])

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

return x, y


def kfunction(nearest, upperbound, intensity, nsteps=10):
"""Compute a K-Function
def ffunction(nearest, lowerbound, upperbound, npts, nsteps=10):
"""Compute an F-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
npts : int
pointpattern.npoints
nsteps : int
The number of distance bands. Default is 10. Must be
Expand All @@ -453,7 +443,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 @@ -462,10 +455,16 @@ 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 `f`
if cnt > 0:
f = cnt / float(npts)
# otherwise set `f` to zero
else:
f = 0

return x, y
# label `f` on the y-axis
y[i] = f

return x, y

0 comments on commit 3421d64

Please sign in to comment.