def test_with_dataset(): ar = IndentationGroup(jpkfile)[0] # Initially, fit properties are not set assert not ar.fit_properties assert isinstance(ar.fit_properties, dict) # Prepprocessing ar.apply_preprocessing(["compute_tip_position", "correct_tip_offset", "correct_force_offset"]) # Fitting the data set will populate the dict ar.fit_model() assert isinstance(ar.fit_properties, dict) assert isinstance(ar.fit_properties, FitProperties) assert "hash" in ar.fit_properties assert ar.fit_properties["success"] hash1 = ar.fit_properties["hash"] # Change something ar.fit_properties["weight_cp"] = 0 assert "hash" not in ar.fit_properties ar.fit_model() hash2 = ar.fit_properties["hash"] assert hash1 != hash2 # Change it back ar.fit_properties["weight_cp"] = FP_DEFAULT["weight_cp"] ar.fit_model() hash3 = ar.fit_properties["hash"] assert hash1 == hash3
def test_poc_estimation_via_indent(method, contact_point): fd = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] fd.apply_preprocessing( ["compute_tip_position", "correct_force_offset", "correct_tip_offset"], options={"correct_tip_offset": { "method": method }}) assert np.argmin(np.abs(fd["tip position"])) == contact_point
def test_unknown_method(): idnt = IndentationGroup(datadir / "spot3-0192.jpk-force")[0] try: idnt.apply_preprocessing(["compute_tip_position", "unknown_method"]) except KeyError: pass else: assert False, "Preprocessing with an unknown method must not work."
def test_correct_split_approach_retract(): fd = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] fd.apply_preprocessing( ["compute_tip_position", "correct_force_offset", "correct_tip_offset"]) assert fd.appr["segment"].size == 2000 fd.apply_preprocessing([ "compute_tip_position", "correct_force_offset", "correct_tip_offset", "correct_split_approach_retract" ]) assert fd.appr["segment"].size == 2006
def test_smooth(): idnt = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] idnt.apply_preprocessing( ["compute_tip_position", "correct_force_offset", "correct_tip_offset"]) orig = np.array(idnt["tip position"], copy=True) # now apply smoothing filter idnt.apply_preprocessing([ "compute_tip_position", "correct_force_offset", "correct_tip_offset", "smooth_height" ]) new = np.array(idnt["tip position"], copy=True) assert not np.all(orig == new)
def test_change_model_key(): ar = IndentationGroup(jpkfile)[0] # Prepprocessing ar.apply_preprocessing( ["compute_tip_position", "correct_tip_offset", "correct_force_offset"]) # Fitting the data set will populate the dict ar.fit_model(model_key="hertz_para") # Change the model assert ar.fit_properties["params_initial"] is not None ar.fit_properties["model_key"] = "hertz_cone" # Changing the model key should reset the initial parameters assert ar.fit_properties["params_initial"] is None
def test_wrong_params_initial(): ar = IndentationGroup(jpkfile)[0] # Prepprocessing ar.apply_preprocessing( ["compute_tip_position", "correct_tip_offset", "correct_force_offset"]) md = model.models_available["hertz_para"] params = md.get_parameter_defaults() ar.fit_properties["model_key"] = "hertz_cone" try: ar.fit_model(params_initial=params) except FitKeyError: # We forced the wrong fitting parameters. pass else: raise ValueError("Should not be able to use wrong fit parameters!")
def test_process_flipsign(): # This is a curve extracted from a map file. When loading it # with nanite, the sign of the force curve was flipped. flipped = datadir / "flipsign_2015.05.22-15.31.49.352.jpk-force" idnt = IndentationGroup(flipped)[0] idnt.apply_preprocessing([ "compute_tip_position", "correct_force_offset", "correct_tip_offset", "correct_split_approach_retract" ]) # We set the baseline fixed, because this test was written so) params_initial = idnt.get_initial_fit_parameters(model_key="hertz_para") params_initial["baseline"].set(vary=False) idnt.fit_model(model_key="hertz_para", weight_cp=False, params_initial=params_initial) assert np.allclose(idnt.fit_properties["params_fitted"]["E"].value, 5257.047288859021)
def setup_indent(): idnt = IndentationGroup(jpkfile)[0] idnt.apply_preprocessing(["compute_tip_position"]) inparams = model.model_hertz_paraboloidal.get_parameter_defaults() inparams["baseline"].vary = True inparams["contact_point"].set(1.8e-5) # Fit with absolute full range idnt.fit_model(model_key="hertz_para", params_initial=inparams, range_x=(0, 0), range_type="absolute", x_axis="tip position", y_axis="force", segment="approach", weight_cp=False) return idnt
def test_changed_fit_properties(): ar = IndentationGroup(jpkfile)[0] # Initially, fit properties are not set assert not ar.fit_properties assert isinstance(ar.fit_properties, dict) # Prepprocessing ar.apply_preprocessing( ["compute_tip_position", "correct_tip_offset", "correct_force_offset"]) ar.fit_model() hash1 = ar.fit_properties["hash"] pinit = copy.deepcopy(ar.fit_properties["params_initial"]) pinit["E"].vary = False assert "hash" in ar.fit_properties, "make sure we didn't change anything" ar.fit_properties["params_initial"] = pinit assert "hash" not in ar.fit_properties ar.fit_model() assert hash1 != ar.fit_properties["hash"]
def test_poc_details_fit_line_polynomial(): fd = IndentationGroup( data_path / "fmt-jpk-fd_single_tilted-baseline-mitotic_2021-01-29.jpk-force")[0] details = fd.apply_preprocessing( ["compute_tip_position", "correct_tip_offset"], options={"correct_tip_offset": { "method": "fit_line_polynomial" }}, ret_details=True) pocd = details["correct_tip_offset"] for key in ["plot force", "plot fit", "plot poc"]: assert key in pocd assert np.allclose(pocd["plot poc"][0][0], 15602, atol=0) assert np.allclose( fd["force"][15602], 1.7313584441928315e-09, atol=0, )
def test_poc_details_deviation_from_baseline(): fd = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] details = fd.apply_preprocessing( ["compute_tip_position", "correct_force_offset", "correct_tip_offset"], options={"correct_tip_offset": { "method": "deviation_from_baseline" }}, ret_details=True) pocd = details["correct_tip_offset"] for key in [ "plot force", "plot baseline mean", "plot baseline threshold", "plot poc" ]: assert key in pocd assert np.allclose(np.mean(pocd["plot baseline threshold"][1]), 8.431890003513514e-11, atol=0) assert np.allclose(np.mean(pocd["plot baseline mean"][1]), -7.573822405258677e-12, atol=0)
def test_poc_estimation_details(method, contact_point): fd = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] fd.apply_preprocessing(["compute_tip_position", "correct_force_offset"]) _, details = poc.compute_poc(fd["force"], method, ret_details=True) assert np.all(np.array(details["plot poc"][0]) == contact_point)
def test_poc_estimation(method, contact_point): fd = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] fd.apply_preprocessing(["compute_tip_position", "correct_force_offset"]) assert poc.compute_poc(fd["force"], method) == contact_point
def test_wrong_order_2(): idnt = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] with pytest.raises(ValueError, match="requires the steps"): # order matters idnt.apply_preprocessing( ["correct_split_approach_retract", "compute_tip_position"])
def test_unknown_method(): idnt = IndentationGroup(data_path / "fmt-jpk-fd_spot3-0192.jpk-force")[0] with pytest.raises(KeyError, match="unknown_method"): idnt.apply_preprocessing(["compute_tip_position", "unknown_method"])