Commit 40ab2808 authored by Gregory Herschlag's avatar Gregory Herschlag
Browse files

updating with idealpop control and linting

parent 390e560c
Loading
Loading
Loading
Loading

data/nc_pop_2020.csv

0 → 100644
+100 −0
Original line number Diff line number Diff line
Alamance,171415
Alexander,36444
Alleghany,10888
Anson,22055
Ashe,26577
Avery,17806
Beaufort,44652
Bertie,17934
Bladen,29606
Brunswick,136693
Buncombe,269452
Burke,87570
Cabarrus,225804
Caldwell,80652
Camden,10355
Carteret,67686
Caswell,22736
Catawba,160610
Chatham,76285
Cherokee,28774
Chowan,13708
Clay,11089
Cleveland,99519
Columbus,50623
Craven,100720
Cumberland,334728
Currituck,28100
Dare,36915
Davidson,168930
Davie,42712
Duplin,48715
Durham,324833
Edgecombe,48900
Forsyth,382590
Franklin,68573
Gaston,227943
Gates,10478
Graham,8030
Granville,60992
Greene,20451
Guilford,541299
Halifax,48622
Harnett,133568
Haywood,62089
Henderson,116281
Hertford,21552
Hoke,52082
Hyde,4589
Iredell,186693
Jackson,43109
Johnston,215999
Jones,9172
Lee,63285
Lenoir,55122
Lincoln,86810
Macon,37014
Madison,21193
Martin,22031
McDowell,44578
Mecklenburg,1115482
Mitchell,14903
Montgomery,25751
Moore,99727
Nash,94970
New Hanover,225702
Northampton,17471
Onslow,204576
Orange,148696
Pamlico,12276
Pasquotank,40568
Pender,60203
Perquimans,13005
Person,39097
Pitt,170243
Polk,19328
Randolph,144171
Richmond,42946
Robeson,116530
Rockingham,91096
Rowan,146875
Rutherford,64444
Sampson,59036
Scotland,34174
Stanly,62504
Stokes,44520
Surry,71359
Swain,14117
Transylvania,32986
Tyrrell,3245
Union,238267
Vance,42578
Wake,1129410
Warren,18642
Washington,11003
Watauga,54086
Wayne,117333
Wilkes,65969
Wilson,78784
Yadkin,37214
Yancey,18470
 No newline at end of file
+45 −42
Original line number Diff line number Diff line
@@ -28,11 +28,11 @@ for i in range(len(colorMap)):
myCmap = matplotlib.colors.ListedColormap(colorMap[:5])
transparentColorMap = matplotlib.colors.ListedColormap([(0, 0, 0, 0)])

################################################################################
###############################################################################


# if popHeader is not the empty string, it will be used; otherwise use popIndex
def readPopDict(keys, popDataPath, popHeader = "", popIndex = 1, 
                popFileDelim = ","):
def readPopDict(keys, popDataPath, popHeader="", popIndex=1, popFileDelim=","):
    with open(popDataPath) as pF:
        lines = pF.readlines()
    header_huh = len(lines) > len(keys)
@@ -57,14 +57,14 @@ def readPopDict(keys, popDataPath, popHeader = "", popIndex = 1,
        popDict[key] = val
    return popDict

################################################################################
###############################################################################


def mergePopulationData(gpdFile, popDataPath, keyField="", popHeader="", 
                        popIndex=1, popName="", popFileDelim=","):
    keyField = gpdExtractor.checkField(gpdFile, keyField)
    keys = gpdFile[keyField].tolist()


    if popName == "":
        if popHeader != "":
            popName = popHeader
@@ -79,14 +79,14 @@ def mergePopulationData(gpdFile, popDataPath, keyField = "", popHeader = "",
                        right_index=True, suffixes=('', '_pop'))
    return mergedSF
    
################################################################################
###############################################################################


def greedyColor(gpdFile, colorField='color'):
    gpdFile[colorField] = -1
    for ii, f in gpdFile.iterrows():
        geom = f.geometry
        intersects = gpdFile[gpdFile.geometry.intersects(geom)]
        intersectingClusts = intersects.clstCnties.tolist()
        nbrColors = intersects.color.tolist()
        nbrColors = [nC for nC in nbrColors if nC != -1]
        color = 0
@@ -94,18 +94,19 @@ def greedyColor(gpdFile, colorField = 'color'):
            color += 1
        gpdFile.at[ii, colorField] = color

################################################################################
###############################################################################


def saveMap(clusterDF, cntyDF, clusterIndex, outDir="output", 
                   prefix = "Clustering", suffix = ".png", figsize = (25, 15),
                   dpi = 200):
            prefix="Clustering", suffix=".png", figsize=(25, 15), dpi=200):
    if not os.path.exists(outDir):
        os.makedirs(outDir)
    outPath = os.path.join(outDir, prefix + str(clusterIndex) + suffix)
    
    greedyColor(clusterDF)

    clusterDF['coords'] = clusterDF['geometry'].apply(lambda x: 
    geometries = clusterDF['geometry']
    clusterDF['coords'] = geometries.apply(lambda x: 
                                           x.representative_point().coords[:])
    clusterDF['coords'] = [coords[0] for coords in clusterDF['coords']]

@@ -114,11 +115,13 @@ def saveMap(clusterDF, cntyDF, clusterIndex, outDir = "output",
    cntyDF.plot(ax=base, cmap=transparentColorMap, linewidth=0.5, 
                edgecolor=(0, 0, 0, 0.5))
    for idx, row in clusterDF.iterrows():
        plt.annotate(text = "(" + str(row['noDstsInClst']) + ")", 
        plt.annotate(#s="(" + str(row['noDstsInClst']) + ")", 
                     text="(" + str(row['noDstsInClst']) + ")", 
                     xy=row['coords'],
                     horizontalalignment='center', 
                     bbox = dict(boxstyle = "round4", fc = "w"))
    plt.axis('off');
                     bbox=dict(boxstyle="round4", fc="w")
                     )
    plt.axis('off')
    plt.savefig(outPath, bbox_inches='tight', dpi=dpi)

################################################################################
 No newline at end of file
###############################################################################
+5 −3
Original line number Diff line number Diff line
import sys
import os


def addSubPaths(path):
    sys.path.append(path)
    subPaths = [sp for sp in os.listdir(path) 
@@ -8,5 +9,6 @@ def addSubPaths(path):
    for sp in subPaths:
        addSubPaths(os.path.join(path, sp))


qgSourcePath = os.path.join("..", "src")
addSubPaths(qgSourcePath)
+4.35 KiB (583 KiB)
Loading image diff...
−574 KiB
Loading image diff...
Loading