<commit_before># -*- coding: utf-8 -*-
u"""Controls execution template.

:copyright: Copyright (c) 2020 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkcollections import PKDict
from pykern.pkdebug import pkdp
from sirepo.template import template_common
import copy
import csv
import sirepo.sim_data
import sirepo.simulation_db
import sirepo.template.lattice
import sirepo.template.madx

_SIM_DATA, SIM_TYPE, _SCHEMA = sirepo.sim_data.template_globals()
_SUMMARY_CSV_FILE = 'summary.csv'


def background_percent_complete(report, run_dir, is_running):
    if is_running:
        return PKDict(
            percentComplete=0,
            frameCount=0,
            elementValues=_read_summary_line(run_dir)
        )
    return PKDict(
        percentComplete=100,
        frameCount=1,
        elementValues=_read_summary_line(
            run_dir,
            _SCHEMA.constants.maxBPMPoints,
        )
    )


def get_application_data(data, **kwargs):
    if data.method == 'get_madx_sim_list':
        res = []
        for f in pkio.sorted_glob(
            _SIM_DATA.controls_madx_dir().join(
                '*',
                sirepo.simulation_db.SIMULATION_DATA_FILE,
            ),
        ):
            m = sirepo.simulation_db.read_json(f).models
            res.append(PKDict(
                name=m.simulation.name,
                simulationId=m.simulation.simulationId,
            ))
        return PKDict(simList=res)
    elif data.method == 'get_external_lattice':
        return _get_external_lattice(data.simulationId)


def python_source_for_model(data, model):
    return _generate_parameters_file(data)


def write_parameters(data, run_dir, is_parallel):
    pkio.write_text(
        run_dir.join(template_common.PARAMETERS_PYTHON_FILE),
        _generate_parameters_file(data),
    )


def _dedup_madx_elements(data):
    def _reduce_to_elements(beamline_id):
        def _do(beamline_id):
            for i, item_id in enumerate(beamline_map[beamline_id]['items']):
                item_id = abs(item_id)
                if item_id in beamline_map:
                    _do(item_id)
                    continue
                res.append(item_id)

        res = []
        _do(beamline_id)
        return res

    def _do_dedup(elem_ids):
        nonlocal max_id

        res = []
        for i, e in enumerate(elem_ids):
            l = data.models.elements[element_map[e]]
            if e not in res:
                res.append(e)
                _set_element_name(l, i)
                continue
            n = copy.deepcopy(l)
            _set_element_name(l, i)
            max_id += 1
            n._id = max_id
            data.models.elements.append(n)
            res.append(n._id)
        return res

    def _set_element_name(elem, index):
        elem['name'] = f'{elem.type[0]}{index}'

    max_id = sirepo.template.lattice.LatticeUtil.max_id(data)
    beamline_map = PKDict(
        {
            e.id: PKDict(
                items=e['items'],
                index=i,
            ) for i, e in enumerate(data.models.beamlines)
        },
    )
    element_map = PKDict({e._id: i for i, e in enumerate(data.models.elements)})<commit_msg>Fix #3191: Use bring elem names over when importing from madx<commit_after># -*- coding: utf-8 -*-
u"""Controls execution template.

:copyright: Copyright (c) 2020 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkio
from pykern.pkcollections import PKDict
from pykern.pkdebug import pkdp
from sirepo.template import template_common
import copy
import csv
import sirepo.sim_data
import sirepo.simulation_db
import sirepo.template.lattice
import sirepo.template.madx

_SIM_DATA, SIM_TYPE, _SCHEMA = sirepo.sim_data.template_globals()
_SUMMARY_CSV_FILE = 'summary.csv'


def background_percent_complete(report, run_dir, is_running):
    if is_running:
        return PKDict(
            percentComplete=0,
            frameCount=0,
            elementValues=_read_summary_line(run_dir)
        )
    return PKDict(
        percentComplete=100,
        frameCount=1,
        elementValues=_read_summary_line(
            run_dir,
            _SCHEMA.constants.maxBPMPoints,
        )
    )


def get_application_data(data, **kwargs):
    if data.method == 'get_madx_sim_list':
        res = []
        for f in pkio.sorted_glob(
            _SIM_DATA.controls_madx_dir().join(
                '*',
                sirepo.simulation_db.SIMULATION_DATA_FILE,
            ),
        ):
            m = sirepo.simulation_db.read_json(f).models
            res.append(PKDict(
                name=m.simulation.name,
                simulationId=m.simulation.simulationId,
            ))
        return PKDict(simList=res)
    elif data.method == 'get_external_lattice':
        return _get_external_lattice(data.simulationId)


def python_source_for_model(data, model):
    return _generate_parameters_file(data)


def write_parameters(data, run_dir, is_parallel):
    pkio.write_text(
        run_dir.join(template_common.PARAMETERS_PYTHON_FILE),
        _generate_parameters_file(data),
    )


def _dedup_madx_elements(data):
    def _reduce_to_elements(beamline_id):
        def _do(beamline_id):
            for i, item_id in enumerate(beamline_map[beamline_id]['items']):
                item_id = abs(item_id)
                if item_id in beamline_map:
                    _do(item_id)
                    continue
                res.append(item_id)

        res = []
        _do(beamline_id)
        return res

    def _do_dedup(elem_ids):
        nonlocal max_id

        res = []
        for i, e in enumerate(elem_ids):
            l = data.models.elements[element_map[e]]
            if e not in res:
                res.append(e)
                _set_element_name(l, i)
                continue
            n = copy.deepcopy(l)
            _set_element_name(l, i)
            max_id += 1
            n._id = max_id
            data.models.elements.append(n)
            res.append(n._id)
        return res

    def _set_element_name(elem, index):
        elem['name'] = f'{elem.name[0]}_{index}'

    max_id = sirepo.template.lattice.LatticeUtil.max_id(data)
    beamline_map = PKDict(
        {
            e.id: PKDict(
                items=e['items'],
                index=i,
            ) for i, e in enumerate(data.models.beamlines)
        },
    )
    element_map = PKDict({e._id: i for i, e in enumerate(data.models.elements)})
{'fraction_non_alphanumeric': 0.09265604051839714, 'fraction_numerical': 0.005064799642484731, 'mean_word_length': 2.1654879773691653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4198791', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2108, 'n_words': 399}