2929
3030
3131def from_csv (
32- path : Union [str , List [str ], None ] = None , method : Optional [str ] = None
32+ path : Union [str , List [str ], os .PathLike , None ] = None ,
33+ method : Optional [str ] = None ,
3334) -> Union [CmdStanMCMC , CmdStanMLE , CmdStanVB , None ]:
3435 """
3536 Instantiate a CmdStan object from a the Stan CSV files from a CmdStan run.
@@ -61,22 +62,22 @@ def from_csv(
6162 csvfiles = []
6263 if isinstance (path , list ):
6364 csvfiles = path
64- elif isinstance (path , str ):
65- if '*' in path :
66- splits = os . path . split ( path )
67- if splits [0 ] is not None :
68- if not ( os . path . exists ( splits [ 0 ]) and os . path . isdir ( splits [ 0 ])):
69- raise ValueError (
70- 'Invalid path specification, {} '
71- ' unknown directory: {}' . format ( path , splits [ 0 ] )
72- )
73- csvfiles = glob . glob ( path )
74- elif os .path .exists (path ) and os .path .isdir (path ):
65+ elif isinstance (path , str ) and '*' in path :
66+ splits = os . path . split ( path )
67+ if splits [ 0 ] is not None :
68+ if not ( os . path . exists ( splits [0 ]) and os . path . isdir ( splits [ 0 ])) :
69+ raise ValueError (
70+ 'Invalid path specification, {} '
71+ ' unknown directory: {}' . format ( path , splits [ 0 ])
72+ )
73+ csvfiles = glob . glob ( path )
74+ elif isinstance ( path , ( str , os . PathLike )):
75+ if os .path .exists (path ) and os .path .isdir (path ):
7576 for file in os .listdir (path ):
76- if file . endswith ( ".csv" ) :
77+ if os . path . splitext ( file )[ 1 ] == ".csv" :
7778 csvfiles .append (os .path .join (path , file ))
7879 elif os .path .exists (path ):
79- csvfiles .append (path )
80+ csvfiles .append (str ( path ) )
8081 else :
8182 raise ValueError ('Invalid path specification: {}' .format (path ))
8283 else :
@@ -85,7 +86,7 @@ def from_csv(
8586 if len (csvfiles ) == 0 :
8687 raise ValueError ('No CSV files found in directory {}' .format (path ))
8788 for file in csvfiles :
88- if not (os .path .exists (file ) and file . endswith ( ' .csv' ) ):
89+ if not (os .path .exists (file ) and os . path . splitext ( file )[ 1 ] == " .csv" ):
8990 raise ValueError (
9091 'Bad CSV file path spec,'
9192 ' includes non-csv file: {}' .format (file )
0 commit comments