Skip to content

Commit

Permalink
Move get_scan_path to contrib directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Ercius ncem-gauss jupyter committed May 22, 2023
1 parent 870c4d7 commit 27a96a0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
48 changes: 48 additions & 0 deletions python/stempy/contrib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

def get_scan_path(directory, scan_num=None, scan_id=None, th=None):
"""
Get the file path for a 4D Camera scan from a directory on NERSC
using the scan number, the Distiller scan id, and/or threshold.
scan_id should always be unique and is the best option to load a dataset.
A ValueError is raised if more than one file matches the input. Then the user
needs to input more information to narrow down the choices.
:param directory: The path to the directory containing the file.
:type path: pathlib.Path or str
:param scan_num: The 4D Camera scan number. Optional,
:type scan_num: int, optional
:param scan_id: The Distiller scan id.
:type scan_id: int, optional
:param th: The threshold for counting. This was added to the filename in older files. Optional.
:type th: int, optional
:return: A tuple containing the file Path that matches the input information, the scan_num, and the scan_id.
:rtype: (pathlib.Path, int, int)
"""

if scan_id is not None:
# This should be unique
file_path = list(directory.glob(f'*_id{scan_id}*.h5'))
elif scan_num is not None:
# older files might include the threshold (th)
if th is not None:
file_path = list(directory.glob(f'data_scan{scan_num}_th{th}_electrons.h5'))
else:
file_path = list(directory.glob(f'data_scan{scan_num}*electrons.h5'))
else:
raise TypeError('Missing scan_num or scan_id input.')

if len(file_path) > 1:
raise ValueError('Multiple files match that input. Add scan_id to be more specific.')
elif len(file_path) == 1:
file_path = file_path[0]
# Determine the scan_id and scan_num for use later (i.e. getting DM4 file)
spl = file_path.name.split('_')
for ii in spl:
if 'id' in ii:
scan_id = int(ii[len('id'):])
elif 'scan' in ii:
scan_num = int(ii[len('scan'):])
else:
raise FileNotFoundError('No file with those parameters can be found.')
return file_path, scan_num, scan_id
52 changes: 0 additions & 52 deletions python/stempy/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,55 +304,3 @@ def write_hdf5(path, reader, format=SectorReader.H5Format.Frame):
:type format: SectorReader.H5Format
"""
reader.to_hdf5(path, format)

def get_scan_path(directory, scan_num=None, scan_id=None, th=None):
""" Get the file path for a 4D Camera scan on NERSC using the scan number,
the Distiller scan id, and/or threshold. scan_id should always
be unique and is the best option to load a dataset.
A ValueError is raised if more than one file matches the input. Then the user
needs to input more information to narrow down the choices.
Parameters
----------
directory : pathlib.Path or str
The path to the directory containing the file.
scan_id : int, optional
The Distiller scan id.
scan_num : int, optional
The 4D Camera scan number. Optional
th : float, optional
The threshold for counting. This was added to the filename in older files.
Returns
-------
: tuple
The tuple contains the file that matches the input information and the
scan_num and scan_id as a tuple.
"""
if scan_id is not None:
# This should be unique
file_path = list(directory.glob(f'*_id{scan_id}*.h5'))
elif scan_num is not None:
# older files might include the threshold (th)
if th is not None:
file_path = list(directory.glob(f'data_scan{scan_num}_th{th}_electrons.h5'))
else:
file_path = list(directory.glob(f'data_scan{scan_num}*electrons.h5'))
else:
raise TypeError('Missing scan_num or scan_id input.')

if len(file_path) > 1:
raise ValueError('Multiple files match that input. Add scan_id to be more specific.')
elif len(file_path) == 1:
file_path = file_path[0]
# Determine the scan_id and scan_num for use later (i.e. getting DM4 file)
spl = file_path.name.split('_')
for ii in spl:
if 'id' in ii:
scan_id = int(ii[len('id'):])
elif 'scan' in ii:
scan_num = int(ii[len('scan'):])
else:
raise FileNotFoundError('No file with those parameters can be found.')
return file_path, scan_num, scan_id

0 comments on commit 27a96a0

Please sign in to comment.