def add_book_to_library(): book_name = input('Enter the book name.') author_name = input('Enter the author name.') available_copies = 0 try: available_copies = int(input('Enter the number of copies.')) except ValueError: print('Available copies should be an integer') raise query = "INSERT INTO books(bookName, author, totalCopies, copiesIssued) VALUES('{}','{}',{}, 0 )".format( book_name, author_name, available_copies) DbInterface.execute_query(query)
def get_issued_ids(): query = 'SELECT id FROM issued' issued_ids = DbInterface.execute_query(query) issued_book_ids = [] for issue_id in issued_ids: issued_book_ids.append(issue_id[0]) return issued_book_ids
def get_available_book_ids(): query = 'SELECT bookId FROM books WHERE totalCopies - copiesIssued > 0' available_book_ids = DbInterface.execute_query(query) book_ids = [] for book in available_book_ids: book_ids.append(book[0]) return book_ids
def issue_book(): name = input('Enter your Name:') Books.display_available_books() try: book_id = int(input('Choose the bookId which you want to issue:') ) except ValueError: print('Book Id should be an integer') return book_ids = Books.get_available_book_ids() if book_id in book_ids: query = "INSERT INTO issued(name, issuedBook) VALUES('{}',{})".format(name, book_id ) DbInterface.execute_query(query) query = "UPDATE books SET copiesIssued = copiesIssued + 1 WHERE bookId = '{}' ".format(book_id) DbInterface.execute_query(query) else: print('Invalid book Id.')
def return_book(): Books.display_issued_books() issue_id = int(input('Enter the issue id of book which you want to return:') ) issued_ids = Books.get_issued_ids() if issue_id in issued_ids: query = 'SELECT issuedBook FROM issued WHERE id = {}'.format(issue_id) book_id = DbInterface.execute_query(query) book_id = book_id[0][0] query = "UPDATE books SET copiesIssued = copiesIssued - 1 WHERE bookId = '{}' ".format(book_id) DbInterface.execute_query(query) query = "DELETE FROM issued WHERE id = {}".format(issue_id) DbInterface.execute_query(query) else: print('Invalid issue id.')
def run_job(path, det, modelsDict, modelNames): # Read Images from folder # Don't recurse into sub-folders, so get only reg files # files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] #files = files[:min(n,len(files))] # Get only JPGs #imageList = list(filter(lambda x: os.path.splitext(x)[1] == '.jpg', files)) #imageList = list(map(lambda x: os.path.join(path, x), imageList)) # Get random subset not exceeding 600 images imageList = randomized_samples(path) print('Reading in features for {} images'.format(len(imageList))) if 'amur' in path.lower(): imType = 'amur' elif 'jaguar' in path.lower(): imType = 'jaguar' else: imType = 'elp' keyfile = path + '/normalized_class_mapping.txt' if not os.path.exists(keyfile): print('No KeyFile (class_mapping.txt) for folder {}, skipping!'.format( path)) return imageIds = readIdsForImages(imageList, imType, keyfile) print('Found {} images in folder {}, Using Image Type {}'.format( len(imageList), path, imType)) client = DbInterface() for modelName in modelNames: initializeDB(client, getDbName(imType, modelName)) print('Processing {} images using Model {} for Set Type {}'.format( len(imageList), modelName, imType)) processImagesAndStoreFeatures(client, imType, modelName, modelsDict, imageList, imageIds, det)
def display_all_books(): query = 'SELECT * FROM books' all_books = DbInterface.execute_query(query) display(all_books)
def display_issued_books(): query = 'SELECT issued.id, issued.name, books.bookName FROM issued INNER JOIN books WHERE issued.issuedBook = books.bookId' issued_books = DbInterface.execute_query(query) display(issued_books)
def display_available_books(): query = 'SELECT * FROM books WHERE totalCopies - copiesIssued > 0' available_books = DbInterface.execute_query(query) display(available_books)
def find_best_svm_model(dsName, modelNames, num_classes): # modelNames = ['alexnet', 'googlenet'] # modelNames = ['alexnet'] # datasetNames = ['amur'] client = DbInterface() overall_max_acc = -1 max_acc_layer = None max_acc_transform = None max_acc_model = None best_kernel_type = None outfile = 'svm_accuracy.csv' if not os.path.exists(outfile): with open(outfile, 'w') as f: f.write( 'Dataset Name,Model Name,Layer Name,Kernel Type,Transform Type,Accuracy\n' ) f.close() sum_times = 0 count = 0 for modelName in modelNames: dbCursor = client.getDB(getDbName(dsName, modelName)) layerNames = dbCursor.getCollectionNames() """ # Ids should be same for any layer recordIds = [x for x in dbCursor.getRecordIds(layerNames[0])] details = {} for x in recordIds: rec = dbCursor.getRecord(layerNames[0], x) if rec.animalId not in details: details[rec.animalId] = [] details[rec.animalId].append(rec.imageFile) # Select only 5 records for each id num_images_per_id = 5 details = {k:v for k,v in details.items() if len(v) >= num_images_per_id} print('Only {} ids have more than {} images, removed the rest'.format(len(details), num_images_per_id)) # Randomly remove all images for ids having more than num_images_per_id images new_details = {} np.random.seed(20004) for k,v in details.items(): if len(v) == 5: new_details[k] = v else: pick = np.random.choice(len(v), num_images_per_id, replace=False) new_details[k] = [v[x] for x in pick] print('Training on {} images over {} ids...'.format(sum([len(v) for k,v in new_details.items()]), len(new_details))) imageNames = [] for _,v in new_details.items(): imageNames.extend(v) print('{}: {}'.format(len(imageNames), len(set(imageNames)))) """ imageNames = None layerNames = ['res5a_relu'] for layer in layerNames: print('Starting Training for model {}, over layer {}'.format( modelName, layer)) acc, trType, kernelType, times = train_svm_for_layer( client, dsName, modelName, layer, outfile, imageNames, num_classes) count += 1 sum_times += times if acc > overall_max_acc: # print('Global Max accuracy being set, Value: {}, Model: {}, Layer: {}, Transform: {}'. # format(acc, modelName, layer, trType)) overall_max_acc = acc max_acc_layer = layer max_acc_model = modelName max_acc_transform = trType best_kernel_type = kernelType avg_time = sum_times if count > 0: avg_time = sum_times / count return overall_max_acc, max_acc_layer, max_acc_model, max_acc_transform, best_kernel_type, avg_time
# Author Dalin Akrasi # Student no.1528923 # # # # from collections import Counter from db_interface import DbInterface import tweet_tools as twtools from pprint import pprint db = DbInterface() def isSameYear(_tweet): return db.isYearExisting(_tweet) def isSameMonth(_tweet): month = twtools.getTweetMonth(_tweet) year = twtools.getTweetYear(_tweet) yearID = db.getYearID(year) return db.isMonthExisting(month, yearID) def isSameWeek(week, month, year): return db.isWeekExisting(week, month, year) def insertTweet(value, tweet):
def display_borrowers(): query = 'SELECT * FROM issued' borrowers = DbInterface.execute_query(query) display(borrowers)
def remove_book_from_library(): book_name = input('Enter the book name:') query = 'DELETE FROM books WHERE bookName = "{}"'.format(book_name) DbInterface.execute_query(query)