Example #1
0
def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data.report == 'beamStatisticsReport':
        res = _run_beam_statistics(cfg_dir, data)
    else:
        res = _run_shadow(cfg_dir, data)
    template_common.write_sequential_result(res)
Example #2
0
def save_sequential_report_data(data, run_dir):
    report = data.models[data.report]
    res = None
    if data.report == 'twissReport':
        report['x'] = 's'
        col_names, rows = _read_data_file(run_dir.join(_TWISS_FILE_NAME))
        x = _column_data(report.x, col_names, rows)
        y_range = None
        plots = []
        for f in ('y1', 'y2', 'y3'):
            if report[f] == 'none':
                continue
            plots.append({
                'points': _column_data(report[f], col_names, rows),
                'label': '{} {}'.format(report[f], _units(report[f])),
            })
        res = PKDict(
            title='',
            x_range=[min(x), max(x)],
            y_label='',
            x_label='{} {}'.format(report.x, _units(report.x)),
            x_points=x,
            plots=plots,
            y_range=template_common.compute_plot_color_and_range(plots),
        )
    elif 'bunchReport' in data.report:
        res = _bunch_plot(report, run_dir, 0)
        res.title = ''
    else:
        raise AssertionError('unknown report: {}'.format(report))
    template_common.write_sequential_result(
        res,
        run_dir=run_dir,
    )
Example #3
0
def save_sequential_report_data(data, run_dir):
    if 'bunchReport' in data.report:
        import synergia.bunch
        with h5py.File(str(run_dir.join(OUTPUT_FILE.twissReport)), 'r') as f:
            twiss0 = dict(
                map(
                    lambda k: (k, format_float(f[k][0])),
                    ('alpha_x', 'alpha_y', 'beta_x', 'beta_y'),
                ))
        report = data.models[data.report]
        bunch = data.models.bunch
        if bunch.distribution == 'file':
            bunch_file = _SIM_DATA.lib_file_name_with_model_field(
                'bunch', 'particleFile', bunch.particleFile)
        else:
            bunch_file = OUTPUT_FILE.bunchReport
        if not run_dir.join(bunch_file).exists():
            return
        with h5py.File(str(run_dir.join(bunch_file)), 'r') as f:
            x = f['particles'][:, getattr(synergia.bunch.Bunch, report['x'])]
            y = f['particles'][:, getattr(synergia.bunch.Bunch, report['y'])]
        res = template_common.heatmap(
            [x, y], report, {
                'title': '',
                'x_label': label(report.x, _SCHEMA.enum.PhaseSpaceCoordinate8),
                'y_label': label(report.y, _SCHEMA.enum.PhaseSpaceCoordinate8),
                'summaryData': {
                    'bunchTwiss': twiss0,
                },
            })
    else:
        report_name = data.report
        x = None
        plots = []
        report = data.models[report_name]
        with h5py.File(str(run_dir.join(OUTPUT_FILE[report_name])), 'r') as f:
            x = f['s'][:].tolist()
            for yfield in ('y1', 'y2', 'y3'):
                if report[yfield] == 'none':
                    continue
                name = report[yfield]
                plots.append({
                    'name':
                    name,
                    'label':
                    label(report[yfield], _SCHEMA.enum.TwissParameter),
                    'points':
                    f[name][:].tolist(),
                })
        res = {
            'title': '',
            'x_range': [min(x), max(x)],
            'y_range': template_common.compute_plot_color_and_range(plots),
            'x_label': 's [m]',
            'y_label': '',
            'x_points': x,
            'plots': plots,
        }
    template_common.write_sequential_result(res, run_dir=run_dir)
Example #4
0
def _run_tunes_report(cfg_dir, data):
    r = template_common.exec_parameters()
    pkio.write_text(template.TUNES_INPUT_FILE, r.tunes_file)
    #TODO(pjm): uses datafile from animation directory
    os.symlink('../animation/zgoubi.fai', 'zgoubi.fai')
    subprocess.call([_TUNES_PATH])
    template_common.write_sequential_result(
        template.extract_tunes_report(cfg_dir, data))
Example #5
0
def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data.report == 'dvhReport':
        template_common.write_sequential_result(_dvh_report(data))
    elif data.report == 'dicom3DReport':
        template_common.write_sequential_result({})
    else:
        assert False, 'unknown report: {}'.format(data.report)
Example #6
0
def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data['report'] == 'twissReport':
        template_common.write_sequential_result(_extract_twiss_report(data))
    elif data['report'] == 'rateCalculationReport':
        _run_jspec(data)
        template_common.write_sequential_result(template.get_rates(cfg_dir))
    else:
        raise AssertionError('unknown report: {}'.format(data['report']))
Example #7
0
def save_sequential_report_data(data, run_dir):
    report_name = data.report
    if 'twissReport' in report_name or 'opticsReport' in report_name:
        enum_name = _REPORT_ENUM_INFO[report_name]
        report = data.models[report_name]
        plots = []
        col_names, rows = _read_data_file(
            py.path.local(run_dir).join(_ZGOUBI_TWISS_FILE))
        error = ''
        for f in ('y1', 'y2', 'y3'):
            if report[f] == 'none':
                continue
            points = column_data(report[f], col_names, rows)
            if any(map(lambda x: math.isnan(x), points)):
                error = 'Twiss data could not be computed for {}'.format(
                    template_common.enum_text(_SCHEMA, enum_name, report[f]), )
                break
            plots.append(
                PKDict(
                    points=points,
                    label=template_common.enum_text(_SCHEMA, enum_name,
                                                    report[f]),
                ))
        if error:
            res = PKDict(error=error, )
        else:
            #TODO(pjm): use template_common
            x = column_data('sums', col_names, rows)
            res = PKDict(
                title='',
                x_range=[min(x), max(x)],
                y_label='',
                x_label='s [m]',
                x_points=x,
                plots=plots,
                y_range=template_common.compute_plot_color_and_range(plots),
                summaryData=_read_twiss_header(run_dir),
            )
    elif report_name == 'twissSummaryReport':
        res = PKDict(
            #TODO(pjm): x_range requied by sirepo-plotting.js
            x_range=[],
            summaryData=_read_twiss_header(run_dir),
        )
    elif 'bunchReport' in report_name:
        report = data.models[report_name]
        col_names, rows = _read_data_file(
            py.path.local(run_dir).join(_ZGOUBI_FAI_DATA_FILE))
        res = _extract_heatmap_data(report, col_names, rows, '')
        summary_file = py.path.local(run_dir).join(BUNCH_SUMMARY_FILE)
        if summary_file.exists():
            res.summaryData = PKDict(
                bunch=simulation_db.read_json(summary_file))
    else:
        raise AssertionError('unknown report: {}'.format(report_name))
    template_common.write_sequential_result(res, run_dir=run_dir)
Example #8
0
def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data.report == 'dvhReport':
        filename = _SIM_DATA.lib_file_for_sim(data, _DVH_FILE_NAME)
        template_common.write_sequential_result(
            simulation_db.read_json(filename))
    elif data.report == 'dicom3DReport':
        template_common.write_sequential_result({})
    else:
        assert False, 'unknown report: {}'.format(data.report)
Example #9
0
def run(cfg_dir):
    template_common.exec_parameters()
    d = pkio.py_path(cfg_dir)
    template_common.write_sequential_result(
        PKDict(elementValues=template._read_summary_line(
            d,
            simulation_db.get_schema(template.SIM_TYPE).constants.maxBPMPoints,
        )),
        run_dir=d,
    )
Example #10
0
def _run_dose_calculation(data, cfg_dir):
    if not feature_config.cfg().rs4pi_dose_calc:
        dicom_dose = _run_dose_calculation_fake(data, cfg_dir)
    else:
        pksubprocess.check_call_with_signals(
            ['bash', str(cfg_dir.join(template.DOSE_CALC_SH))])
        dicom_dose = template.generate_rtdose_file(data, cfg_dir)
    data['models']['dicomDose'] = dicom_dose
    # save results into simulation input data file, this is needed for further calls to get_simulation_frame()
    simulation_db.write_json(template_common.INPUT_BASE_NAME, data)
    template_common.write_sequential_result(PKDict(dicomDose=dicom_dose))
Example #11
0
def extract_report_data(run_dir, sim_in):
    assert sim_in.report in _REPORTS, 'unknown report: {}'.format(sim_in.report)
    if 'reset' in sim_in.report:
        template_common.write_sequential_result({}, run_dir=run_dir)
    if 'geometry' in sim_in.report:
        v_type = sim_in.models.magnetDisplay.viewType
        f_type = sim_in.models.magnetDisplay.fieldType if v_type ==\
            _SCHEMA.constants.viewTypeFields else None
        template_common.write_sequential_result(
            _read_data(sim_in.simulationId, v_type, f_type),
            run_dir=run_dir,
        )
Example #12
0
def save_sequential_report_data(data, run_dir):
    a = copy.deepcopy(data.models[data.report])
    a.frameReport = data.report
    if a.frameReport == 'twissReport':
        a.x = 's'
        a.y = a.y1
    a.frameIndex = 0
    template_common.write_sequential_result(
        _extract_report_data(
            str(run_dir.join(_report_output_filename(a.frameReport))), a),
        run_dir=run_dir,
    )
Example #13
0
def save_sequential_report_data(data, run_dir):
    report = data.models[data.report]
    res = None
    if 'bunchReport' in data.report:
        res = _bunch_plot(report, run_dir, 0)
        res.title = ''
    else:
        raise AssertionError('unknown report: {}'.format(report))
    template_common.write_sequential_result(
        res,
        run_dir=run_dir,
    )
Example #14
0
def run(cfg_dir):
    pksubprocess.check_call_with_signals(
        [sys.executable, template_common.PARAMETERS_PYTHON_FILE], )
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data.report == 'heightWeightReport':
        res = _report(
            'Dog Height and Weight Over Time',
            ('height', 'weight'),
            data,
        )
    else:
        raise AssertionError('unknown report: {}'.format(data.report))
    template_common.write_sequential_result(res)
Example #15
0
def run(cfg_dir):
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if 'analysisReport' in data.report:
        res = template.get_analysis_report(py.path.local(cfg_dir), data)
    elif 'fftReport' in data.report:
        res = template.get_fft(py.path.local(cfg_dir), data)
    elif 'correctorSettingReport' in data.report:
        res = template.get_settings_report(py.path.local(cfg_dir), data)
    elif 'beamPositionReport' in data.report:
        res = template.get_beam_pos_report(py.path.local(cfg_dir), data)
    else:
        raise AssertionError('unknown report: {}'.format(data.report))
    template_common.write_sequential_result(res)
Example #16
0
def run(cfg_dir):
    template_common.exec_parameters()
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data['report'] == 'fieldReport':
        res = template.generate_field_report(data, cfg_dir)
        res['tof_expected'] = field_results.tof_expected
        res['steps_expected'] = field_results.steps_expected,
        res['e_cross'] = field_results.e_cross
    elif data['report'] == 'fieldComparisonReport':
        wp.step(template.COMPARISON_STEP_SIZE)
        res = template.generate_field_comparison_report(data, cfg_dir)
    else:
        raise AssertionError('unknown report: {}'.format(data['report']))
    template_common.write_sequential_result(res)
Example #17
0
def extract_report_data(run_dir, sim_in):
    assert sim_in.report in _REPORTS, 'unknown report: {}'.format(sim_in.report)
    if 'reset' in sim_in.report:
        template_common.write_sequential_result({}, run_dir=run_dir)
    if 'geometry' in sim_in.report:
        v_type = sim_in.models.magnetDisplay.viewType
        f_type = sim_in.models.magnetDisplay.fieldType if v_type ==\
            _SCHEMA.constants.viewTypeFields else None
        template_common.write_sequential_result(
            _read_data(sim_in.simulationId, v_type, f_type),
            run_dir=run_dir,
        )
    if 'kickMapReport' in sim_in.report:
        template_common.write_sequential_result(
            _kick_map_plot(sim_in.simulationId, sim_in.models.kickMapReport),
            run_dir=run_dir,
        )
    if 'fieldLineoutReport' in sim_in.report:
        beam_axis = sim_in.models.simulation.beamAxis
        v_axis = sim_in.models.undulator.gapAxis if \
            sim_in.models.simulation.magnetType == 'undulator' else \
            _GAP_AXIS_MAP[beam_axis]
        h_axis = next(iter(set(_AXES) - {beam_axis, v_axis}))
        template_common.write_sequential_result(
            _field_lineout_plot(
                sim_in.simulationId,
                sim_in.models.simulation.name,
                sim_in.models.fieldLineoutReport.fieldType,
                sim_in.models.fieldLineoutReport.fieldPath,
                beam_axis,
                v_axis,
                h_axis
            ),
            run_dir=run_dir,
        )
Example #18
0
def prepare_sequential_output_file(run_dir, data):
    if data.report == 'fieldComparisonReport' or data.report == 'fieldReport':
        fn = simulation_db.json_filename(template_common.OUTPUT_BASE_NAME, run_dir)
        if fn.exists():
            fn.remove()
            if data.report == 'fieldComparisonReport':
                template_common.write_sequential_result(
                    generate_field_comparison_report(data, run_dir),
                    run_dir=run_dir,
                )
            else:
                template_common.write_sequential_result(
                    generate_field_report(data, run_dir),
                    run_dir=run_dir,
                )
Example #19
0
def run(cfg_dir):
    """Run Hellweg in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run hellweg in
    """
    _run_hellweg(cfg_dir)
    sim_in = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    r = sim_in.report.replace
    template_common.write_sequential_result(
        template_common.sim_frame_dispatch(
            copy.deepcopy(sim_in.models[r]).pkupdate(
                frameReport=r.replace('Report', 'Animation'),
                run_dir=pkio.py_path(cfg_dir),
                sim_in=sim_in,
            ),
        ),
    )
Example #20
0
File: srw.py Project: ahebnl/Sirepo
def run(cfg_dir):
    """Run srw in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run srw in
    """
    sim_in = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    r = template_common.exec_parameters()
    # special case for importing python code
    m = sim_in.report
    if m == 'backgroundImport':
        template_common.write_sequential_result(
            PKDict({sirepo.template.srw.PARSED_DATA_ATTR: r.parsed_data}))
    else:
        template_common.write_sequential_result(
            sirepo.template.srw.extract_report_data(
                sirepo.template.srw.get_filename_for_model(m),
                sim_in,
            ), )
Example #21
0
def extract_report_data(run_dir, sim_in):
    if 'mlModelGraph' in sim_in.report:
        svg = pkio.read_text('modelGraph.svg')
        template_common.write_sequential_result(
            PKDict(svg=svg),
            run_dir=run_dir,
        )
        return
    if sim_in.report == 'partitionSelectionReport':
        template_common.write_sequential_result(
            _extract_partition_selection(run_dir, sim_in),
            run_dir=run_dir,
        )
        return
    assert 'fileColumnReport' in sim_in.report
    idx = sim_in.models[sim_in.report].columnNumber
    x, y, col_name, source_name = _extract_column(run_dir, sim_in, idx)
    template_common.write_sequential_result(
        _plot_info(
            x,
            [
                PKDict(
                    points=y.tolist(),
                    label='',
                ),
            ],
            col_name,
        ),
        run_dir=run_dir,
    )
Example #22
0
def run(cfg_dir):
    """Run code in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run code in
    """
    template = sirepo.template.import_module(pkinspect.module_basename(run))
    _run_code()
    a = PKDict(
        # see template.warppba.open_data_file (opens last frame)
        frameIndex=None,
        run_dir=pkio.py_path(cfg_dir),
        sim_in=simulation_db.read_json(template_common.INPUT_BASE_NAME),
    )
    a.frameReport = a.sim_in.report
    a.update(a.sim_in.models[a.frameReport])
    if a.frameReport == 'laserPreviewReport':
        res = template.sim_frame_fieldAnimation(a)
    elif a.frameReport == 'beamPreviewReport':
        res = template.sim_frame_beamAnimation(a)
    else:
        raise AssertionError('invalid report: {}'.format(a.frameReport))
    template_common.write_sequential_result(res)
Example #23
0
def run(cfg_dir):
    """Run shadow in ``cfg_dir``

    Args:
        cfg_dir (str): directory to run shadow in
    """
    beam = template_common.exec_parameters().beam
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    model = data['models'][data['report']]
    column_values = _SCHEMA['enum']['ColumnValue']

    if 'y' in model:
        x_range = None
        y_range = None
        if model['overrideSize'] == '1':
            x_range = (numpy.array([
                model['horizontalOffset'] - model['horizontalSize'] / 2,
                model['horizontalOffset'] + model['horizontalSize'] / 2,
            ]) * _MM_TO_CM).tolist()
            y_range = (numpy.array([
                model['verticalOffset'] - model['verticalSize'] / 2,
                model['verticalOffset'] + model['verticalSize'] / 2,
            ]) * _MM_TO_CM).tolist()
        ticket = beam.histo2(int(model['x']),
                             int(model['y']),
                             nbins=template_common.histogram_bins(
                                 model['histogramBins']),
                             ref=int(model['weight']),
                             nolost=1,
                             calculate_widths=0,
                             xrange=x_range,
                             yrange=y_range)
        _scale_ticket(ticket)
        values = ticket['histogram'].T
        assert not numpy.isnan(values).any(), 'nan values found'
        res = {
            'x_range':
            [ticket['xrange'][0], ticket['xrange'][1], ticket['nbins_h']],
            'y_range':
            [ticket['yrange'][0], ticket['yrange'][1], ticket['nbins_v']],
            'x_label':
            _label_with_units(model['x'], column_values),
            'y_label':
            _label_with_units(model['y'], column_values),
            'z_label':
            'Frequency',
            'title':
            u'{}, {}'.format(_label(model['x'], column_values),
                             _label(model['y'], column_values)),
            'z_matrix':
            values.tolist(),
            'frameCount':
            1,
        }
    else:
        weight = int(model['weight'])
        ticket = beam.histo1(int(model['column']),
                             nbins=template_common.histogram_bins(
                                 model['histogramBins']),
                             ref=weight,
                             nolost=1,
                             calculate_widths=0)
        _scale_ticket(ticket)
        res = {
            'title':
            _label(model['column'], column_values),
            'x_range':
            [ticket['xrange'][0], ticket['xrange'][1], ticket['nbins']],
            'y_label':
            u'{}{}'.format(
                'Number of Rays', u' weighted by {}'.format(
                    _label_for_weight(model['weight'], column_values))
                if weight else ''),
            'x_label':
            _label_with_units(model['column'], column_values),
            'points':
            ticket['histogram'].T.tolist(),
            'frameCount':
            1,
        }
        #pkdlog('range amount: {}', res['x_range'][1] - res['x_range'][0])
        #1.55431223448e-15
        dist = res['x_range'][1] - res['x_range'][0]
        #TODO(pjm): only rebalance range if outside of 0
        if dist < 1e-14:
            #TODO(pjm): include offset range for client
            res['x_range'][0] = 0
            res['x_range'][1] = dist
    template_common.write_sequential_result(res)
Example #24
0
def save_sequential_report_data(data, run_dir):
    template_common.write_sequential_result(
        _extract_report_data(data, run_dir),
        run_dir=run_dir,
    )
Example #25
0
File: ml.py Project: cchall/sirepo
def _write_report(x, plots, title=''):
    template_common.write_sequential_result(_report_info(x, plots, title))
Example #26
0
def _run_dvh(data, cfg_dir):
    dvh_report = data['models']['dvhReport']
    assert dvh_report['roiNumbers'], 'No selection'
    y_range = None
    plots = []
    max_x = 0
    for roi_number in data['models']['dvhReport']['roiNumbers']:
        roi_number = int(roi_number)
        dp = dicomparser.DicomParser(
            _parent_file(cfg_dir, template.RTSTRUCT_EXPORT_FILENAME))
        for roi in dp.ds.ROIContourSequence:
            if roi.ReferencedROINumber == roi_number:
                for c in roi.ContourSequence:
                    if 'ContourImageSequence' not in c:
                        c.ContourImageSequence = []
        s = dp.GetStructures()[roi_number]
        s['planes'] = dp.GetStructureCoordinates(roi_number)
        s['thickness'] = dp.CalculatePlaneThickness(s['planes'])

        rtdose = dicomparser.DicomParser(
            _parent_file(cfg_dir, template._DOSE_DICOM_FILE))
        calcdvh = dvhcalc.calculate_dvh(s, rtdose, None, True, None)
        counts = np.append(calcdvh.histogram, 0.0)
        if dvh_report['dvhType'] == 'cumulative':
            counts = counts[::-1].cumsum()[::-1]
        else:
            counts = np.append(abs(np.diff(counts) * -1), [0])
        if dvh_report['dvhVolume'] == 'relative':
            if dvh_report['dvhType'] == 'differential':
                counts = counts[::-1].cumsum()[::-1]
            if counts.any() and counts.max() > 0:
                counts = 100 * counts / counts.max()
            if dvh_report['dvhType'] == 'differential':
                counts = np.append(abs(np.diff(counts) * -1), [0])
        else:
            counts /= 10
        max_x = max(max_x, counts.size)
        min_y = np.min(counts)
        max_y = np.max(counts)
        if y_range:
            if min_y < y_range[0]:
                y_range[0] = min_y
            if max_y > y_range[1]:
                y_range[1] = max_y
        else:
            y_range = [min_y, max_y]
        rgb = s['color']
        plots.append({
            'points':
            counts.tolist(),
            'color':
            '#{:02x}{:02x}{:02x}'.format(int(rgb[0]), int(rgb[1]),
                                         int(rgb[2])),
            'label':
            s['name'],
        })
    res = {
        'title':
        '',
        'x_range': [0, max_x / 100.0, max_x],
        'y_label':
        'Volume [{}]'.format('%' if dvh_report['dvhVolume'] ==
                             'relative' else 'm³'),
        'x_label':
        'Dose [gy]',
        'y_range':
        y_range,
        'plots':
        sorted(plots, key=lambda v: v['label'].lower()),
    }
    template_common.write_sequential_result(res)