@@ -743,7 +743,7 @@ def _test_anonymize_info(base_info, tmp_path):
743743 base_info ["subject_info" ].update (
744744 birthday = date (1987 , 4 , 8 ),
745745 his_id = "foobar" ,
746- sex = 0 ,
746+ sex = 1 ,
747747 )
748748
749749 # generate expected info...
@@ -812,7 +812,7 @@ def _adjust_back(e_i, dt):
812812 exp_info_2 = exp_info .copy ()
813813 with exp_info_2 ._unlock ():
814814 exp_info_2 ["subject_info" ]["his_id" ] = "foobar"
815- exp_info_2 ["subject_info" ]["sex" ] = 0
815+ exp_info_2 ["subject_info" ]["sex" ] = 1
816816 exp_info_2 ["subject_info" ]["hand" ] = 1
817817
818818 # exp 3 tests is a supplied daysback
@@ -842,12 +842,54 @@ def _check_equiv(got, want, err_msg):
842842 new_info = anonymize_info (base_info .copy (), keep_his = True )
843843 _check_equiv (new_info , exp_info_2 , err_msg = "anon keep_his mismatch" )
844844
845+ # keep only his_id
846+ new_info = anonymize_info (base_info .copy (), keep_his = "his_id" )
847+ assert new_info ["subject_info" ]["his_id" ] == "foobar"
848+ assert new_info ["subject_info" ]["sex" ] == 0
849+ assert "hand" not in new_info ["subject_info" ]
850+
851+ # keep only sex
852+ new_info = anonymize_info (base_info .copy (), keep_his = "sex" )
853+ assert new_info ["subject_info" ]["his_id" ] == "0"
854+ assert new_info ["subject_info" ]["sex" ] == 1
855+ assert "hand" not in new_info ["subject_info" ]
856+
857+ # keep only hand
858+ new_info = anonymize_info (base_info .copy (), keep_his = "hand" )
859+ assert new_info ["subject_info" ]["his_id" ] == "0"
860+ assert new_info ["subject_info" ]["sex" ] == 0
861+ assert new_info ["subject_info" ]["hand" ] == 1
862+
863+ # keep his_id and sex
864+ new_info = anonymize_info (base_info .copy (), keep_his = ["his_id" , "sex" ])
865+ assert new_info ["subject_info" ]["his_id" ] == "foobar"
866+ assert new_info ["subject_info" ]["sex" ] == 1
867+ assert "hand" not in new_info ["subject_info" ]
868+
869+ # keep only hand
870+ new_info = anonymize_info (base_info .copy (), keep_his = ["hand" ])
871+ assert new_info ["subject_info" ]["his_id" ] == "0"
872+ assert new_info ["subject_info" ]["sex" ] == 0
873+ assert new_info ["subject_info" ]["hand" ] == 1
874+
875+ # keep his_id and hand
876+ new_info = anonymize_info (base_info .copy (), keep_his = ("his_id" , "hand" ))
877+ assert new_info ["subject_info" ]["his_id" ] == "foobar"
878+ assert new_info ["subject_info" ]["sex" ] == 0
879+ assert new_info ["subject_info" ]["hand" ] == 1
880+
881+ # invalid keep_his values
882+ with pytest .raises (ValueError , match = "Invalid value" ):
883+ anonymize_info (base_info .copy (), keep_his = "invalid_field" )
884+
885+ with pytest .raises (ValueError , match = "Invalid value" ):
886+ anonymize_info (base_info .copy (), keep_his = ["his_id" , "invalid" ])
887+
845888 new_info = anonymize_info (base_info .copy (), daysback = delta_t_2 .days )
846889 _check_equiv (new_info , exp_info_3 , err_msg = "anon daysback mismatch" )
847890
848891 with pytest .raises (RuntimeError , match = "anonymize_info generated" ):
849892 anonymize_info (base_info .copy (), daysback = delta_t_3 .days )
850- # assert_object_equal(new_info, exp_info_4)
851893
852894 # test with meas_date = None
853895 with base_info ._unlock ():
0 commit comments