def get(self): """Renders a page where the user can classify MRS data.""" # Get list of saved classifiers. conn = ds.create_sqlite_connection() classifiers = ds.fetch_all_classifiers(conn) # Render the web page. template = JINJA_ENVIRONMENT.get_template('classifydata.html') self.response.write(template.render(classifiers=classifiers))
def get(self): """Shows all MRS data on download page.""" # Query for all MRS data entries in the database. conn = ds.create_sqlite_connection() LOGGER.debug('Querying for all MRS data in database...') db_entries = ds.fetch_all_mrs_data(conn) LOGGER.debug('Found %d MRS data entries in database.', len(db_entries)) # Render download page. template = JINJA_ENVIRONMENT.get_template('downloaddata.html') self.response.write(template.render(mrs_data=db_entries))
def get(self): """Renders a page where the user can configure classifier training.""" # Get list of saved classifiers. conn = ds.create_sqlite_connection() classifiers = ds.fetch_all_classifiers(conn) # Get list of available MRS data. mrs_data = ds.fetch_all_mrs_data(conn) # Render the web page. template = JINJA_ENVIRONMENT.get_template('trainclassifier.html') self.response.write(template.render( classifiers=classifiers, mrs_data=mrs_data))
def post(self): """Serves requested file to the client.""" # Retrieve specified MRS data from the database. mrs_data_id = self.request.get('mrs_data_id') db_entry = ds.fetch_mrs_data(ds.create_sqlite_connection(), mrs_data_id) # Set response headers. self.response.headers['Content-Type'] = 'application/octet-stream' self.response.headers['Content-Description'] = 'File Transfer' self.response.headers['Content-Transfer-Encoding'] = 'binary' self.response.headers['Content-Disposition'] = 'attachment; filename=\"%s\"' % db_entry[1] self.response.headers['Content-Length'] = sys.getsizeof(db_entry[2]) # Set response content. self.response.out.write(db_entry[2])
def post(self): """Classifies given data using specified classifier.""" # Retrieve specified classifier from database. classifier_id = self.request.POST['classifier_id'] conn = ds.create_sqlite_connection() db_entry = ds.fetch_classifier(conn, classifier_id) classifier = db_entry[3] # Transform given MRS data for classifier input. file_name = self.request.POST['myfile'].filename raw_data = self.request.POST['myfile'].file.read() d = dataparser.get_xy_data(raw_data) fftd = fourier_transformer.get_fft(d) # Classify the transformed MRS data. test_input = np.array([fftd]) classification = classifier.predict(test_input) # Show classification results. template = JINJA_ENVIRONMENT.get_template('classificationresults.html') self.response.write(template.render( classification=classification, file_name=file_name))
def post(self): """Saves user-uploaded MRS data to the database.""" # Get raw file contents from request. file_name = self.request.POST['myfile'].filename file_contents = buffer(self.request.POST['myfile'].file.read()) # Label for this data's therapy group (e.g. "groupA", "groupB"). group_label = self.request.POST['grouplabel'] # Generate a random UUID for the file. database_id = str(uuid.uuid4().hex) # Save MRS data to the database. conn = ds.create_sqlite_connection() LOGGER.debug('Saving MRS data to database...') ds.store_mrs_data(conn, database_id, file_name, file_contents, group_label) LOGGER.debug('MRS data saved to database.') # Signal upload success to the user. template = JINJA_ENVIRONMENT.get_template('uploadcomplete.html') self.response.write(template.render( file_name=file_name, group_label=group_label, database_id=database_id))
def load_specified_classifier(self): """Loads the user-specified classifier. Returns: Tuple containing (classifier, classifier_name, classifier_type). If the user did not specify a classifier, then classifier and classifier_name will be None. """ classifier = None classifier_name = None classifier_type = self.request.POST['classifier_type'] # Load a saved classifier if specified by the user. if self.request.POST['load_classifier'] == 'true': # Query database for classifier with specified ID. classifier_id = self.request.POST['classifier_id'] db_entry = ds.fetch_classifier( ds.create_sqlite_connection(), classifier_id) classifier = db_entry[3] classifier_type = db_entry[2] classifier_name = db_entry[1] return (classifier, classifier_name, classifier_type)
def prepare_mrs_data_set(self): """Retrieves all specified MRS data entries and processes each entry. Each MRS file is parsed and FFT is applied if specified. Returns: Tuple containing (list of sample inputs, list of sample outputs). """ training_data_ids = self.request.get_all("training_data_ids") apply_fft = 'apply_fft' in self.request.POST LOGGER.debug('Processing MRS data: apply_fft=%s', apply_fft) # Retrieve specified training data from the database. conn = ds.create_sqlite_connection() db_entries = [ds.fetch_mrs_data(conn, data_id) for data_id in training_data_ids] # Separate each database entry into input and output. sample_inputs = [] sample_outputs = [] for entry in db_entries: # Parse data points from the file contents. mrs_data = dataparser.get_xy_data(str(entry[2])) # Apply FFT to the data points if specified by user. if apply_fft: mrs_data = fourier_transformer.get_fft(mrs_data) # Add input, output pair to separate lists. sample_inputs.append(mrs_data) sample_outputs.append(entry[3]) # Format the data for classifier input. n_samples = len(sample_inputs) n_features = len(sample_inputs[0]) sample_inputs = np.array(sample_inputs) # convert before using as buffer sample_inputs = np.ndarray( shape=(n_samples, n_features), dtype=float, buffer=sample_inputs) # Labels for training. sample_outputs = np.array(sample_outputs) # Return processed MRS data. return (sample_inputs, sample_outputs)
def post(self): """Stores specified classifier in the database.""" # Get request parameters. classifier_id = self.request.POST['classifier_id'] classifier_name = self.request.POST['classifier_name'] classifier_type = self.request.POST['classifier_type'] LOGGER.debug( 'Got save classifier request: %s, %s, %s', classifier_id, classifier_name, classifier_type) # Retrieve classifier from the "cache". classifier = CACHE[classifier_id] if classifier is None: self.response.out.write('No classifier with ID %s' % classifier_id) # Save the classifier in the database. conn = ds.create_sqlite_connection() ds.store_classifier( conn, classifier_id, classifier_name, classifier_type, classifier) # Signal save success to user. template = JINJA_ENVIRONMENT.get_template('classifiersaved.html') self.response.write(template.render())
def test_create_sqlite_connection(self): """A SQLite Connection object is created.""" conn = ds.create_sqlite_connection(':memory:') # Make sure a Connection object is returned. self.assertIsNotNone(conn)
def setUp(self): """Create a new in-memory database for each test case.""" self.conn = ds.create_sqlite_connection(':memory:')