def annotations(): callback = request.GET.get('callback') documentId = request.GET.get('document_id') #classifier_name = request.GET.get('classifier_name') classifier_dump = request.GET.get('classifier') questionId = request.GET.get('question_id') collection_id = request.GET.get('collection_id') range = request.GET.get('range') meh = StringIO(classifier_dump) buff = meh.getvalue() clf = pickle.loads(buff) features_train, labels_train = wtf.getArrays(collection_id) print("params :") #print("classifier_name" + classifier_name) print("classifier_dump" + classifier_dump) print("question_id" + question_id) print("collection_id" + collection_id) print("range" + range) print("document_id" + document_id) preditions = clf.predict(features_train) data = preds_to_send(questionId, documentId, preditions[0], range) put.send("http://localhost:8080/dataprocessing/rest-api/annotations", data) return '{0}({1})'.format(callback, {'a': 1, 'b': 2})
def annotations(): callback = request.GET.get('callback') documentId = request.GET.get('document_id') #classifier_name = request.GET.get('classifier_name') classifier_dump = request.GET.get('classifier') questionId = request.GET.get('question_id') collection_id = request.GET.get('collection_id') range = request.GET.get('range') meh = StringIO(classifier_dump) buff = meh.getvalue() clf = pickle.loads(buff) features_train, labels_train = wtf.getArrays(collection_id) print("params :") #print("classifier_name" + classifier_name) print("classifier_dump" + classifier_dump) print("question_id" + question_id) print("collection_id" + collection_id) print("range" + range) print("document_id" + document_id) preditions = clf.predict(features_train) data = preds_to_send(questionId,documentId,preditions[0],range) put.send("http://localhost:8080/dataprocessing/rest-api/annotations", data) return '{0}({1})'.format(callback, {'a':1, 'b':2})
def clientThread(clientSocket, address): doc_type = '' while True: msg = recvall(clientSocket) msg_data = msg.split('\r\n\r\n'.encode()) headers = msg_data[0] try: header = headers.decode() except UnicodeDecodeError: pass try: pieces = header.split('\n') except AttributeError: pass if len(msg) != 0: if len(pieces) > 0: request = pieces[0].split(' ') #GET request if request[0] == 'GET': for line in pieces: header = line.split(':') if header[0] == 'Accept-Encoding': encoding = header[1].strip() break else: encoding = '' if request[1].find('.png') == -1 and request[1].find( '.jpg') == -1: response_html = GET.get(pieces, address) try: if ((encoding == 'zlib' or encoding == 'gzip') and response_html.find('404') < 0 and response_html.find('406') < 0): clientSocket.sendall(response_html) else: clientSocket.sendall(response_html.encode()) except (TypeError, BrokenPipeError): try: clientSocket.sendall(response_html) except TypeError: clientSocket.sendall(response_html.encode()) else: try: response_html, response_image = GET.get( pieces, address) try: clientSocket.sendall(response_html.encode() + response_image) except BrokenPipeError: pass except ValueError: response_html = GET.get(pieces, address) try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass #HEAD request elif request[0] == 'HEAD': response_html = HEAD.head(pieces, address) clientSocket.sendall(response_html.encode()) #DELETE request elif request[0] == 'DELETE': if request[1].find('.png') == -1 and request[1].find( '.jpg') == -1: response_html = DELETE.delete(pieces, address) try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass else: try: response_html, response_image = DELETE.delete( pieces, address) try: clientSocket.sendall(response_html.encode() + response_image) except BrokenPipeError: pass except ValueError: response_html = DELETE.delete(pieces, address) try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass elif request[0] == 'PUT': for line in pieces: header = line.split(':') if header[0] == 'Content-Type': doc_type = header[1] break if doc_type.find('html') >= 0 or doc_type.find('plain') >= 0: payload = msg_data[1].decode() response_html = PUT.put(pieces, address, payload) try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass else: try: payload = msg_data[1] except IndexError: pass try: response_html, response_image = PUT.put( pieces, address, payload) try: clientSocket.sendall(response_html.encode() + response_image) except BrokenPipeError: pass except ValueError: response_html = PUT.put(pieces, address, '') try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass elif request[0] == 'POST': payload = msg_data[1].decode() response_html = POST.post(pieces, address, payload) try: clientSocket.sendall(response_html.encode()) except BrokenPipeError: pass try: clientSocket.shutdown(socket.SHUT_RDWR) except OSError: #Transport endpoint not connected pass else: clientSocket.close() remove(clientSocket) break
def train(): callback = request.GET.get('callback') classifier_name = request.GET.get('classifier_name') classifier_id = request.GET.get('classifier_id') user_id = request.GET.get('user_id') classifier_type = request.GET.get('classifier_type') classifier_params = request.GET.get('classifier_params') cross_validation_type = request.GET.get('cross_validation_type') cross_validation_params = request.GET.get('cross_validation_params') result_test_classifiers_id = request.GET.get('result_test_classifiers_id') collection_id = request.GET.get('collection_id') vectorized_document_collection_id = request.GET.get( 'vectorized_document_collection_id') train_size = request.GET.get('train_size') #data = classifier_to_send(user_id, classifier_name, classifier_params, "", "", 1) #post.send("http://localhost:8080/dataprocessing/rest-api/classifiers/",data) print("Params :") print(classifier_name) print(classifier_type) print(classifier_params) print(cross_validation_type) print(cross_validation_params) print(collection_id) print(train_size) clf = classifier.configure_classifier(classifier_type, classifier_params) #features_train, labels_train = makeTerrainData(n_points=200) features_train, labels_train, features_test, labels_test = makeTerrainData( ) #print(features_train) if (cross_validation_type == 'None'): cross_validation_type = None cv = classifier.configure_cross_validation(cross_validation_type, cross_validation_params, n=len(features_train)) print("features_train :") print(len(features_train)) print("labels_train :") print(len(labels_train)) clf.fit(features_train, labels_train) fig = classifier.train(clf, train_sizes=np.linspace(.1, 1.0, train_size), cv=cv, params=" ", features=features_train, labels=labels_train) imgdata = StringIO() fig.savefig(imgdata, format='svg') imgdata.seek(0) # rewind the data svg_dta = imgdata.getvalue() # this is svg data import pickle s = pickle.dumps(clf) print("classifier dump:") #print(s) data = classifier_to_send( user_id=user_id, name=classifier_name, vectorizedDocumentCollectionId=vectorized_document_collection_id, parameter=classifier_params, learningCurve=svg_dta, content=s, flag=1) put.send("http://localhost:8080/dataprocessing/rest-api/classifiers/", classifier_id, data) pred = clf.predict(features_train) from sklearn.metrics import accuracy_score acc = accuracy_score(labels_train, pred) precision = precision_score(labels_train, pred) recall = recall_score(labels_train, pred) print("result_test_classifiers_id: " + result_test_classifiers_id) data = test_data_to_send( id_result_test_classifier=result_test_classifiers_id, user_id=user_id, classifierId=classifier_id, vectorizedDocumentCollectionId=vectorized_document_collection_id, parameter=" ", precision=precision, accuracy=acc, recall=recall) print(data) put.send( "http://localhost:8080/dataprocessing/rest-api/resultTestClassifiers/", result_test_classifiers_id, data) return '{0}({1})'.format(callback, {'a': 1, 'b': 2})
def preds_to_send(questionId, documentId, value, range): data ={ "userId" : "2", "questionId" : questionId, "documentId" : documentId, "value" : value, "range" : range, } return data def test_data_to_send(classifierId, vectoriziedDocumentCollectionId, parameter, precision, accuracy, recall): data ={ "classifierId" : classifierId, "vectoriziedDocumentCollectionId" : vectoriziedDocumentCollectionId, "parameter" : parameter, "precision" : precision, "accuracy" : accuracy, "recall" : recall } return data data1 = test_data_to_send(1, 1, 1) post.send("http://localhost:8080/dataprocessing/rest-api/resultTestClassifiers",data1) data2 = preds_to_send(2, 2, -5, 5) post.send("http://localhost:8080/dataprocessing/rest-api/annotations",data2) data3 = classfier_to_send(2, 2, 1) put.send("http://localhost:8080/dataprocessing/rest-api/classifiers/",id,data3)
def train(): callback = request.GET.get('callback') classifier_name = request.GET.get('classifier_name') classifier_id = request.GET.get('classifier_id') user_id = request.GET.get('user_id') classifier_type = request.GET.get('classifier_type') classifier_params = request.GET.get('classifier_params') cross_validation_type = request.GET.get('cross_validation_type') cross_validation_params = request.GET.get('cross_validation_params') result_test_classifiers_id = request.GET.get('result_test_classifiers_id') collection_id = request.GET.get('collection_id') vectorized_document_collection_id = request.GET.get('vectorized_document_collection_id') train_size = request.GET.get('train_size') #data = classifier_to_send(user_id, classifier_name, classifier_params, "", "", 1) #post.send("http://localhost:8080/dataprocessing/rest-api/classifiers/",data) print("Params :") print(classifier_name) print(classifier_type) print(classifier_params) print(cross_validation_type) print(cross_validation_params) print(collection_id) print(train_size) clf = classifier.configure_classifier(classifier_type,classifier_params) #features_train, labels_train = makeTerrainData(n_points=200) features_train, labels_train, features_test, labels_test = makeTerrainData() #print(features_train) if(cross_validation_type == 'None') : cross_validation_type = None cv = classifier.configure_cross_validation(cross_validation_type,cross_validation_params, n = len(features_train)) print("features_train :") print(len(features_train)) print("labels_train :") print(len(labels_train)) clf.fit(features_train, labels_train) fig = classifier.train(clf, train_sizes = np.linspace(.1, 1.0,train_size), cv = cv, params = " ", features = features_train, labels = labels_train ) imgdata = StringIO() fig.savefig(imgdata, format='svg') imgdata.seek(0) # rewind the data svg_dta = imgdata.getvalue() # this is svg data import pickle s = pickle.dumps(clf) print("classifier dump:") #print(s) data = classifier_to_send(user_id = user_id, name = classifier_name, vectorizedDocumentCollectionId= vectorized_document_collection_id, parameter = classifier_params, learningCurve = svg_dta, content = s, flag = 1) put.send("http://localhost:8080/dataprocessing/rest-api/classifiers/",classifier_id, data) pred = clf.predict(features_train) from sklearn.metrics import accuracy_score acc = accuracy_score(labels_train, pred) precision = precision_score(labels_train, pred) recall = recall_score(labels_train, pred) print("result_test_classifiers_id: " + result_test_classifiers_id ) data = test_data_to_send(id_result_test_classifier = result_test_classifiers_id, user_id = user_id, classifierId = classifier_id, vectorizedDocumentCollectionId = vectorized_document_collection_id, parameter = " ", precision = precision, accuracy = acc, recall = recall) print(data) put.send("http://localhost:8080/dataprocessing/rest-api/resultTestClassifiers/", result_test_classifiers_id, data) return '{0}({1})'.format(callback, {'a':1, 'b':2})