-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
utils.py
899 lines (815 loc) · 28.9 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
"""
Utility functions
"""
import os
from collections import OrderedDict
from collections.abc import Sequence
from numbers import Integral, Real
try:
import ujson as json
except ImportError:
import json
import math
import platform
import re
import subprocess
import shutil
import tempfile
import logging
import sys
from typing import Dict, TextIO, List, Union, Tuple
import numpy as np
import pandas as pd
from cmdstanpy import _TMPDIR, _CMDSTAN_WARMUP, _CMDSTAN_SAMPLING, _CMDSTAN_THIN
EXTENSION = '.exe' if platform.system() == 'Windows' else ''
def get_logger():
"""cmdstanpy logger"""
logger = logging.getLogger('cmdstanpy')
if len(logger.handlers) == 0:
logging.basicConfig(level=logging.INFO)
return logger
def get_latest_cmdstan(dot_dir: str) -> str:
"""
Given a valid directory path, find all installed CmdStan versions
and return highest (i.e., latest) version number.
Assumes directory populated via script `install_cmdstan`.
"""
versions = [
name.split('-')[1]
for name in os.listdir(dot_dir)
if os.path.isdir(os.path.join(dot_dir, name))
and name.startswith('cmdstan-')
and name[8].isdigit()
]
versions.sort(key=lambda s: list(map(int, s.split('.'))))
if len(versions) == 0:
return None
latest = 'cmdstan-{}'.format(versions[len(versions) - 1])
return latest
class MaybeDictToFilePath:
"""Context manager for json files."""
def __init__(self, *objs: Union[str, dict], logger: logging.Logger = None):
self._unlink = [False] * len(objs)
self._paths = [''] * len(objs)
self._logger = logger or get_logger()
i = 0
for obj in objs:
if isinstance(obj, dict):
data_file = create_named_text_file(
dir=_TMPDIR, prefix='', suffix='.json'
)
self._logger.debug('input tempfile: %s', data_file)
if any(
not item
for item in obj
if isinstance(item, (Sequence, np.ndarray))
):
rdump(data_file, obj)
else:
jsondump(data_file, obj)
self._paths[i] = data_file
self._unlink[i] = True
elif isinstance(obj, str):
if not os.path.exists(obj):
raise ValueError("File doesn't exist {}".format(obj))
self._paths[i] = obj
elif obj is None:
self._paths[i] = None
elif i == 1 and isinstance(obj, (Integral, Real)):
self._paths[i] = obj
else:
raise ValueError('data must be string or dict')
i += 1
def __enter__(self):
return self._paths
def __exit__(self, exc_type, exc_val, exc_tb):
for can_unlink, path in zip(self._unlink, self._paths):
if can_unlink and path:
try:
os.remove(path)
except PermissionError:
pass
def validate_cmdstan_path(path: str) -> None:
"""
Validate that CmdStan directory exists and binaries have been built.
Throws exception if specified path is invalid.
"""
if not os.path.isdir(path):
raise ValueError('no such CmdStan directory {}'.format(path))
if not os.path.exists(os.path.join(path, 'bin', 'stanc' + EXTENSION)):
raise ValueError(
'no CmdStan binaries found, '
'run command line script "install_cmdstan"'
)
class TemporaryCopiedFile:
"""Context manager for tmpfiles, handles spaces in filepath."""
def __init__(self, file_path: str):
self._path = None
self._tmpdir = None
if ' ' in os.path.abspath(file_path) and platform.system() == 'Windows':
base_path, file_name = os.path.split(os.path.abspath(file_path))
os.makedirs(base_path, exist_ok=True)
try:
short_base_path = windows_short_path(base_path)
if os.path.exists(short_base_path):
file_path = os.path.join(short_base_path, file_name)
except RuntimeError:
pass
if ' ' in os.path.abspath(file_path):
tmpdir = tempfile.mkdtemp()
if ' ' in tmpdir:
raise RuntimeError(
'Unable to generate temporary path without spaces! \n'
+ 'Please move your stan file to location without spaces.'
)
_, path = tempfile.mkstemp(suffix='.stan', dir=tmpdir)
shutil.copy(file_path, path)
self._path = path
self._tmpdir = tmpdir
else:
self._path = file_path
def __enter__(self):
return self._path, self._tmpdir is not None
def __exit__(self, exc_type, exc_val, exc_tb):
if self._tmpdir:
shutil.rmtree(self._tmpdir, ignore_errors=True)
def set_cmdstan_path(path: str) -> None:
"""
Validate, then set CmdStan directory path.
"""
validate_cmdstan_path(path)
os.environ['CMDSTAN'] = path
def set_make_env(make: str) -> None:
"""
set MAKE environmental variable.
"""
os.environ['MAKE'] = make
def cmdstan_path() -> str:
"""
Validate, then return CmdStan directory path.
"""
cmdstan = ''
if 'CMDSTAN' in os.environ:
cmdstan = os.environ['CMDSTAN']
else:
cmdstan_dir = os.path.expanduser(os.path.join('~', '.cmdstanpy'))
if not os.path.exists(cmdstan_dir):
raise ValueError(
'no CmdStan installation found, '
'run command line script "install_cmdstan"'
)
latest_cmdstan = get_latest_cmdstan(cmdstan_dir)
if latest_cmdstan is None:
raise ValueError(
'no CmdStan installation found, '
'run command line script "install_cmdstan"'
)
cmdstan = os.path.join(cmdstan_dir, latest_cmdstan)
os.environ['CMDSTAN'] = cmdstan
validate_cmdstan_path(cmdstan)
return cmdstan
def cxx_toolchain_path(version: str = None) -> Tuple[str]:
"""
Validate, then activate C++ toolchain directory path.
"""
if platform.system() != 'Windows':
raise RuntimeError(
'Functionality is currently only supported on Windows'
)
if version is not None and not isinstance(version, str):
raise TypeError('Format version number as a string')
logger = get_logger()
toolchain_root = ''
if 'CMDSTAN_TOOLCHAIN' in os.environ:
toolchain_root = os.environ['CMDSTAN_TOOLCHAIN']
if os.path.exists(os.path.join(toolchain_root, 'mingw_64')):
compiler_path = os.path.join(
toolchain_root,
'mingw_64' if (sys.maxsize > 2 ** 32) else 'mingw_32',
'bin',
)
if os.path.exists(compiler_path):
tool_path = os.path.join(toolchain_root, 'bin')
if not os.path.exists(tool_path):
tool_path = ''
compiler_path = ''
logger.warning(
'Found invalid installion for RTools35 on %s',
toolchain_root,
)
toolchain_root = ''
else:
compiler_path = ''
logger.warning(
'Found invalid installion for RTools35 on %s',
toolchain_root,
)
toolchain_root = ''
elif os.path.exists(os.path.join(toolchain_root, 'mingw64')):
compiler_path = os.path.join(
toolchain_root,
'mingw64' if (sys.maxsize > 2 ** 32) else 'mingw32',
'bin',
)
if os.path.exists(compiler_path):
tool_path = os.path.join(toolchain_root, 'usr', 'bin')
if not os.path.exists(tool_path):
tool_path = ''
compiler_path = ''
logger.warning(
'Found invalid installion for RTools40 on %s',
toolchain_root,
)
toolchain_root = ''
else:
compiler_path = ''
logger.warning(
'Found invalid installion for RTools40 on %s',
toolchain_root,
)
toolchain_root = ''
else:
rtools_dir = os.path.expanduser(
os.path.join('~', '.cmdstanpy', 'RTools')
)
if not os.path.exists(rtools_dir):
raise ValueError(
'no RTools installation found, '
'run command line script "install_cxx_toolchain"'
)
compiler_path = ''
tool_path = ''
if version not in ('4', '40', '4.0') and os.path.exists(
os.path.join(rtools_dir, 'RTools35')
):
toolchain_root = os.path.join(rtools_dir, 'RTools35')
compiler_path = os.path.join(
toolchain_root,
'mingw_64' if (sys.maxsize > 2 ** 32) else 'mingw_32',
'bin',
)
if os.path.exists(compiler_path):
tool_path = os.path.join(toolchain_root, 'bin')
if not os.path.exists(tool_path):
tool_path = ''
compiler_path = ''
logger.warning(
'Found invalid installion for RTools35 on %s',
toolchain_root,
)
toolchain_root = ''
else:
compiler_path = ''
logger.warning(
'Found invalid installion for RTools35 on %s',
toolchain_root,
)
toolchain_root = ''
if (
not toolchain_root or version in ('4', '40', '4.0')
) and os.path.exists(os.path.join(rtools_dir, 'RTools40')):
toolchain_root = os.path.join(rtools_dir, 'RTools40')
compiler_path = os.path.join(
toolchain_root,
'mingw64' if (sys.maxsize > 2 ** 32) else 'mingw32',
'bin',
)
if os.path.exists(compiler_path):
tool_path = os.path.join(toolchain_root, 'usr', 'bin')
if not os.path.exists(tool_path):
tool_path = ''
compiler_path = ''
logger.warning(
'Found invalid installion for RTools40 on %s',
toolchain_root,
)
toolchain_root = ''
else:
compiler_path = ''
logger.warning(
'Found invalid installion for RTools40 on %s',
toolchain_root,
)
toolchain_root = ''
if not toolchain_root:
raise ValueError(
'no C++ toolchain installation found, '
'run command line script "install_cxx_toolchain"'
)
logger.info('Adds C++ toolchain to $PATH: %s', toolchain_root)
os.environ['PATH'] = ';'.join(
list(
OrderedDict.fromkeys(
[compiler_path, tool_path] + os.getenv('PATH', '').split(';')
)
)
)
return compiler_path, tool_path
def _rdump_array(key: str, val: np.ndarray) -> str:
"""Flatten numpy ndarray, format as Rdump variable declaration."""
c = 'c(' + ', '.join(map(str, val.T.flat)) + ')'
if (val.size,) == val.shape:
return '{key} <- {c}'.format(key=key, c=c)
else:
dim = '.Dim = c{}'.format(val.shape)
struct = '{key} <- structure({c}, {dim})'.format(key=key, c=c, dim=dim)
return struct
def jsondump(path: str, data: Dict) -> None:
"""Dump a dict of data to a JSON file."""
data = data.copy()
for key, val in data.items():
if isinstance(val, np.ndarray):
val = val.tolist()
data[key] = val
with open(path, 'w') as fd:
json.dump(data, fd)
def rdump(path: str, data: Dict) -> None:
"""Dump a dict of data to a R dump format file."""
with open(path, 'w') as fd:
for key, val in data.items():
if isinstance(val, (np.ndarray, Sequence)):
line = _rdump_array(key, np.asarray(val))
else:
line = '{} <- {}'.format(key, val)
print(line, file=fd)
def rload(fname: str) -> dict:
"""Parse data and parameter variable values from an R dump format file.
This parser only supports the subset of R dump data as described
in the "Dump Data Format" section of the CmdStan manual, i.e.,
scalar, vector, matrix, and array data types.
"""
data_dict = {}
with open(fname, 'r') as fd:
lines = fd.readlines()
# Variable data may span multiple lines, parse accordingly
idx = 0
while idx < len(lines) and '<-' not in lines[idx]:
idx += 1
if idx == len(lines):
return None
start_idx = idx
idx += 1
while True:
while idx < len(lines) and '<-' not in lines[idx]:
idx += 1
next_var = idx
var_data = ''.join(lines[start_idx:next_var]).replace('\n', '')
lhs, rhs = [item.strip() for item in var_data.split('<-')]
lhs = lhs.replace('"', '') # strip optional Jags double quotes
rhs = rhs.replace('L', '') # strip R long int qualifier
data_dict[lhs] = parse_rdump_value(rhs)
if idx == len(lines):
break
start_idx = next_var
idx += 1
return data_dict
def parse_rdump_value(rhs: str) -> Union[int, float, np.array]:
"""Process right hand side of Rdump variable assignment statement.
Value is either scalar, vector, or multi-dim structure.
Use regex to capture structure values, dimensions.
"""
pat = re.compile(
r'structure\(\s*c\((?P<vals>[^)]*)\)'
r'(,\s*\.Dim\s*=\s*c\s*\((?P<dims>[^)]*)\s*\))?\)'
)
val = None
try:
if rhs.startswith('structure'):
parse = pat.match(rhs)
if parse is None or parse.group('vals') is None:
raise ValueError(rhs)
vals = [float(v) for v in parse.group('vals').split(',')]
val = np.array(vals, order='F')
if parse.group('dims') is not None:
dims = [int(v) for v in parse.group('dims').split(',')]
val = np.array(vals).reshape(dims, order='F')
elif rhs.startswith('c(') and rhs.endswith(')'):
val = np.array([float(item) for item in rhs[2:-1].split(',')])
elif '.' in rhs or 'e' in rhs:
val = float(rhs)
else:
val = int(rhs)
except TypeError:
raise ValueError('bad value in Rdump file: {}'.format(rhs))
return val
def check_sampler_csv(
path: str,
is_fixed_param: bool = False,
iter_sampling: int = None,
iter_warmup: int = None,
save_warmup: bool = False,
thin: int = None,
) -> Dict:
"""Capture essential config, shape from stan_csv file."""
meta = scan_sampler_csv(path, is_fixed_param)
if thin is None:
thin = _CMDSTAN_THIN
elif thin > _CMDSTAN_THIN:
if 'thin' not in meta:
raise ValueError(
'bad csv file {}, '
'config error, expected thin = {}'.format(path, thin)
)
if meta['thin'] != thin:
raise ValueError(
'bad csv file {}, '
'config error, expected thin = {}, found {}'.format(
path, thin, meta['thin']
)
)
draws_sampling = iter_sampling
if draws_sampling is None:
draws_sampling = _CMDSTAN_SAMPLING
draws_warmup = iter_warmup
if draws_warmup is None:
draws_warmup = _CMDSTAN_WARMUP
draws_warmup = int(math.ceil(draws_warmup / thin))
draws_sampling = int(math.ceil(draws_sampling / thin))
if meta['draws_sampling'] != draws_sampling:
raise ValueError(
'bad csv file {}, expected {} draws, found {}'.format(
path, draws_sampling, meta['draws_sampling']
)
)
if save_warmup:
if not ('save_warmup' in meta and meta['save_warmup'] == 1):
print(meta)
raise ValueError(
'bad csv file {}, '
'config error, expected save_warmup = 1'.format(path)
)
if meta['draws_warmup'] != draws_warmup:
raise ValueError(
'bad csv file {}, '
'expected {} warmup draws, found {}'.format(
path, draws_warmup, meta['draws_warmup']
)
)
return meta
def scan_sampler_csv(path: str, is_fixed_param: bool = False) -> Dict:
"""Process sampler stan_csv output file line by line."""
dict = {}
lineno = 0
with open(path, 'r') as fd:
lineno = scan_config(fd, dict, lineno)
lineno = scan_column_names(fd, dict, lineno)
if not is_fixed_param:
lineno = scan_warmup_iters(fd, dict, lineno)
lineno = scan_metric(fd, dict, lineno)
lineno = scan_sampling_iters(fd, dict, lineno)
return dict
def scan_optimize_csv(path: str) -> Dict:
"""Process optimizer stan_csv output file line by line."""
dict = {}
lineno = 0
with open(path, 'r') as fd:
lineno = scan_config(fd, dict, lineno)
lineno = scan_column_names(fd, dict, lineno)
line = fd.readline().lstrip(' #\t').rstrip()
xs = line.split(',')
dict['mle'] = [float(x) for x in xs]
return dict
def scan_generated_quantities_csv(path: str) -> Dict:
"""
Process standalone generated quantities stan_csv output file line by line.
"""
dict = {}
lineno = 0
with open(path, 'r') as fd:
lineno = scan_config(fd, dict, lineno)
lineno = scan_column_names(fd, dict, lineno)
return dict
def scan_variational_csv(path: str) -> Dict:
"""Process advi stan_csv output file line by line."""
dict = {}
lineno = 0
with open(path, 'r') as fd:
lineno = scan_config(fd, dict, lineno)
lineno = scan_column_names(fd, dict, lineno)
line = fd.readline().lstrip(' #\t').rstrip()
lineno += 1
if not line.startswith('Stepsize adaptation complete.'):
raise ValueError(
'line {}: expecting adaptation msg, found:\n\t "{}"'.format(
lineno, line
)
)
line = fd.readline().lstrip(' #\t\n')
lineno += 1
if not line.startswith('eta = 1'):
raise ValueError(
'line {}: expecting eta = 1, found:\n\t "{}"'.format(
lineno, line
)
)
line = fd.readline().lstrip(' #\t\n')
lineno += 1
xs = line.split(',')
variational_mean = [float(x) for x in xs]
dict['variational_mean'] = variational_mean
dict['variational_sample'] = pd.read_csv(
path, comment='#', skiprows=lineno, header=None
)
return dict
def scan_config(fd: TextIO, config_dict: Dict, lineno: int) -> int:
"""
Scan initial stan_csv file comments lines and
save non-default configuration information to config_dict.
"""
cur_pos = fd.tell()
line = fd.readline().strip()
while len(line) > 0 and line.startswith('#'):
lineno += 1
if not line.endswith('(Default)'):
line = line.lstrip(' #\t')
key_val = line.split('=')
if len(key_val) == 2:
if key_val[0].strip() == 'file' and not key_val[1].endswith(
'csv'
):
config_dict['data_file'] = key_val[1].strip()
elif key_val[0].strip() != 'file':
raw_val = key_val[1].strip()
try:
val = int(raw_val)
except ValueError:
try:
val = float(raw_val)
except ValueError:
val = raw_val
config_dict[key_val[0].strip()] = val
cur_pos = fd.tell()
line = fd.readline().strip()
fd.seek(cur_pos)
return lineno
def scan_warmup_iters(fd: TextIO, config_dict: Dict, lineno: int) -> int:
"""
Check warmup iterations, if any.
"""
if 'save_warmup' not in config_dict:
return lineno
cur_pos = fd.tell()
line = fd.readline().strip()
draws_found = 0
while len(line) > 0 and not line.startswith('#'):
lineno += 1
draws_found += 1
cur_pos = fd.tell()
line = fd.readline().strip()
fd.seek(cur_pos)
config_dict['draws_warmup'] = draws_found
return lineno
def scan_column_names(fd: TextIO, config_dict: Dict, lineno: int) -> int:
"""
Process columns header, add to config_dict as 'column_names'
"""
line = fd.readline().strip()
lineno += 1
names = line.split(',')
config_dict['column_names'] = tuple(names)
config_dict['num_params'] = len(names) - 1
return lineno
def parse_var_dims(names: Tuple[str, ...]) -> Dict:
"""
Use Stan CSV file column names to get variable names, dimensions.
Assumes that CSV file has been validated and column names are correct.
"""
if names is None:
raise ValueError('missing argument "names"')
vars_dict = {}
idx = 0
while idx < len(names):
if names[idx].endswith('__'):
pass
elif '.' not in names[idx]:
vars_dict[names[idx]] = 1
else:
vs = names[idx].split('.')
if idx < len(names) - 1 and names[idx + 1].split('.')[0] == vs[0]:
idx += 1
continue
dims = [int(vs[x]) for x in range(1, len(vs))]
vars_dict[vs[0]] = dims
idx += 1
return vars_dict
def scan_metric(fd: TextIO, config_dict: Dict, lineno: int) -> int:
"""
Scan stepsize, metric from stan_csv file comment lines,
set config_dict entries 'metric' and 'num_params'
"""
if 'metric' not in config_dict:
config_dict['metric'] = 'diag_e'
metric = config_dict['metric']
line = fd.readline().strip()
lineno += 1
if not line == '# Adaptation terminated':
raise ValueError(
'line {}: expecting metric, found:\n\t "{}"'.format(lineno, line)
)
line = fd.readline().strip()
lineno += 1
label, stepsize = line.split('=')
if not label.startswith('# Step size'):
raise ValueError(
'line {}: expecting stepsize, '
'found:\n\t "{}"'.format(lineno, line)
)
try:
float(stepsize.strip())
except ValueError:
raise ValueError(
'line {}: invalid stepsize: {}'.format(lineno, stepsize)
)
line = fd.readline().strip()
lineno += 1
if not (
(
metric == 'diag_e'
and line == '# Diagonal elements of inverse mass matrix:'
)
or (
metric == 'dense_e' and line == '# Elements of inverse mass matrix:'
)
):
raise ValueError(
'line {}: invalid or missing mass matrix '
'specification'.format(lineno)
)
line = fd.readline().lstrip(' #\t')
lineno += 1
num_params = len(line.split(','))
config_dict['num_params'] = num_params
if metric == 'diag_e':
return lineno
else:
for _ in range(1, num_params):
line = fd.readline().lstrip(' #\t')
lineno += 1
if len(line.split(',')) != num_params:
raise ValueError(
'line {}: invalid or missing mass matrix '
'specification'.format(lineno)
)
return lineno
def scan_sampling_iters(fd: TextIO, config_dict: Dict, lineno: int) -> int:
"""
Parse sampling iteration, save number of iterations to config_dict.
"""
draws_found = 0
num_cols = len(config_dict['column_names'])
cur_pos = fd.tell()
line = fd.readline().strip()
while len(line) > 0 and not line.startswith('#'):
lineno += 1
draws_found += 1
data = line.split(',')
if len(data) != num_cols:
raise ValueError(
'line {}: bad draw, expecting {} items, found {}'.format(
lineno, num_cols, len(line.split(','))
)
)
cur_pos = fd.tell()
line = fd.readline().strip()
config_dict['draws_sampling'] = draws_found
fd.seek(cur_pos)
return lineno
def read_metric(path: str) -> List[int]:
"""
Read metric file in JSON or Rdump format.
Return dimensions of entry "inv_metric".
"""
if path.endswith('.json'):
with open(path, 'r') as fd:
metric_dict = json.load(fd)
if 'inv_metric' in metric_dict:
dims = np.asarray(metric_dict['inv_metric'])
return list(dims.shape)
else:
raise ValueError(
'metric file {}, bad or missing'
' entry "inv_metric"'.format(path)
)
else:
dims = list(read_rdump_metric(path))
if dims is None:
raise ValueError(
'metric file {}, bad or missing'
' entry "inv_metric"'.format(path)
)
return dims
def read_rdump_metric(path: str) -> List[int]:
"""
Find dimensions of variable named 'inv_metric' in Rdump data file.
"""
metric_dict = rload(path)
if not (
'inv_metric' in metric_dict
and isinstance(metric_dict['inv_metric'], np.ndarray)
):
raise ValueError(
'metric file {}, bad or missing entry "inv_metric"'.format(path)
)
return list(metric_dict['inv_metric'].shape)
def do_command(cmd: str, cwd: str = None, logger: logging.Logger = None) -> str:
"""
Spawn process, print stdout/stderr to console.
Throws RuntimeError on non-zero returncode.
"""
if logger:
logger.debug('cmd: %s', cmd)
proc = subprocess.Popen(
cmd,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=os.environ,
)
stdout, stderr = proc.communicate()
if proc.returncode:
if stderr:
msg = 'ERROR\n {} '.format(stderr.decode('utf-8').strip())
raise RuntimeError(msg)
if stdout:
return stdout.decode('utf-8').strip()
return None
def windows_short_path(path: str) -> str:
"""
Gets the short path name of a given long path.
http://stackoverflow.com/a/23598461/200291
On non-Windows platforms, returns the path
If (base)path does not exist, function raises RuntimeError
"""
if platform.system() != 'Windows':
return path
if os.path.isfile(path) or (
not os.path.isdir(path) and os.path.splitext(path)[1] != ''
):
base_path, file_name = os.path.split(path)
else:
base_path, file_name = path, ''
if not os.path.exists(base_path):
raise RuntimeError(
'Windows short path function needs a valid directory. '
'Base directory does not exist: "{}"'.format(base_path)
)
import ctypes
from ctypes import wintypes
# pylint: disable=invalid-name
_GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
_GetShortPathNameW.argtypes = [
wintypes.LPCWSTR,
wintypes.LPWSTR,
wintypes.DWORD,
]
_GetShortPathNameW.restype = wintypes.DWORD
output_buf_size = 0
while True:
output_buf = ctypes.create_unicode_buffer(output_buf_size)
needed = _GetShortPathNameW(base_path, output_buf, output_buf_size)
if output_buf_size >= needed:
short_base_path = output_buf.value
break
else:
output_buf_size = needed
short_path = (
os.path.join(short_base_path, file_name)
if file_name
else short_base_path
)
return short_path
def create_named_text_file(dir: str, prefix: str, suffix: str) -> str:
"""
Create a named unique file.
"""
fd = tempfile.NamedTemporaryFile(
mode='w+', prefix=prefix, suffix=suffix, dir=dir, delete=False
)
path = fd.name
fd.close()
return path
def install_cmdstan(version: str = None, dir: str = None) -> bool:
"""
Run 'install_cmdstan' -script
"""
logger = get_logger()
python = sys.executable
here = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(here, 'install_cmdstan.py')
cmd = [python, path]
if version is not None:
cmd.extend(['--version', version])
if dir is not None:
cmd.extend(['--dir', dir])
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ
)
while proc.poll() is None:
output = proc.stdout.readline().decode('utf-8').strip()
if output:
logger.info(output)
proc.communicate()
if proc.returncode:
logger.warning('CmdStan installation failed')
return False
return True