DataFolder#

class nextnanopy.DataFolder(fullpath)#

Bases: object

Helper for the navigation in the output folder of the nextnano simulations.

Lists the files the directory holds and a DataFolder for each subfolder, built recursively when the object is made – so it is a snapshot of the tree as it stood then.

A subfolder can be reached three ways:

folder.folders['results']
folder.go_to('results', 'bias_0')
folder.results.bias_0

The last needs a name that is a usable Python identifier and that DataFolder is not already using: a clashing name such as files warns and gets no attribute, and a name holding a space or a dot gets one that no dotted expression can reach. go_to works whatever the name.

Parameters:
fullpathstr or pathlib.Path

Path to the directory.

Attributes:
fullpathstr or pathlib.Path

Path to the directory.

foldersDictList

A DataFolder for each direct subfolder, keyed by subfolder name.

fileslist of str

Path of each file lying directly in the directory, in name order. Files held by the subfolders are not among them.

namestr

Name of the directory itself, without the path leading to it. Read-only.

find(template, deep=False)#

Return the files whose name contains template.

Parameters:
templatestr

Substring to look for. Matched against the file name alone, not the folders leading to it, and taken literally – case matters and there is no globbing, so *.dat finds nothing while .dat finds every one.

deepbool, default=False

Whether to search the subfolders as well, at every depth.

Returns:
list of str

Path of every file that matched, this directory’s first and then each subfolder’s. Empty when nothing matched.

Examples

>>> folder = DataFolder('output')
>>> folder.find('bias')
['output/a_bias.dat', 'output/b_bias.dat']

Reaching into the subfolders as well:

>>> folder.find('bias', deep=True)
['output/a_bias.dat', 'output/b_bias.dat', 'output/sweep/z_bias.dat']

The template is a plain substring, so a glob matches nothing and case matters:

>>> folder.find('*.dat')
[]
>>> folder.find('BIAS')
[]
find_multiple(templates, deep=False)#

Return the files whose name contains every one of templates.

Parameters:
templateslist of str

Substrings that must all appear in the file name, in any order. Each is matched the way find matches its own: against the name alone, taken literally, case included. An empty list matches every file.

deepbool, default=False

Whether to search the subfolders as well, at every depth.

Returns:
list of str

Path of every file that matched, this directory’s first and then each subfolder’s. Empty when nothing matched.

See also

find

Search for a single substring.

Notes

Pass a list, never a bare string. A string is iterated character by character, so find_multiple('bias') asks for the letters b, i, a and s in any order rather than for the word.

Examples

>>> folder = DataFolder('output')
>>> folder.find_multiple(['bias', '.dat'], deep=True)
['output/a_bias.dat', 'output/b_bias.dat', 'output/sweep/z_bias.dat']

Every template has to appear, so adding one narrows the result:

>>> folder.find_multiple(['bias', 'a_'])
['output/a_bias.dat']
go_to(*args)#

Follow a path down from this directory and return what is there.

Parameters:
*args

Names to descend through, one per level, joined onto fullpath. With none, the directory itself is returned.

Returns:
DataFolder or str

A new DataFolder when the path names a directory, the path itself when it names a file.

Raises:
ValueError

If the path names neither a file nor a directory.

Notes

The directory is scanned again, so the object handed back is a new one and not the DataFolder already sitting in folders. It therefore sees files written since, at the cost of walking that part of the tree once more.

Examples

>>> folder = DataFolder('output')
>>> sweep = folder.go_to('sweep')
>>> sweep.name
'sweep'

A file comes back as its path, ready to hand to DataFile:

>>> folder.go_to('sweep', 'z_bias.dat')
'output/sweep/z_bias.dat'
show_tree(with_files=True, deep=True)#

Print the folder tree.

Parameters:
with_filesbool, default=True

Whether to list the files as well as the folders.

deepbool, default=True

Whether to descend past the direct subfolders.

read_sweep_infodict()#

Read the sweep information stored in this directory.

Returns:
dict of {strdict}

The swept variable combination of each simulation, keyed by its output folder, as Sweep.execute wrote it to sweep_infodict.json.

Raises:
FileNotFoundError

If the directory holds no sweep_infodict.json, so it is not the output of a sweep.