@@ -105,75 +105,6 @@ def test_parse_comments_header_and_draws() -> None:
105105 assert draws_lines == [b"3\n " ]
106106
107107
108- def test_parsing_adaptation_lines () -> None :
109- lines = [
110- b"# Adaptation terminated\n " ,
111- b"# Step size = 0.787025\n " ,
112- b"# Diagonal elements of inverse mass matrix:\n " ,
113- b"# 1\n " ,
114- b"# Elapsed Time\n " ,
115- ]
116- step_size , mass_matrix = stancsv .parse_hmc_adaptation_lines (lines )
117- assert step_size == 0.787025
118- print (mass_matrix )
119- assert mass_matrix == 1
120-
121-
122- def test_parsing_adaptation_lines_diagonal () -> None :
123- lines = [
124- b"diag_e" , # Will be present in the Stan CSV config
125- b"# Adaptation terminated\n " ,
126- b"# Step size = 0.787025\n " ,
127- b"# Diagonal elements of inverse mass matrix:\n " ,
128- b"# 1,2,3\n " ,
129- ]
130- step_size , mass_matrix = stancsv .parse_hmc_adaptation_lines (lines )
131- assert step_size == 0.787025
132- assert mass_matrix is not None
133- assert np .array_equal (mass_matrix , np .array ([1 , 2 , 3 ]))
134-
135-
136- def test_parsing_adaptation_lines_dense () -> None :
137- lines = [
138- b"# Adaptation terminated\n " ,
139- b"# Step size = 0.775147\n " ,
140- b"# Elements of inverse mass matrix:\n " ,
141- b"# 2.84091, 0.230843, 0.0509365\n " ,
142- b"# 0.230843, 3.92459, 0.126989\n " ,
143- b"# 0.0509365, 0.126989, 3.82718\n " ,
144- ]
145- step_size , mass_matrix = stancsv .parse_hmc_adaptation_lines (lines )
146- expected = np .array (
147- [
148- [2.84091 , 0.230843 , 0.0509365 ],
149- [0.230843 , 3.92459 , 0.126989 ],
150- [0.0509365 , 0.126989 , 3.82718 ],
151- ],
152- dtype = np .float64 ,
153- )
154- assert step_size == 0.775147
155- assert mass_matrix is not None
156- assert np .array_equal (mass_matrix , expected )
157-
158-
159- def test_parsing_adaptation_lines_missing_everything () -> None :
160- lines = [
161- b"# Adaptation terminated\n " ,
162- b"# Elements of inverse mass matrix:\n " ,
163- ]
164- assert stancsv .parse_hmc_adaptation_lines (lines ) == (None , None )
165-
166-
167- def test_parsing_adaptation_lines_no_free_params () -> None :
168- lines = [
169- b"# Adaptation terminated\n " ,
170- b"# Step size = 1.77497\n " ,
171- b"# No free parameters for unit metric\n " ,
172- ]
173- _ , mass_matrix = stancsv .parse_hmc_adaptation_lines (lines )
174- assert mass_matrix is None
175-
176-
177108def test_csv_polars_and_numpy_equiv () -> None :
178109 lines = [
179110 b"-6.76206,1,0.787025,1,1,0,6.81411,0.229458\n " ,
@@ -512,138 +443,6 @@ def test_inconsistent_draws_shape_empty() -> None:
512443 stancsv .raise_on_inconsistent_draws_shape ("" , [])
513444
514445
515- def test_invalid_adaptation_block_good () -> None :
516- csv_path = os .path .join (DATAFILES_PATH , "bernoulli_output_1.csv" )
517- comments , * _ = stancsv .parse_comments_header_and_draws (csv_path )
518- stancsv .raise_on_invalid_adaptation_block (comments )
519-
520-
521- def test_invalid_adaptation_block_missing () -> None :
522- lines = [
523- b"# metric = diag_e (Default)\n " ,
524- (
525- b"lp__,accept_stat__,stepsize__,treedepth__,"
526- b"n_leapfrog__,divergent__,energy__,theta\n "
527- ),
528- b"-6.76206,1,0.787025,1,1,0,6.81411,0.229458\n " ,
529- b"# \n " ,
530- b"# Elapsed Time: 0.001332 seconds (Warm-up)\n " ,
531- ]
532- with pytest .raises (ValueError , match = "expecting metric" ):
533- stancsv .raise_on_invalid_adaptation_block (lines )
534-
535-
536- def test_invalid_adaptation_block_no_metric () -> None :
537- lines = [
538- (
539- b"lp__,accept_stat__,stepsize__,treedepth__,"
540- b"n_leapfrog__,divergent__,energy__,theta\n "
541- ),
542- b"# Adaptation terminated\n " ,
543- b"# Step size = 0.787025\n " ,
544- b"# Diagonal elements of inverse mass matrix:\n " ,
545- b"# 1\n " ,
546- ]
547- with pytest .raises (ValueError , match = "No reported metric" ):
548- stancsv .raise_on_invalid_adaptation_block (lines )
549-
550-
551- def test_invalid_adaptation_block_invalid_step_size () -> None :
552- lines = [
553- b"# metric = diag_e (Default)\n " ,
554- (
555- b"lp__,accept_stat__,stepsize__,treedepth__,"
556- b"n_leapfrog__,divergent__,energy__,theta\n "
557- ),
558- b"# Adaptation terminated\n " ,
559- b"# Step size = bad\n " ,
560- b"# Diagonal elements of inverse mass matrix:\n " ,
561- b"# 1\n " ,
562- ]
563- with pytest .raises (ValueError , match = "invalid step size" ):
564- stancsv .raise_on_invalid_adaptation_block (lines )
565-
566-
567- def test_invalid_adaptation_block_mismatched_structure () -> None :
568- lines = [
569- b"# metric = diag_e (Default)\n " ,
570- (
571- b"lp__,accept_stat__,stepsize__,treedepth__,"
572- b"n_leapfrog__,divergent__,energy__,theta\n "
573- ),
574- b"# Adaptation terminated\n " ,
575- b"# Step size = 0.787025\n " ,
576- b"# Elements of inverse mass matrix:\n " ,
577- b"# 1\n " ,
578- ]
579- with pytest .raises (ValueError , match = "invalid or missing" ):
580- stancsv .raise_on_invalid_adaptation_block (lines )
581-
582-
583- def test_invalid_adaptation_block_missing_step_size () -> None :
584- lines = [
585- b"# metric = diag_e (Default)\n " ,
586- (
587- b"lp__,accept_stat__,stepsize__,treedepth__,"
588- b"n_leapfrog__,divergent__,energy__,theta\n "
589- ),
590- b"# Adaptation terminated\n " ,
591- b"# Diagonal elements of inverse mass matrix:\n " ,
592- b"# 1\n " ,
593- ]
594- with pytest .raises (ValueError , match = "expecting step size" ):
595- stancsv .raise_on_invalid_adaptation_block (lines )
596-
597-
598- def test_invalid_adaptation_block_unit_e () -> None :
599- lines = [
600- b"# metric = unit_e\n " ,
601- (
602- b"lp__,accept_stat__,stepsize__,treedepth__,"
603- b"n_leapfrog__,divergent__,energy__,theta\n "
604- ),
605- b"# Adaptation terminated\n " ,
606- b"# Step size = 1.77497\n " ,
607- b"# No free parameters for unit metric\n " ,
608- ]
609- stancsv .raise_on_invalid_adaptation_block (lines )
610-
611-
612- def test_invalid_adaptation_block_dense_e_valid () -> None :
613- lines = [
614- b"# metric = dense_e\n " ,
615- (
616- b"lp__,accept_stat__,stepsize__,treedepth__,"
617- b"n_leapfrog__,divergent__,energy__,theta.1,theta.2,theta.3\n "
618- ),
619- b"# Adaptation terminated\n " ,
620- b"# Step size = 0.775147\n " ,
621- b"# Elements of inverse mass matrix:\n " ,
622- b"# 2.84091, 0.230843, 0.0509365\n " ,
623- b"# 0.230843, 3.92459, 0.126989\n " ,
624- b"# 0.0509365, 0.126989, 3.82718\n " ,
625- ]
626- stancsv .raise_on_invalid_adaptation_block (lines )
627-
628-
629- def test_invalid_adaptation_block_dense_e_invalid () -> None :
630- lines = [
631- b"# metric = dense_e\n " ,
632- (
633- b"lp__,accept_stat__,stepsize__,treedepth__,"
634- b"n_leapfrog__,divergent__,energy__,theta.1,theta.2,theta.3\n "
635- ),
636- b"# Adaptation terminated\n " ,
637- b"# Step size = 0.775147\n " ,
638- b"# Elements of inverse mass matrix:\n " ,
639- b"# 2.84091, 0.230843, 0.0509365\n " ,
640- b"# 2.84091, 0.230843\n " ,
641- b"# 0.230843, 3.92459\n " ,
642- ]
643- with pytest .raises (ValueError , match = "invalid or missing" ):
644- stancsv .raise_on_invalid_adaptation_block (lines )
645-
646-
647446def test_parsing_timing_lines () -> None :
648447 lines = [
649448 b"# \n " ,
0 commit comments