Skip to content

Commit

Permalink
minor removals of comments and old code
Browse files Browse the repository at this point in the history
  • Loading branch information
demisjohn committed Mar 27, 2019
1 parent 21b7dac commit bd35fe4
Showing 1 changed file with 8 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Demis D. John, Univ. of California Santa Barbara, 2019-03-27
'''

mc_DEBUG = False # Send debugging info to the log file?
mc_DEBUG = False # Print debugging info to the log file?

## Import some modules:
from ij import IJ, ImagePlus, WindowManager
Expand Down Expand Up @@ -52,12 +52,7 @@ def run():


imp = IJ.getImage() # get the current Image as ImagePlus object
#ImagePath = imp.getOriginalFileInfo().directory + os.path.sep + imp.getOriginalFileInfo().fileName
#if mc_DEBUG:
# print( "imp=", imp )
# print( "ImagePath=", ImagePath )




CalIdx, SetGlobalScale, AddScaleBar = uScopeCalDialog(cal) # show Calibrations dialog

Expand All @@ -73,7 +68,7 @@ def run():

newPixelWidth = 1. / newPixelPerUnit
newPixelHeight = newcal.pixelWidth * newAspect
print( "Chosen Cal=", cal.cals[CalIdx], " px/unit" )
print "Chose `", cal.names[CalIdx], "` : ", newPixelPerUnit, " px/", newUnit
else:
''' Assume we'll be loading a custom function/class '''
# call the class' `classObj.cal( ImagePlusObject )` function to get the scale value:
Expand All @@ -83,8 +78,8 @@ def run():

newPixelWidth = 1. / newPixelPerUnit
newPixelHeight = newPixelWidth * newAspect
if mc_DEBUG: print( "newPixelWidth, newUnit = ", newPixelWidth, newUnit )
#end if(cal.name is a string)
print "Chose `", cal.names[CalIdx].name, "` : ", newPixelPerUnit, " px/", newUnit
#end if(cal.name is a string or function)


#end if CalIdx
Expand Down Expand Up @@ -119,7 +114,7 @@ def run():


def uScopeCalDialog(cal):
'''
''' Pop up a dialog asking user to choose from the calibration names etc.
CalIdx, SetGlobalScale, AddScaleBar = uScopeCalDialog(cal)
Expand Down Expand Up @@ -152,8 +147,6 @@ def uScopeCalDialog(cal):
CalStr = []
CalType_IsFunc = []

# add option for JEOL SEM (CalIdx = 0)
#CalStr.append( "JEOL SEM - auto cal from .txt")

for ii, name in enumerate(cal.names):
if mc_DEBUG: print( "item #%i: name=" % (ii), name, "\n\t type=", type(name) )
Expand Down Expand Up @@ -202,70 +195,12 @@ def uScopeCalDialog(cal):



#print ChosenRadio,CalIdx, SetGlobalScale
#print "end uScopeCalDialog()."
if mc_DEBUG: print( ChosenRadio,CalIdx, SetGlobalScale )
#if mc_DEBUG: print( "end uScopeCalDialog()." )
return CalIdx, SetGlobalScale, AddScaleBar
#end uScopeCalDialog()


def getJEOLSEMCal( filepath ):
'''Find accompying *.txt file that contains pixel-to-unitlength info, and apply the scale automatically.
Designed against a JEOL 7600F SEM. Expects the *.xtx files to be next to the accompying image files of same name.'''
import re # RegEx matching


txtpath = os.path.splitext( filepath )[0] + ".txt"
if mc_DEBUG: print "txtpath = ", txtpath
if not os.path.isfile(txtpath): raise IOError("Text File not found at: \n\t" + txtpath)

# set up regex matching:
re_bar = re.compile( r'\$\$SM_MICRON_BAR (\d*)' ) # groups the digits in "$$SM_MICRON_BAR 90"
re_barmark = re.compile( r'\$\$SM_MICRON_MARKER (\d*)([a-zA-Z]*)') # groups the decimals and units in "$$SM_MICRON_MARKER 100nm"


# try to load the .txt file:
BarLength_px = None
BarLength_dist = None
BarLength_unit = None

txtfile = open(txtpath, 'r')
try:
while True:
txtline = txtfile.readline()
if len(txtline) == 0:
# end of file, exit the loop
break
#end(if end-of-file)

# search for strings/values:
match1 = re_bar.search( txtline )
if match1:
BarLength_px = float( match1.group(1) ) # this is pixel width of the scale bar
if mc_DEBUG: print 'Scale Bar Pixel Length found:', match1.groups(), ' --> ', BarLength_px
#end if(match1)

match2 = re_barmark.search( txtline )
if match2:
BarLength_dist = float( match2.group(1) ) # this is physical width of the scale bar
BarLength_unit = str( match2.group(2) )
if mc_DEBUG: print 'Scale Bar Distance Length found:', match2.groups(), ' --> ', BarLength_dist, BarLength_unit
#end if(match2)

#end while(file-reading)

except IOError:
raise IOError("Could not load text file that accompanies this image file. Expected the text file to have the same filename as the image, except with '.txt' extension. Expected file to be here:\n\t" + txtpath )

finally:
# make sure python closes the file no matter what
txtfile.close()
#end try(txtfile)

# return pixel-per-unit & units
return (BarLength_px/BarLength_dist), BarLength_unit
#end getJEOLSEMCal()


run() # Run the script function!


0 comments on commit bd35fe4

Please sign in to comment.