How to use value_ranges method in pandera

Best Python code snippet using pandera_python

sheet.py

Source:sheet.py Github

copy

Full Screen

1from google_documents.entities.from_itemable import FromItemable2class Color(FromItemable):3 red: float4 green: float5 blue: float6 def __init__(self, red=0, green=0, blue=0):7 self.red = red8 self.blue = blue9 self.green = green10 def to_item(self):11 return {12 "green": self.green,13 "blue": self.blue,14 "red": self.red15 }16class GridProperties(FromItemable):17 def __init__(self, row_count, column_count):18 self.row_count = row_count19 self.column_count = column_count20 def to_item(self):21 return {22 'rowCount': self.row_count,23 'columnCount': self.column_count24 }25 @classmethod26 def from_item(cls, item):27 return cls(item['rowCount'], item['columnCount'])28class Sheet(FromItemable):29 index: int30 title: str31 tab_color: Color = None32 grid_properties: GridProperties = None33 # Optional - if assigned, makes available sheet read/write methods34 spreadsheet = None35 def assign_spreadsheet(self, spreadsheet):36 self.spreadsheet = spreadsheet37 def _get_spreadsheet_range_name(self, sheet_range_name):38 """39 Returns range name in the sheet40 """41 return f"{self.title}!{sheet_range_name}"42 def read(self, range_name):43 """44 Reads data from the sheet45 :param range_name: Range to read46 """47 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."48 return self.spreadsheet.read(49 self._get_spreadsheet_range_name(range_name)50 )51 def write(self, range_name, data, value_input_option="RAW"):52 """53 Writes data into the sheet54 :param range_name: Range to write in55 :param data: Data to write56 :param value_input_option: How to recognize input data57 :return:58 """59 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."60 return self.spreadsheet.write(61 range_name=self._get_spreadsheet_range_name(range_name),62 data=data,63 value_input_option=value_input_option,64 )65 def clear(self, range_name):66 """67 Clears range in the sheet68 :param range_name: Range to clear69 :return:70 """71 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."72 return self.spreadsheet.clear(73 self._get_spreadsheet_range_name(range_name)74 )75 def batch_read(self, ranges_names):76 """77 Reads multiple ranges from the spreasheet78 :param ranges_names:79 :return:80 """81 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."82 # Convert everything to range names with the sheet name83 ranges_names = list(map(84 self._get_spreadsheet_range_name, ranges_names85 ))86 return self.spreadsheet.batch_read(ranges_names)87 def batch_write(self, value_ranges, value_input_option="RAW"):88 """89 Writes data tu the multiple ranges in the Google Sheet90 :param value_ranges: List of dicts like91 {"range": "A1:B2", data: [[data to write]]}92 :param value_input_option: How to recognize input data93 :return:94 """95 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."96 # Convert ranges in value_ranges to the ranges with the sheet name97 value_ranges = list(map(98 lambda r: {99 **r,100 "range": self._get_spreadsheet_range_name(r["range"])101 },102 value_ranges103 ))104 return self.spreadsheet.batch_write(value_ranges, value_input_option)105 def batch_clear(self, ranges_names):106 """107 Clears multiple ranges in the Google Sheets108 :param ranges_names: Ranges to clear109 :return:110 """111 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."112 ranges_names = list(map(113 self._get_spreadsheet_range_name, ranges_names114 ))115 return self.spreadsheet.batch_clear(ranges_names)116 def delete(self):117 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."118 response = self.spreadsheet._sheets_api_service. \119 spreadsheets().batchUpdate(120 spreadsheetId=self.spreadsheet.id,121 body={"requests": [{"deleteSheet": {"sheetId": self.id}}]}122 ).execute()123 # Delete spreadhseet from sheet124 self.spreadsheet = None125 return response126 @classmethod127 def from_item(cls, item):128 properties = item["properties"]129 grid_properties = None130 if properties.get("gridProperties"):131 grid_properties = GridProperties.from_item(132 properties["gridProperties"])133 tab_color = None134 if properties.get("tabColor"):135 tab_color = Color.from_item(properties["tabColor"])136 return cls(137 id=properties["sheetId"],138 index=properties["index"],139 title=properties["title"],140 tab_color=tab_color,141 grid_properties=grid_properties,142 )143 def to_item(self):144 return {145 "sheetId": self.id,146 "index": self.index,147 "title": self.title,148 "gridProperties": self.grid_properties.to_item()149 if self.grid_properties else None,150 "tabColor": self.tab_color and self.tab_color.to_item(),151 }152 def __getitem__(self, item):153 """154 Allows get data from the spreadsheet using sheet["A1:B2"]155 or batch get using sheet[["A1:B2", "A5:B6"]]156 """157 if isinstance(item, str):158 return self.read(item)159 else:160 return self.batch_read(item)161 set_item_value_input_option = "RAW"162 def __setitem__(self, item, value):163 """164 Allows writing data into the spreadsheet using165 sheet["A1:B2"] = [["l", "o"], ["l", "!"]]166 or using batch writing167 sheet[["A1:B2", "A5:B6]] = \168 ([["l", "o"], ["l", "!"]], [["f", "o"], ["o", "!"]])169 Value input option should be set here (if needed)170 via set_item_value_input_option171 """172 if isinstance(item, str):173 self.write(item, value, self.set_item_value_input_option)174 else:175 # There should be equal count of ranges and values to set176 if not len(item) == len(value):177 raise ValueError("Length of ranges and vales is not equal")178 value_ranges = [179 {"range": range_name,180 "values": values}181 for range_name, values182 in zip(item, value)183 ]184 self.batch_write(value_ranges, self.set_item_value_input_option)185 def __init__(186 self,187 id=None,188 index=None,189 title=None,190 tab_color=None,191 grid_properties=None192 ):193 self.id = id194 self.index = index195 self.title = title196 self.tab_color = tab_color197 self.grid_properties = grid_properties198 def __eq__(self, other):199 assert self.spreadsheet, "Spreadsheet for the sheet is unknown."200 assert other.spreadsheet, "Spreadsheet for the sheet is unknown."201 return self.spreadsheet == other.spreadsheet and self.id == other.id202 def __repr__(self):203 return f'<Sheet title="{self.title}" index="{self.index}">'...

Full Screen

Full Screen

gridsearch.py

Source:gridsearch.py Github

copy

Full Screen

1# coding=utf-82import nevo.eval.fitness as fitness3import nevo.chromgen as chromgen4import math5import operator6from itertools import product7logger = None8QUEUESIZE = 1009#------------------------------------------------10def start(pconf, **args):11 mode = pconf.get("mode", "Simulation")12 global logger13 logger = pconf.get_logger("gridsearch")14 proj_name = pconf.parse_project_data()["proj_name"]15 num_currents = int(pconf.get_list("currents", "Simulation")[0])16 fitness_args = { "pconf" : pconf,17 "mode" : mode,18 "proj_name" : proj_name,19 "numCurrents": num_currents,20 }21 for item in pconf.cfg.items("fitness.calc_fitness_candidates"):22 fitness_args[item[0]] = eval(item[1])23 24 (l_bounds, u_bounds, deltas) = ([], [], [])25 for channel in ["ar", "cal", "cat", "k2", "ka", "kahp", "hc", "alpha", "km", "naf", "nap", "pas"]:26 _range = map(eval, pconf.get(channel, "gridsearch").split(','))27 if len(_range) == 1:28 l_bounds.append(_range[0])29 u_bounds.append(_range[0])30 deltas.append(1)31 elif len(_range) == 3:32 l_bounds.append(_range[0])33 u_bounds.append(_range[1])34 deltas.append(_range[2])35 else:36 raise RuntimeError("Invalid parameters for channel " +repr(channel))37 grid = []38 gridmode = pconf.get("gridmode", "gridsearch")39 logger.info("Generating grid...")40 if(gridmode == "linear"):41 grid = generate_linear_grid(l_bounds, u_bounds, deltas)42 elif(gridmode == "exponential"):43 grid = generate_exponential_grid(l_bounds, u_bounds, deltas)44 else:45 raise RuntimeError("Invalid gridmode: " + repr(gridmode))46 logger.info("Generated " + gridmode + " grid:")47 logger.info("Lower bounds: " + repr(l_bounds))48 logger.info("Upper bounds: " + repr(u_bounds))49 logger.info("Steps : " + repr(deltas))50 gs = GridSearch(pconf, fitness_args)51 best = gs.evaluate(grid)52 logger.info("Grid search complete.")53 logger.info("Best result:" +repr(best))54 55def generate_linear_grid(l_bounds, u_bounds, deltas):56 value_ranges = map(list, map(generate_steps, l_bounds, u_bounds, deltas))57 58 logger.info("Value ranges:")59 for x in value_ranges:60 logger.info(repr(x))61 logger.info("Grid size: " + repr(reduce(operator.mul, map(len, value_ranges), 1)))62 # return cartesian product, i.e. all combinations of values63 return product(*value_ranges)64#------------------------------------------------65def generate_exponential_grid(l_bounds, u_bounds, deltas):66 exp_l_bounds = map(math.log10, l_bounds)67 exp_u_bounds = map(math.log10, u_bounds)68 exp_value_ranges = map(generate_steps, exp_l_bounds, exp_u_bounds, deltas)69 value_ranges = [map(lambda x:10**x, list(l)) for l in exp_value_ranges]70 # Special case: scalar in position 7:71 value_ranges[7] = list(generate_steps(l_bounds[7], u_bounds[7], deltas[7]))72 logger.info("Value ranges:")73 for x in value_ranges:74 logger.info(repr(x))75 logger.info("Grid size: " + repr(reduce(operator.mul, map(len, value_ranges), 1)))76 # return cartesian product, i.e. all combinations of values77 return product(*value_ranges)78#------------------------------------------------79def generate_steps(l_bound, u_bound, delta):80 yield l_bound81 l_bound += delta82 while l_bound <= u_bound:83 yield l_bound84 l_bound += delta85#------------------------------------------------86class GridSearch(object):87 #------------------------------------------------88 def __init__(self, pconf, fitness_args):89 self.pconf = pconf90 self.fitness_args = fitness_args91 self.outfile = pconf.local_path("grid.csv")92 self.queue = []93 self.best = ([], -20000)94 #------------------------------------------------95 def evaluate(self, grid):96 for point in grid:97 self.add(point)98 self.update()99 return self.best100 #------------------------------------------------101 def add(self, point):102 self.queue.append(list(point))103 if len(self.queue) > QUEUESIZE:104 self.update()105 #------------------------------------------------106 def update(self):107 results = fitness.calc_fitness_candidates(self.queue, self.fitness_args)108 with open(self.outfile, "a") as file:109 for i in range(len(results)):110 s = repr(results[i]) + ", " + repr(self.queue[i])111 logger.info(s)112 file.write(s + "\n")113 if results[i] > self.best[1]:114 self.best = (self.queue[i], results[i])...

Full Screen

Full Screen

histogram_peak.py

Source:histogram_peak.py Github

copy

Full Screen

1class HistogramPeak:2 def __init__(self, peaks=None, range_length=100, parameter_theta=0.1):3 self.histogram = None4 self.peaks = peaks5 self.parameter_theta = parameter_theta6 self.valid_peak_counts = [0] * range_length7 self.value_ranges = self._init_value_range(range_length)8 def _init_value_range(self, range_length):9 value_ranges = list()10 interval_value = 1.0 / range_length11 for i in range(0, range_length):12 value_ranges.append(round(interval_value * (i + 1), 2))13 return value_ranges14 def extract_peak_count(self):15 for peak in self.peaks:16 value = peak.x.value17 if value < self.parameter_theta:18 continue19 index = -120 for point in self.value_ranges:21 index += 122 if value < point:23 break24 self.valid_peak_counts[index] += 125 def extract_feature(self):26 for value in self.histogram:27 if value < self.parameter_theta:28 continue29 index = -130 for point in self.value_ranges:31 index += 132 if value < point:33 break34 self.valid_peak_counts[index] += 135 def to_string(self, label):36 feature = '{0}'.format(label)37 index = 038 for count in self.valid_peak_counts:39 feature += ' {0}:{1}'.format(index, count)40 index += 1...

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 pandera 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