Skip to content

Commit 55d005d

Browse files
committed
implement new polymask routine for all frame & polygon mask uses
1 parent 6b5099b commit 55d005d

File tree

2 files changed

+45
-79
lines changed

2 files changed

+45
-79
lines changed

GSASII/GSASIIimage.py

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -788,46 +788,46 @@ def EdgeFinder(image,data):
788788
tay = ma.compressed(ma.array(tay.flatten(),mask=tam))
789789
return zip(tax,tay)
790790

791-
def MakeFrameMask(data,frame):
792-
'''Assemble a Frame mask for a image, according to the input supplied.
793-
Note that this requires use of the Fortran polymask routine that is limited
794-
to 1024x1024 arrays, so this computation is done in blocks (fixed at 512)
795-
and the master image is assembled from that.
791+
# def MakeFrameMask(data,frame): #obsolete
792+
# '''Assemble a Frame mask for a image, according to the input supplied.
793+
# Note that this requires use of the Fortran polymask routine that is limited
794+
# to 1024x1024 arrays, so this computation is done in blocks (fixed at 512)
795+
# and the master image is assembled from that.
796796

797-
:param dict data: Controls for an image. Used to find the image size
798-
and the pixel dimensions.
799-
:param list frame: Frame parameters, typically taken from ``Masks['Frames']``.
800-
:returns: a mask array with dimensions matching the image Controls.
801-
'''
802-
import polymask as pm
803-
pixelSize = data['pixelSize']
804-
scalex = pixelSize[0]/1000.
805-
scaley = pixelSize[1]/1000.
806-
blkSize = 512
807-
Nx,Ny = data['size']
808-
nXBlks = (Nx-1)//blkSize+1
809-
nYBlks = (Ny-1)//blkSize+1
810-
tam = ma.make_mask_none(data['size'])
811-
for iBlk in range(nXBlks):
812-
iBeg = iBlk*blkSize
813-
iFin = min(iBeg+blkSize,Nx)
814-
for jBlk in range(nYBlks):
815-
jBeg = jBlk*blkSize
816-
jFin = min(jBeg+blkSize,Ny)
817-
nI = iFin-iBeg
818-
nJ = jFin-jBeg
819-
tax,tay = np.mgrid[iBeg+0.5:iFin+.5,jBeg+.5:jFin+.5] #bin centers not corners
820-
tax = np.asfarray(tax*scalex,dtype=np.float32)
821-
tay = np.asfarray(tay*scaley,dtype=np.float32)
822-
tamp = ma.make_mask_none((1024*1024))
823-
tamp = ma.make_mask(pm.polymask(nI*nJ,tax.flatten(),
824-
tay.flatten(),len(frame),frame,tamp)[:nI*nJ])^True #switch to exclude around frame
825-
if tamp.shape:
826-
tamp = np.reshape(tamp[:nI*nJ],(nI,nJ))
827-
tam[iBeg:iFin,jBeg:jFin] = ma.mask_or(tamp[0:nI,0:nJ],tam[iBeg:iFin,jBeg:jFin])
828-
else:
829-
tam[iBeg:iFin,jBeg:jFin] = True
830-
return tam.T
797+
# :param dict data: Controls for an image. Used to find the image size
798+
# and the pixel dimensions.
799+
# :param list frame: Frame parameters, typically taken from ``Masks['Frames']``.
800+
# :returns: a mask array with dimensions matching the image Controls.
801+
# '''
802+
# import polymask as pm
803+
# pixelSize = data['pixelSize']
804+
# scalex = pixelSize[0]/1000.
805+
# scaley = pixelSize[1]/1000.
806+
# blkSize = 512
807+
# Nx,Ny = data['size']
808+
# nXBlks = (Nx-1)//blkSize+1
809+
# nYBlks = (Ny-1)//blkSize+1
810+
# tam = ma.make_mask_none(data['size'])
811+
# for iBlk in range(nXBlks):
812+
# iBeg = iBlk*blkSize
813+
# iFin = min(iBeg+blkSize,Nx)
814+
# for jBlk in range(nYBlks):
815+
# jBeg = jBlk*blkSize
816+
# jFin = min(jBeg+blkSize,Ny)
817+
# nI = iFin-iBeg
818+
# nJ = jFin-jBeg
819+
# tax,tay = np.mgrid[iBeg+0.5:iFin+.5,jBeg+.5:jFin+.5] #bin centers not corners
820+
# tax = np.asfarray(tax*scalex,dtype=np.float32)
821+
# tay = np.asfarray(tay*scaley,dtype=np.float32)
822+
# tamp = ma.make_mask_none((1024*1024))
823+
# tamp = ma.make_mask(pm.polymask(nI*nJ,tax.flatten(),
824+
# tay.flatten(),len(frame),frame,tamp)[:nI*nJ])^True #switch to exclude around frame
825+
# if tamp.shape:
826+
# tamp = np.reshape(tamp[:nI*nJ],(nI,nJ))
827+
# tam[iBeg:iFin,jBeg:jFin] = ma.mask_or(tamp[0:nI,0:nJ],tam[iBeg:iFin,jBeg:jFin])
828+
# else:
829+
# tam[iBeg:iFin,jBeg:jFin] = True
830+
# return tam.T
831831

832832
def CalcRings(G2frame,ImageZ,data,masks):
833833
pixelSize = data['pixelSize']
@@ -864,7 +864,7 @@ def CalcRings(G2frame,ImageZ,data,masks):
864864
frame = masks['Frames']
865865
tam = ma.make_mask_none(ImageZ.shape)
866866
if frame:
867-
tam = ma.mask_or(tam,MakeFrameMask(data,frame))
867+
tam = ma.mask_or(tam,ma.make_mask(np.abs(polymask(data,frame)-255)))
868868
for iH,H in enumerate(HKL):
869869
if debug: print (H)
870870
dsp = H[3]
@@ -942,7 +942,7 @@ def ImageRecalibrate(G2frame,ImageZ,data,masks,getRingsOnly=False):
942942
frame = masks['Frames']
943943
tam = ma.make_mask_none(ImageZ.shape)
944944
if frame:
945-
tam = ma.mask_or(tam,MakeFrameMask(data,frame))
945+
tam = ma.mask_or(tam,ma.make_mask(np.abs(polymask(data,frame)-255)))
946946
for iH,H in enumerate(HKL):
947947
if debug: print (H)
948948
dsp = H[3]
@@ -1917,40 +1917,6 @@ def calc2Peak(values,nxy,pixSize,img):
19171917
else:
19181918
return None
19191919

1920-
# Original version
1921-
# def AutoPixelMask(Image,Masks,Controls,numChans,dlg=None):
1922-
# '''Find "bad" regions on an image and remove them with a pixel mask.
1923-
# This works by masking pixels that are well outside the range of the
1924-
# radial average.
1925-
# Original version from RBVD, takes 1-5 min per image. No longer in use.
1926-
# '''
1927-
# #if GSASIIpath.GetConfigValue('debug'): print('original AutoPixelMask starting')
1928-
# frame = Masks['Frames']
1929-
# tam = ma.make_mask_none(Image.shape)
1930-
# if frame:
1931-
# tam = ma.mask_or(tam,MakeFrameMask(Controls,frame))
1932-
# LUtth = np.array(Controls['IOtth'])
1933-
# dtth = (LUtth[1]-LUtth[0])/numChans
1934-
# esdMul = Masks['SpotMask']['esdMul']
1935-
# prob = 100.*sc.erf(esdMul/np.sqrt(2.))
1936-
# print(' Spots greater than %.2f of band intensity are masked'%prob)
1937-
# mask = ma.make_mask_none(Image.shape)
1938-
# band = ma.array(Image,mask=ma.nomask)
1939-
# TA = Make2ThetaAzimuthMap(Controls,(0,Image.shape[0]),(0,Image.shape[1]))[0] #2-theta array
1940-
# TThs = np.linspace(LUtth[0],LUtth[1],numChans,False)
1941-
# for it,TTh in enumerate(TThs):
1942-
# band.mask = ma.masked_outside(TA,TTh,TTh+dtth).mask+tam
1943-
# pcmax = np.percentile(band.compressed(),[prob,50.])
1944-
# mband = ma.masked_greater(band,pcmax[0])
1945-
# std = ma.std(mband)
1946-
# anom = ma.masked_greater((band-pcmax[1])/std,esdMul)
1947-
# mask ^= (anom.mask^band.mask)
1948-
# if not dlg is None:
1949-
# GoOn = dlg.Update(it,newmsg='Processed 2-theta rings = %d'%(it))
1950-
# if not GoOn[0]:
1951-
# break
1952-
# return mask
1953-
19541920
def TestFastPixelMask():
19551921
'''Test if the fast (C) version of Auto Pixel Masking is available.
19561922
@@ -1994,7 +1960,7 @@ def FastAutoPixelMask(Image, Masks, Controls, numChans, dlg=None):
19941960
frame = Masks['Frames']
19951961
tam = ma.make_mask_none(Image.shape)
19961962
if frame:
1997-
tam = ma.mask_or(tam,MakeFrameMask(Controls,frame))
1963+
tam = ma.mask_or(tam,ma.make_mask(np.abs(polymask(Controls,frame)-255)))
19981964
ttmin = float(Masks['SpotMask'].get('SearchMin',0.0))
19991965
ttmax = float(Masks['SpotMask'].get('SearchMax',180.0))
20001966
esdMul = float(Masks['SpotMask']['esdMul'])
@@ -2061,7 +2027,7 @@ def MAD(args,**kwargs):
20612027
frame = Masks['Frames']
20622028
tam = ma.make_mask_none(Image.shape)
20632029
if frame:
2064-
tam = ma.mask_or(tam,MakeFrameMask(Controls,frame))
2030+
tam = ma.mask_or(tam,ma.make_mask(np.abs(polymask(Controls,frame)-255)))
20652031
LUtth = np.array(Controls['IOtth'])
20662032
dtth = (LUtth[1]-LUtth[0])/numChans
20672033
esdMul = Masks['SpotMask']['esdMul']

GSASII/GSASIIscriptable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6382,7 +6382,7 @@ def MaskFrameMask(self):
63826382
ImageZ = _getCorrImage(Readers['Image'],self.proj,self)
63836383
tam = ma.make_mask_none(ImageZ.shape)
63846384
if frame:
6385-
tam = ma.mask_or(tam,G2img.MakeFrameMask(Controls,frame))
6385+
tam = ma.mask_or(tam,ma.make_mask(np.abs(G2img.polymask(Controls,frame)-255)))
63866386
return tam
63876387

63886388
def getVary(self,*args):
@@ -6696,7 +6696,7 @@ def GeneratePixelMask(self,esdMul=3.0,ttmin=0.,ttmax=180.,
66966696
frame = Masks['Frames']
66976697
tam = ma.make_mask_none(Image.shape)
66986698
if frame:
6699-
tam = ma.mask_or(tam,G2img.MakeFrameMask(Controls,frame))
6699+
tam = ma.mask_or(tam,ma.make_mask(np.abs(G2img.polymask(Controls,frame)-255)))
67006700
else:
67016701
tam = FrameMask
67026702
if ThetaMap is None:

0 commit comments

Comments
 (0)