Beispiel #1
0
def _extract_evolution_plot(run_dir, data, model_data):
    frame_index = int(data['frameIndex'])
    args = template_common.parse_animation_args(
        data,
        {
            '': ['y1', 'y2', 'y3', 'startTime'],
        },
    )
    datfile = np.loadtxt(str(run_dir.join(_GRID_EVOLUTION_FILE)))
    stride = 20
    x = datfile[::stride, 0]
    plots = []
    for plot in _PLOT_COLUMNS[model_data.models.simulation.get(
            'flashType', 'RTFlame')]:
        plots.append({
            'name': plot[0],
            'label': plot[0],
            'points': datfile[::stride, plot[1]].tolist(),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': 'time [s]',
        'x_points': x.tolist(),
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #2
0
def run(cfg_dir):
    with pkio.save_chdir(cfg_dir):
        exec(pkio.read_text(template_common.PARAMETERS_PYTHON_FILE), locals(),
             locals())
    data = simulation_db.read_json(template_common.INPUT_BASE_NAME)
    if data['report'] == 'dogReport':
        dog = data.models.dog
        max_age = _max_age(dog.weight)
        x = np.linspace(0, max_age, int(max_age) + 1).tolist()
        plots = [
            _plot(dog, 'height', x),
            _plot(dog, 'weight', x),
        ]
        res = {
            'title': 'Dog Height and Weight Over Time',
            'x_range': [0, max_age],
            'y_label': '',
            'x_label': 'Age (years)',
            'x_points': x,
            'plots': plots,
            'y_range': template_common.compute_plot_color_and_range(plots),
        }
    else:
        raise RuntimeError('unknown report: {}'.format(data['report']))
    simulation_db.write_result(res)
Beispiel #3
0
def _extract_twiss_report(data):
    report = data['models'][data['report']]
    report['x'] = 's'
    report['y1'] = 'betx'
    report['y2'] = 'bety'
    report['y3'] = 'dx'
    #TODO(pjm): global twiss file name
    col_names, rows = _read_data_file('out.twiss')
    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]),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': '{} [{}]'.format(report['x'], 'm'),
        'x_points': x,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #4
0
def _run_twiss_report(data, report_name):
    x = None
    plots = []
    report = data['models'][report_name]
    with h5py.File(template.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':
                template.label(report[yfield],
                               _SCHEMA['enum']['TwissParameter']),
                'points':
                f[name][:].tolist(),
            })
    return {
        '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,
    }
Beispiel #5
0
def save_report_data(data, run_dir):
    report_name = data['report']
    error = ''
    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))
        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]))
            plots.append({
                'points':
                points,
                'label':
                template_common.enum_text(_SCHEMA, enum_name, report[f]),
            })
        #TODO(pjm): use template_common
        x = column_data('sums', col_names, rows)
        res = {
            '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 = {
            #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'] = {
                'bunch': simulation_db.read_json(summary_file)
            }
    else:
        raise RuntimeError('unknown report: {}'.format(report_name))
    if error:
        res = {
            'error': error,
        }
    simulation_db.write_result(
        res,
        run_dir=run_dir,
    )
Beispiel #6
0
def extract_report_data(xFilename, data, page_index, page_count=0):
    xfield = data['x'] if 'x' in data else data[_X_FIELD]
    # x, column_names, x_def, err
    x_col = sdds_util.extract_sdds_column(xFilename, xfield, page_index)
    if x_col['err']:
        return x_col['err']
    x = x_col['values']
    if not _is_histogram_file(xFilename, x_col['column_names']):
        # parameter plot
        plots = []
        filename = {
            'y1': xFilename,
            #TODO(pjm): y2Filename, y3Filename are not currently used. Would require rescaling x value across files.
            'y2': xFilename,
            'y3': xFilename,
        }
        for f in ('y1', 'y2', 'y3'):
            if re.search(r'^none$', data[f], re.IGNORECASE) or data[f] == ' ':
                continue
            yfield = data[f]
            y_col = sdds_util.extract_sdds_column(filename[f], yfield,
                                                  page_index)
            if y_col['err']:
                return y_col['err']
            y = y_col['values']
            plots.append({
                'points': y,
                'label': _field_label(yfield, y_col['column_def'][1]),
            })
        title = ''
        if page_count > 1:
            title = 'Plot {} of {}'.format(page_index + 1, page_count)
        return {
            'title': title,
            'x_range': [min(x), max(x)],
            'y_label': '',
            'x_label': _field_label(xfield, x_col['column_def'][1]),
            'x_points': x,
            'plots': plots,
            'y_range': template_common.compute_plot_color_and_range(plots),
        }
    yfield = data['y1'] if 'y1' in data else data['y']
    y_col = sdds_util.extract_sdds_column(xFilename, yfield, page_index)
    if y_col['err']:
        return y_col['err']
    y = y_col['values']
    bins = data['histogramBins']
    hist, edges = np.histogramdd([x, y], template_common.histogram_bins(bins))
    return {
        'x_range': [float(edges[0][0]),
                    float(edges[0][-1]),
                    len(hist)],
        'y_range': [float(edges[1][0]),
                    float(edges[1][-1]),
                    len(hist[0])],
        'x_label': _field_label(xfield, x_col['column_def'][1]),
        'y_label': _field_label(yfield, y_col['column_def'][1]),
        'title': _plot_title(xfield, yfield, page_index, page_count),
        'z_matrix': hist.T.tolist(),
    }
Beispiel #7
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,
    )
Beispiel #8
0
def _extract_evolution_plot(report, run_dir):
    plots = []
    with h5py.File(str(run_dir.join(OUTPUT_FILE['beamEvolutionAnimation'])), 'r') as f:
        x = f['s'][:].tolist()
        for yfield in ('y1', 'y2', 'y3'):
            if report[yfield] == 'none':
                continue
            points = _plot_values(f, report[yfield])
            for v in points:
                if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                    return parse_error_log(run_dir) or {
                        'error': 'Invalid data computed',
                    }
            plots.append({
                'points': points,
                'label': label(report[yfield], _SCHEMA['enum']['BeamColumn']),
            })
        return {
            '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),
        }
Beispiel #9
0
def _extract_twiss_report(data):
    report = data['models'][data['report']]
    report['x'] = _X_FIELD
    values = madx_parser.parse_tfs_file(
        _elegant_to_madx(data['models']['ring']))
    # special case if dy and/or dpy are missing, default to 0s
    for opt_col in ('dy', 'dpy'):
        if opt_col not in values and 'dx' in values:
            values[opt_col] = ['0'] * len(values['dx'])
    x = _float_list(values[report['x']])
    y_range = None
    plots = []
    for f in ('y1', 'y2', 'y3'):
        if report[f] == 'none':
            continue
        plots.append({
            'points':
            _float_list(values[report[f]]),
            'label':
            '{} [{}]'.format(report[f], _FIELD_UNITS[report[f]])
            if report[f] in _FIELD_UNITS else report[f],
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': '{} [{}]'.format(report['x'], 'm'),
        'x_points': x,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #10
0
def sim_frame_beamEvolutionAnimation(frame_args):
    plots = []
    n = str(frame_args.run_dir.join(OUTPUT_FILE.beamEvolutionAnimation))
    with h5py.File(n, 'r') as f:
        x = f['s'][:].tolist()
        for yfield in ('y1', 'y2', 'y3'):
            if frame_args[yfield] == 'none':
                continue
            points = _plot_values(f, frame_args[yfield])
            for v in points:
                if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                    return _parse_synergia_log(frame_args.run_dir) or PKDict(
                        error='Invalid data computed',
                    )
            plots.append(PKDict(
                points=points,
                label=label(frame_args[yfield], _SCHEMA.enum.BeamColumn),
            ))
        return 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),
        )
Beispiel #11
0
def sim_frame_beamEvolutionAnimation(frame_args):
    filename = str(frame_args.run_dir.join(_BEAM_EVOLUTION_OUTPUT_FILENAME))
    x_col = sdds_util.extract_sdds_column(filename, _X_FIELD, 0)
    if x_col['err']:
        return x_col['err']
    x = x_col['values']
    plots = []
    for f in ('y1', 'y2', 'y3'):
        if frame_args[f] == 'none':
            continue
        yfield = _map_field_name(frame_args[f])
        y_col = sdds_util.extract_sdds_column(filename, yfield, 0)
        if y_col['err']:
            return y_col['err']
        plots.append({
            'points':
            y_col['values'],
            'label':
            '{}{}'.format(
                _field_label(yfield, y_col['column_def']),
                _field_description(yfield, frame_args.sim_in),
            ),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': _field_label(_X_FIELD, x_col['column_def']),
        'x_points': x,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #12
0
def _extract_evolution_plot(report, run_dir):
    filename = str(run_dir.join(_BEAM_EVOLUTION_OUTPUT_FILENAME))
    data = simulation_db.read_json(run_dir.join(template_common.INPUT_BASE_NAME))
    x_col = sdds_util.extract_sdds_column(filename, _X_FIELD, 0)
    if x_col['err']:
        return x_col['err']
    x = x_col['values']
    plots = []
    for f in ('y1', 'y2', 'y3'):
        if report[f] == 'none':
            continue
        yfield = _map_field_name(report[f])
        y_col = sdds_util.extract_sdds_column(filename, yfield, 0)
        if y_col['err']:
            return y_col['err']
        plots.append({
            'points': y_col['values'],
            'label': '{}{}'.format(_field_label(yfield, y_col['column_def']), _field_description(yfield, data)),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': _field_label(_X_FIELD, x_col['column_def']),
        'x_points': x,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #13
0
def _extract_evolution_plot(report, run_dir):
    plots = []
    with h5py.File(str(run_dir.join(OUTPUT_FILE['beamEvolutionAnimation'])),
                   'r') as f:
        x = f['s'][:].tolist()
        for yfield in ('y1', 'y2', 'y3'):
            if report[yfield] == 'none':
                continue
            points = _plot_values(f, report[yfield])
            for v in points:
                if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                    return parse_error_log(run_dir) or {
                        'error': 'Invalid data computed',
                    }
            plots.append({
                'points':
                points,
                'label':
                label(report[yfield], _SCHEMA['enum']['BeamColumn']),
            })
        return {
            '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),
        }
Beispiel #14
0
def _crystal_plot(frame_args, x_column, y_column, x_heading, scale):
    x = None
    plots = []
    with open(str(frame_args.run_dir.join(_CRYSTAL_CSV_FILE))) as f:
        for r in csv.reader(f):
            if x is None and r[0] == x_column:
                r.pop(0)
                r.pop(0)
                x = [float(v) * scale for v in r]
            elif r[0] == y_column:
                r.pop(0)
                t = r.pop(0)
                plots.append(
                    PKDict(
                        points=[float(v) for v in r],
                        label='{:.1f} sec'.format(float(t)),
                    ))
    return PKDict(
        title='',
        x_range=[min(x), max(x)],
        y_label='Temperature [°C]',
        x_label=x_heading,
        x_points=x,
        plots=plots,
        y_range=template_common.compute_plot_color_and_range(plots),
        summaryData=_summary_data(frame_args),
    )
Beispiel #15
0
def _extract_twiss_report(data):
    report = data['models'][data['report']]
    report['x'] = _X_FIELD
    values = _parse_madx(_elegant_to_madx(data['models']['ring']))
    x = _float_list(values[report['x']])
    y_range = None
    plots = []
    for f in ('y1', 'y2', 'y3'):
        if report[f] == 'none':
            continue
        plots.append({
            'points':
            _float_list(values[report[f]]),
            'label':
            '{} [{}]'.format(report[f], _FIELD_UNITS[report[f]])
            if report[f] in _FIELD_UNITS else report[f],
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': '{} [{}]'.format(report['x'], 'm'),
        'x_points': x,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #16
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)
Beispiel #17
0
def save_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 = template_common.lib_file_name('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'])]
        hist, edges = np.histogramdd([x, y], template_common.histogram_bins(report['histogramBins']))
        res = {
            'title': '',
            'x_range': [float(edges[0][0]), float(edges[0][-1]), len(hist)],
            'y_range': [float(edges[1][0]), float(edges[1][-1]), len(hist[0])],
            'x_label': label(report['x'], _SCHEMA['enum']['PhaseSpaceCoordinate8']),
            'y_label': label(report['y'], _SCHEMA['enum']['PhaseSpaceCoordinate8']),
            'z_matrix': hist.T.tolist(),
            '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,
        }
    simulation_db.write_result(res, run_dir=run_dir)
Beispiel #18
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)
Beispiel #19
0
def _plot_info(x, plots, title=''):
    return PKDict(
        title=title,
        x_range=[float(min(x)), float(max(x))],
        y_label='',
        x_label='',
        x_points=x.tolist(),
        plots=plots,
        y_range=template_common.compute_plot_color_and_range(plots),
    )
Beispiel #20
0
def extract_report_data(xFilename, data, page_index, page_count=0):
    xfield = data['x'] if 'x' in data else data[_X_FIELD]
    # x, column_names, x_def, err
    x_col = sdds_util.extract_sdds_column(xFilename, xfield, page_index)
    if x_col['err']:
        return x_col['err']
    x = x_col['values']
    if not _is_histogram_file(xFilename, x_col['column_names']):
        # parameter plot
        plots = []
        filename = {
            'y1': xFilename,
            #TODO(pjm): y2Filename, y3Filename are not currently used. Would require rescaling x value across files.
            'y2': xFilename,
            'y3': xFilename,
        }
        for f in ('y1', 'y2', 'y3'):
            if re.search(r'^none$', data[f], re.IGNORECASE) or data[f] == ' ':
                continue
            yfield = data[f]
            y_col = sdds_util.extract_sdds_column(filename[f], yfield, page_index)
            if y_col['err']:
                return y_col['err']
            y = y_col['values']
            plots.append({
                'points': y,
                'label': _field_label(yfield, y_col['column_def'][1]),
            })
        title = ''
        if page_count > 1:
            title = 'Plot {} of {}'.format(page_index + 1, page_count)
        return {
            'title': title,
            'x_range': [min(x), max(x)],
            'y_label': '',
            'x_label': _field_label(xfield, x_col['column_def'][1]),
            'x_points': x,
            'plots': plots,
            'y_range': template_common.compute_plot_color_and_range(plots),
        }
    yfield = data['y1'] if 'y1' in data else data['y']
    y_col = sdds_util.extract_sdds_column(xFilename, yfield, page_index)
    if y_col['err']:
        return y_col['err']
    y = y_col['values']
    bins = data['histogramBins']
    hist, edges = np.histogramdd([x, y], template_common.histogram_bins(bins))
    return {
        'x_range': [float(edges[0][0]), float(edges[0][-1]), len(hist)],
        'y_range': [float(edges[1][0]), float(edges[1][-1]), len(hist[0])],
        'x_label': _field_label(xfield, x_col['column_def'][1]),
        'y_label': _field_label(yfield, y_col['column_def'][1]),
        'title': _plot_title(xfield, yfield, page_index, page_count),
        'z_matrix': hist.T.tolist(),
    }
Beispiel #21
0
def _report(title, fields, data):
    dog = data.models.dog
    cols = _csv_to_cols()
    x_points = cols['year']
    plots = [_plot(dog, f, cols) for f in fields]
    return {
        'title': title,
        'x_range': [x_points[0], x_points[-1]],
        'y_label': _label(fields[0]) if len(fields) == 1 else '',
        'x_label': 'Age (years)',
        'x_points': x_points,
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #22
0
def _run_beam_statistics(cfg_dir, data):
    template_common.exec_parameters()
    report = data.models.beamStatisticsReport
    d = pkjson.load_any(py.path.local(cfg_dir).join(template.BEAM_STATS_FILE))
    x = [record.s for record in d]
    plots = []

    for y in ('y1', 'y2', 'y3'):
        if report[y] == 'none':
            continue
        label = report[y]
        if label in ('sigmax', 'sigmaz'):
            label += ' [m]'
        elif label in ('sigdix', 'sigdiz', 'angxz', 'angxpzp'):
            label += ' [rad]'
        plots.append(PKDict(
            field=report[y],
            label=label,
        ))
    for item in d:
        for p in plots:
            if p.field == 'angxz':
                sigmax = numpy.array(_beam_statistics_values(d, 'sigmax'))
                sigmaz = numpy.array(_beam_statistics_values(d, 'sigmaz'))
                sigmaxz = numpy.array(_beam_statistics_values(d, 'sigmaxz'))
                p.points = ((1 / 2) *
                            numpy.arctan(2.e-4 * sigmaxz /
                                         (sigmax**2 - sigmaz**2))).tolist()
            elif p.field == 'angxpzp':
                sigdix = numpy.array(_beam_statistics_values(d, 'sigdix'))
                sigdiz = numpy.array(_beam_statistics_values(d, 'sigdiz'))
                sigmaxpzp = numpy.array(_beam_statistics_values(
                    d, 'sigmaxpzp'))
                p.points = ((1 / 2) *
                            numpy.arctan(2 * sigmaxpzp /
                                         (sigdix**2 - sigdiz**2))).tolist()
            else:
                p.points = _beam_statistics_values(d, p.field)
    return PKDict(
        aspectRatio=0.3,
        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),
    )
Beispiel #23
0
def sim_frame_wavefrontSummaryAnimation(frame_args):
    beamline = frame_args.sim_in.models.beamline
    if 'element' not in frame_args:
        frame_args.element = 'all'
    idx = 0
    title = ''
    if frame_args.element != 'all':
        # find the element index from the element id
        for item in beamline:
            if item.id == int(frame_args.element):
                title = item.title
                break
            idx += 1
    #TODO(pjm): use column headings from csv
    cols = ['count', 'pos', 'sx', 'sy', 'xavg', 'yavg']
    v = np.genfromtxt(str(frame_args.run_dir.join(_SUMMARY_CSV_FILE)),
                      delimiter=',',
                      skip_header=1)
    if frame_args.element != 'all':
        # the wavefront csv include intermediate values, so take every other row
        counts = _counts_for_beamline(int((v[-1][0] + 1) / 2), beamline)[1]
        v2 = []
        for row in counts[idx]:
            v2.append(v[(row - 1) * 2])
        v = np.array(v2)
    #TODO(pjm): generalize, use template_common parameter_plot()?
    plots = []
    for col in ('sx', 'sy'):
        plots.append(
            PKDict(
                points=v[:, cols.index(col)].tolist(),
                label=f'{col} [m]',
            ))
    x = v[:, cols.index('pos')].tolist()
    return PKDict(
        aspectRatio=1 / 5.0,
        title='{} Wavefront Dimensions'.format(title),
        x_range=[float(min(x)), float(max(x))],
        y_label='',
        x_label='s [m]',
        x_points=x,
        plots=plots,
        y_range=template_common.compute_plot_color_and_range(plots),
        summaryData=_summary_data(frame_args),
    )
Beispiel #24
0
def sim_frame_turnComparisonAnimation(frame_args):
    turn_count = frame_args.sim_in.models.simulationSettings.turn_count
    plots = []
    with h5py.File(
            str(frame_args.run_dir.join(OUTPUT_FILE.beamEvolutionAnimation)),
            'r') as f:
        x = f['s'][:].tolist()
        points = _plot_values(f, frame_args.y)
        for v in points:
            if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                return _parse_synergia_log(frame_args.run_dir) or {
                    'error': 'Invalid data computed',
                }
        steps = (len(points) - 1) / turn_count
        x = x[0:int(steps + 1)]
        if not frame_args.turn1 or int(frame_args.turn1) > turn_count:
            frame_args.turn1 = 1
        if not frame_args.turn2 or int(frame_args.turn2) > turn_count or int(
                frame_args.turn1) == int(frame_args.turn2):
            frame_args.turn2 = turn_count
        for yfield in ('turn1', 'turn2'):
            turn = int(frame_args[yfield])
            p = points[int((turn - 1) * steps):int((turn - 1) * steps + steps +
                                                   1)]
            if not p:
                return {
                    'error': 'Simulation data is not yet available',
                }
            plots.append({
                'points':
                p,
                'label':
                '{} turn {}'.format(
                    label(frame_args.y, _SCHEMA.enum.BeamColumn), turn),
            })
        return {
            '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),
        }
Beispiel #25
0
def save_report_data(data, run_dir):
    report_name = data['report']
    if 'twissReport' in report_name or 'opticsReport' in report_name:
        filename, enum_name, x_field = _REPORT_INFO[report_name]
        report = data['models'][report_name]
        plots = []
        col_names, rows = read_data_file(py.path.local(run_dir).join(filename))
        for f in ('y1', 'y2', 'y3'):
            if report[f] == 'none':
                continue
            plots.append({
                'points':
                column_data(report[f], col_names, rows),
                'label':
                template_common.enum_text(_SCHEMA, enum_name, report[f]),
            })
        x = column_data(x_field, col_names, rows)
        res = {
            '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),
        }
    elif 'bunchReport' in report_name:
        report = data['models'][report_name]
        col_names, rows = read_data_file(
            py.path.local(run_dir).join(_ZGOUBI_DATA_FILE))
        res = _extract_bunch_data(report, col_names, rows, '')
        summary_file = py.path.local(run_dir).join(BUNCH_SUMMARY_FILE)
        if summary_file.exists():
            res['summaryData'] = {
                'bunch': simulation_db.read_json(summary_file)
            }
    else:
        raise RuntimeError('unknown report: {}'.format(report_name))
    simulation_db.write_result(
        res,
        run_dir=run_dir,
    )
Beispiel #26
0
def _extract_turn_comparison_plot(report, run_dir, turn_count):
    plots = []
    with h5py.File(str(run_dir.join(OUTPUT_FILE['beamEvolutionAnimation'])),
                   'r') as f:
        x = f['s'][:].tolist()
        points = _plot_values(f, report['y'])
        for v in points:
            if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                return parse_error_log(run_dir) or {
                    'error': 'Invalid data computed',
                }
        steps = (len(points) - 1) / turn_count
        x = x[0:int(steps + 1)]
        if not report['turn1'] or int(report['turn1']) > turn_count:
            report['turn1'] = 1
        if not report['turn2'] or int(report['turn2']) > turn_count or int(
                report['turn1']) == int(report['turn2']):
            report['turn2'] = turn_count
        for yfield in ('turn1', 'turn2'):
            turn = int(report[yfield])
            p = points[int((turn - 1) * steps):int((turn - 1) * steps + steps +
                                                   1)]
            if not len(p):
                return {
                    'error': 'Simulation data is not yet available',
                }
            plots.append({
                'points':
                p,
                'label':
                '{} turn {}'.format(
                    label(report['y'], _SCHEMA['enum']['BeamColumn']), turn),
            })
        return {
            '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),
        }
Beispiel #27
0
def sim_frame_oneDimensionProfileAnimation(frame_args):
    # def _interpolate_max(files):
    #     m = -1
    #     for f in files:
    #         d = yt.load(f)
    #         m = max(d.domain_width[0] + d.index.grid_left_edge[0][0], m)
    #     return m

    def _files():
        if frame_args.selectedPlotFiles:
            return sorted([str(frame_args.run_dir.join(f)) for f in frame_args.selectedPlotFiles.split(',')])
        return [str(_h5_file_list(frame_args.run_dir)[-1])]

    #_init_yt()
    plots = []
    x_points = []
    f = _files()
    xs, ys, times = rsflash.plotting.extracts.get_lineouts(
        f,
        frame_args.var,
        frame_args.axis,
        _LINEOUTS_SAMPLING_SIZE,
        # interpolate_max=_interpolate_max(f),
    )
    x = xs[0]
    for i, _ in enumerate(ys):
        assert x.all() == xs[i].all(), 'Plots must use the same x values'
        y = ys[i]
        plots.append(PKDict(
            name=i,
            label=_time_and_units(times[i]),
            points=y.tolist(),
        ))
    return PKDict(
        plots=plots,
        title=frame_args.var,
        x_label=_PLOT_VARIABLE_LABELS.length,
        x_points = x.tolist(),
        x_range=[np.min(x), np.max(x)],
        y_label=_PLOT_VARIABLE_LABELS.get(frame_args.var, ''),
        y_range=template_common.compute_plot_color_and_range(plots),
    )
Beispiel #28
0
def sim_frame_gridEvolutionAnimation(frame_args):
    dat = np.loadtxt(str(frame_args.run_dir.join(_GRID_EVOLUTION_FILE)))
    stride = 20
    x = dat[::stride, 0]
    plots = []
    for plot in _PLOT_COLUMNS[
            frame_args.sim_in.models.simulation['flashType']]:
        plots.append({
            'name': plot[0],
            'label': plot[0],
            'points': dat[::stride, plot[1]].tolist(),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': 'time [s]',
        'x_points': x.tolist(),
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #29
0
def save_report_data(data, run_dir):
    report_name = data['report']
    if 'twissReport' in report_name or 'opticsReport' in report_name:
        filename, enum_name, x_field = _REPORT_INFO[report_name]
        report = data['models'][report_name]
        plots = []
        col_names, rows = read_data_file(py.path.local(run_dir).join(filename))
        for f in ('y1', 'y2', 'y3'):
            if report[f] == 'none':
                continue
            plots.append({
                'points': column_data(report[f], col_names, rows),
                'label': template_common.enum_text(_SCHEMA, enum_name, report[f]),
            })
        x = column_data(x_field, col_names, rows)
        res = {
            '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),
        }
    elif 'bunchReport' in report_name:
        report = data['models'][report_name]
        col_names, rows = read_data_file(py.path.local(run_dir).join(_ZGOUBI_DATA_FILE))
        res = _extract_bunch_data(report, col_names, rows, '')
        summary_file = py.path.local(run_dir).join(BUNCH_SUMMARY_FILE)
        if summary_file.exists():
            res['summaryData'] = {
                'bunch': simulation_db.read_json(summary_file)
            }
    else:
        raise RuntimeError('unknown report: {}'.format(report_name))
    simulation_db.write_result(
        res,
        run_dir=run_dir,
    )
Beispiel #30
0
def sim_frame_gridEvolutionAnimation(frame_args):
    c = _grid_evolution_columns(frame_args.run_dir)
    dat = np.loadtxt(str(frame_args.run_dir.join(_GRID_EVOLUTION_FILE)))
    stride = 20
    x = dat[::stride, 0]
    plots = []
    for v in 'y1', 'y2', 'y3':
        n = frame_args[v]
        plots.append({
            'name': n,
            'label': n,
            'points': dat[::stride, c.index(n)].tolist(),
        })
    return {
        'title': '',
        'x_range': [min(x), max(x)],
        'y_label': '',
        'x_label': 'time [s]',
        'x_points': x.tolist(),
        'plots': plots,
        'y_range': template_common.compute_plot_color_and_range(plots),
    }
Beispiel #31
0
def _extract_turn_comparison_plot(report, run_dir, turn_count):
    plots = []
    with h5py.File(str(run_dir.join(OUTPUT_FILE['beamEvolutionAnimation'])), 'r') as f:
        x = f['s'][:].tolist()
        points = _plot_values(f, report['y'])
        for v in points:
            if isinstance(v, float) and (math.isinf(v) or math.isnan(v)):
                return parse_error_log(run_dir) or {
                    'error': 'Invalid data computed',
                }
        steps = (len(points) - 1) / turn_count
        x = x[0:int(steps + 1)]
        if not report['turn1'] or int(report['turn1']) > turn_count:
            report['turn1'] = 1
        if not report['turn2'] or int(report['turn2']) > turn_count or int(report['turn1']) == int(report['turn2']):
            report['turn2'] = turn_count
        for yfield in ('turn1', 'turn2'):
            turn = int(report[yfield])
            p = points[int((turn - 1) * steps):int((turn - 1) * steps + steps + 1)]
            if not len(p):
                return {
                    'error': 'Simulation data is not yet available',
                }
            plots.append({
                'points': p,
                'label': '{} turn {}'.format(label(report['y'], _SCHEMA['enum']['BeamColumn']), turn),
            })
        return {
            '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),
        }
Beispiel #32
0
def sim_frame_wavefrontSummaryAnimation(frame_args):
    cols = ['count', 'pos', 'sx', 'sy', 'xavg', 'yavg']
    v = np.genfromtxt(str(frame_args.run_dir.join(_SUMMARY_CSV_FILE)),
                      delimiter=',',
                      skip_header=1)
    #TODO(pjm): generalize, use template_common parameter_plot()?
    plots = []
    for col in ('sx', 'sy'):
        plots.append(
            PKDict(
                points=v[:, cols.index(col)].tolist(),
                label=f'{col} [m]',
            ))
    x = v[:, cols.index('pos')].tolist()
    return PKDict(
        aspectRatio=1 / 5.0,
        title='Wavefront Dimensions',
        x_range=[float(min(x)), float(max(x))],
        y_label='',
        x_label='s [m]',
        x_points=x,
        plots=plots,
        y_range=template_common.compute_plot_color_and_range(plots),
    )