How to use from_results method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

farm_layout.py

Source:farm_layout.py Github

copy

Full Screen

1import json2import numpy as np3import pandas as pd4import matplotlib.pyplot as plt5import foxes.constants as FC6import foxes.variables as FV7from foxes.output.output import Output8class FarmLayoutOutput(Output):9 """10 Plot the farm layout11 Parameters12 ----------13 farm : foxes.WindFarm14 The wind farm15 farm_results : xarray.Dataset, optional16 The wind farm calculation results17 from_results : bool, optional18 Flag for coordinates from results data19 results_state : int, optional20 The state index, for from_res21 D : float, optional22 The rotor diameter, if not from data23 Attributes24 ----------25 farm : foxes.WindFarm26 The wind farm27 fres : xarray.Dataset28 The wind farm calculation results29 from_res : bool30 Flag for coordinates from results data31 results_state : int32 The state index, for from_res33 D : float34 The rotor diameter, if not from data35 """36 def __init__(37 self, farm, farm_results=None, from_results=False, results_state=None, D=None38 ):39 self.farm = farm40 self.fres = farm_results41 self.from_res = from_results42 self.rstate = results_state43 self.D = D44 if from_results and farm_results is None:45 raise ValueError(f"Missing farm_results for switch from_results.")46 if from_results and results_state is None:47 raise ValueError(f"Please specify results_state for switch from_results.")48 def get_layout_data(self):49 """50 Returns wind farm layout.51 Returns52 -------53 numpy.ndarray :54 The wind farm layout, shape:55 (n_turbines, 3) where the 356 represents x, y, h57 """58 data = np.zeros([self.farm.n_turbines, 3], dtype=FC.DTYPE)59 if self.from_res:60 data[:, 0] = self.fres[FV.X][self.rstate]61 data[:, 1] = self.fres[FV.Y][self.rstate]62 data[:, 2] = self.fres[FV.H][self.rstate]63 else:64 for ti, t in enumerate(self.farm.turbines):65 data[ti, :2] = t.xy66 data[ti, 2] = t.H67 return data68 def get_layout_dict(self):69 """70 Returns wind farm layout.71 Returns72 -------73 dict :74 The wind farm layout in dict75 format, as in json output76 """77 data = self.get_layout_data()78 out = {self.farm.name: {}}79 for ti, p in enumerate(data):80 t = self.farm.turbines[ti]81 out[self.farm.name][t.name] = {82 "id": t.index,83 "name": t.name,84 "UTMX": p[0],85 "UTMY": p[1],86 }87 return out88 def get_figure(89 self,90 fontsize=8,91 figsize=None,92 annotate=1,93 title=None,94 fig=None,95 ax=None,96 normalize_D=False,97 ret_im=False,98 **kwargs,99 ):100 """101 Creates farm layout figure.102 Parameters103 ----------104 fontsize : int, optional105 Size of the turbine numbers106 figsize : tuple, optional107 The figsize for plt.Figure108 annotate : int, optional109 Turbine index printing, Choices:110 0 = No annotation111 1 = Turbine indices112 2 = Turbine names113 title : str, optional114 The plot title, or None for automatic115 fig : matplotlib.pyplot.Figure, optional116 The figure object to which to add117 ax : matplotlib.pyplot.Axis, optional118 The axis object, to which to add119 normalize_D : bool120 Normalize x, y wrt rotor diameter121 ret_im : bool122 Flag for returned image object123 **kwargs : dict, optional124 Parameters forwarded to `matplotlib.pyplot.scatter`125 Returns126 -------127 ax : matplotlib.pyplot.Axis128 The axis object129 im : matplotlib.pyplot.PathCollection, optional130 The image object131 """132 if fig is None:133 fig = plt.figure(figsize=figsize)134 ax = fig.add_subplot(111)135 else:136 ax = fig.axes[0] if ax is None else ax137 # for b in self.farm.boundary_geometry:138 # b.add_to_figure(fig, ax)139 D = self.D140 if normalize_D and D is None:141 if self.from_res:142 if self.fres[FV.D].min() != self.fres[FV.D].max():143 raise ValueError(f"Expecting uniform D, found {self.fres[FV.D]}")144 D = self.fres[FV.D][0]145 else:146 D = None147 for ti, t in enumerate(self.farm.turbines):148 hD = t.D149 if D is None:150 D = hD151 elif D != hD:152 raise ValueError(153 f"Turbine {ti} has wrong rotor diameter, expecting D = {D} m, found D = {hD} m"154 )155 if D is None:156 raise ValueError(157 f"Variable '{FV.D}' not found in turbines. Maybe set explicitely, or try from_results?"158 )159 data = self.get_layout_data()160 x = data[:, 0] / D if normalize_D else data[:, 0]161 y = data[:, 1] / D if normalize_D else data[:, 1]162 n = range(len(x))163 kw = {"c": "orange"}164 kw.update(**kwargs)165 im = ax.scatter(x, y, **kw)166 if annotate == 1:167 for i, txt in enumerate(n):168 ax.annotate(int(txt), (x[i], y[i]), size=fontsize)169 elif annotate == 2:170 for i, t in enumerate(self.farm.turbines):171 ax.annotate(t.name, (x[i], y[i]), size=fontsize)172 ti = (173 title174 if title is not None175 else self.farm.name176 if D is None or not normalize_D177 else f"{self.farm.name} (D = {D} m)"178 )179 ax.set_title(ti)180 ax.set_xlabel("x [m]" if not normalize_D else "x [D]")181 ax.set_ylabel("y [m]" if not normalize_D else "y [D]")182 ax.grid()183 # if len(self.farm.boundary_geometry) \184 # or ( min(x) != max(x) and min(y) != max(y) ):185 if min(x) != max(x) and min(y) != max(y):186 ax.set_aspect("equal", adjustable="box")187 ax.autoscale_view(tight=True)188 if ret_im:189 return ax, im190 return ax191 def write_plot(self, file_path=None, fontsize=8, **kwargs):192 """193 Writes the layout plot to file.194 The kwargs are forwarded to self.get_figure195 Parameters196 ----------197 file_path : str198 The file into which to plot, or None199 for default200 fontsize : int201 Size of the turbine numbers202 """203 ax = self.get_figure(fontsize=fontsize, ret_im=False, **kwargs)204 fig = ax.get_figure()205 fname = file_path if file_path is not None else self.farm.name + ".png"206 fig.savefig(fname, bbox_inches="tight")207 plt.close(fig)208 def write_xyh(self, file_path=None):209 """210 Writes xyh layout file.211 Parameters212 ----------213 file_path : str214 The file into which to plot, or None215 for default216 """217 data = self.get_layout_data()218 fname = file_path if file_path is not None else self.farm.name + ".xyh"219 np.savetxt(fname, data, header="x y h")220 def write_csv(self, file_path=None):221 """222 Writes csv layout file.223 Parameters224 ----------225 file_path : str226 The file into which to plot, or None227 for default228 """229 data = self.get_layout_data()230 fname = file_path if file_path is not None else self.farm.name + ".csv"231 lyt = pd.DataFrame(232 index=range(len(data)), columns=["id", "name", "x", "y", "h", "D"]233 )234 lyt.index.name = "index"235 lyt["id"] = [t.info["id"] for t in self.farm.turbines]236 lyt["name"] = [t.info["name"] for t in self.farm.turbines]237 lyt["x"] = data[:, 0]238 lyt["y"] = data[:, 1]239 lyt["h"] = data[:, 2]240 lyt["D"] = [t.D for t in self.farm.turbines]241 lyt.to_csv(fname)242 def write_json(self, file_path=None):243 """244 Writes xyh layout file.245 Parameters246 ----------247 file_path : str248 The file into which to plot, or None249 for default250 """251 data = self.get_layout_dict()252 fname = file_path if file_path is not None else self.farm.name + ".json"253 with open(fname, "w") as outfile:...

Full Screen

Full Screen

test_performance_curves.py

Source:test_performance_curves.py Github

copy

Full Screen

...47 np.testing.assert_almost_equal(curves.tpr(), tp / 9)48 np.testing.assert_almost_equal(curves.fpr(), (10 - tn) / 10)49 @patch("Orange.evaluation.performance_curves.Curves.__init__",50 return_value=None)51 def test_curves_from_results(self, init):52 res = Results()53 ytrue, probs = self.data.T54 res.actual = ytrue.astype(float)55 res.probabilities = np.vstack((1 - probs, probs)).T.reshape(1, -1, 2)56 Curves.from_results(res)57 cytrue, cprobs = init.call_args[0]58 np.testing.assert_equal(cytrue, ytrue)59 np.testing.assert_equal(cprobs, probs)60 Curves.from_results(res, target_class=0)61 cytrue, cprobs = init.call_args[0]62 np.testing.assert_equal(cytrue, 1 - ytrue)63 np.testing.assert_equal(cprobs, 1 - probs)64 res.actual = ytrue.astype(float)65 res.probabilities = np.random.random((2, 19, 2))66 res.probabilities[1] = np.vstack((1 - probs, probs)).T67 Curves.from_results(res, model_index=1)68 cytrue, cprobs = init.call_args[0]69 np.testing.assert_equal(cytrue, ytrue)70 np.testing.assert_equal(cprobs, probs)71 self.assertRaises(ValueError, Curves.from_results, res)72 ytrue[ytrue == 0] = 2 * (np.arange(10) % 2)73 res.actual = ytrue.astype(float)74 res.probabilities = np.random.random((2, 19, 3))75 res.probabilities[1] = np.vstack(76 ((1 - probs) / 3, probs, (1 - probs) * 2 / 3)).T77 Curves.from_results(res, model_index=1, target_class=1)78 cytrue, cprobs = init.call_args[0]79 np.testing.assert_equal(cytrue, ytrue == 1)80 np.testing.assert_equal(cprobs, probs)81 Curves.from_results(res, model_index=1, target_class=0)82 cytrue, cprobs = init.call_args[0]83 np.testing.assert_equal(cytrue, ytrue == 0)84 np.testing.assert_equal(cprobs, (1 - probs) / 3)85 Curves.from_results(res, model_index=1, target_class=2)86 cytrue, cprobs = init.call_args[0]87 np.testing.assert_equal(cytrue, ytrue == 2)88 np.testing.assert_equal(cprobs, (1 - probs) * 2 / 3)89 self.assertRaises(ValueError, Curves.from_results, res, model_index=1)90 @patch("Orange.evaluation.performance_curves.Curves.__init__",91 return_value=None)92 def test_curves_from_results_nans(self, init):93 res = Results()94 ytrue, probs = self.data.T95 ytrue[0] = np.nan96 probs[-1] = np.nan97 res.actual = ytrue.astype(float)98 res.probabilities = np.vstack((1 - probs, probs)).T.reshape(1, -1, 2)99 Curves.from_results(res)100 cytrue, cprobs = init.call_args[0]101 np.testing.assert_equal(cytrue, ytrue[1:-1])...

Full Screen

Full Screen

generate_truncated_json

Source:generate_truncated_json Github

copy

Full Screen

1#!/usr/bin/python2.72"""3Given a full set of results, generate new JSON files with truncated iterations.4MUST be run before plot_truncated_iterations.5"""6import argparse7import os.path8import sys9sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "warmup_stats"))10from warmup.krun_results import read_krun_results_file, write_krun_results_file11MAX_ITERS = 199012MIN_ITERS = 1013ITER_STEP = -1014WINDOW_PROPORTION = 0.1 # Needed for outlier script options.15STEADY_PROPORTION = 0.25 # Needed for changepoint script options.16_BLANK_BENCHMARK = { 'wallclock_times': dict(), # Measurement data.17 'core_cycle_counts': dict(), 'aperf_counts': dict(),18 'mperf_counts': dict(), 'audit': dict(), 'config': '',19 'reboots': 0, 'starting_temperatures': list(),20 'eta_estimates': list(), 'error_flag': list(),21 'all_outliers': dict(), 'unique_outliers': dict(),22 'common_outliers': dict(), }23def parse_krun_file_with_changepoints(json_files):24 """Simplified version of the same function from krun_results."""25 data_dictionary = dict()26 window_size = None27 for filename in json_files:28 assert os.path.exists(filename), 'File %s does not exist.' % filename29 data = read_krun_results_file(filename)30 machine_name = data['audit']['uname'].split(' ')[1]31 if '.' in machine_name: # Remove domain, if there is one.32 machine_name = machine_name.split('.')[0]33 if machine_name not in data_dictionary:34 data_dictionary[machine_name] = data35 else:36 assert 'Found two datasets from machine %s (expected one)' % machine_name37 if window_size is None:38 window_size = data['window_size']39 else:40 assert window_size == data['window_size'], \41 ('Cannot summarise categories generated with different window-size '42 'options. Please re-run the mark_outliers_in_json script.')43 return data['audit'], window_size, data_dictionary44def copy_results(last_iter, audit, window_size, from_results, to_results):45 """Copy results into a new dict. ASSUME only one machine per results file."""46 to_results['audit'] = audit47 to_results['window_size'] = window_size48 for mc in from_results:49 for key in from_results[mc]['wallclock_times']:50 to_results['wallclock_times'][key] = list()51 for p_exec in xrange(len(from_results[mc]['wallclock_times'][key])):52 to_results['wallclock_times'][key].append(from_results[mc]['wallclock_times'][key][p_exec][:last_iter])53def create_outlier_filename(in_file_name, window):54 directory = os.path.dirname(in_file_name)55 basename = os.path.basename(in_file_name)56 if basename.endswith('.json.bz2'):57 root_name = basename[:-9]58 else:59 root_name = os.path.splitext(basename)[0]60 base_out = ('%s_outliers_w%d.json.bz2' % (root_name, window))61 return os.path.join(directory, base_out)62def generate_truncated_json(json_file):63 """Generate an output file for each truncated data set."""64 warmup_stats_bin_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "warmup_stats", "bin")65 outlier_script = os.path.join(warmup_stats_bin_dir, 'mark_outliers_in_json')66 cpt_script = os.path.join(warmup_stats_bin_dir, 'mark_changepoints_in_json')67 directory = os.path.dirname(json_file)68 audit, window_size, original_json = parse_krun_file_with_changepoints([json_file])69 for last_iter in range(MAX_ITERS, MIN_ITERS, ITER_STEP):70 print 'Truncated to iteration:', last_iter71 new_json_data = _BLANK_BENCHMARK72 copy_results(last_iter, audit, window_size, original_json, new_json_data)73 outfile = os.path.join(directory, ('truncated_%g.json.bz2') % last_iter)74 write_krun_results_file(new_json_data, outfile)75 window = int(last_iter * WINDOW_PROPORTION)76 os.system('%s %s --window %d' % (outlier_script, outfile, window))77 steady = int(last_iter * STEADY_PROPORTION)78 os.system('%s %s --steady %d' % (cpt_script, create_outlier_filename(outfile, window), steady))79def create_cli_parser():80 """Create a parser to deal with command line switches."""81 script = os.path.basename(__file__)82 description = (('Generate results files by repeatedly truncating a Krun data file.\n' +83 '\n\nExample usage:\n\n' +84 '\t$ python %s results.json.bz2') % script)85 parser = argparse.ArgumentParser(description=description,86 formatter_class=argparse.RawTextHelpFormatter)87 parser.add_argument('results_file', action='store', default='.', type=str,88 help='Results file to be truncated.')89 return parser90if __name__ == '__main__':91 parser = create_cli_parser()92 options = parser.parse_args()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful