transform – Transform pyparsing result

This module converts pyparsing objects in python or numpy objects.

It is called in the module grammar via pyparsing.ParserElement.setParseAction functions. It calls the general module common.

Note

This module is not standalone, needs a pyparsing result in input.

valjean.eponine.tripoli4.transform.compose2(f, g)[source]

Functions composition (2 functions), like fog in mathematics.

Params func f:

last function to compose, takes result from g as arguments

Params func g:

first function to apply

Returns:

composition of the 2 functions (func)

valjean.eponine.tripoli4.transform.compose(*fs)[source]

General composition in case more than 2 functions are needed. For example for fogoh(x) = f(g(h(x))). Takes as many functions as needed as argument.

valjean.eponine.tripoli4.transform.convert_spectrum(toks, spectrum_type)[source]

Convert spectrum to numpy object using common.

Parameters:
  • toks (pyparsing.ParseResults) – spectrum result

  • spectrum_type (str) – type of spectrum that defines the names of the columns

  • Default column names: ['score', 'sigma', 'score/lethargy']

  • Other column names taken into account:

    • vov: adding 'vov' to previous columns names

    • uncert: names are ['sigma2(means)', 'mean(sigma_n2)', 'sigma(sigma_n2)', 'fisher test']

    • nu and za: no 'score/lethargy' is available

Returns:

dictionary containing spectrum as 7-dimensions numpy structured array for result, numpy.ndarray for binnings, discarded batchs, used batchs, integrated result (depending on availability)

See also

common.convert_spectrum and more generally common

valjean.eponine.tripoli4.transform.convert_mesh(toks)[source]

Convert mesh to numpy object using common.

Parameters:

toks (pyparsing.ParseResults) – mesh result

Returns:

dictionary containing meshes (integrated over energy or not) as 7-dimensions numpy structured array, binnings, etc. depending on availability

valjean.eponine.tripoli4.transform.convert_green_bands(toks)[source]

Convert Green bands to numpy object using common.

Parameters:

toks (pyparsing.ParseResults) – Green bands result

Returns:

dictionary containing Green bands as 6-dimensions numpy structured array, numpy.ndarray for binnings, etc. depending on availability

See also

common.convert_green_bands and more generally common

valjean.eponine.tripoli4.transform.convert_correspondence_table(toks)[source]

Convert correspondence table to dict (volume id, volume name).

Returns:

tuple(tuple)

valjean.eponine.tripoli4.transform.convert_scoring_zone_id(toks)[source]

Convert scoring zone id (volume numbers, cells, points, etc) into native python objects (str, tuple, numpy object).

valjean.eponine.tripoli4.transform.convert_list_to_tuple(toks)[source]

Convert list from pyparsing to tuple.

This function is only used to transform the parseResult in standard python list and send it to convert_list_to_tuple in common.

valjean.eponine.tripoli4.transform.convert_batch_numbers(score)[source]

Convert batch numbers (used and discarded) in proper results.

Parameters:

toks (pyparsing.ParseResults) – score result interpreted as dictionary

Returns:

dictionary with maximum 2 keys ('used_batches_res' and 'discarded_batches_res')

Return type:

dict

valjean.eponine.tripoli4.transform.convert_score(toks)[source]

Convert score to numpy and python objects. Calls various conversion functions depending on input key (mesh, spectrum, Green bands, default python dict, etc.).

Parameters:

toks (pyparsing.ParseResults) – score result interpreted as dictionary

Returns:

dictionary using the previous keys and numpy objects as values.

valjean.eponine.tripoli4.transform.convert_generic_adjoint(toks)[source]

Convert adjoint output in numpy object using common.

This method does not take into account sensitivities calculated via the IFP method.

Parameters:

toks (pyparsing.ParseResults) – Adjoint result (got thanks to IFP or Wielandt method)

Returns:

list(dict) compatible with Browser and Index.

See also

common.convert_generic_adjoint and more generally common

valjean.eponine.tripoli4.transform.convert_generic_kinetic(toks)[source]

Convert generic scores for kinetic simulations into a numpy object.

Parameters:

toks (pyparsing.ParseResults) – parsed tokens

Returns:

list(dict) compatible with Browser and Index.

See also

convert_generic_kinetic and more generally common

valjean.eponine.tripoli4.transform.convert_keff(toks)[source]

Convert keff response in python dictionary including numpy.matrix and using common.

Parameters:

toks (pyparsing.ParseResults) – keff result interpreted as dictionary

Returns:

dictionary using numpy objects including numpy.matrix

Note

For the moment, common.convert_keff_with_matrix is called. It is possible to call common.convert_keff instead.

See also

common.convert_keff and more generally common

valjean.eponine.tripoli4.transform.convert_kij_sources(toks)[source]

Convert kij sources to python dictionary containing numpy objects and using common.

Parameters:

toks (pyparsing.ParseResults) – kij source result (interpreted as dictionary)

Returns:

dictionary where kij values are inside numpy.ndarray

See also

common.convert_kij_sources and more generally common

valjean.eponine.tripoli4.transform.convert_kij_result(toks)[source]

Convert kij result to dictionary of numpy objects using common.

Parameters:

toks (pyparsing.ParseResults) – kij results (interpreted as dictionary)

Returns:

dictionary of numpy.ndarray and numpy.matrix

See also

common.convert_kij_result and more generally common

valjean.eponine.tripoli4.transform.convert_kij_keff(toks)[source]

Convert keff estimated from kij to dictionary of numpy objects using common.

Parameters:

toks (pyparsing.ParseResults) – keff block result interpreted as a list t. Only last element is used here (others go to convert_keff).

Returns:

dictionary of numpy.ndarray and numpy.matrix.

Note

It is possible to add a check on estimator if issue.

See also

common.convert_kij_keff and more generally common

valjean.eponine.tripoli4.transform.convert_parna_keff(pdict)[source]

Convert Parna likelihood results to python dictionary.

This result is part of the keff_auto results.

Parameters:

pdict – parna results block to be converted in dictionary (with filter).

Returns:

dictionary

valjean.eponine.tripoli4.transform.convert_keff_auto(toks)[source]

Convert auto keff estimation (old or default output, not printed as a usual response) in standard python objects.

Add the 'response_type' key to the dictionary with 'keff_auto_res' as associated value (to match browser and data_convertor requirements).

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python list corresponding to input pyparsing list

valjean.eponine.tripoli4.transform.convert_sensitivities(toks)[source]

Convert sensitivity results to dictionary of numpy objects using common.

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python list corresponding to input pyparsing list

valjean.eponine.tripoli4.transform.convert_shr(toks)[source]

Convert results on spherical harmonics to dictionary of numpy.ndarray using common.

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python list corresponding to input pyparsing list

valjean.eponine.tripoli4.transform.convert_ifp_adj_crit_ed(toks)[source]

Convert IFP adjoint criticality edition in kinematic array.

valjean.eponine.tripoli4.transform.to_final_dict(toks)[source]

Convert to dictionary result of pyparsing.

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python dictionary corresponding to input pyparsing dictionary

valjean.eponine.tripoli4.transform.lod_to_dot(toks)[source]

List of dictionaries to dictionary of tuples. This function is dedicated to cases where all the dictionaries (or close) in the list have the same keys.

Parameters:

toks (list(dict)) – list of dictionaries

Returns:

dict(tuple)

>>> from pprint import pprint
>>> from pyparsing import OneOrMore, Group, Word, nums
>>> menu = OneOrMore(Group(Word(nums)('egg') + ','
...                        + Word(nums)('bacon') + ','
...                        + Word(nums)('spam')))
>>> lod = menu.parseString('1,2,0 2,0,1 0,3,1')
>>> dot = lod_to_dot(lod)
>>> pprint(dot)  
{'bacon': ('2', '0', '3'), 'egg': ('1', '2', '0'), 'spam': ('0', '1', '1')}
>>> lod = [{'egg': 1, 'bacon': 2, 'spam': 0},
...        {'egg': 2, 'bacon': 0, 'spam': 1},
...        {'egg': 0, 'bacon': 3, 'spam': 1}]
>>> dot = lod_to_dot(lod)
>>> pprint(dot)  
{'bacon': (2, 0, 3), 'egg': (1, 2, 0), 'spam': (0, 1, 1)}
>>> lod = [{'egg': 1, 'bacon': 2, 'spam': 0}]
>>> dot = lod_to_dot(lod)
>>> pprint(dot)  
{'bacon': (2,), 'egg': (1,), 'spam': (0,)}

We always get a tuple as value of the keys, even in case of a single element.

valjean.eponine.tripoli4.transform.finalize_response_dict(s, loc, toks)[source]

Finalize the dictionary of response.

Extract the real response results from pyparsing structure and set it under the results key. The previous unique key of the dictionary under the results key is stored under the result_type key.

The input is the whole response, i.e. the results and the response description. This second part is not “modified” here, only copied in the new dictionary, at the exception of the metadata stored under the compos_details key. In that case the dictionary stored under the compos_details key is moved to the upper level, i.e. in the response dictionary. As a consequence, the compos_details key disappears.

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python dict corresponding to input pyparsing response result

valjean.eponine.tripoli4.transform.extract_all_metadata(list_of_dicts)[source]

Extract metadata from nested lists of dictionaries. The metadata to be extracted are here in the second level of list of dictionaries as in the example:

>>> from pprint import pprint
>>> lod = [{'bla': 'X', 'results': [
...     {'data1_res': 'D1', 'data2_res': 2, 'md1': 'MD1', 'md2': 'MD2'},
...     {'data1_res': 'D3', 'md1': 'MD1', 'md3': 3}]}]
>>> pprint(extract_all_metadata(lod))  
[{'bla': 'X', 'md1': 'MD1', 'md2': 'MD2', 'results': {'data1': 'D1', 'data2': 2}}, {'bla': 'X', 'md1': 'MD1', 'md3': 3, 'results': {'data1': 'D3'}}]
Parameters:

list_of_dicts (list(dict)) – list of dictionaries

Returns:

list of dictionaries with no list of dict under ‘results’ key

Return type:

list(dict)

valjean.eponine.tripoli4.transform.extract_metadata(ldict)[source]

Extract metadata from a list of dictionaries to put it in the surrounding dictionary.

Parameters:

ldict (dict) – dictionary corresponding to a response

Return type:

list(dict)

valjean.eponine.tripoli4.transform.index_elements(key)[source]

Add an item key in dictionary corresponding to index in the list containing the dictionary.

Parameters:

key (str) – name of the index

Returns:

function that really insert the index in the dict contained in a list

>>> from pprint import pprint
>>> lod = [{'a': 1, 'b': 5}, {'c': -3}, {'d': 4, 'e': 6, 'f': 8}]
>>> func1 = index_elements('index')
>>> nlod = func1('', 0, lod)
>>> pprint(nlod)  
[{'a': 1, 'b': 5, 'index': 0}, {'c': -3, 'index': 1}, {'d': 4, 'e': 6, 'f': 8, 'index': 2}]
valjean.eponine.tripoli4.transform.propagate_all_metadata(list_of_dicts)[source]

Concatenate metadata from a list of dictionaries.

It applies propagate_top_metadata for all dictionaries in the list of dictionaries, one by one.

The metadata to be propagated are here in the first level of list of dictionaries as in the example:

>>> from pprint import pprint
>>> lod = [{'ext_metadata': {'emd1': 'EMD1', 'emd2': 'EMD2'},
...         'list_responses': [
...     {'bla': 'X', 'md1': 'MD1', 'md2': 'MD2',
...      'results': {'data1_res': 'D1', 'data2_res': 2}},
...     {'bla': 'X', 'md1': 'MD1', 'md3': 3,
...      'results': {'data1_res': 'D3'}}],
...         'omd': 0}]
>>> pprint(propagate_all_metadata(lod))  
[{'bla': 'X', 'emd1': 'EMD1', 'emd2': 'EMD2', 'md1': 'MD1', 'md2': 'MD2', 'omd': 0, 'results': {'data1_res': 'D1', 'data2_res': 2}}, {'bla': 'X', 'emd1': 'EMD1', 'emd2': 'EMD2', 'md1': 'MD1', 'md3': 3, 'omd': 0, 'results': {'data1_res': 'D3'}}]
Parameters:

list_of_dicts (list(dict)) – list of dictionaries

Returns:

list of dictionaries with common metadata in all responses

Return type:

list(dict)

valjean.eponine.tripoli4.transform.propagate_top_metadata(ldict)[source]

Extract metadata from a list of dictionaries to put them in the surrounding dictionary.

One of the dictionary key has to be 'list_responses', i.e. generic scores or arrays built from a list of responses where metadata have already been extracted. This case typically apply to perturbations (not IFP ones).

Parameters:

ldict (dict) – dictionary corresponding to a response

Return type:

list(dict)

valjean.eponine.tripoli4.transform.group_to_dict(toks)[source]

Transform a named group in a dictionary with a single key (removes duplicated levels).

This method also takes into account internal dict that should be part of the main one, like in integrated_res case when units is required and score has no unit.

Parameters:

toks (pyparsing.ParseResults) – pyparsing element

Returns:

python dict corresponding to input pyparsing named group

valjean.eponine.tripoli4.transform.fail_spectrum(s, loc, expr, err)[source]

Parsing error when all bins were at 0 if option ‘-a’ was used while running Tripoli-4.

valjean.eponine.tripoli4.transform.fail_parsing(s, loc, expr, err)[source]

Parsing error with clear message of failing line.