def test_infinite_coord(self): with open("test/resources/infinite_coord.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual(custom_message.exception.message, "z in row 2 is not finite")
def test_too_many_columns(self): with open("test/resources/too_many_columns.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual( custom_message.exception.message, "Incorrect number of columns (15) in row 2", )
def test_invalid_version(self): with open("test/resources/invalid_version.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual( custom_message.exception.message, "Markups fiducial file version 3.5 too low", )
def test_incorrect_desc(self): with open("test/resources/incorrect_desc.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual( custom_message.exception.message, "Row label 2 does not match row description dummy", )
def test_invalid_content(self): with open("test/resources/invalid_content.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual( custom_message.exception.message, "Missing or invalid header in fiducial file", ) with open("test/resources/invalid_content_valid_header.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual(custom_message.exception.message, "Row has no value label")
def test_valid_fcsv_flip(self): with open("test/resources/valid_flip.fcsv", "r") as fcsv: fcsv_afids = model.csv_to_afids(fcsv.read()) self.assertTrue(fcsv_afids.validate()) self.assertEqual( fcsv_afids.AC_x, -0.07077182344203692, ) self.assertEqual(fcsv_afids.AC_y, 0.2548674381652525)
def validate(): """Present the validator form, or validate an AFIDs set.""" form = Average(request.form) result = "" distances = [] labels = [] template_afids = None # Set all dropdown choices form_choices = os.listdir(current_app.config["AFIDS_DIR"]) form_choices = [choice.capitalize() for choice in form_choices] form_choices.sort() # Get time stamp timestamp = str( datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S %Z") ) if not (request.method == "POST" and request.files): return render_template( "validator.html", form=form, form_choices=form_choices, result=result, template_afids=template_afids, index=[], labels=labels, distances=distances, ) upload = request.files[form.filename.name] upload_ext, file_check = allowed_file(upload.filename) if not (upload and file_check): result = f"Invalid file: extension not allowed ({timestamp})" return render_template( "validator.html", form=form, form_choices=form_choices, result=result, template_afids=template_afids, index=[], labels=labels, distances=distances, ) try: if upload_ext in current_app.config["ALLOWED_EXTENSIONS"][:2]: user_afids = csv_to_afids(upload.read().decode("utf-8")) else: user_afids = json_to_afids(upload.read().decode("utf-8")) except InvalidFileError as err: result = f"Invalid file: {err.message} ({timestamp})" return render_template( "validator.html", form=form, form_choices=form_choices, result=result, template_afids=template_afids, index=[], labels=labels, distances=distances, current_user=current_user, ) if user_afids.validate(): result = f"Valid file ({timestamp})" else: result = ( f"Invalid AFIDs set, please double check your file ({timestamp})" ) fid_species = request.form["fid_species"] fid_species = fid_species.lower() fid_template = request.form["fid_template"] if fid_template == "Validate file structure": return render_template( "validator.html", form=form, form_choices=form_choices, result=result, template_afids=template_afids, index=[], labels=labels, distances=distances, current_user=current_user, ) result = f"{result}<br>{fid_template} selected" # Need to pull from correct folder when more templates are added template_file_path = f"{current_app.config['AFIDS_DIR']}/{fid_species.lower()}/tpl-{fid_template}_afids.fcsv" with open(template_file_path, "r") as template_file: template_afids = csv_to_afids(template_file.read()) if request.form.get("db_checkbox"): if current_user.is_authenticated: user_afids.afids_user_id = current_user.id db.session.add(user_afids) db.session.commit() print("Fiducial set added") else: print("DB option unchecked, user data not saved") for desc in EXPECTED_DESCS: distances.append( "{:.5f}".format( np.linalg.norm( np.array( [ getattr( template_afids, desc[-1] ).__composite_values__() ] ) - np.array( [getattr(user_afids, desc[-1]).__composite_values__()] ) ) ) ) labels.append(desc[-1]) return render_template( "validator.html", form=form, form_choices=form_choices, result=result, template_afids=template_afids, index=list(range(len(EXPECTED_DESCS))), labels=labels, distances=distances, timestamp=timestamp, scatter_html=generate_3d_scatter(template_afids, user_afids), histogram_html=generate_histogram(template_afids, user_afids), current_user=current_user, )
def test_too_many_rows(self): with open("test/resources/too_many_rows.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual(custom_message.exception.message, "Too many rows")
def test_valid_nhp(self): with open("test/resources/valid_nhp.fcsv", "r") as fcsv: self.assertTrue(model.csv_to_afids(fcsv.read()).validate())
def test_valid_fcsv(self): with open("test/resources/valid.fcsv", "r") as fcsv: afids = model.csv_to_afids(fcsv.read()) self.assertTrue(afids.validate())
def test_missing_row(self): with open("test/resources/missing_row_10.fcsv", "r") as fcsv: with self.assertRaises(model.InvalidFileError) as custom_message: model.csv_to_afids(fcsv.read()) self.assertEqual(custom_message.exception.message, "Too few rows")