示例#1
0
 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))
示例#2
0
    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 setUpClass(cls):
     """Parse data from a MRS data file."""
     mrs_data_string = str(open('data/05_E2', 'r').read())
     cls.mrs_data = dataparser.get_xy_data(mrs_data_string)