<commit_before># -*- coding: utf-8 -*-
"""Wrapper to run SRW from the command line.

:copyright: Copyright (c) 2015 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 pkcollections
from pykern import pkconfig
from pykern import pkio
from pykern.pkdebug import pkdp, pkdc
from sirepo import mpi
from sirepo import simulation_db
from sirepo.template import template_common
from sirepo.template.srw import extract_report_data
import os
import re
import srwl_bl
import srwlib


def python_to_json(run_dir='.', in_py='in.py', out_json='out.json'):
    """Run importer in run_dir trying to import py_file

    Args:
        run_dir (str): clean directory except for in_py
        in_py (str): name of the python file in run_dir
        out_json (str): valid json matching SRW schema
    """
    import sirepo.importer
    with pkio.save_chdir(run_dir):
        out = sirepo.importer.python_to_json(in_py)
        with open(out_json, 'w') as f:
            f.write(out)
    return 'Created: {}'.format(out_json)


def run(cfg_dir):
    """Run srw in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run srw in
    """
    with pkio.save_chdir(cfg_dir):
        _run_srw()


def run_background(cfg_dir):
    """Run srw with mpi in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run srw in
    """
    with pkio.save_chdir(cfg_dir):
        script = pkio.read_text(template_common.PARAMETERS_PYTHON_FILE)
        p = dict(pkcollections.map_items(cfg))
        if pkconfig.channel_in('dev'):
            p['particles_per_core'] = 1
        p['cores'] = mpi.cfg.cores
        #TODO(pjm): move parameters/config to template/srw.py and set directly in srw.py.jinja
        script += '''
import srwl_bl
v = srwl_bl.srwl_uti_parse_options(varParam, use_sys_argv=False)
source_type, mag = srwl_bl.setup_source(v)
v.wm_na = v.sm_na = {particles_per_core}<commit_msg>#335 - fixed number of particles per core to avoid a bug in SRW.<commit_after># -*- coding: utf-8 -*-
"""Wrapper to run SRW from the command line.

:copyright: Copyright (c) 2015 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 pkcollections
from pykern import pkconfig
from pykern import pkio
from pykern.pkdebug import pkdp, pkdc
from sirepo import mpi
from sirepo import simulation_db
from sirepo.template import template_common
from sirepo.template.srw import extract_report_data
import os
import re
import srwl_bl
import srwlib


def python_to_json(run_dir='.', in_py='in.py', out_json='out.json'):
    """Run importer in run_dir trying to import py_file

    Args:
        run_dir (str): clean directory except for in_py
        in_py (str): name of the python file in run_dir
        out_json (str): valid json matching SRW schema
    """
    import sirepo.importer
    with pkio.save_chdir(run_dir):
        out = sirepo.importer.python_to_json(in_py)
        with open(out_json, 'w') as f:
            f.write(out)
    return 'Created: {}'.format(out_json)


def run(cfg_dir):
    """Run srw in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run srw in
    """
    with pkio.save_chdir(cfg_dir):
        _run_srw()


def run_background(cfg_dir):
    """Run srw with mpi in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run srw in
    """
    with pkio.save_chdir(cfg_dir):
        script = pkio.read_text(template_common.PARAMETERS_PYTHON_FILE)
        p = dict(pkcollections.map_items(cfg))
        if pkconfig.channel_in('dev'):
            p['particles_per_core'] = 5
        p['cores'] = mpi.cfg.cores
        #TODO(pjm): move parameters/config to template/srw.py and set directly in srw.py.jinja
        script += '''
import srwl_bl
v = srwl_bl.srwl_uti_parse_options(varParam, use_sys_argv=False)
source_type, mag = srwl_bl.setup_source(v)
v.wm_na = v.sm_na = {particles_per_core}
{'fraction_non_alphanumeric': 0.10327828444663545, 'fraction_numerical': 0.004683263495193493, 'mean_word_length': 3.401301518438178, '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': '5858627', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1390, 'n_words': 385}