How to use calculate_percentages method in localstack

Best Python code snippet using localstack_python

data_preparation_helper_functions.py

Source:data_preparation_helper_functions.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3------ What is this file? ------4This script contains some helper functions that are used in the scripts found under src/data_preparation.5The unit tests for these functions can be found at:6 tests/unit_tests/helper_functions/test_data_preparation_helper_functions.py7"""8#%% --- Import Required Packages ---9import pandas as pd10import matplotlib.pyplot as plt11import seaborn as sns12#%% --- Set proper directory to assure integration with doit ---13# abspath = os.path.abspath(__file__)14# dname = os.path.dirname(abspath)15# os.chdir(dname)16#%% --- FUNCTION: sample_and_read_from_df ---17 # --- Main Function --- #18 19def sample_and_read_from_df(dataframe, sample_size):20 """21 Sample n(sample_size) rows from the dataset(dataframe). Print out the samples in the console in a column-by-column basis.22 Keyword arguments:23 dataframe -- Accepts a pandas dataframe.24 sample-size -- Accepts an integer. (Default 10.)25 """26 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(dataframe))27 if not isinstance(dataframe, pd.DataFrame):28 raise ValueError(valerror_text)29 30 valerror_text = "sample_size must be type int, got {}".format(type(sample_size))31 if not isinstance(sample_size, int):32 raise ValueError(valerror_text)33 34 index_error_text = ("dataframe length must be larger than or equal to sample_size. "35 "dataframe length is {}, sample_size is {}").format(len(dataframe), sample_size)36 if not len(dataframe) >= sample_size:37 raise IndexError(index_error_text)38 39 sample = dataframe.sample(sample_size)40 sample_columns = sample.columns41 for column in sample_columns:42 print("Commencing with " + column + " column of the dataframe")43 print("")44 for i in range(0,len(sample)):45 selection = sample.iloc[i]46 print(str(selection[column]).encode("utf-8"))47 print("") 48 49#%% --- FUNCTION: report_null_values ---50 # --- Helper Functions ---51 52def is_null_values_dataframe(dataframe):53 """54 Checks if a dataframe is null_values_dataframe.55 Parameters56 ----------57 dataframe : pandas.DataFrame58 Returns59 -------60 Returns Boolean True if the dataframe is a null_values_dataframe.61 Returns Boolean False if the dataframe is not a null_values_dataframe62 """63 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(dataframe))64 if not isinstance(dataframe, pd.DataFrame):65 raise ValueError(valerror_text)66 67 is_null_values_dataframe = False68 69 if "null_count" in dataframe.columns:70 is_null_values_dataframe = True71 72 return is_null_values_dataframe73def is_extended_null_values_dataframe(null_values_dataframe):74 """75 Checks if dataframe is a extended null_values_dataframe76 Parameters77 ----------78 null_values_dataframe : pandas.DataFrame79 Returns80 -------81 Returns Boolean True if the dataframe is a extended null_values_dataframe.82 Returns Boolean False if the dataframe is not a extended null_values_dataframe83 """84 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(null_values_dataframe))85 if not isinstance(null_values_dataframe, pd.DataFrame):86 raise ValueError(valerror_text)87 88 valerror_text = "dataframe does not contain null_count information."89 if not is_null_values_dataframe(null_values_dataframe):90 raise ValueError(valerror_text)91 92 is_extended_null_values_dataframe = False93 94 if "null_percentage" in null_values_dataframe.columns:95 is_extended_null_values_dataframe = True96 97 return is_extended_null_values_dataframe98def plot_null_values_bar_chart(null_values_dataframe):99 """100 Creates a bar chart from a null_values_dataframe.101 If the null_values_dataframe is an extended null_values_dataframe,102 creates a double bar chart view which displays percentage information.103 104 Parameters105 ----------106 null_values_dataframe : pandas.DataFrame.107 Returns108 -------109 Returns a matplotlib.pyplot.Figure object.110 """111 112 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(null_values_dataframe))113 if not isinstance(null_values_dataframe, pd.DataFrame):114 raise ValueError(valerror_text)115 116 valerror_text = "dataframe does not contain null_count information."117 if not is_null_values_dataframe(null_values_dataframe):118 raise ValueError(valerror_text)119 120 fig = plt.Figure(figsize = (9.60,7.20))121 122 # Ax creating is wrapped inside if-else statement because creating123 #two instances of ax_1 DOES NOT override each other.124 if is_extended_null_values_dataframe(null_values_dataframe):125 ax_1 = fig.add_subplot(1,2,1)126 ax_2 = fig.add_subplot(1,2,2)127 128 else:129 ax_1 = fig.add_subplot(1,1,1)130 131 labels = list(null_values_dataframe.index)132 133 from numpy import arange134 bar_positions = arange(len(labels)) + 1135 136 bar_heights_1 = null_values_dataframe.loc[:,"null_count"]137 138 if is_extended_null_values_dataframe(null_values_dataframe):139 bar_heights_2 = null_values_dataframe.loc[:,"null_percentage"]140 141 ax_1.bar(bar_positions, bar_heights_1,142 width = 0.7,143 align = "center")144 145 ax_1.set_xticks(bar_positions)146 ax_1.set_xticklabels(labels, rotation = 90)147 148 if is_extended_null_values_dataframe(null_values_dataframe):149 ax_2.bar(bar_positions, bar_heights_2,150 width = 0.7,151 align = "center")152 153 ax_2.set_xticks(bar_positions)154 ax_2.set_xticklabels(labels, rotation = 90)155 156 plt.show()157 return fig158def plot_null_values_matrix(dataframe):159 """160 161 Parameters162 ----------163 dataframe : TYPE164 DESCRIPTION.165 Returns166 -------167 fig : TYPE168 DESCRIPTION.169 """170 171 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(dataframe))172 if not isinstance(dataframe, pd.DataFrame):173 raise ValueError(valerror_text)174 175 #Create a boolean dataframe based on whether values are null or not176 df_null = dataframe.isnull()177 #create a heatmap of the boolean dataframe178 plot_object = sns.heatmap(df_null,179 cbar = False)180 181 plt.xticks(rotation=90, size='x-large')182 183 fig = plot_object.get_figure()184 return fig 185 186 # --- Subfunction : calculate_null_values ---187 188def calculate_null_values(dataframe, calculate_percentages = True):189 """190 191 Calculates the null values in the given dataframe in a column-by-column192 basis. Returns a dataframe whose index is column names of dataframe and whose193 values are the null value numbers.194 195 If report_percentages is True, also calculates of much of each column196 is made out of null values.197 Parameters198 ----------199 dataframe : pandas.DataFrame.200 calculate_percentages : Boolean value, optional201 Sets whether the null value percentage of columns will be calculated.202 The default is True.203 Returns204 -------205 null_values_report : pandas.DataFrame.206 A pandas.DataFrame that encodes information about207 null values in the original dataframe in a column-by-column basis.208 209 """210 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(dataframe))211 if not isinstance(dataframe, pd.DataFrame):212 raise ValueError(valerror_text)213 214 valerror_text = "calculate_percentages must be type boolean True or False, got {}".format(type(calculate_percentages))215 if calculate_percentages not in [True, False]:216 raise ValueError(valerror_text)217 218 null_counts = dataframe.isnull().sum()219 220 if calculate_percentages == True:221 #Divide null count by total length of each col to find a percentage222 null_counts_pct = (null_counts / dataframe.shape[0]) * 100223 null_values_report = pd.concat([null_counts, null_counts_pct],224 axis = 1)225 null_values_report.rename(columns = {0:"null_count",1:"null_percentage"},226 inplace = True)227 else:228 null_values_report = null_counts.rename("null_count").to_frame()229 230 return null_values_report231 # --- Subfunction : print_null_values ---232 233def print_null_values(null_values_dataframe):234 """235 236 Prints out into the console a formatted message that explains237 a null_values_dataframe.238 239 Parameters240 ----------241 null_values_dataframe : pandas.DataFrame.242 A pandas.DataFrame that is produced as the result of calling243 calculate_null_values() on a dataframe. Has to contain a column244 called "null_count" at minimum.245 246 Returns247 -------248 None.249 """250 251 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(null_values_dataframe))252 if not isinstance(null_values_dataframe, pd.DataFrame):253 raise ValueError(valerror_text)254 255 valerror_text = "dataframe does not contain null_count information."256 if not is_null_values_dataframe(null_values_dataframe):257 raise ValueError(valerror_text)258 259 if is_extended_null_values_dataframe(null_values_dataframe):260 for column in null_values_dataframe.index:261 column_null_count = str(null_values_dataframe.loc[column,"null_count"])262 print("Column {} has {} null values.".format(column,column_null_count))263 column_null_percentage = str(null_values_dataframe.loc[column, "null_percentage"])264 print("{} percent of column {} is null values.".format(column_null_percentage,column))265 else:266 for column in null_values_dataframe.index:267 column_null_count = str(null_values_dataframe.loc[column,"null_count"])268 print("Column {} has {} null values.".format(column,column_null_count))269 270 # --- Subfunction : visualize_null_values ---271 272def visualize_null_values(null_values_dataframe, kind = "bar_chart"):273 """274 Returns a visual representation of a given null_values_dataframe.275 Parameters276 ----------277 null_values_dataframe : pandas.DataFrame.278 A pandas.DataFrame that is produced as the result of calling279 calculate_null_values() on a dataframe. Has to contain a column280 called "null_count" at minimum.281 282 kind : One of the following strings "bar_chart", "matrix", "heatmap"283 The default is "bar_chart". "matrix" and "heatmap" is not implemented yet.284 Returns285 -------286 Returns a matplotlib.pyplot.Figure object.287 """288 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(null_values_dataframe))289 if not isinstance(null_values_dataframe, pd.DataFrame):290 raise ValueError(valerror_text)291 292 valerror_text = "dataframe does not contain null_count information."293 if not is_null_values_dataframe(null_values_dataframe):294 raise ValueError(valerror_text)295 296 accepted_kinds = ["bar_chart", "matrix", "heatmap"]297 valerror_text = "Parameter kind must be a string and one of bar_chart, matrix or heatmap. Got \"{}\" as type {}.".format(str(kind), type(kind))298 if str(kind) not in accepted_kinds:299 raise ValueError(valerror_text)300 301 if kind == "bar_chart":302 plot = plot_null_values_bar_chart(null_values_dataframe)303 304 elif kind == "matrix":305 raise NotImplementedError306 307 elif kind == "heatmap": 308 raise NotImplementedError309 310 return plot311 # --- Main function: report_null_values ---312def report_null_values(dataframe, calculate_percentages = True,313 visualize_results = False, print_results = False):314 """315 Composes a report about the null values within the given dataframe.316 Parameters317 ----------318 dataframe : pandas.DataFrame.319 A pandas.DataFrame that will be evaluated for nullity information.320 321 calculate_percentages: Boolean True or False.322 Related to the subfuction calculate_percentages. Determines whether or not323 percentages related to nullity will be calculated.324 325 visualize_results: Boolean True or False.326 Related to the subfunction visualize_null_values. Determines whether or not327 this function will return a matplotlib.Figure object.328 329 print_results: Boolean True or False.330 Related to the subfunction print_null_values. Determines whether or not331 this function will print out a formatted report.332 333 Returns334 -------335 Depending on the arguments passed, returns one of the following:336 - pandas.DataFrame object337 - matplotlib.Figure object338 - None (print out a report)339 """340 341 valerror_text = "dataframe must be type pd.DataFrame, got {}".format(type(dataframe))342 if not isinstance(dataframe, pd.DataFrame):343 raise ValueError(valerror_text)344 345 parameters = [calculate_percentages, visualize_results, print_results]346 for parameter in parameters:347 valerror_text = "{} must be type boolean True or False, got {}".format(parameter, type(parameter))348 if parameter not in [True, False]:349 raise ValueError(valerror_text)350 351 valerror_text = "Parameters visualize_results and print_results cannot be both boolean True."352 if visualize_results == True and print_results == True:353 raise ValueError(valerror_text)354 355 null_values_dataframe = calculate_null_values(dataframe, calculate_percentages = calculate_percentages)356 if visualize_results == True:357 plot = visualize_null_values(null_values_dataframe)358 return plot359 360 elif print_results == True:361 print_null_values(null_values_dataframe)362 return363 364 else:365 return null_values_dataframe366 ...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

...3from django.db import connection4from django.db.models import Count5from referendum.models import ActiveVote6from referendum import tasks7def calculate_percentages(counts, digits=2):8 '''9 Calculate percentages, given a list of counts.10 It sensibly rounds them, so the sum of all percentages is guaranteed to be 100.11 Exception: all counts are 0.12 >>> calculate_percentages([3, 1])13 [75, 25]14 >>> calculate_percentages([35, 1])15 [97, 3]16 >>> calculate_percentages([35, 1], digits=3)17 [972, 28]18 >>> calculate_percentages([1023, 432])19 [70, 30]20 >>> calculate_percentages([1023, 432], digits=4)21 [7031, 2969]22 >>> calculate_percentages([32, 124, 81, 45])23 [11, 44, 29, 16]24 >>> calculate_percentages([0, 0])25 [0, 0]26 >>> calculate_percentages([0])27 [0]28 '''29 total = sum(counts)30 precision = 10**digits31 if total == 0:32 return [0] * len(counts)33 whole = [precision * x // total for x in counts]34 leftover = [(precision * counts[i] % total, i) for i in range(len(counts))]35 assigned = sum(whole)36 leftover.sort()37 leftover.reverse()38 i = 039 while assigned < precision:40 if counts[leftover[i][1]] > 0:41 whole[leftover[i][1]] += 142 assigned += 143 i += 144 if i == len(counts):45 i = 046 return whole47#TODO napisi dekorator @cache_value48def get_active_vote(facebook_id, force=False):49 '''50 Gets the active vote of a particular facebook user.51 '''52 key = 'vote_{}'.format(facebook_id)53 vote = cache.get(key)54 if vote is None or force:55 try:56 vote = ActiveVote.objects.get(facebook_id=facebook_id)57 except ObjectDoesNotExist:58 vote = None59 except MultipleObjectsReturned:60 vote = tasks.fix_votes(facebook_id)61 cache.set(key, vote)62 return vote63def get_global_results(force=False):64 '''65 Gets the global results from cache.66 If there are no results, we calculate them and save to cache.67 '''68 key = 'global_results'69 result = cache.get(key)70 if result is None or force:71 query_result = ActiveVote.objects.values('vote').annotate(Count('vote'))72 result = [0] * 273 for q in query_result:74 result[q['vote']] = q['vote__count']75 cache.set(key, result)76 return result77def get_global_count(force=False):78 '''79 Gets the total number of votes casted.80 '''81 return sum(get_global_results(force))82def get_friends_results(user_id, force=False):83 '''84 Gets the results for facebook friends of the user.85 If there are no results, we calculate them and save to cache.86 '''87 key = 'friends_{}'.format(user_id)88 result = cache.get(key)89 if result is None or force:90 cursor = connection.cursor()91 cursor.execute(92 'SELECT vote, COUNT(vote) ' +93 'FROM django_facebook_facebookuser AS fb ' +94 'JOIN referendum_activevote AS v ' +95 'ON fb.facebook_id = v.facebook_id ' +96 'WHERE fb.user_id=%s ' +97 'GROUP BY vote',98 [user_id]99 )100 results_raw = cursor.fetchall()101 result = [0] * 2102 for r in results_raw:103 result[r[0]] = int(r[1])104 cache.set(key, result)105 return result106def get_full_results(user_id, force=False):107 ret = {}108 friends_results = get_friends_results(user_id, force)109 ret['friends_results'] = {110 'percentages': calculate_percentages(friends_results),111 'raw_numbers': friends_results,112 'responses': sum(friends_results),113 }114#TODO: add results by age115 global_results = get_global_results(force)116 ret['global_results'] = {117 'percentages': calculate_percentages(global_results),118 'raw_numbers': global_results,119 'responses': sum(global_results)120 }121 return ret122def get_georesults(scope, location, force=False):123 if scope != 'county' and scope != 'country':124 raise KeyError('scope value incorrect!')125 if location != 'current' and location != 'hometown':126 raise KeyError('location value incorrect!')127 # our database is stupid128 if location == 'current':129 location = 'location'130 key = 'georesults_{}_{}'.format(scope, location)131 results = cache.get(key)...

Full Screen

Full Screen

WeightCalc.py

Source:WeightCalc.py Github

copy

Full Screen

...6 print ("Get Stronger!")7 weight = int(input('For real, enter your max weight:'))8 else:9 break10def calculate_percentages( weight , percent ):11 # This takes the weight, and determines the 90, 80, 70, 60 % values12 result = weight * (percent / 100)13 print ( "%s percent of the weight %s lbs is %s" % (percent , weight, result) )14 return result15def plate_subtract ( weight , plateWeight , plates):16 double_plateWeight = plateWeight * 217 if weight >= double_plateWeight:18 plates.append(plateWeight)19 weight = weight - double_plateWeight20 return plates , weight21def plate_count ( weight ):22 plates = []23 # subtract the bar24 weight = weight - 45...

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