11"""testing stancsv parsing"""
22
33from typing import List
4+ from unittest import mock
45
56import numpy as np
6- import polars as pl
77import pytest
88
99from cmdstanpy .utils import stancsv
@@ -30,6 +30,28 @@ def test_csv_bytes_to_numpy_no_header():
3030 assert arr_out [0 ].dtype == np .float32
3131
3232
33+ def test_csv_bytes_to_numpy_no_header_no_polars ():
34+ lines = [
35+ b"-6.76206,1,0.787025,1,1,0,6.81411,0.229458\n " ,
36+ b"-6.81411,0.983499,0.787025,1,1,0,6.8147,0.20649\n " ,
37+ b"-6.85511,0.994945,0.787025,2,3,0,6.85536,0.310589\n " ,
38+ b"-6.85511,0.812189,0.787025,1,1,0,7.16517,0.310589\n " ,
39+ ]
40+ expected = np .array (
41+ [
42+ [- 6.76206 , 1 , 0.787025 , 1 , 1 , 0 , 6.81411 , 0.229458 ],
43+ [- 6.81411 , 0.983499 , 0.787025 , 1 , 1 , 0 , 6.8147 , 0.20649 ],
44+ [- 6.85511 , 0.994945 , 0.787025 , 2 , 3 , 0 , 6.85536 , 0.310589 ],
45+ [- 6.85511 , 0.812189 , 0.787025 , 1 , 1 , 0 , 7.16517 , 0.310589 ],
46+ ],
47+ dtype = np .float32 ,
48+ )
49+ with mock .patch .dict ("sys.modules" , {"polars" : None }):
50+ arr_out = stancsv .csv_bytes_list_to_numpy (lines , includes_header = False )
51+ assert np .array_equiv (arr_out , expected )
52+ assert arr_out [0 ].dtype == np .float32
53+
54+
3355def test_csv_bytes_to_numpy_with_header ():
3456 lines = [
3557 (
@@ -54,21 +76,65 @@ def test_csv_bytes_to_numpy_with_header():
5476 assert np .array_equiv (arr_out , expected )
5577
5678
79+ def test_csv_bytes_to_numpy_with_header_no_polars ():
80+ lines = [
81+ (
82+ b"lp__,accept_stat__,stepsize__,treedepth__,"
83+ b"n_leapfrog__,divergent__,energy__,theta\n "
84+ ),
85+ b"-6.76206,1,0.787025,1,1,0,6.81411,0.229458\n " ,
86+ b"-6.81411,0.983499,0.787025,1,1,0,6.8147,0.20649\n " ,
87+ b"-6.85511,0.994945,0.787025,2,3,0,6.85536,0.310589\n " ,
88+ b"-6.85511,0.812189,0.787025,1,1,0,7.16517,0.310589\n " ,
89+ ]
90+ expected = np .array (
91+ [
92+ [- 6.76206 , 1 , 0.787025 , 1 , 1 , 0 , 6.81411 , 0.229458 ],
93+ [- 6.81411 , 0.983499 , 0.787025 , 1 , 1 , 0 , 6.8147 , 0.20649 ],
94+ [- 6.85511 , 0.994945 , 0.787025 , 2 , 3 , 0 , 6.85536 , 0.310589 ],
95+ [- 6.85511 , 0.812189 , 0.787025 , 1 , 1 , 0 , 7.16517 , 0.310589 ],
96+ ],
97+ dtype = np .float32 ,
98+ )
99+ with mock .patch .dict ("sys.modules" , {"polars" : None }):
100+ arr_out = stancsv .csv_bytes_list_to_numpy (lines , includes_header = True )
101+ assert np .array_equiv (arr_out , expected )
102+
103+
57104def test_csv_bytes_to_numpy_empty ():
58105 lines = [b"" ]
59- with pytest .raises (pl . exceptions . NoDataError ):
106+ with pytest .raises (ValueError ):
60107 stancsv .csv_bytes_list_to_numpy (lines )
61108
62109
110+ def test_csv_bytes_to_numpy_empty_no_polars ():
111+ lines = [b"" ]
112+ with mock .patch .dict ("sys.modules" , {"polars" : None }):
113+ with pytest .raises (ValueError ):
114+ stancsv .csv_bytes_list_to_numpy (lines )
115+
116+
63117def test_csv_bytes_to_numpy_header_no_draws ():
64118 lines = [
65119 (
66120 b"lp__,accept_stat__,stepsize__,treedepth__,"
67121 b"n_leapfrog__,divergent__,energy__,theta\n "
68122 ),
69123 ]
70- arr_out = stancsv .csv_bytes_list_to_numpy (lines )
71- assert arr_out .shape == (0 , 8 )
124+ with pytest .raises (ValueError ):
125+ stancsv .csv_bytes_list_to_numpy (lines )
126+
127+
128+ def test_csv_bytes_to_numpy_header_no_draws_no_polars ():
129+ lines = [
130+ (
131+ b"lp__,accept_stat__,stepsize__,treedepth__,"
132+ b"n_leapfrog__,divergent__,energy__,theta\n "
133+ ),
134+ ]
135+ with mock .patch .dict ("sys.modules" , {"polars" : None }):
136+ with pytest .raises (ValueError ):
137+ stancsv .csv_bytes_list_to_numpy (lines )
72138
73139
74140def test_parsing_with_rules ():
0 commit comments