Skip to content

Commit 99723d3

Browse files
committed
ac and rag
1 parent d5d3b7b commit 99723d3

File tree

4 files changed

+168
-54
lines changed

4 files changed

+168
-54
lines changed

geospatial_learn/handyplots.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,39 @@ def plt_confmat(trueVals, predVals, cmap = plt.cm.gray, fmt="%d"):
119119
The confusion matrix and a plot
120120
"""
121121
labels = np.unique(trueVals)
122+
# the above heatmap function is used to create the plot
122123

123124
skplt.metrics.plot_confusion_matrix(trueVals, predVals, normalize=True)
124125

125126
conf = confusion_matrix(trueVals, predVals)
126127

128+
129+
# ax = plt.gca()
130+
# # to be used for confusion matrix
131+
#
132+
# ax.invert_yaxis()
133+
# #ax.set_anchor('NW')
134+
# #ax.invert_yaxis()
135+
# # plot the mean cross-validation scores
136+
# img = ax.pcolor(conf, cmap=cmap, vmin=None, vmax=None)
137+
# img.update_scalarmappable()
138+
# ax.set_xlabel('True')
139+
# ax.set_ylabel('Predicted')
140+
# ax.set_xticks(np.arange(len(labels)) + .5)
141+
# ax.set_yticks(np.arange(len(labels)) + .5)
142+
# ax.set_xticklabels(labels)
143+
# ax.set_yticklabels(labels)
144+
#
145+
# ax.set_aspect(1)
146+
#
147+
# for p, color, value in zip(img.get_paths(), img.get_facecolors(), img.get_array()):
148+
# x, y = p.vertices[:-2, :].mean(0)
149+
# if np.mean(color[:3]) > 0.5:
150+
# c = 'k'
151+
# else:
152+
# c = 'w'
153+
# ax.text(x, y, fmt % value, color=c, ha="center", va="center")
154+
127155
return
128156
conf
129157

@@ -218,7 +246,30 @@ def plot3d(data, features, feature_names, point_color = 0):
218246

219247
plt.show()
220248

249+
250+
def plot_S2_cloud(user, paswd, start_date, end_date, aoiJson, print_date=True):
251+
"""
252+
253+
Plot Sentinel 2 cloud percentage for a given time period
254+
255+
Parameters
256+
------------------
257+
258+
data : np array
259+
the aformentioned array of features
260+
261+
features : list
262+
a list of feature indices, eg [1,2,3] or [4,3,1]
263+
264+
feature_names : list of strings
265+
a list of feature names ef ['red', 'green', 'blue']
266+
267+
"""
268+
dF, products = data.sent2_query(user, paswd, aoiJson, start_date, end_date)
269+
270+
dF.sort_values('ingestiondate', inplace=True)
221271

272+
dF.plot.line('ingestiondate', 'cloudcoverpercentage')
222273

223274
def plot_change(inArray):
224275
""" This assumes that features are column wise and rows are samples

geospatial_learn/raster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def tile_rasters(inImage, outputImage, tilesize):
214214
exit_codes = [p.wait() for p in procList]
215215
#print(i)
216216

217-
def batch_translate(folder, wildcard, FMT=None):
217+
def batch_translate(folder, wildcard, FMT='Gtiff'):
218218
"""
219219
Using the gdal python API, this function translates the format of files
220220
to commonly used formats
@@ -243,7 +243,7 @@ def batch_translate(folder, wildcard, FMT=None):
243243
if FMT == 'Gtiff':
244244
fmt = '.tif'
245245

246-
fileList = glob2.glob(path.join(folder,'**', '**','*', wildcard))
246+
fileList = glob2.glob(path.join(folder,'*'+wildcard))
247247
outList = list()
248248
files = np.arange(len(fileList))
249249

geospatial_learn/setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from setuptools import setup
2+
from Cython.Build import cythonize
3+
4+
setup(
5+
name='Hello world app',
6+
ext_modules=cythonize("hello.pyx"),
7+
zip_safe=False,
8+
)

geospatial_learn/utilities.py

Lines changed: 107 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -374,25 +374,36 @@ def ms_toposeg(inRas, outShp, iterations=100, algo='ACWE', band=2, dist=30,
374374

375375
if usemin == True:
376376
minIm = peak_local_max(invert(img), min_distance=dist, indices=False)
377-
378-
379-
380-
ste = selem.square(se)
381-
dilated = binary_dilation(maxIm, selem=ste)
382-
seg, _ = ndi.label(dilated)
383-
cnt = list(np.unique(seg))
384-
385-
cnt.pop(0)
386-
#levelsets = [seg==s for s in cnt]
387-
388-
iters = np.arange(iterations)
389-
390-
orig = seg>0
377+
378+
if algo=='ACWE':
379+
ste = selem.square(se)
380+
dilated = binary_dilation(maxIm, selem=ste)
381+
seg, _ = ndi.label(dilated)
382+
cnt = list(np.unique(seg))
383+
384+
cnt.pop(0)
385+
#levelsets = [seg==s for s in cnt]
386+
387+
iters = np.arange(iterations)
388+
389+
orig = seg>0
391390
#TODO - get fuse burner algo in this
392391

393392
if algo=='GAC':
394393

394+
ste = selem.square(se)
395395
gimg = inverse_gaussian_gradient(img)
396+
maxIm = peak_local_max(gimg, min_distance=dist, indices=False)
397+
dilated = binary_dilation(maxIm, selem=ste)
398+
seg, _ = ndi.label(dilated)
399+
cnt = list(np.unique(seg))
400+
401+
cnt.pop(0)
402+
#levelsets = [seg==s for s in cnt]
403+
404+
iters = np.arange(iterations)
405+
406+
orig = seg>0
396407

397408

398409

@@ -413,6 +424,47 @@ def ms_toposeg(inRas, outShp, iterations=100, algo='ACWE', band=2, dist=30,
413424
orig[bw==1]=1
414425
del inv, sk
415426

427+
if usemin==True:
428+
429+
minIm = peak_local_max(invert(gimg), min_distance=dist, indices=False)
430+
dilated = binary_dilation(minIm, selem=ste)
431+
seg, _ = ndi.label(dilated)
432+
cnt = list(np.unique(seg))
433+
434+
cnt.pop(0)
435+
iters = np.arange(iterations)
436+
orig = seg>0
437+
e2 = mh.bwperim(bw)
438+
edge[e2==1]=1
439+
440+
441+
if init != None:
442+
initBw = orig
443+
orig = mcv(img, iterations=init,init_level_set=initBw,
444+
smoothing=smooth, lambda1=1,
445+
lambda2=1)
446+
orig = orig>0
447+
448+
for i in tqdm(iters):
449+
inv = invert(orig)
450+
sk = skeletonize(inv)
451+
sk = _skelprune(sk)
452+
bw2 = mcv(img, iterations=1,init_level_set=orig, smoothing=smooth, lambda1=1,
453+
lambda2=1)
454+
bw2[sk==1]=0
455+
if useedge == True:
456+
bw2[edge==1]=0
457+
# why do this? I think seg=bw will result in a pointer....
458+
orig = np.zeros_like(bw2, dtype=np.bool)
459+
orig[bw2==1]=1
460+
461+
del inv, sk
462+
463+
bw[bw2==1]=1
464+
newseg, _ = nd.label(bw)
465+
466+
467+
416468
else:
417469
# let it run for a bit to avoid over seg
418470
if init != None:
@@ -438,48 +490,51 @@ def ms_toposeg(inRas, outShp, iterations=100, algo='ACWE', band=2, dist=30,
438490

439491
del inv, sk
440492

441-
if usemin==True:
442-
443-
493+
494+
495+
496+
if usemin==True:
497+
498+
499+
500+
dilated = binary_dilation(minIm, selem=ste)
501+
seg, _ = ndi.label(dilated)
502+
cnt = list(np.unique(seg))
444503

445-
dilated = binary_dilation(minIm, selem=ste)
446-
seg, _ = ndi.label(dilated)
447-
cnt = list(np.unique(seg))
504+
cnt.pop(0)
505+
iters = np.arange(iterations)
506+
orig = seg>0
507+
e2 = mh.bwperim(bw)
508+
edge[e2==1]=1
448509

449-
cnt.pop(0)
450-
iters = np.arange(iterations)
451-
orig = seg>0
452-
e2 = mh.bwperim(bw)
453-
edge[e2==1]=1
454-
455-
456-
if init != None:
457-
initBw = orig
458-
orig = mcv(img, iterations=init,init_level_set=initBw,
459-
smoothing=smooth, lambda1=1,
460-
lambda2=1)
461-
orig = orig>0
462-
463-
for i in tqdm(iters):
464-
inv = invert(orig)
465-
sk = skeletonize(inv)
466-
sk = _skelprune(sk)
467-
bw2 = mcv(img, iterations=1,init_level_set=orig, smoothing=smooth, lambda1=1,
510+
511+
if init != None:
512+
initBw = orig
513+
orig = mcv(img, iterations=init,init_level_set=initBw,
514+
smoothing=smooth, lambda1=1,
468515
lambda2=1)
469-
bw2[sk==1]=0
470-
if useedge == True:
471-
bw2[edge==1]=0
472-
# why do this? I think seg=bw will result in a pointer....
473-
orig = np.zeros_like(bw2, dtype=np.bool)
474-
orig[bw2==1]=1
475-
476-
del inv, sk
477-
# remove_small_objects(bw, min_size=3, in_place=True)
478-
bw[bw2==1]=1
479-
newseg, _ = nd.label(bw)
516+
orig = orig>0
480517

481-
else:
482-
newseg, _ = nd.label(bw)
518+
for i in tqdm(iters):
519+
inv = invert(orig)
520+
sk = skeletonize(inv)
521+
sk = _skelprune(sk)
522+
bw2 = mcv(img, iterations=1,init_level_set=orig, smoothing=smooth, lambda1=1,
523+
lambda2=1)
524+
bw2[sk==1]=0
525+
if useedge == True:
526+
bw2[edge==1]=0
527+
# why do this? I think seg=bw will result in a pointer....
528+
orig = np.zeros_like(bw2, dtype=np.bool)
529+
orig[bw2==1]=1
530+
531+
del inv, sk
532+
# remove_small_objects(bw, min_size=3, in_place=True)
533+
bw[bw2==1]=1
534+
newseg, _ = nd.label(bw)
535+
536+
else:
537+
newseg, _ = nd.label(bw)
483538

484539
if close==True:
485540
ste2 = selem.square(3)

0 commit comments

Comments
 (0)