def zspline(typ, n1, n2, n3, n4, xin, n): # remove z-trend with a z-spline fn32 = n3 - n2 x2 = xin[n2 - 1] x3 = xin[n3 - 1] x = xin.copy() if n1 < 1 or n2 < n1 + 1 or n3 < n2 + 1 or n4 < n3 + 1 or n4 > n: print 'wrong parameters for zspline!' sys.exit(1) a, b = trend(typ, n1, n2, n3, n4, x[n1:], n2 - n1) #for i in np.arange(n1, n, 1): # x[i] = xa[i-n1] a1 = a b1 = b # print 'n1: ', n1 # print 'n2: ', n2 # print 'n3: ', n3 # print 'n4: ', n4 # print 'a1/b1:', a1, b1 a, b = trend(typ, n1, n2, n3, n4, x[n3:], n4 - n3) #for i in np.arange(n3, n, 1): # x[i] = xa[i-n3] a3 = a b3 = b # print 'a3/b3:', a3, b3 #for j in np.arange(n1,n2+1,1): for j in np.arange(n1 - 1, n2, 1): x[j] = x[j] - (a1 + b1 * (j - n1 + 2)) #for j in np.arange(n2+1,n3,1): for j in np.arange(n2, n3 - 1, 1): x[j] = x[j] - (x2 + (j - n2 + 1) / fn32 * (x3 - x2)) #for j in np.arange(n3,n4,1): for j in np.arange(n3 - 1, n4 - 1, 1): x[j] = x[j] - (a3 + b3 * (j - n3 + 2)) return x
def zspline(typ, n1, n2, n3, n4, xin, n): # remove z-trend with a z-spline fn32 = n3 - n2 x2 = xin[n2-1] x3 = xin[n3-1] x = xin.copy() if n1 < 1 or n2 < n1+1 or n3 < n2+1 or n4 < n3+1 or n4 > n: print 'wrong parameters for zspline!' sys.exit(1) a, b = trend(typ, n1, n2, n3, n4, x[n1:], n2-n1) #for i in np.arange(n1, n, 1): # x[i] = xa[i-n1] a1 = a b1 = b # print 'n1: ', n1 # print 'n2: ', n2 # print 'n3: ', n3 # print 'n4: ', n4 # print 'a1/b1:', a1, b1 a, b = trend(typ, n1, n2, n3, n4, x[n3:], n4-n3) #for i in np.arange(n3, n, 1): # x[i] = xa[i-n3] a3 = a b3 = b # print 'a3/b3:', a3, b3 #for j in np.arange(n1,n2+1,1): for j in np.arange(n1-1,n2,1): x[j] = x[j] - (a1 + b1 * (j - n1 + 2)) #for j in np.arange(n2+1,n3,1): for j in np.arange(n2,n3-1,1): x[j] = x[j] - (x2 + (j - n2 + 1) / fn32 * (x3 - x2)) #for j in np.arange(n3,n4,1): for j in np.arange(n3-1,n4-1,1): x[j] = x[j] - (a3 + b3 * (j - n3 + 2)) return x
def mspline(typ, n1, n2, n3, n4, xin, n): # remove trend with a m-spline fn32 = n3 - n2 x2 = xin[n2-1] x3 = xin[n3-1] x = xin.copy() if n1 < 1 or n2 < n1+1 or n3 < n2+1 or n4 < n3+1 or n4 > n: print 'wrong parameters for zspline!' sys.exit(1) a, b = trend(typ, n1, n2, n3, n4, x[n1:], n2-n1) a1 = a b1 = b a, b = trend(typ, n1, n2, n3, n4, x[n3:], n4-n3) a3 = a b3 = b for j in np.arange(n1-1,n4,1): x[j] = x[j] - (a1 + b1 * (j - n1 + 2)) / 2.0 for j in np.arange(n1-1,n4-1,1): x[j] = x[j] - (a3 + b3 * (j - n3 + 2)) / 2.0 return x
def test_1(): """An example from the IPL dataset question : annual trend of avg win_by_runs over date range 2008-05-08 to 2017-04-12 """ table = pandas.read_csv('data/matches.csv') query_result = trend.trend(table, 'win_by_runs', Granularities.ANNUALLY, SummaryOperators.MAX, date_range=('2008-05-08', '2010-04-12'), date_column_name='date', date_format='%Y-%m-%d') print(query_result) expected_result = """ date win_by_runs 0 2008-01-01 105 1 2009-01-01 92 2 2010-01-01 98""" assert(expected_result == query_result.to_string())
def walking_bolBands(data, interval, ran, std): count_of_up_punches = 0 count_of_down_punches = 0 transdata = data[::-1].reset_index(drop=True) MSE_STD = [[transdata.Close[i: interval + i].mean(), transdata.Close[i: interval + i].std()] for i in range(ran)] up = [] down = [] for i in MSE_STD: up.append(i[0] + std * i[1]) down.append(i[0] - std * i[1]) for i in range(ran): if transdata.High[i] > up[i]: count_of_up_punches += 1 if transdata.Low[i] < down[i]: count_of_down_punches += 1 trend_bolB = trend(data, ran)[0] return trend_bolB, count_of_up_punches, count_of_down_punches
def dispcal(data, samprate, fcs, hs, gap, volts, displ, bfc=0.0, bm=0): dt = 1.0 / samprate npts = len(data) npol = 3 perc1 = 50 perc2 = 95 fac = volts / displ * 0.001 # remove offset using the first 6 seconds that have to be quiet! z = np.ones(npts) offset = np.sum(data[0:int(6.0*samprate)]) data = data - offset # apply butterworth low pass filter if required if bfc > 0.0 and bm > 0: bt0 = 1.0 / bfc mm = int(bm / 2.0) # if odd order apply first order low pass if bm > 2 * mm: f0_lp1, f1_lp1, f2_lp1, g1_lp1, g2_lp1 = lp1(bt0*samprate) data = rekfl(data, npts, f0_lp1, f1_lp1, f2_lp1, g1_lp1, g2_lp1) # now apply second order low pass for i in range(mm): h = np.sin(np.pi / 2.0 * (2.0 * i - 1.0) / bm) f0_lp2, f1_lp2, f2_lp2, g1_lp2, g2_lp2 = lp2(bt0*samprate, h) data = rekfl(data, npts, f0_lp2, f1_lp2, f2_lp2, g1_lp2, g2_lp2) # report filtering print 'Input data filtered with low pass Butterworth filter:' print ' corner frequency: ' + str(bfc) + 'Hz' print ' order: ' + str(bm) + '.' raw = data.copy() # deconvolution to velocity step # done by appling an exact invers second order high pass filter (infinit period) t0 = 1.0e12 h = 1.0 t0s = 1.0 / fcs f0_he2, f1_he2, f2_he2, g1_he2, g2_he2 = he2(t0/dt, t0s/dt, h, hs) data = rekfl(data, npts, f0_he2, f1_he2, f2_he2, g1_he2, g2_he2) data = politrend(npol, data, npts, z) x = data.copy() deconv = data.copy() # store deconvolves data np.savetxt('dispcal_vel.txt', deconv) # loop over fife trial values if no gap is given if gap > 0.0: onegap = True ngap = 1 else: #gap = 0.125 #onegap = False #ngap = 5 print 'parameter "gap" not specified!' sys.exit(1) for k in range(ngap): avglen = 6.0 * gap iab = int(gap / dt) iavg = int(avglen / dt) data = x z = np.ones(npts) P0 = np.zeros(npts) y = np.zeros(npts) ja = np.ones(99) # search for quiet section of minimum length 2*iab+1 samples P = krum(data, npts, 2*iab+1, P0) for j in range(npts): P[j] = np.log10(max(P[j], 1.0e-6)) P1 = P.copy() # import ipdb; ipdb.set_trace() z = heapsort(npts, P1) n50 = int(perc1 * npts / 100.0) n95 = int(perc2 * npts / 100.0) zlim = 0.5 * (z[n50] + z[n95]) P = P - zlim z = z - zlim # create trigger z = 0 if quiet, z = 1 if pulse for j in range(npts): if P[j] < 0: z[j] = 1.0 else: z[j] = 0.0 #zn1 = z[npts-1] #zn = z[n] #z[n-1] = -0.4 #z[n] = 1.4 #z[n-1] = zn1 #z[n] = zn data = politrend(npol, data, npts, z) data_c = data.copy() # cut out pulses quiet = data * z # identifiy pulses number = 0 i = 0 while i < npts and z[i] < 0.5: i = i + 1 n1 = i while i < npts and z[i] > 0.5: i = i + 1 n2 = i - 1 if i == npts: print 'No pulses found! Check dispcal.??? for pulses!' sys.exit(1) # continue if interval is too small (< gap) while n2 < n1 + iab: while i < npts and z[i] < 0.5: i = i + 1 n1 = i while i < npts and z[i] > 0.5: i = i + 1 n2 = i - 1 # now we have the first (n1) and the last (n2) sample of the first quiet interval if i == n: print 'No pulses found! Check dispcal.??? for pulses!' sys.exit(1) # now continue with the next pulses steps = [] marks = [] while i < npts: while i < npts and z[i] < 0.5: i = i + 1 if i == npts: break n3 = i while i < npts and z[i] > 0.5: i = i + 1 if i == npts: break n4 = i - 1 # continue if interval is too small (< gap) while n4 < n3 + iab: while i < npts and z[i] < 0.5: i = i + 1 if i == npts: break n3 = i while i < npts and z[i] > 0.5: i = i + 1 if i == npts: break n4 = i - 1 # now we have the first (n1) and the last (n2) sample of the next quiet interval number = number + 1 if number == 1: data_c = zspline('zsp', 1, n2, n3, npts, data_c, npts) n2null = n2 elif n4 >= n3+iab: data_c = zspline('zsp', n1, n2, n3, npts, data_c, npts) n3null = n3 marks.append((n2,n3-1)) print 'found pulse # ' + str(number) + ': from sample ' + str(n2) + ' to sample ' + str(n3 - 1) n1a = 0 n2a = n2 - n1 n3a = n3 - n1 n4a = n4 - n1 n1a = max(n1a, n2a-iavg) n4a = min(n4a, n3a+iavg) for ii in range(n4a): y[ii] = data[n1-1+ii] y_z = zspline('zsp', n1a, n2a, n3a, n4a, y, n4a) y_int = integrate(y_z, npts, dt) stp = step(y_int, n1a, n2a, n3a, n4a, n4a) n1 = n3 n2 = n4 steps.append(stp) dtr = trend('tre', 0, n2-n1+1, 0, 0, data_c[n1:], npts-n1) data2 = data_c.copy() for i in np.arange(n1, npts, 1): data2[i] = dtr[i-n1] y = data_c * z ymin = 0.0 ymax = 0.0 for i in np.arange(n2null+1, n3null-1,1): ymin = min(ymin,y[i]) ymax = max(ymax,y[i]) for i in range(n2null): y[i] = min(ymax, max(ymin, y[i])) for i in np.arange(n3null, npts, 1): y[i] = min(ymax, max(ymin, y[i])) np.savetxt('dispcal_res.txt', y) y_dis = integrate(data2, npts, dt) # remove offset from displacement offset = np.mean(y_dis) y_dis = y_dis - offset displ = y_dis.copy() np.savetxt('dispcal_dis.txt', displ) ############################################################## # Statistics total = sum(np.abs(steps)) avgs = total / number sigma = np.std(np.abs(steps)) print '' print '-------------------------------------------------------------' print 'Raw average step: ' + str(np.round(avgs,3)) + ' +/- ' + str(np.round(sigma, 3)) print 'Raw generator constant: ' + str(np.round((avgs * fac) * 1e6,3)) + ' +/- ' + str(np.round((sigma * fac) *1e6, 3)) + 'Vs/m' print '-------------------------------------------------------------' if number <= 2: return raw, deconv, displ, marks # import ipdb; ipdb.set_trace() ja = np.ones(len(steps)) for nrest in np.arange(number-1,int(float(number+1.0)/2.0),-1): siga = sigma kmax = np.argmax(steps) ja[kmax] = 0 total = 0 #for k in range(number): # total = total + ja[k] * steps[k] stepsa = ja * steps total = sum(np.abs(stepsa)) #print total avgs = total / nrest #print avgs total = 0 for k in range(number): total = total + ja[k] * (abs(steps[k]) - avgs)**2 sigma = np.sqrt(total / nrest) print 'Pulse ' + str(kmax) + ' eliminated; ' + str(nrest) + ' pulses remaning: ' + str(np.round((avgs * fac) * 1e6,3)) + ' +/- ' + str(np.round((sigma * fac) *1e6, 3)) + 'Vs/m' # if sigma < 0.05*avgs and siga-sigma < siga/ nrest: break steps[kmax] = 0.0 print '------------------------------------------------------------' print '' return raw, deconv, displ, marks
from market import * from market import supports most_goals = max(map(partition(goals, 'team'), len)) total_goals = len(goals) first_goal = attr(nth(goals, 0), 'team') assert supports(result, most_goals) assert supports(result, total_goals) assert supports(trend, most_goals) assert supports(trend, total_goals) assert supports(selns, most_goals) assert supports(selns, total_goals) print result(most_goals, football_state) print result(most_goals, football_nil) print selns(most_goals) print trend(most_goals, football_nil, football_state) print trend(most_goals, football_nil, football_nil) print result(total_goals, football_state) print result(total_goals, football_nil) print selns(total_goals) print trend(total_goals, football_nil, football_state) calculate = gfootball(football_state) print calculate(most_goals) print calculate(total_goals) print calculate(first_goal)
def hello_http(request): """HTTP Cloud Function. Args: request (flask.Request): The request object. <http://flask.pocoo.org/docs/1.0/api/#flask.Request> Returns: The response text, or any set of values that can be turned into a Response object using `make_response` <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>. """ request_json = request.get_json(silent=True) request_args = request.args # extracting the intent parameters from the json intent = _get_value(request_json, 'intent') table = _get_value(request_json, 'table') metric = _get_value(request_json, 'metric') dimensions = _get_value(request_json, 'dimensions') summary_operator = _get_value(request_json, 'summaryOperator') slices = _get_value(request_json, 'slices') is_asc = _get_value(request_json, 'isAsc') k = _get_value(request_json, 'topKLimit') slices = _get_value(request_json, 'slices') slice_comparision_arg = _get_value(request_json, 'comparisonValue') time_comparision_arg = _get_value(request_json, 'compareDateRange') date = _get_value(request_json, 'dateRange') time_granularity = _get_value(request_json, 'timeGranularity') correlation_metrics = _get_value(request_json, 'correlationMetrics') rangeA1Notation = _get_value(request_json, 'rangeA1Notation') # Converting the list of list into a pandas dataframe. query_table = [] for row in range(1, len(table)): if row != 0: query_table.append(table[row]) query_table_dataframe = pandas.DataFrame(query_table, columns=table[0]) (all_dimensions, all_metrics) = _list_all_dimensions_metrics(query_table_dataframe, dimensions, metric) # Remove empty columns query_table_dataframe = remove_empty_columns(query_table_dataframe) # Remove duplicate named columns query_table_dataframe = remove_duplicate_named_columns( query_table_dataframe) # Converting the variables that contain denote the # date range into the desired format. date_column_name = None date_range = None day_first = None if date != None: date_columns = request_json['dateColumns'] date_column_name = date['dateCol'] date_range = (date['dateStart'], date['dateEnd']) day_first = date_columns[date_column_name]['day_first'] # Converting the Slices passed in the json into a # list of tuples (col, operator, val) slices_list = None if slices != None: slices_list = [] for item in slices: val = item['sliceVal'] col = item['sliceCol'] operator = _str_to_filter_enum(item['sliceOp']) slices_list.append((col, operator, val)) if dimensions == 'null': dimensions = None if slice_comparision_arg is not None: slice_compare_column = slice_comparision_arg['comparisonColumn'] slice1 = slice_comparision_arg['slice1'] slice2 = slice_comparision_arg['slice2'] if time_comparision_arg is not None: time_compare_column = time_comparision_arg['dateCol'] date_range1 = (time_comparision_arg['dateStart1'], time_comparision_arg['dateEnd1']) date_range2 = (time_comparision_arg['dateStart2'], time_comparision_arg['dateEnd2']) day_first = request_json['dateColumns'][time_compare_column][ 'day_first'] if metric == 'null': metric = None summary_operator = _str_to_summary_operator_enum(summary_operator) time_granularity = _str_to_time_granularity_enum(time_granularity) suggestions = [] wrong_points_suggestion = wrong_points.wrong_points(query_table_dataframe) if intent == 'show': query_table_dataframe = show(query_table_dataframe, slices=slices_list, metric=metric, dimensions=dimensions, summary_operator=summary_operator, date_column_name=date_column_name, day_first=day_first, date_range=date_range) if summary_operator == enums.SummaryOperators.MEAN: suggestions.append(get_hardcoded_mean_vs_median_suggestion()) updated_suggestions = [] for suggestion in suggestions: updated_suggestion = suggestion if 'change_list' in suggestion.keys(): updated_suggestion['json'] = func(request_json, suggestion['change_list']) updated_suggestions.append(updated_suggestion) suggestions = updated_suggestions elif intent == 'topk': query_result = topk.topk(query_table_dataframe, metric, dimensions, is_asc, k, summary_operator=summary_operator, slices=slices_list, date_column_name=date_column_name, day_first=day_first, date_range=date_range) query_table_dataframe = query_result[0] suggestions = query_result[1] updated_suggestions = [] for suggestion in suggestions: updated_suggestion = suggestion if 'change_list' in suggestion.keys(): updated_suggestion['json'] = func(request_json, suggestion['change_list']) updated_suggestion['oversight'] = updated_suggestion[ 'oversight'].name updated_suggestions.append(updated_suggestion) suggestions = updated_suggestions elif intent == 'slice_compare': query_result = slice_compare.slice_compare( query_table_dataframe, metric, all_dimensions, all_metrics, slice_compare_column, slice1, slice2, summary_operator, date_column_name=date_column_name, date_range=date_range, day_first=day_first, slices=slices_list, dimensions=dimensions) query_table_dataframe = query_result[0] suggestions = query_result[1] updated_suggestions = [] for suggestion in suggestions: updated_suggestion = suggestion if 'change_list' in suggestion.keys(): updated_suggestion['json'] = func(request_json, suggestion['change_list']) updated_suggestion['oversight'] = updated_suggestion[ 'oversight'].name updated_suggestions.append(updated_suggestion) suggestions = updated_suggestions elif intent == 'time_compare': query_result = time_compare.time_compare(query_table_dataframe, metric, all_dimensions, time_compare_column, date_range1, date_range2, day_first, summary_operator, slices=slices_list, dimensions=dimensions) query_table_dataframe = query_result[0] suggestions = query_result[1] updated_suggestions = [] for suggestion in suggestions: updated_suggestion = suggestion if 'change_list' in suggestion.keys(): updated_suggestion['json'] = func(request_json, suggestion['change_list']) updated_suggestion['oversight'] = updated_suggestion[ 'oversight'].name updated_suggestions.append(updated_suggestion) suggestions = updated_suggestions elif intent == 'correlation': query_table_dataframe = correlation.correlation( query_table_dataframe, correlation_metrics['metric1'], correlation_metrics['metric2'], slices=slices_list, date_column_name=date_column_name, day_first=day_first, date_range=date_range, dimensions=dimensions) elif intent == 'trend': query_table_dataframe = trend.trend(query_table_dataframe, metric, time_granularity, summary_operator, date_column_name=date_column_name, day_first=day_first, date_range=date_range, slices=slices_list) else: raise Exception("Intent name does not match") if wrong_points_suggestion is not None: wrong_points_suggestion['oversight'] = wrong_points_suggestion[ 'oversight'].name suggestions = [wrong_points_suggestion] + suggestions final_table = [] # converting into a json object and returning final_table = query_table_dataframe.values.tolist() final_table.insert(0, list(query_table_dataframe.columns.values)) json_ret = {'outputTable': final_table, 'suggestions': suggestions} if rangeA1Notation is not None: all_row_labels = _get_all_row_labels(rangeA1Notation) all_column_labels = _get_all_column_labels(rangeA1Notation) cheader_to_clabel = _get_cheader_to_clabel(table, all_column_labels) if slices_list is not None: json_ret[ 'slicing_passed_list'] = insert_as_column.insert_as_column_show( table, cheader_to_clabel, all_row_labels[0], all_row_labels[-1], all_column_labels[0], all_column_labels[-1], slices=slices_list) if intent == 'topk' and summary_operator is None: filter_column_label_number = _get_number_of_column_label( all_column_labels[-1]) + 1 filter_column_label = _get_label_from_number( filter_column_label_number) json_ret[ 'list_topk_indices'] = insert_as_column.insert_as_column_topk_column( table, cheader_to_clabel, all_row_labels[0], all_row_labels[-1], all_column_labels[0], all_column_labels[-1], filter_column_label, metric, is_asc, k) json_string = json.dumps(json_ret) return json_string
def dispcal(data, samprate, fcs, hs, gap, volts, displ, bfc=0.0, bm=0): dt = 1.0 / samprate npts = len(data) npol = 3 perc1 = 50 perc2 = 95 fac = volts / displ * 0.001 # remove offset using the first 6 seconds that have to be quiet! z = np.ones(npts) offset = np.sum(data[0:int(6.0 * samprate)]) data = data - offset # apply butterworth low pass filter if required if bfc > 0.0 and bm > 0: bt0 = 1.0 / bfc mm = int(bm / 2.0) # if odd order apply first order low pass if bm > 2 * mm: f0_lp1, f1_lp1, f2_lp1, g1_lp1, g2_lp1 = lp1(bt0 * samprate) data = rekfl(data, npts, f0_lp1, f1_lp1, f2_lp1, g1_lp1, g2_lp1) # now apply second order low pass for i in range(mm): h = np.sin(np.pi / 2.0 * (2.0 * i - 1.0) / bm) f0_lp2, f1_lp2, f2_lp2, g1_lp2, g2_lp2 = lp2(bt0 * samprate, h) data = rekfl(data, npts, f0_lp2, f1_lp2, f2_lp2, g1_lp2, g2_lp2) # report filtering print 'Input data filtered with low pass Butterworth filter:' print ' corner frequency: ' + str(bfc) + 'Hz' print ' order: ' + str(bm) + '.' raw = data.copy() # deconvolution to velocity step # done by appling an exact invers second order high pass filter (infinit period) t0 = 1.0e12 h = 1.0 t0s = 1.0 / fcs f0_he2, f1_he2, f2_he2, g1_he2, g2_he2 = he2(t0 / dt, t0s / dt, h, hs) data = rekfl(data, npts, f0_he2, f1_he2, f2_he2, g1_he2, g2_he2) data = politrend(npol, data, npts, z) x = data.copy() deconv = data.copy() # store deconvolves data np.savetxt('dispcal_vel.txt', deconv) # loop over fife trial values if no gap is given if gap > 0.0: onegap = True ngap = 1 else: #gap = 0.125 #onegap = False #ngap = 5 print 'parameter "gap" not specified!' sys.exit(1) for k in range(ngap): avglen = 6.0 * gap iab = int(gap / dt) iavg = int(avglen / dt) data = x z = np.ones(npts) P0 = np.zeros(npts) y = np.zeros(npts) ja = np.ones(99) # search for quiet section of minimum length 2*iab+1 samples P = krum(data, npts, 2 * iab + 1, P0) for j in range(npts): P[j] = np.log10(max(P[j], 1.0e-6)) P1 = P.copy() # import ipdb; ipdb.set_trace() z = heapsort(npts, P1) n50 = int(perc1 * npts / 100.0) n95 = int(perc2 * npts / 100.0) zlim = 0.5 * (z[n50] + z[n95]) P = P - zlim z = z - zlim # create trigger z = 0 if quiet, z = 1 if pulse for j in range(npts): if P[j] < 0: z[j] = 1.0 else: z[j] = 0.0 #zn1 = z[npts-1] #zn = z[n] #z[n-1] = -0.4 #z[n] = 1.4 #z[n-1] = zn1 #z[n] = zn data = politrend(npol, data, npts, z) data_c = data.copy() # cut out pulses quiet = data * z # identifiy pulses number = 0 i = 0 while i < npts and z[i] < 0.5: i = i + 1 n1 = i while i < npts and z[i] > 0.5: i = i + 1 n2 = i - 1 if i == npts: print 'No pulses found! Check dispcal.??? for pulses!' sys.exit(1) # continue if interval is too small (< gap) while n2 < n1 + iab: while i < npts and z[i] < 0.5: i = i + 1 n1 = i while i < npts and z[i] > 0.5: i = i + 1 n2 = i - 1 # now we have the first (n1) and the last (n2) sample of the first quiet interval if i == n: print 'No pulses found! Check dispcal.??? for pulses!' sys.exit(1) # now continue with the next pulses steps = [] marks = [] while i < npts: while i < npts and z[i] < 0.5: i = i + 1 if i == npts: break n3 = i while i < npts and z[i] > 0.5: i = i + 1 if i == npts: break n4 = i - 1 # continue if interval is too small (< gap) while n4 < n3 + iab: while i < npts and z[i] < 0.5: i = i + 1 if i == npts: break n3 = i while i < npts and z[i] > 0.5: i = i + 1 if i == npts: break n4 = i - 1 # now we have the first (n1) and the last (n2) sample of the next quiet interval number = number + 1 if number == 1: data_c = zspline('zsp', 1, n2, n3, npts, data_c, npts) n2null = n2 elif n4 >= n3 + iab: data_c = zspline('zsp', n1, n2, n3, npts, data_c, npts) n3null = n3 marks.append((n2, n3 - 1)) print 'found pulse # ' + str(number) + ': from sample ' + str( n2) + ' to sample ' + str(n3 - 1) n1a = 0 n2a = n2 - n1 n3a = n3 - n1 n4a = n4 - n1 n1a = max(n1a, n2a - iavg) n4a = min(n4a, n3a + iavg) for ii in range(n4a): y[ii] = data[n1 - 1 + ii] y_z = zspline('zsp', n1a, n2a, n3a, n4a, y, n4a) y_int = integrate(y_z, npts, dt) stp = step(y_int, n1a, n2a, n3a, n4a, n4a) n1 = n3 n2 = n4 steps.append(stp) dtr = trend('tre', 0, n2 - n1 + 1, 0, 0, data_c[n1:], npts - n1) data2 = data_c.copy() for i in np.arange(n1, npts, 1): data2[i] = dtr[i - n1] y = data_c * z ymin = 0.0 ymax = 0.0 for i in np.arange(n2null + 1, n3null - 1, 1): ymin = min(ymin, y[i]) ymax = max(ymax, y[i]) for i in range(n2null): y[i] = min(ymax, max(ymin, y[i])) for i in np.arange(n3null, npts, 1): y[i] = min(ymax, max(ymin, y[i])) np.savetxt('dispcal_res.txt', y) y_dis = integrate(data2, npts, dt) # remove offset from displacement offset = np.mean(y_dis) y_dis = y_dis - offset displ = y_dis.copy() np.savetxt('dispcal_dis.txt', displ) ############################################################## # Statistics total = sum(np.abs(steps)) avgs = total / number sigma = np.std(np.abs(steps)) print '' print '-------------------------------------------------------------' print 'Raw average step: ' + str(np.round(avgs, 3)) + ' +/- ' + str( np.round(sigma, 3)) print 'Raw generator constant: ' + str(np.round( (avgs * fac) * 1e6, 3)) + ' +/- ' + str( np.round((sigma * fac) * 1e6, 3)) + 'Vs/m' print '-------------------------------------------------------------' if number <= 2: return raw, deconv, displ, marks # import ipdb; ipdb.set_trace() ja = np.ones(len(steps)) for nrest in np.arange(number - 1, int(float(number + 1.0) / 2.0), -1): siga = sigma kmax = np.argmax(steps) ja[kmax] = 0 total = 0 #for k in range(number): # total = total + ja[k] * steps[k] stepsa = ja * steps total = sum(np.abs(stepsa)) #print total avgs = total / nrest #print avgs total = 0 for k in range(number): total = total + ja[k] * (abs(steps[k]) - avgs)**2 sigma = np.sqrt(total / nrest) print 'Pulse ' + str(kmax) + ' eliminated; ' + str( nrest) + ' pulses remaning: ' + str( np.round((avgs * fac) * 1e6, 3)) + ' +/- ' + str( np.round((sigma * fac) * 1e6, 3)) + 'Vs/m' # if sigma < 0.05*avgs and siga-sigma < siga/ nrest: break steps[kmax] = 0.0 print '------------------------------------------------------------' print '' return raw, deconv, displ, marks
ola Valtercio bem vindo de volta. Estou a sua desposição. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ''') perg = str(input('Voce: ')) # entrada com o nome jarvis print() jarvis = '' if perg == 'não quero fazer nada': # comando para sair do Bot print() # para da espaços entre as linhas print('Ok senhor, até a proxima...') if perg == 'jarvis': # comando para iniciar o Bot jarvis = str( input('ola senhor o que deseja? ')) # escolha das funções do Bot. print() # para da espaços entre as linhas if jarvis == 'faca uma busca' or jarvis == 'faça uma busca': # Bot faz uma busca no google ou youtube google.google() if jarvis == 'abra um site' or jarvis == 'quero abrir um site': # Bot abrirá um site web.site() # funcao para abrir sites if jarvis == 'quero conversar': # para conversar com o Bot chat.conv() # funcao do chat if jarvis == 'faca uma busca na wikipedia' or jarvis == 'faça uma busca na wikipedia': # Bot pesquisa na wikipedia wikipedia.wiki() # funcao da wikipedia if jarvis == 'faca uma busca na trend': trend.trend()