def createHMap(self): self.maporigin = (self.bounds[0], self.bounds[1]) self.mapsize = (self.bounds[2] - self.bounds[0], self.bounds[3] - self.bounds[1]) self.tilesize = self.maptiles[0]["ncols"] self.resolution = self.maptiles[0]["cellsize"] print("OSGB origin: " + str(self.bounds[0]) + "," + str(self.bounds[1])) print("Contour map size: " + str(self.mapsize[0]) + "x" + str(self.mapsize[1])) self.x = np.arange(0, self.mapsize[0], self.resolution) self.y = np.arange(0, self.mapsize[1], self.resolution) self.x, self.y = np.meshgrid(self.x, self.y) self.z = np.zeros_like(self.x, dtype=np.float) for tile in self.maptiles: ieast = int((tile["W_GB"] - self.maporigin[0]) / self.resolution) inorth = int((tile["S_GB"] - self.maporigin[1]) / self.resolution) self.z[inorth : inorth + self.tilesize, ieast : ieast + self.tilesize] = np.flipud( np.genfromtxt(tile["filename"], dtype=np.float, delimiter=" ", skip_header=6) ) print("Read {} files".format(len(self.maptiles))) self.z[self.z < 0.0] = 0.0 return self.z
def relu(feature_map): #Preparing the output of the ReLU activation function. relu_out = np.zeros(feature_map.shape) for map_num in range(feature_map.shape[-1]): for r in np.arange(0,feature_map.shape[0]): for c in np.arange(0, feature_map.shape[1]): relu_out[r, c, map_num] = np.max([feature_map[r, c, map_num], 0]) return relu_out
def pooling(feature_map, size=2, stride=2): #Preparing the output of the pooling operation. pool_out = np.zeros((np.uint16((feature_map.shape[0]-size+1)/stride+1), np.uint16((feature_map.shape[1]-size+1)/stride+1), feature_map.shape[-1])) for map_num in range(feature_map.shape[-1]): r2 = 0 for r in np.arange(0,feature_map.shape[0]-size+1, stride): c2 = 0 for c in np.arange(0, feature_map.shape[1]-size+1, stride): pool_out[r2, c2, map_num] = np.max([feature_map[r:r+size, c:c+size, map_num]]) c2 = c2 + 1 r2 = r2 +1 return pool_out
def __init__(self, x, p, Nrl=1000): self.x = x self.pdf = p / p.sum() self.cdf = self.pdf.cumsum() self.inversecdfbins = Nrl self.Nrl = Nrl y = np.arange(Nrl) / float(Nrl) _delta = 1.0 / Nrl self.inversecdf = np.zeros(Nrl) self.inversecdf[0] = self.x[0] cdf_idx = 0 for n in range(1, self.inversecdfbins): while self.cdf[cdf_idx] < y[n] and cdf_idx < Nrl: cdf_idx += 1 # Seems a linear interpolation self.inversecdf[n] = (self.x[cdf_idx - 1] + (self.x[cdf_idx] - self.x[cdf_idx - 1]) * (y[n] - self.cdf[cdf_idx - 1]) / (self.cdf[cdf_idx] - self.cdf[cdf_idx - 1]) ) if cdf_idx >= Nrl: break self.delta_inversecdf = np.concatenate( (np.diff(self.inversecdf), [0]) )
def detect(self, data): try: frame = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) frame = imutils.resize(frame, width=400) cv2.imshow("Frame", frame) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (224, 224)), 0.007843, (224, 224), 127.5) # pass the blob through the network and obtain the detections and # predictions self.net.setInput(blob) detections = self.net.forward() rois = [] # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > self.args["confidence"]: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # draw the prediction on the frame label = "{}: {:.2f}%".format(self.CLASSES[idx], confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), self.COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.COLORS[idx], 2) if self.CLASSES[idx] is "person": rois.append((startX, startY, endX, endY)) self.pub_roi(rois) # show the output frame cv2.imshow("Frame", frame) #key = cv2.waitKey(1) & 0xFF cv2.waitKey(3) #Only publish if we've detected a person #if (len(pick) > 0): # try: self.image_pub.publish(self.bridge.cv2_to_imgmsg(frame, "bgr8"))
def loss(T, data): # given a (1, 10) vector of troop positions, T, what is the expected value of winning # needs to account for ties better. prizes = np.arange(1, 11, dtype=float) my_score = (data < T).dot(prizes) opp_score = (data > T).dot(prizes) return (my_score > opp_score).mean()
def encode(self): input_text = encode_c(self.encode_text.toPlainText()) if input_text == '': msgBox = QMessageBox() msgBox.setIcon(QMessageBox.Information) msgBox.setText("不能是空的") msgBox.setWindowTitle("系统警告") msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) msgBox.buttonClicked.connect(msgButtonClick) returnValue = msgBox.exec() if returnValue == QMessageBox.Ok: return length = 35 temp = [ input_text[i:i + length] for i in range(0, len(input_text), length) ] for i in range(len(temp)): length_payload = str(format(len(temp[i]), 'b')) # 7자리 if len(length_payload) < 8: length_payload = length_payload.zfill(8) print(temp[i]) total = preamble + length_payload + temp[i] signal = ','.join(list(total)) time_signal = N * len(total) / sampling_rate t = np.arange(0, time_signal, Ts) np_signal = np.fromstring(signal, dtype='int', sep=',') sample = np.repeat(np_signal, N) if (len(t) % 10) != 0: t = t[:len(t) - 1] y = np.sin(2 * np.pi * (f + sample * 2000) * t) write('first.wav', sampling_rate, y) samplerate, data = sio.wavfile.read('first.wav') sd.play(data, samplerate) time.sleep(time_signal + 1)
def plotHistogram(hist_count,path): histogram, bins = np.histogram(hist_count, bins=GRAYLEVEL) center = (bins[:-1] + bins[1:]) / 2 width = 0.50 ax = pyplot.subplot() ax.set_ylabel('Counts') ax.set_xlabel('Graylevel') ax.set_xticks(np.arange(0,GRAYLEVEL,30)) ax.set_title(('Histogram')) ax.bar(center, histogram, width=width, color='y') pyplot.savefig(path) pyplot.clf()
def conv_(img, conv_filter): filter_size = conv_filter.shape[1] result = np.zeros((img.shape)) #Looping through the image to apply the convolution operation. for r in np.uint16(np.arange(filter_size/2.0, img.shape[0]-filter_size/2.0+1)): for c in np.uint16(np.arange(filter_size/2.0, img.shape[1]-filter_size/2.0+1)): """ Getting the current region to get multiplied with the filter. How to loop through the image and get the region based on the image and filer sizes is the most tricky part of convolution. """ curr_region = img[r-np.uint16(np.floor(filter_size/2.0)):r+np.uint16(np.ceil(filter_size/2.0)), c-np.uint16(np.floor(filter_size/2.0)):c+np.uint16(np.ceil(filter_size/2.0))] #Element-wise multipliplication between the current region and the filter. curr_result = curr_region * conv_filter conv_sum = np.sum(curr_result) #Summing the result of multiplication. result[r, c] = conv_sum #Saving the summation in the convolution layer feature map. #Clipping the outliers of the result matrix. final_result = result[np.uint16(filter_size/2.0):result.shape[0]-np.uint16(filter_size/2.0), np.uint16(filter_size/2.0):result.shape[1]-np.uint16(filter_size/2.0)] return final_result
def createHMap(self): self.maporigin = (self.bounds[0],self.bounds[1]) self.mapsize = (self.bounds[2]-self.bounds[0],self.bounds[3]-self.bounds[1]) self.tilesize = self.maptiles[0]['ncols'] self.resolution = self.maptiles[0]['cellsize'] print ('OSGB origin: '+str(self.bounds[0])+','+str(self.bounds[1])) print ('Contour map size: '+str(self.mapsize[0])+'x'+str(self.mapsize[1])) self.x = np.arange(0, self.mapsize[0], self.resolution) self.y = np.arange(0, self.mapsize[1], self.resolution) self.x,self.y = np.meshgrid(self.x,self.y) self.z = np.zeros_like(self.x,dtype=np.float) for tile in self.maptiles: ieast = int((tile['W_GB']-self.maporigin[0])/self.resolution) inorth = int((tile['S_GB']-self.maporigin[1])/self.resolution) self.z[inorth:inorth+self.tilesize,ieast:ieast+self.tilesize] = np.flipud(np.genfromtxt(tile['filename'],dtype=np.float,delimiter=' ',skip_header=6)) print ('Read {} files'.format(len(self.maptiles))) self.z[self.z<0.0] = 0.0 return self.z
def test_3D(self, sc, dtype): a3d = np.arange(12).reshape((2, 3, 2)) self.assertIdenticalArray(sc(a3d), np.array(a3d, dtype=dtype))
def test_2D(self, sc, dtype): a2d = np.arange(12).reshape((3, 4)) self.assertIdenticalArray(sc(a2d), np.array(a2d, dtype=dtype))
def test_2D(self, sc, dtype): a2d = np.arange(12).reshape((3,4)) self.assertIdenticalArray(sc(a2d), np.array(a2d, dtype=dtype))
import operator import csv import math form_class = uic.loadUiType("gui_for_translate.ui")[0] sampling_rate = 48000 symbol_duration = 0.025 f = 4000 N = sampling_rate * symbol_duration Ts = 1 / sampling_rate FILENAME = "tmp.wav" preamble = '01010101010101010101' preamble_signal = ','.join(list(preamble)) preamble_time_signal = N * len(preamble) / sampling_rate preamble_t = np.arange(0, preamble_time_signal, Ts) preamble_np_signal = np.fromstring(preamble_signal, dtype='int', sep=',') preamble_sample = np.repeat(preamble_np_signal, N) preamble_y = np.sin(2 * np.pi * (f + preamble_sample * 2000) * preamble_t) def encode_c(s): return ''.join([bin(ord(c)).replace('0b', '') for c in s]) def decode_c(s): return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]]) class ThreadClass(QThread): def __init__(self):
#Function to compute the mean of absolute values of non-diagonal elements def meanNonDiag(whitenedMatrix): whitenedCov = np.abs(np.cov(whitenedMatrix)) sumNonDiag = np.sum(whitenedCov) - np.trace(whitenedCov) n = whitenedCov.shape[0] return sumNonDiag / (n * n - n) meanNonDiagCleanFromClean = meanNonDiag(YCleanFromClean) meanNonDiagNoisyFromClean = meanNonDiag(YNoisyFromClean) meanNonDiagNoisyFromNoisy = meanNonDiag(YNoisyFromNoisy) meanNonDiagCleanFromNoisy = meanNonDiag(YCleanFromNoisy) freq = np.arange(1, 129) """ # Spectrograms for (a) figure, axes = plt.subplots(2, 2) axes[0,0].pcolor(timeClean, freq, specClean) axes[0,0].set(xlabel='Time', ylabel='Frequency') axes[0,0].set_title('Original Clean Spectrogram') axes[1,0].pcolor(timeClean, freq, YCleanFromClean) axes[1,0].set(xlabel='Time', ylabel='Frequency') axes[1,0].set_title('Clean Whitened using Clean') axes[0,1].pcolor(timeNoisy, freq, specNoisy) axes[0,1].set(xlabel='Time', ylabel='Frequency') axes[0,1].set_title('Original Noisy Spectrogram')
def test_2D(self, sc, dtype): a2d = np.arange(12).reshape((3, 4)) self.assertIdenticalArray( sc[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], np.array(a2d, dtype=dtype))
def test_2D(self): a2d = np.arange(12).reshape((3, 4)) self.assertIdenticalArray(np(a2d), np.array(a2d))
# 2Write a NumPy program to compute the determinant of an array x = np.array([[1, 2], [3, 4]]) print("original arr:") print(x) result = np.linalg.det(x) print("determinant:") print(result, '\n') # 3 Write a NumPy program to compute the sum of the diagonal element of a given array x = np.array([[2, 4, 5], [8, 9, 10], [12, 15, 16]]) print(x, "matrix:") diag = np.diagonal(x) print("\nDiagonal matrix:") print(diag) print("\nSum of diagonal matrix:") print(sum(diag)) # 4Write a NumPy program to compute the inverse of a given matrix x = np.array([[1, 2], [3, 4]]) print("original matrix:") print(x) result = np.linalg.inv(x) print("inverse matrix:") print(result, "\n") #5Write a NumPy program to generate matrix and write it to a file, then again read from file that matrix. x = np.arange(15) np.save('another_x', x) read_file_info = np.load('another_x.npy') print(read_file_info)
data = StingIO("1 2 3\n 4 5 6") data = StringIO("1 2 3\n 4 5 6") np.genfromtxt(data,dtypr=(int,float,int)) np.genfromtxt(data,dtype=(int,float,int)) convertfunc = lambda x: float(x.strip(b"%"))/100. data = u"1,2.3%,45.\n6,78.9%,0" np.genfromtxt(StringIO(data),delimiter='',names=names,converters={1:convertfunc}) names = ['a','b','c'] np.genfromtxt(StringIO(data),delimiter='',names=names,converters={1:convertfunc}) data = u"N/A, 2, 3\n4,,???" kwargs = dict(delimiter=',',dtype=int,names='a,b,c',missing_values={0:"N"}) kwargs = dict(delimiter=',',dtype=int,names='a,b,c',missing_values={0:"N/A",'b':0,2:"???"},filling_values={0:0,'b':0,2:-999}) np.gemfromtxt(SringIO(data),**kwargs) np.genfromtxt(SringIO(data),**kwargs) np.genfromtxt(StringIO(data),**kwargs) x = np.arange(10,1,-1) x y = np.linspace(1,19,4) y x[np.array([3,3,1,8])] z = x[np.array([3,3,1,8])] z.base z.base() y = x y.base y.base() np.base(y) x.base z.flags.owndata y.flags.owndata x.flags.owndata
def test_3D(self): a3d = np.arange(12).reshape((2,3,2)) self.assertIdenticalArray(np(a3d), np.array(a3d))
def test_2D(self): a2d = np.arange(12).reshape((3,4)) self.assertIdenticalArray(np(a2d), np.array(a2d))
def test_3D(self): a3d = np.arange(12).reshape((2,3,2)) self.assertIdenticalArray(np[[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]], np.array(a3d))
def test_2D(self): a2d = np.arange(12).reshape((3,4)) self.assertIdenticalArray(np[[0,1,2,3],[4,5,6,7],[8,9,10,11]], np.array(a2d))
def test_3D(self, sc, dtype): a3d = np.arange(12).reshape((2,3,2)) self.assertIdenticalArray(sc(a3d), np.array(a3d, dtype=dtype))
def test_2D(self): a2d = np.arange(12).reshape((3, 4)) self.assertIdenticalArray( np[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], np.array(a2d))
def test_3D(self): a3d = np.arange(12).reshape((2, 3, 2)) self.assertIdenticalArray( np[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]], np.array(a3d))
1595151900, 1595151960, ]) #设置相似度 y = np.array([ 0.8579087793827057, 0.8079087793827057, 0.9679087793827057, 0.679087793827057, 0.5579087793827057, 0.9579087793827057, 0.3079087793827057, 0.3009087793827057, 0.2579087793827057, 0.2009087793827057, -0.1999087793827057, 0.9579087793827057, 0.0099087793827057, 0.0079087793827057, 0.0069087793827057, 0.0019087793827057, 0.0000087793827057 ]) #插值法之后的x轴值,表示从0到10间距为0.5的200个数 xnew = np.arange(1595151000, 1595151960, 0.1) #实现函数 func = interpolate.interp1d(x, y, kind='cubic') #利用xnew和func函数生成ynew,xnew数量等于ynew数量 ynew = func(xnew) # 原始折线 plt.plot(x, y, "r", linewidth=1) #平滑处理后曲线 plt.plot(xnew, ynew) #设置x,y轴代表意思 plt.xlabel("The distance between POI and user(km)") plt.ylabel("probability")
def test_3D(self): a3d = np.arange(12).reshape((2, 3, 2)) self.assertIdenticalArray(np(a3d), np.array(a3d))
detector_shape = (2048, 2048) pixel_area = detector_shape detector_area = (plate_scale ** 2 * detector_shape[0] * detector_shape[1]) / 3600. nstars = int(round(detector_area * (scmodel.integral_counts(magmax) - scmodel.integral_counts(magmin)))) print('nstars', nstars) seeing = 1.0 scale = 2 * sqrt(2 * log(2)) delta = 0.1 steps = int(round((magmax - magmin) / delta)) x = magmin + delta * np.arange(steps) p = np.array([scmodel.differential_counts(i) for i in x]) g = GeneralRandom(x, p) mags = g.random(nstars) y = np.random.uniform(high=pixel_area[0], size=nstars) x = np.random.uniform(high=pixel_area[1], size=nstars) np.random.seed(100) x = np.random.uniform(high=2048, size=nstars) y = np.random.uniform(high=2048, size=nstars) sigmas = [seeing / scale / plate_scale] * nstars ints = [100] * nstars mag = g.random(N=nstars)
def test_3D(self, sc, dtype): a3d = np.arange(12).reshape((2, 3, 2)) self.assertIdenticalArray( sc[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]], np.array(a3d, dtype=dtype))
def test_2D(self, sc, dtype): a2d = np.arange(12).reshape((3,4)) self.assertIdenticalArray(sc[[0,1,2,3],[4,5,6,7],[8,9,10,11]], np.array(a2d, dtype=dtype))
import os import asyncio import datetime import random import functools import websockets import np from http import HTTPStatus MIME_TYPES = {"html": "text/html", "js": "text/javascript", "css": "text/css"} sampling_rate = 1000 freq = 5 samples = 1000 x = np.arange(samples) y = 100 * np.sin(2 * np.pi * freq * x / sampling_rate) # print(y) async def process_request(path, request_headers): """Serves a file when doing a GET request with a valid path.""" if "Upgrade" in request_headers: return # Probably a WebSocket connection if path == '/': path = '/index.html' response_headers = [ ('Server', 'asyncio websocket server'), ('Connection', 'close'),
infile.close() return n_flows, cardinality, flow_set_coverage, heavy_hitter_detection_f1_score, heavy_hitter_are, flow_size_estimation if __name__ == "__main__": hf_n_flows, hf_cardinality, hf_flowset_coverage, hf_heavy_hitter_detection_f1_score, hf_heavy_hitter_are, hf_flow_size_estimation, = parse_file( "./HashFlow.txt") hp_n_flows, hp_cardinality, hp_flowset_coverage, hp_heavy_hitter_detection_f1_score, hp_heavy_hitter_are, hp_flow_size_estimation, = parse_file( "./HashPipe.txt") es_n_flows, es_cardinality, es_flowset_coverage, es_heavy_hitter_detection_f1_score, es_heavy_hitter_are, es_flow_size_estimation, = parse_file( "./ElasticSketch.txt") fr_n_flows, fr_cardinality, fr_flowset_coverage, fr_heavy_hitter_detection_f1_score, fr_heavy_hitter_are, fr_flow_size_estimation, = parse_file( "./FlowRadar.txt") plt.figure(1) plt.xticks(np.arange(0, 250001, 50000), ("0", "50K", "100K", "150K", "200K", "250K")) plt.plot(hf_n_flows, hf_flow_size_estimation, label="HashFlow", marker="x", markersize=10) plt.plot(hp_n_flows, hp_flow_size_estimation, label="HashPipe", marker="^", markersize=10) plt.plot(es_n_flows, es_flow_size_estimation, label="ElasticSketch", marker="<",
def gaussian_kernel_1d(sigma): kernel_radius = np.ceil(sigma) * 3 kernel_size = kernel_radius * 2 + 1 ax = np.arange(-kernel_radius, kernel_radius + 1., dtype=np.float32) kernel = np.exp(-(ax**2) / (2. * sigma**2)) return (kernel / np.sum(kernel)).reshape(1, kernel.shape[0])
def test_3D(self, sc, dtype): a3d = np.arange(12).reshape((2,3,2)) self.assertIdenticalArray(sc[[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]], np.array(a3d, dtype=dtype))
x_data = [10, 15, 20, 22.5] #second y_data = [2278.04, 362.78, 517.35, 602.78] def forward(x): return x * w def loss(x, y): y_pred = forward(x) return (y_pred - y) * (y_pred - y) mse_list = [] w_list = [] for w in np.arange(10, 22.5, 2.4): print("w=", w) l_sum = 0.0 for x_val, y_val in zip(x_data, y_data): y_pred_val = forward(x_val) l = loss(x_val, y_val) l_sum += l print("\t", x_val, y_val, y_pred_val, l) print("MSE=", l_sum / 3) w_list.append(w) mse_list.append(l_sum / 3) plt.plot(w_list, mse_list) plt.ylabel("Loss") plt.xlabel("w")
infile.close() return n_flows, cardinality, flow_set_coverage, heavy_hitter_f1_score, heavy_hitter_are, flow_size_estimation if __name__ == "__main__": basic_n_flows, basic_cardinality, basic_flowset_coverage, basic_heavy_hitter_f1_score, basic_heavy_hitter_are, basic_flow_size_estimation = parse_file( "./result_basic_hashflow_84454.txt") _06_n_flows, _06_cardinality, _06_flowset_coverage, _06_heavy_hitter_f1_score, _06_heavy_hitter_are, _06_flow_size_estimation = parse_file( "./result_06_hashflow_84454.txt") _07_n_flows, _07_cardinality, _07_flowset_coverage, _07_heavy_hitter_f1_score, _07_heavy_hitter_are, _07_flow_size_estimation = parse_file( "./result_07_hashflow_84454.txt") _08_n_flows, _08_cardinality, _08_flowset_coverage, _08_heavy_hitter_f1_score, _08_heavy_hitter_are, _08_flow_size_estimation = parse_file( "./result_08_hashflow_84454.txt") plt.figure(1) plt.xticks(np.arange(0, 60001, 10000), ("0", "10K", "20K", "30K", "40K", "50K", "60K")) plt.plot(basic_n_flows[0:6], basic_flow_size_estimation[0:6], label="Multi-hash", marker="x", markerfacecolor="none", markersize=markersize, color="blue") plt.plot(_06_n_flows[0:6], _06_flow_size_estimation[0:6], label=r"$\alpha$=0.6", marker="^", markerfacecolor="none", markersize=markersize, color="red")
z = hdata.createHMap() np.savetxt("foo.csv", z, delimiter=",") #Define contour levels hlims = [np.amin(z), np.amax(z)] hrange = np.ptp(z) cint = max(PREF_INTERVALS) layers = int(np.ceil(hrange/cint)) for try_i in PREF_INTERVALS: try_layers = int(np.ceil(hrange/try_i)) if try_layers > layers and try_layers < DEF_MAX_LAYERS: cint = try_i layers = try_layers print('{} layers. {}m interval'.format(layers,cint)) cheights = np.arange(np.floor(hlims[0]/cint)*cint, np.ceil(hlims[1]/cint)*cint, cint) #Generate contours contourdata = cntr.Cntr(hdata.x, hdata.y, z) cvs = pyx.canvas.canvas() cvs.text(0, 0, "Hello, world!") cvs.stroke(pyx.path.line(0, 0, 2, 0)) for cheight in cheights: clist = contourdata.trace(cheight, cheight, 0) clist = clist[:len(clist)//2] print('Level {}m, {} contours'.format(cheight,len(clist))) for contour in clist: contour = contour*SCALE path = [pyx.path.moveto(*contour[0])] + [pyx.path.lineto(*c) for c in contour[1:]]
def query(self, n, model, train_dataset, pool_dataset, budget=10000): device = model.state_dict()['softmax.bias'].device full_dataset = ConcatDataset([pool_dataset, train_dataset]) pool_len = len(pool_dataset) self.embeddings = self.get_embeddings(model, device, full_dataset) # Calc distance matrix num_images = self.embeddings.shape[0] dist_mat = self.calc_distance_matrix(num_images) # We need to get k centers start with greedy solution upper_bound = gb.UB lower_bound = upper_bound / 2.0 max_dist = upper_bound _x, _y = np.where(dist_mat <= max_dist) _distances = dist_mat[_x, _y] subset = [i for i in range(1)] model = solve_fac_loc(_x, _y, subset, num_images, budget) # model.setParam( 'OutputFlag', False ) x, y, z = model.__data delta = 1e-7 while upper_bound - lower_bound > delta: print("State", upper_bound, lower_bound) current_radius = (upper_bound + lower_bound) / 2.0 violate = np.where(_distances > current_radius) # Point distances which violate the radius new_max_d = np.min(_distances[_distances >= current_radius]) new_min_d = np.max(_distances[_distances <= current_radius]) print("If it succeeds, new max is:", new_max_d, new_min_d) for v in violate[0]: x[_x[v], _y[v]].UB = 0 # The upper bound for points, which violate the radius are set to zero model.update() r = model.optimize() if model.getAttr(gb.GRB.Attr.Status) == gb.GRB.INFEASIBLE: failed = True print("Infeasible") elif sum([z[i].X for i in range(len(z))]) > 0: failed = True print("Failed") else: failed = False if failed: lower_bound = max(current_radius, new_max_d) # failed so put edges back for v in violate[0]: x[_x[v], _y[v]].UB = 1 else: print("solution found", current_radius, lower_bound, upper_bound) upper_bound = min(current_radius, new_min_d) model.write("s_{}_solution_{}.sol".format(budget, current_radius)) idxs_labeled = np.arange(start=pool_len, stop=pool_len + len(train_dataset)) # Perform kcenter greedy self.update_distances(idxs_labeled, idxs_labeled, only_new=False, reset_dist=True) sel_ind = [] for _ in range(n): ind = np.argmax(self.min_distances) # Get sample with highest distance assert ind not in idxs_labeled, "Core-set picked index already labeled" self.update_distances([ind], idxs_labeled, only_new=True, reset_dist=False) sel_ind.append(ind) assert len(set(sel_ind)) == len(sel_ind), "Core-set picked duplicate samples" remaining_ind = list(set(np.arange(pool_len)) - set(sel_ind)) return sel_ind, remaining_ind
np.savetxt("foo.csv", z, delimiter=",") #Define contour levels hlims = [np.amin(z), np.amax(z)] hrange = np.ptp(z) cint = max(PREF_INTERVALS) layers = int(np.ceil(hrange / cint)) for try_i in PREF_INTERVALS: try_layers = int(np.ceil(hrange / try_i)) if try_layers > layers and try_layers < DEF_MAX_LAYERS: cint = try_i layers = try_layers print('{} layers. {}m interval'.format(layers, cint)) cheights = np.arange( np.floor(hlims[0] / cint) * cint, np.ceil(hlims[1] / cint) * cint, cint) #Generate contours contourdata = cntr.Cntr(hdata.x, hdata.y, z) cvs = pyx.canvas.canvas() cvs.text(0, 0, "Hello, world!") cvs.stroke(pyx.path.line(0, 0, 2, 0)) for cheight in cheights: clist = contourdata.trace(cheight, cheight, 0) clist = clist[:len(clist) // 2] print('Level {}m, {} contours'.format(cheight, len(clist))) for contour in clist: contour = contour * SCALE
#https://matplotlib.org/tutorials/introductory/pyplot.html import matplotlib.pyplot as plt from matplotlib.ticker import NullFormatter # useful for `logit` scale import np # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the interval ]0, 1[ y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure(1) # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True)