brixs.finder

use finder to avoid running functions multiple types with same input parameters

Let’s say you have a function processing_function(a, b, c) that returns a br.Spectrum type based on parameters a, b, and c.

You can use the finder functionality to avoid running processing_function() multiple times with the same set of a, b, and c parameters.

How it works. First time you run processing_function(a, b, c), it will process/calculate the desired spectrum based on a, b, and c parameters. It will then save it somewhere. If you try to run processing_function(a, b, c) with the same parameters set, it will load and return the already calculated spectrum.

USAGE:

There are two ways to user the finder functionality

  1. via decorators (recommended)

1a) function that saves one Spectrum

# define path where files will be saved

>>> br.finder.folderpath = '<some-path>'

# verbose for finder can be set (default is True)

>>> br.finder.verbose = True

# apply the decorator to you function

>>> @br.finder.track
>>> def processing_function(a, b, c):
>>>    s = <does something with a, b and c and returns s>
>>>    return s

1b) function that saves one multiple Spectrum

# define path where files will be saved

>>> br.finder.folderpath = '<some-path>'

# verbose for finder can be set (default is True)

>>> br.finder.verbose = True

# apply the decorator to you function

>>> @br.finder.track2
>>> def processing_function(a, b, c):
>>>    s1 = <does something with a, b and c and returns s1>
>>>    s2 = <does something with a, b and c and returns s2>
>>>    s3 = <does something with a, b and c and returns s3>
>>>    return s1, s2, s3

1c) function that saves one Spectra

# define path where files will be saved

>>> br.finder.folderpath = '<some-path>'

# verbose for finder can be set (default is True)

>>> br.finder.verbose = True

# apply the decorator to you function

>>> @br.finder.trackss
>>> def processing_function(a, b, c):
>>>    ss = <does something with a, b and c and returns ss>
>>>    return ss
  1. manually setting up the finder function, where files will be save in folderpath

# verbose for finder can be set (default is True) br.finder.verbose = True

# your processing function must be defined like this, where parameters is a dictionary # with the calculation parameters

>>> def processing_function(parameters, folderpath):
>>>
>>>     # try and find if spectrum has already been calculated
>>>     s = search(parameters, folderpath=folderpath)
>>>     if s is not None:
>>>         return s
>>>
>>>     #### process something
>>>     <include-code-here>
>>>
>>>     # save spectra so it is not needed to run it again
>>>     save(s=s, parameters=parameters, folderpath=folderpath)
>>>
>>>     return s

Developers note: in the future, maybe we can make a better way to get the last file number in function _save()

brixs.addons.finder.track(func)

test

brixs.addons.finder.reset(folderpath_=None)

delete all spectrum and restart finder file

brixs.addons.finder.search(parameters, folderpath)

Check if data has already been calculated/processed with similar parameters

Parameters:
  • string (str) – string to search inside filepath. String must be representative of all parameters used to process the spectrum

  • folderpath (string or Path) – folderpath to save spectra. This folder path must contain a file named ‘finder.txt’. If not, one will be created.

Raises:

keyerror – if finder_values have keys that do not match with finder_tags.

Returns

spectrum if data is found, or None.

brixs.addons.finder.searchss(parameters, folderpath)

Check if data has already been calculated/processed with similar parameters

Parameters:
  • string (str) – string to search inside filepath. String must be representative of all parameters used to process the spectrum

  • folderpath (string or Path) – folderpath to save spectra. This folder path must contain a file named ‘finder.txt’. If not, one will be created.

Raises:

keyerror – if finder_values have keys that do not match with finder_tags.

Returns

spectra if data is found, or None.

brixs.addons.finder.save(parameters, s, folderpath)

saves processed/calculated spectrum so one does not have to process it again

Parameters:
  • folderpath (string or Path) – folderpath to save spectra.

  • string (string) – string to save in the finder file

  • s (br.spectrum) – spectrum to be saved

  • filepath (Path or str) – filepath that points to a file which odd lines

  • line (list the parameters used to process a certain spectrum and the next)

  • spectrum. (gives the associated filepath of this)

Returns:

None