def __init__(self, ssdo, dependentVar, independentVars, weightsFile, outputReportFile=None, outputTable=None, maxIndVars=5, minIndVars=1, minR2=.5, maxCoef=.01, maxVIF=5.0, minJB=.1, minMI=.1): ARCPY.env.overwriteOutput = True #### Set Initial Attributes #### UTILS.assignClassAttr(self, locals()) self.masterField = self.ssdo.masterField self.warnedTProb = False #### Set Boolean For Passing All Moran's I #### self.allMIPass = UTILS.compareFloat(0.0, self.minMI, rTol=.00000001) #### Assess Whether SWM File Being Used #### if weightsFile: weightSuffix = weightsFile.split(".")[-1].lower() if weightSuffix == "swm": self.weightsType = "SWM" self.weightsMatrix = self.weightsFile else: self.weightsType = "GWT" self.weightsMatrix = WU.buildTextWeightDict( weightsFile, self.ssdo.master2Order) else: #### If No Weightsfile Provided, Use 8 Nearest Neighbors #### if ssdo.numObs <= 9: nn = ssdo.numObs - 2 ARCPY.AddIDMessage("WARNING", 1500, 8, nn) else: nn = 8 self.weightsType = "GA" gaSearch = GAPY.ga_nsearch(self.ssdo.gaTable) gaSearch.init_nearest(0.0, nn, "euclidean") self.weightsMatrix = gaSearch #### Initialize Data #### self.runModels()
def __init__(self, ssdo, dependentVar, independentVars, weightsFile, outputReportFile = None, outputTable = None, maxIndVars = 5, minIndVars = 1, minR2 = .5, maxCoef = .01, maxVIF = 5.0, minJB = .1, minMI = .1): ARCPY.env.overwriteOutput = True #### Set Initial Attributes #### UTILS.assignClassAttr(self, locals()) self.masterField = self.ssdo.masterField self.warnedTProb = False #### Set Boolean For Passing All Moran's I #### self.allMIPass = UTILS.compareFloat(0.0, self.minMI, rTol = .00000001) #### Assess Whether SWM File Being Used #### if weightsFile: weightSuffix = weightsFile.split(".")[-1].lower() if weightSuffix == "swm": self.weightsType = "SWM" self.weightsMatrix = self.weightsFile else: self.weightsType = "GWT" self.weightsMatrix = WU.buildTextWeightDict(weightsFile, self.ssdo.master2Order) else: #### If No Weightsfile Provided, Use 8 Nearest Neighbors #### if ssdo.numObs <= 9: nn = ssdo.numObs - 2 ARCPY.AddIDMessage("WARNING", 1500, 8, nn) else: nn = 8 self.weightsType = "GA" gaSearch = GAPY.ga_nsearch(self.ssdo.gaTable) gaSearch.init_nearest(0.0, nn, "euclidean") self.weightsMatrix = gaSearch #### Initialize Data #### self.runModels()
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile potentialField = self.potentialField #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("ERROR", 906) raise SystemExit() #### Create Summed Variables #### self.intRange = NUM.arange(numObs) self.floatN = self.numObs * 1.0 ySum = self.y.sum() ySum2 = (self.y**2.0).sum() self.yBar = ySum / self.floatN self.S = NUM.sqrt((ySum2 / self.floatN) - self.yBar**2.0) self.nm1 = self.floatN - 1.0 #### Create Base Data Structures/Variables #### self.gi = NUM.zeros(numObs) self.pVals = NUM.ones(numObs) if self.permutations: self.pseudoPVals = NUM.ones(numObs) #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard self.swm = swm #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("ERROR", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType=contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, self.numNeighs, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type=wType, exponent=exponent, row_standard=False, include_self=True) #### Create Progressor #### msg = ARCPY.GetIDMessage(84007) if self.permutations: msg += ": Using Permutations = %i" % self.permutations ARCPY.SetProgressor("step", msg, 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.y, rowStandard=rowStandard, potVals=self.potVals) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.y, potVals=self.potVals, allowSelf=True) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.y, rowStandard=False, potVals=self.potVals) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF_Potent( neighWeights, i, self.y, self.potVals) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiVal, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo( masterID, nhIDs, nhVals, weights) if nn: #### Calculate Local G #### self.calculateGI(orderID, yiVal, nhVals, weights) ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors(failAllNoNeighs=False) self.setNullValues(ni.idsNoNeighs) #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni #### Set p-values for Gi Bins #### if self.permutations: #### Use Pseudo p-values #### pv = self.pseudoPVals else: #### Use Traditional p-values #### pv = self.pVals toolMSG = ARCPY.GetIDMessage(84466) if self.applyFDR: #### Set Bins Using FDR #### msg = ARCPY.GetIDMessage(84472).format(toolMSG) ARCPY.SetProgressor("default", msg) self.giBins = STATS.fdrTransform(pv, self.gi) else: msg = ARCPY.GetIDMessage(84473).format(toolMSG) ARCPY.SetProgressor("default", msg) self.giBins = STATS.pValueBins(pv, self.gi)
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Deviation Variables #### self.yBar = NUM.mean(self.y) self.yDev = self.y - self.yBar self.nm1 = numObs - 1. self.nm2 = numObs - 2. self.nm12 = self.nm1 * self.nm2 yDev2 = self.yDev**2.0 yDev2Norm = yDev2 / self.nm1 self.yDev2NormSum = sum(yDev2Norm) yDev4 = self.yDev**4.0 yDev4Norm = yDev4 / self.nm1 yDev4NormSum = sum(yDev4Norm) self.b2i = yDev4NormSum / (self.yDev2NormSum**2.0) #### Create Base Data Structures/Variables #### self.li = NUM.zeros(numObs) self.ei = NUM.zeros(numObs) self.vi = NUM.zeros(numObs) self.zi = NUM.zeros(numObs) self.pVals = NUM.ones(numObs) if self.permutations: self.pseudoPVals = NUM.ones(numObs) self.moranInfo = {} #### Keep Track of Features with No Neighbors #### self.idsNoNeighs = [] #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard self.swm = swm #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### msg = ARCPY.GetIDMessage(84007) if self.permutations: msg += ": Using Permutations = %i" % self.permutations ARCPY.SetProgressor("step", msg , 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.yDev, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.yDev) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.yDev, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.yDev) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiDev, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Calculate Local I #### self.calculateLI(orderID, yiDev, nhVals, weights) ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() self.setNullValues(ni.idsNoNeighs) #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni #### Set p-values for Gi Bins #### if self.permutations: #### Use Pseudo p-values #### pv = self.pseudoPVals else: #### Use Traditional p-values #### pv = self.pVals #### Calculate FDR and Moran Bins #### toolMSG = ARCPY.GetIDMessage(84474) if self.applyFDR: #### Set Bins Using FDR #### msg = ARCPY.GetIDMessage(84472).format(toolMSG) ARCPY.SetProgressor("default", msg) fdrBins = STATS.fdrTransform(pv, self.li) self.moranBins = STATS.moranBinFromPVals(pv, self.moranInfo, fdrBins = fdrBins) else: msg = ARCPY.GetIDMessage(84473).format(toolMSG) ARCPY.SetProgressor("default", msg) self.moranBins = STATS.moranBinFromPVals(pv, self.moranInfo)
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Deviation Variables #### self.yBar = NUM.mean(self.y) self.yDev = self.y - self.yBar #### Create Base Data Structures/Variables #### self.numer = 0.0 self.denom = NUM.sum(self.yDev**2.0) self.rowSum = NUM.zeros(numObs) self.colSum = NUM.zeros(numObs) self.s0 = 0 self.s1 = 0 self.wij = {} #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84007), 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.yDev, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.yDev) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.yDev, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.yDev) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiDev, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Process Feature Contribution to Moran's I #### self.processRow(orderID, yiDev, nhIDs, nhVals, weights) #### Reset Progessor #### ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Check That All Input Values are Positive #### if NUM.sum(self.y < 0.0) != 0: ARCPY.AddIDMessage("Error", 915) raise SystemExit() #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Base Data Structures/Variables #### self.numer = 0.0 self.denom = 0.0 self.rowSum = NUM.zeros(numObs) self.colSum = NUM.zeros(numObs) self.ySum = 0.0 self.y2Sum = 0.0 self.y3Sum = 0.0 self.y4Sum = 0.0 self.s0 = 0 self.s1 = 0 self.wij = {} #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.iterkeys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = xrange(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84007), 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.y, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.y) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.y, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.y) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiVal, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Process Feature Contribution to General G #### self.processRow(orderID, yiVal, nhIDs, nhVals, weights) #### Reset Progessor #### ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni