def array_filter_yield(array, match, inverse=False): ''' Description: yields filtered array, match may either be an individual object or an array ''' if (inverse): for i in array: if (__check__.array_test(match)): if (i not in match): continue else: yield i else: if (i != match): continue else: yield i else: for i in array: if (__check__.array_test(match)): if (i in match): continue else: yield i else: if (i == match): continue else: yield i
def str_filter(string, filtre, inverse=False, print_bool=True): ''' Description: Filters 'filtre' string out of 'string' string, if 'inverse' then the instances of 'filtre' are returned else the entries not equivalent to 'filtre' are returned ''' if (not isinstance(string, str)): try: string = str(string) except: if (print_bool): print( "[str_filter] Error: 'string' input could not be cast as a string object" ) out_list = [] if (inverse): for i in string: if (__check__.array_test(filtre)): if (i in filtre): out_list.append(i) else: if (i == filtre): out_list.append(i) else: for i in string: if (__check__.array_test(filtre)): if (i not in filtre): out_list.append(i) else: if (i != filtre): out_list.append(i) out_str = array_to_str(out_list, spc='', print_bool=print_bool) return out_str
def ismatrix(n, numeric=False): p1 = 'Below is invalid content found in the ' p2 = " entry of 'n'" if (not __check__.array_test(n)): return __func_eprint__( "[ismatrix] Error: input 'n' is not a python array") index_err = [] err = False length_err = False lang = 0 for i in range(len(n)): if (not __check__.array_test(n[i])): print("[ismatrix] Error: the " + __strl__.print_ordinal(i + 1) + " entry in 'n' is not a python array") err = True else: if (i == 0): lang = len(n[i]) if (numeric): err_arr = [j for j in n[i] if not __check__.numeric_test(j)] else: err_arr = [ j for j in n[i] if not __check__.numeric_test(j) and not isinstance(j, str) ] if (len(err_arr) > 0): err = True __strl__.format_fancy(err_arr, header=p1 + __strl__.print_ordinal(i + 1) + p2) if (len(n[i]) != lang): err = True length_err = True if (err): if (length_err): print( "[ismatrix] Error: lengths of the enteries of 'n' are not all equal" ) return False else: return True
def coerce_to_matrix(n, fill='NULL'): if (not __check__.array_test(n)): text = "[coerce_to_matrix] Error: input 'n' is not a python array; could not coerce to matrix" return __func_eprint__(text) newn = list(n) maxl = 0 for i in range(len(newn)): if (not __check__.array_test(newn[i])): text = "[coerce_to_matrix] Error: the " + __strl__.print_ordinal( i + 1) + " entry in 'n' is not a python array" return __func_eprint__(text) if (len(newn[i]) > maxl): maxl = len(newn[i]) for i in range(len(newn)): if (len(newn[i]) < maxl): while (len(newn[i]) < maxl): newn[i].append(fill) return newn
def line_nan_check(array, nan=True, inf=True, null=True): if (not __check__.array_test(array)): print("[line_nan_check] TypeError: 'array' is not a python array") return False check = (nan, inf, null) check_type = __nan_func__(check) index_list = [] for i in range(len(array)): if (__bool_tester__(array[i], check_type)): index_list.append(i) else: pass if (len(index_list) > 0): return index_list else: return False
def format_fancy(obj, header=None, newln=True, indt=4, err=True, list_return=False): if (newln): nl = '\n' else: nl = '' spc = indt * ' ' stylized_list = [] if (__check__.array_test(obj)): if (list_return): stylized_list.append(' \n') else: print(' ') if (isinstance(header, str)): out_val = header + nl if (list_return): stylized_list.append(out_val) else: print(out_val) for i in obj: out_val = spc + str(i) + nl if (list_return): stylized_list.append(out_val) else: print(out_val) if (list_return): stylized_list.append(' \n') return stylized_list else: print(' ') return None elif (isinstance(obj, str)): if (list_return): stylized_list.append(' \n') else: print(' ') if (isinstance(header, str)): out_val = spc + header + nl if (list_return): stylized_list.append(out_val) else: print(out_val) out_val = obj + nl if (list_return): stylized_list.append(out_val) return stylized_list else: print(out_val) return None else: print( "TypeError: 'obj' input object must be either an 'array' or a 'string'" )
def table_str_to_fill_numeric(line_list, space=' ', fill='NULL', nval=False, header=False, entete=True, columns=True, nanopt=True, nantup=(True, True, True), spc=' ', genre=float, debug=True): ''' Input Variables: line_list : A array (list or tuple) of strings which form a numeric table (header option allowed) space : A string of spaces, corrosponds to the number of spaces required for an empty entry fill : A string, corrosponds to the value which will be added in the place of an empty entry nval : An integer, corrosponds to the number of values in a row, to which the table will be coerced If False, the table is read according to parsed spaces header : If True, treats the first line in the input array as a header and not with the data entete : If header is True, attempts to return the header with the output numeric table columns : If True, attempts to return lists of each columns of data, rather than each rows as found in 'line_list' nanopt : If True, NaN style strings values do not return error values as specified in nantup nantup : A tuple in the form (nan,inf,null), each value is true if it is allowed sep : A string, seperator for numeric values in the input table (',' for CSV, space ' ' is default) genre : A python object function: attempts to coerce object type to 'genre', float is default debug : If True, checks performs dummy-check on input data, returns printing of point or location of failure Purpose: Takes a list of strings and attempts to convert into a table of numeric values Useful if working with formatted data tables ''' nan, inf, null = nantup null = True if (debug): fail_test = not __check__.array_test( line_list) # True if failed array test if (fail_test): print( "[table_str_to_numeric] TypeError: input 'line_list' is not a python array" ) return False for i in range(len(line_list) ): # checking if each object in 'line_list' is a string fail_test = not isinstance(line_list[i], str) # True if failed string test if (fail_test): print( "[table_str_to_numeric] TypeError: non-string object at line " + str(i + 1) + "of 'line_list'") return False if (header): head = line_list[0] op_list = line_list[1:-1] n = len(op_list) try: head = __strl__.str_to_list(head, spc=spc, filtre=True) nhead = len(head) except: print( "[table_str_to_fill_numeric] Warning: header could not be parsed" ) header = False else: op_list = list(line_list) # Parsing list of lines for i in range(len(op_list)): string = op_list[i] temp = __strl__.str_to_fill_list(string, lngspc=space, fill=fill, nval=nval, spc=spc, numeric=False) # Parsing numeric values found in each list if (nanopt): nan_list = line_nan_check(temp, nan=nan, inf=inf, null=null) if (nan_list > 0): for j in range(len(temp)): if (temp[j] not in nan_list and temp[j] != fill): try: temp[j] = genre(temp[j]) except: temp[j] = float(temp[j]) else: nan_list = line_nan_check(temp, False, False, True) if (nan_list > 0): for j in range(len(temp)): if (temp[j] not in nan_list and temp[j] != fill): try: temp[j] = genre(temp[j]) except: temp[j] = float(temp[j]) op_list[i] = temp if (header and entete): op_list.insert(0, head) if (columns): output = table_trans(op_list, coerce=True, fill=fill) if (output == False): print( "[table_str_to_numeric] Warning: could not transpose table; returning as is" ) return op_list else: return output else: return op_list
def table_str_to_numeric(line_list, header=False, entete=False, columns=True, nanopt=True, nantup=(True, True, True), sep=' ', genre=float, debug=True): ''' Input Variables: line_list : A python array (list or tuple) of strings which form a numeric table (header option allowed) header : If True, treats the first line in the input array as a header and not with the data entete : If header, attempts to return the header with the output numeric table columns : If True, attempts to return each columns of data, rather than input rows found in 'line_list' nanopt : If True, 'NaN' values do not return error values nantup : A tuple in the form (nan,inf,null), each value is true if it is allowed sep : string, seperator for numeric values in the input table (',' for CSV, space ' ' is default) genre : A python object function: attempts to coerce object type to 'genre', float is default (str, int) debug : If True, checks performs dummy-check on input data, returns printing of point or location of failure Purpose: Takes a list of strings and attempts to convert into a table of numeric values Useful if working with formatted data tables ''' nan, inf, null = nantup if (debug): fail_test = not __check__.array_test( line_list) # True if failed array test if (fail_test): print( "[table_str_to_numeric] TypeError: input 'line_list' is not a python array" ) return False for i in range(len(line_list) ): # checking if each object in 'line_list' is a string fail_test = not isinstance(line_list[i], str) # True if failed string test if (fail_test): print( "[table_str_to_numeric] TypeError: non-string object at line " + str(i + 1) + "of 'line_list'") return False new_line_list = __strl__.array_filter_spaces(list(line_list)) n = len(new_line_list) if (header): head = new_line_list[0] new_line_list = new_line_list[1:-1] n = len(new_line_list) try: head = __strl__.str_to_list(head, spc=sep, filtre=True) nhead = len(head) except: print("[table_str_to_numeric] Error: header could not be parsed]") return False nanopt_test = False for i in range(n): new_line_list[i] = __strl__.str_to_list(new_line_list[i], spc=sep, filtre=True) for j in range(len(new_line_list[i])): try: if (nanopt): nanopt_test = nan_check(new_line_list[i][j], nan, inf, null) if ((genre == int or genre == long) and nanopt_test == False): new_line_list[i][j] = genre(float(new_line_list[i][j])) if ((genre == float or genre == str) and nanopt_test == False): new_line_list[i][j] = genre(new_line_list[i][j]) except: print( "[table_str_to_numeric] Error: failed attempting to parsing table as numeric array" ) return False if (header and entete): try: new_line_list.insert(0, head) except: print( "[table_str_to_numeric] Warning: error occured when adding header, returning table without it" ) pass if (columns): output = table_trans(new_line_list) if (output == False): print( "[table_str_to_numeric] Warning: could not translate table, returning as is" ) return new_line_list else: return output else: return new_line_list
def __ecc_plot__(x, y, label=None, lims=None, fontsize=20, save=False, save_name='plot.jpg'): ''' __ecc_plot__ : Error correction code plot return tuple: (xarray, yarray ,labre, limre, fontsize, save, save_name) ''' # ECC # Dummy Check for 'x' and 'y' if (not __check__.array_test(x)): print("[__ecc_plot__] Error: 'x' must be an array") return False if (not __check__.array_test(y)): print("[__ecc_plot__] Error: 'y' must be an array") return False # Checking 'x' for proper formating xnumeric = True xarray = True for i in x: if (__check__.numeric_test(i)): xarray = False elif (__check__.array_test(i)): xnumeric = False for j in i: if (not __check__.numeric_test(j)): xarray = False else: xnumeric, xarray = False, False if (xarray == False and xnumeric == False): print( "[__ecc_plot__] Error: input 'x' must either be a numeric array or an array of numeric arrays" ) return False # Checking 'y' for proper formating ynumeric = True yarray = True for i in y: if (__check__.numeric_test(i)): yarray = False elif (__check__.array_test(i)): ynumeric = False for j in i: if (not __check__.numeric_test(j)): yarray = False else: ynumeric, yarray = False, False if (yarray == False and ynumeric == False): print( "[__ecc_plot__] Error: input 'y' must either be a numeric array or an array of numeric arrays" ) return False # Special check for a set of y arrays if (yarray and not xarray): for i in xrange(len(y)): if (len(x) != len(y[i])): print( "[__ecc_plot__] Error: 'x' should corrospond 1-to-1 for each array in 'y'" ) return False if (xarray and not yarray): print( "[__ecc_plot__] Error: if 'x' is an array of arrays, then 'y' must be as well" ) return False if (xarray): if (len(x) != len(y)): print( "[__ecc_plot__] Error: if 'x' is an array of arrays, then it must have the same length as 'y'" ) return False else: for i in xrange(len(x)): if (len(x[i]) != len(y[i])): errmsg = "[__ecc_plot__] Warning: each respective entry of 'x' " errmsg = errmsg + "should corrospond 1-to-1 to the 'y' entry" print(errmsg) return False # Label checks labre = False if (not __check__.array_test(label)): if (label != None): print( "[__ecc_plot__] Warning: input 'label' is not an array and has been deprecated" ) labre = True else: if (len(label) < 2): print( "[__ecc_plot__] Warning: input 'label' has a length less than 2 and has been deprecated" ) labre = True else: xlabel, ylabel = label[0], label[1] if (not isinstance(xlabel, str)): print( "[__ecc_plot__] Warning: input 'xlabel' is not string so 'label and has been deprecated" ) labre = True if (not isinstance(ylabel, str)): print( "[__ecc_plot__] Warning: input 'ylabel' is not string so 'label and has been deprecated" ) labre = True # Limit checks limre = False if (not __check__.array_test(lims)): if (lims != None): print( "[__ecc_plot__] Warning: input 'lims' is not an array and has been deprecated" ) limre = True else: if (len(lims) < 2): print( "[__ecc_plot__] Warning: input 'lims' has a length less than 2 and has been deprecated" ) limre = True else: xlims, ylims = lims[0], lims[1] if (__check__.array_test(xlims)): if (not __check__.numeric_test(xlims[0]) or not __check__.numeric_test(xlims[1])): print( "[__ecc_plot__] Warning: input 'xlims' is not string so 'lims and has been deprecated" ) limre = True else: limre = True if (__check__.array_test(ylims)): if (not __check__.numeric_test(ylims[0]) or not __check__.numeric_test(ylims[1])): print( "[__ecc_plot__] Warning: input 'ylims' is not string so 'lims and has been deprecated" ) limre = True else: limre = True # Checking the rest of the input variables if (__check__.numeric_test(fontsize)): fontsize = int(fontsize) else: fontsize = 30 if (not isinstance(save, bool)): save = False if (not isinstance(save_name, str)): save_name = 'plot.jpg' output = (xarray, yarray, labre, limre, fontsize, save, save_name) return output
def new_four_plot(tl_data, tr_data, bl_data, br_data, fontsize=20, lwd=2.5, label=None, lims=None, namelab=None, smooth=False, smoothness=300, save=False, save_name=None): fig, axs = __plt__.subplots(2, 2, sharex=False, sharey=False) tl_xysep, tl_ysep, tl_sep = False, False, False tr_xysep, tr_ysep, tr_sep = False, False, False bl_xysep, bl_ysep, bl_sep = False, False, False br_xysep, br_ysep, br_sep = False, False, False if (smooth): spln_inst = __mops__.spline() if (not __check__.array_test(label)): tl_label, tr_label, bl_label, br_label = [[' ', ' '], [' ', ' '], [' ', ' '], [' ', ' ']] else: if (len(label) != 4): tl_label, tr_label, bl_label, br_label = [[' ', ' '], [' ', ' '], [' ', ' '], [' ', ' ']] else: tl_label = label[0] tr_label = label[1] bl_label = label[2] br_label = label[3] try: x, y = tl_data except: print( "Error: input data 'tl_data' must be a python array of length two") test = __ecc_plot__(x, y, tl_label, lims, fontsize, save, save_name) if (test == False): print( "[new_plot] Error: 'tl_data' (top-left) data test failed, see preceding error msg for details" ) return False else: tl_xarray, tl_yarray, tl_labre, tl_limre, tl_fontsize, save, save_name = test if (tl_xarray and tl_yarray): tl_xysep = True elif (tl_yarray): tl_ysep = True else: tl_sep = True if (tl_labre): tl_label = (None, None) if (tl_limre): tl_lims = (None, None) try: x, y = tr_data except: print( "Error: input data 'tr_data' must be a python array of length two") test = __ecc_plot__(x, y, tr_label, lims, fontsize, save, save_name) if (test == False): print( "[new_plot] Error: 'tr_data' (top-right) data test failed, see preceding error msg for details" ) return False else: tr_xarray, tr_yarray, tr_labre, tr_limre, tr_fontsize, save, save_name = test if (tr_xarray and tr_yarray): tr_xysep = True elif (tr_yarray): tr_ysep = True else: tr_sep = True if (tr_labre): tr_label = (None, None) if (tr_limre): tr_lims = (None, None) try: x, y = bl_data except: print( "Error: input data 'bl_data' must be a python array of length two") test = __ecc_plot__(x, y, bl_label, lims, fontsize, save, save_name) if (test == False): print( "[new_plot] Error: 'bl_data' (bottom-left) data test failed, see preceding error msg for details" ) return False else: bl_xarray, bl_yarray, bl_labre, bl_limre, bl_fontsize, save, save_name = test if (bl_xarray and bl_yarray): bl_xysep = True elif (bl_yarray): bl_ysep = True else: bl_sep = True if (bl_labre): bl_label = (None, None) if (bl_limre): bl_lims = (None, None) try: x, y = br_data except: print( "Error: input data 'br_data' must be a python array of length two") test = __ecc_plot__(x, y, br_label, lims, fontsize, save, save_name) if (test == False): print( "[new_plot] Error: 'br_data' (bottom-right) data test failed, see preceding error msg for details" ) return False else: br_xarray, br_yarray, br_labre, br_limre, br_fontsize, save, save_name = test if (br_xarray and br_yarray): br_xysep = True elif (br_yarray): br_ysep = True else: br_sep = True if (br_labre): br_label = (None, None) if (br_limre): br_lims = (None, None) # Top-Left plot x, y = tl_data if (tl_sep): if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, y, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = y axs[0, 0].plot(tempx, tempy, linewidth=lwd) elif (tl_ysep): for i in y: if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, i, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = i axs[0, 0].plot(tempx, tempy, linewidth=lwd) elif (tl_xysep): for i in xrange(len(x)): if (smooth): xsmooth = __mops__.span_vec(x[i], smoothness) spln_inst.pass_vecs(x[i], y[i], xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x[i] tempy = y[i] axs[0, 0].plot(tempx, tempy, linewidth=lwd) if (namelab != None): try: axs[0, 0].set_title(str(namelab[0])) except: pass if (tl_label != (None, None)): axs[0, 0].set(xlabel=tl_label[0], ylabel=tl_label[1]) # Top-Right plot x, y = tr_data if (tr_sep): if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, y, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = y axs[0, 1].plot(tempx, tempy, linewidth=lwd) elif (tr_ysep): for i in y: if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, i, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = i axs[0, 1].plot(tempx, tempy, linewidth=lwd) elif (tr_xysep): for i in xrange(len(x)): if (smooth): xsmooth = __mops__.span_vec(x[i], smoothness) spln_inst.pass_vecs(x[i], y[i], xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x[i] tempy = y[i] axs[0, 1].plot(tempx, tempy, linewidth=lwd) if (namelab != None): try: axs[0, 1].set_title(str(namelab[1])) except: pass if (tr_label != (None, None)): axs[0, 1].set(xlabel=tr_label[0], ylabel=tr_label[1]) # Bottom-Left plot x, y = bl_data if (bl_sep): if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, y, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = y axs[1, 0].plot(tempx, tempy, linewidth=lwd) elif (bl_ysep): for i in y: if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, i, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = i axs[1, 0].plot(tempx, tempy, linewidth=lwd) elif (bl_xysep): for i in xrange(len(x)): if (smooth): xsmooth = __mops__.span_vec(x[i], smoothness) spln_inst.pass_vecs(x[i], y[i], xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x[i] tempy = y[i] axs[1, 0].plot(tempx, tempy, linewidth=lwd) if (namelab != None): try: axs[1, 0].set_title(str(namelab[2])) except: pass if (bl_label != (None, None)): axs[1, 0].set(xlabel=bl_label[0], ylabel=bl_label[1]) # Bottom-Right plot x, y = br_data if (br_sep): if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, y, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = y axs[1, 1].plot(tempx, tempy, linewidth=lwd) elif (br_ysep): for i in y: if (smooth): xsmooth = __mops__.span_vec(x, smoothness) spln_inst.pass_vecs(x, i, xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x tempy = i axs[1, 1].plot(tempx, tempy, linewidth=lwd) elif (br_xysep): for i in xrange(len(x)): if (smooth): xsmooth = __mops__.span_vec(x[i], smoothness) spln_inst.pass_vecs(x[i], y[i], xsmooth) spln_inst.pass_spline() ysmooth = spln_inst.get_spline() tempx = xsmooth tempy = ysmooth else: tempx = x[i] tempy = y[i] axs[1, 1].plot(tempx, tempy, linewidth=lwd) if (namelab != None): try: axs[1, 1].set_title(str(namelab[3])) except: pass if (br_label != (None, None)): axs[1, 1].set(xlabel=br_label[0], ylabel=br_label[1]) __plt__.tight_layout() __plt__.show() if (save): try: fig.savefig(save_name) except: print("Error: attempted and failed to print graph as " + str(save_name)) return True