def combine_scores(scores): scores = {k[:-9]: v for k, v in scores.items() if v} if not scores: return {} s = {} for (k, c), v in co2_utl.stack_nested_keys(scores, depth=2): r = {'models': v['models']} if 'models' in v else {} r.update(v.get('score', {})) co2_utl.get_nested_dicts(s, k, c, default=co2_utl.ret_v(r)) if not co2_utl.are_in_nested_dicts(s, k, 'best'): keys = {'models': 'selected_models', 'success': 'status'} best = dsp_utl.map_dict(keys, dsp_utl.selector(keys, r)) best['from'] = c co2_utl.get_nested_dicts(s, k, 'best', default=co2_utl.ret_v(best)) return {'selections': s, 'scores': scores}
def split_prediction_models( scores, calibrated_models, input_models, cycle_ids=()): sbm, model_sel, par = {}, {}, {} for (k, c), v in dsp_utl.stack_nested_keys(scores, depth=2): r = dsp_utl.selector(['models'], v, allow_miss=True) for m in r.get('models', ()): dsp_utl.get_nested_dicts(par, m, 'calibration')[c] = c r.update(v.get('score', {})) dsp_utl.get_nested_dicts(sbm, k, c, default=co2_utl.ret_v(r)) r = dsp_utl.selector(['success'], r, allow_miss=True) r = dsp_utl.map_dict({'success': 'status'}, r, {'from': c}) dsp_utl.get_nested_dicts(model_sel, k, 'calibration')[c] = r p = {i: dict.fromkeys(input_models, 'input') for i in cycle_ids} models = {i: input_models.copy() for i in cycle_ids} for k, n in sorted(calibrated_models.items()): d = n.get(dsp_utl.NONE, (None, True, {})) for i in cycle_ids: c, s, m = n.get(i, d) if m: s = {'from': c, 'status': s} dsp_utl.get_nested_dicts(model_sel, k, 'prediction')[i] = s models[i].update(m) p[i].update(dict.fromkeys(m, c)) for k, v in dsp_utl.stack_nested_keys(p, ('prediction',), depth=2): dsp_utl.get_nested_dicts(par, k[-1], *k[:-1], default=co2_utl.ret_v(v)) s = { 'param_selections': par, 'model_selections': model_sel, 'score_by_model': sbm, 'scores': scores } return (s,) + tuple(models.get(k, {}) for k in cycle_ids)
def select_outputs(outputs, targets, results): results = dsp_utl.selector(outputs, results, allow_miss=True) results = dsp_utl.map_dict(dict(zip(outputs, targets)), results) it = ((k, results[k]) for k in targets if k in results) return collections.OrderedDict(it)
def _chart2excel(writer, sheet, charts): try: add_chart = writer.book.add_chart m, h, w = 3, 300, 512 for i, (k, v) in enumerate(sorted(charts.items())): chart = add_chart({'type': 'scatter', 'subtype': 'straight'}) for s in v['series']: chart.add_series({ 'name': s['label'], 'categories': _data_ref(s['x']), 'values': _data_ref(s['y']), }) chart.set_size({'width': w, 'height': h}) for s, o in v['set'].items(): eval('chart.set_%s(o)' % s) n = int(i / m) j = i - n * m sheet.insert_chart('A1', chart, { 'x_offset': w * n, 'y_offset': h * j }) except AttributeError: from openpyxl.chart import ScatterChart, Series from xlrd import colname as xl_colname sn = writer.book.get_sheet_names() named_ranges = { '%s!%s' % (sn[d.localSheetId], d.name): d.value for d in writer.book.defined_names.definedName } m, h, w = 3, 7.94, 13.55 for i, (k, v) in enumerate(sorted(charts.items())): chart = ScatterChart() chart.height = h chart.width = w _map = { ('title', 'name'): ('title', ), ('y_axis', 'name'): ('y_axis', 'title'), ('x_axis', 'name'): ('x_axis', 'title'), } _filter = { ('legend', 'position'): lambda x: x[0], } it = { s: _filter[s](o) if s in _filter else o for s, o in dsp_utl.stack_nested_keys(v['set']) } for s, o in dsp_utl.map_dict(_map, it).items(): c = chart for j in s[:-1]: c = getattr(c, j) setattr(c, s[-1], o) for s in v['series']: xvalues = named_ranges[_data_ref(s['x'])] values = named_ranges[_data_ref(s['y'])] series = Series(values, xvalues, title=s['label']) chart.series.append(series) n = int(i / m) j = i - n * m sheet.add_chart(chart, '%s%d' % (xl_colname(8 * n), 1 + 15 * j))