Skip to content

Commit

Permalink
Merge pull request #21 from UU-SCMB/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
MaartenBransen authored Mar 15, 2024
2 parents 1aa41c6 + 3264848 commit 77f1e83
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Set of functions for dealing with data from the electron microscopes at Utrecht
## Info
- Created by: Maarten Bransen
- Email: [email protected]
- Version: 3.2.4
- Version: 3.3.0

## Installation

Expand Down
2 changes: 1 addition & 1 deletion scm_electron_microscopes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.2.5'
__version__ = '3.3.0'

from .tem import tia,sis,velox,velox_image,velox_edx,tecnai,talos
from .sem import helios,phenom,xl30sfeg,ZeissSEM
Expand Down
140 changes: 140 additions & 0 deletions scm_electron_microscopes/sem.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,53 @@ def print_metadata(self):
util.print_metadata(xml_root)


def export_metadata(self,filename=None):
"""
save text file with metadata
Parameters
----------
filename : str, optional
filename to store. The default is the image name with
`'_metadata.txt'` appended.
"""
metadata = self.get_metadata()

if filename is None:
filename = self.filename.rpartition('.')[0]+'_metadata.txt'

with open(filename,'w') as f:
f.write('original file: '+self.filename+'\n\n')

def recursive_write(root,prefix=''):
#if there are subelements, print and call itself on subelements
if root:
if root.attrib:
f.write(prefix + root.tag + ' ' + str(root.attrib) + ':\n')
else:
f.write(prefix + root.tag + ':\n')
for child in root:
recursive_write(child,prefix=prefix+'\t')

#otherwise, just print available info
else:
if not root.text:
root.text = ''
if not root.attrib:#if attributes are empty
f.write(prefix + root.tag + ' = ' + root.text+'\n')
elif 'unit' in root.attrib:#if not, get unit from attributes
f.write(prefix + root.tag + ' = ' + root.text + ' ' + root.attrib['unit']+'\n')
elif root.text:#when attributes not empty check if there is text
f.write(prefix + root.tag + ' = ' + str(root.attrib) + root.text+'\n')
else:
f.write(prefix + root.tag + ' = ' + str(root.attrib)+'\n')

if len(prefix)==0:
f.write('\n')

for root in metadata:
recursive_write(root)

def get_pixelsize(self):
"""
gets the pixel size from the metadata and calculates the unit
Expand Down Expand Up @@ -389,6 +436,52 @@ def print_metadata(self):

util.print_metadata(xml_root)

def export_metadata(self,filename=None):
"""
save text file with metadata
Parameters
----------
filename : str, optional
filename to store. The default is the image name with
`'_metadata.txt'` appended.
"""
metadata = self.get_metadata()

if filename is None:
filename = self.filename.rpartition('.')[0]+'_metadata.txt'

with open(filename,'w') as f:
f.write('original file: '+self.filename+'\n\n')

def recursive_write(root,prefix=''):
#if there are subelements, print and call itself on subelements
if root:
if root.attrib:
f.write(prefix + root.tag + ' ' + str(root.attrib) + ':\n')
else:
f.write(prefix + root.tag + ':\n')
for child in root:
recursive_write(child,prefix=prefix+'\t')

#otherwise, just print available info
else:
if not root.text:
root.text = ''
if not root.attrib:#if attributes are empty
f.write(prefix + root.tag + ' = ' + root.text+'\n')
elif 'unit' in root.attrib:#if not, get unit from attributes
f.write(prefix + root.tag + ' = ' + root.text + ' ' + root.attrib['unit']+'\n')
elif root.text:#when attributes not empty check if there is text
f.write(prefix + root.tag + ' = ' + str(root.attrib) + root.text+'\n')
else:
f.write(prefix + root.tag + ' = ' + str(root.attrib)+'\n')

if len(prefix)==0:
f.write('\n')

for root in metadata:
recursive_write(root)

def get_pixelsize(self):
"""gets the pixel size from the metadata and calculates the unit
Expand Down Expand Up @@ -901,6 +994,53 @@ def print_metadata(self):

util.print_metadata(xml_root)

def export_metadata(self,filename=None):
"""
save text file with metadata
Parameters
----------
filename : str, optional
filename to store. The default is the image name with
`'_metadata.txt'` appended.
"""
metadata = self.get_metadata()

if filename is None:
filename = self.filename.rpartition('.')[0]+'_metadata.txt'

with open(filename,'w') as f:
f.write('original file: '+self.filename+'\n\n')

def recursive_write(root,prefix=''):
#if there are subelements, print and call itself on subelements
if root:
if root.attrib:
f.write(prefix + root.tag + ' ' + str(root.attrib) + ':\n')
else:
f.write(prefix + root.tag + ':\n')
for child in root:
recursive_write(child,prefix=prefix+'\t')

#otherwise, just print available info
else:
if not root.text:
root.text = ''
if not root.attrib:#if attributes are empty
f.write(prefix + root.tag + ' = ' + root.text+'\n')
elif 'unit' in root.attrib:#if not, get unit from attributes
f.write(prefix + root.tag + ' = ' + root.text + ' ' + root.attrib['unit']+'\n')
elif root.text:#when attributes not empty check if there is text
f.write(prefix + root.tag + ' = ' + str(root.attrib) + root.text+'\n')
else:
f.write(prefix + root.tag + ' = ' + str(root.attrib)+'\n')

if len(prefix)==0:
f.write('\n')

for root in metadata:
recursive_write(root)

def get_pixelsize(self):
"""
gets the physical size of a pixel from the metadata
Expand Down
60 changes: 60 additions & 0 deletions scm_electron_microscopes/tem.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ def print_metadata(self):
print(string.expandtabs(l+2))
print('-----------------------------------------------------\n')

def export_metadata(self,filename=None):
"""
save text file with metadata
Parameters
----------
filename : str, optional
filename to store. The default is the image name with
`'_metadata.txt'` appended.
"""
metadata = self.get_metadata(asdict=True)

if filename is None:
filename = self.filename.rsplit('.')[0]+'_metadata.txt'

l = max(len(i) for i in metadata)
with open(filename,'w') as f:
f.write(('original file:\t'+self.filename).expandtabs(l+2)+'\n')
for i,k in metadata.items():
string = i+':\t'+str(k['value'])+' '+str(k['unit'])+'\n'
f.write(string.expandtabs(l+2))

def get_pixelsize(self,convert=None):
"""
Expand Down Expand Up @@ -829,6 +850,45 @@ def _recursive_md_print(self,root,prefix='|'):
else:
print(prefix+key+': '+val)

def export_metadata(self,filename=None):
"""
save text file with metadata
Parameters
----------
filename : str, optional
filename to store. The default is the image name with
`'_metadata.txt'` appended.
"""
metadata = self.get_metadata()

if filename is None:
filename = self.filename.rpartition('.')[0]+\
f'_image-{self.index:02d}_metadata.txt'

with open(filename,'w') as f:
f.write('original file: '+self.filename+'\n')

def recursive_write(root,prefix='\t'):
for key,val in root.items():
#safeguard against infinite recursion
if len(prefix)>20:
f.write(prefix+'\tMAX RECURSION DEPTH\n')
#for a tag, print and call function on child
elif isinstance(val,dict):
f.write(prefix+key+':\n')
recursive_write(val,prefix=prefix+'\t')
#for data, print the root data __repr__ method
else:
f.write(prefix+key+': '+val+'\n')

for key,val in metadata.items():
if isinstance(val,dict):
f.write('\n'+key+':\n')
recursive_write(val)
else:
f.write('\n'+key+': '+val+'\n')

class velox_image(velox_dataset):
"""
Subclass of the `velox_dataset` class for individual images (or image
Expand Down
13 changes: 7 additions & 6 deletions scm_electron_microscopes/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,27 +291,28 @@ def _on_lim_change(call):
#get size of text
from PIL import ImageFont
font = ImageFont.truetype(font,size=int(fontsize))
textsize = font.getsize(text)
offset = font.getoffset(text)
textsize = (textsize[0]-offset[0],textsize[1]-offset[1]+fontbaseline)
text_bbox = font.getbbox(text)
offset = (text_bbox[0],text_bbox[1])
textsize = (text_bbox[2]-text_bbox[0],text_bbox[3]-text_bbox[1])

#correct baseline for mu in case of micrometer
if 'µ' in text:
textsize = (textsize[0],textsize[1]-6*scale)
bb = font.getbbox(text.replace('µ','u'))
textsize = (textsize[0],bb[3]-bb[1])

else:
textsize = (0,0)
fontpad = 0

#determine box height with appropriate paddings
if draw_text and draw_bar:#both
boxheight = barpad + barthickness + fontpad + textsize[1]
boxheight = barpad + barthickness+fontpad+textsize[1]+fontbaseline
boxwidth = max([2*barpad+barsize_px,2*fontpad+textsize[0]])
elif draw_bar:#bar only
boxheight = 2*barpad + barthickness
boxwidth = 2*barpad + barsize_px
else:#text only
boxheight = 2*fontpad + textsize[1]
boxheight = 2*fontpad + textsize[1] + fontbaseline
boxwidth = 2*fontpad + textsize[0]

#determine box/bar/text position based on loc
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="scm_electron_microscopes",
version="3.2.5",
version="3.3.0",
author="Maarten Bransen",
author_email="[email protected]",
license='GNU General Public License v3.0',
Expand Down

0 comments on commit 77f1e83

Please sign in to comment.