-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
executable file
·1089 lines (936 loc) · 38.3 KB
/
datasets.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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
BLINC Adaptive Prosthetics Toolkit
- Bionic Limbs for Improved Natural Control, blinclab.ca
A toolkit for automating online learning experiments
This defines the primitives that Structures can hold, that are parsable
parameters.
A Structure is an object that can construct (and reconstruct) itself from a
file string, command line arguments, and parameter dictionary.
'''
import inspect
import os
import re
from inspect import Parameter, Signature
from pandas import DataFrame, Series
from collections import OrderedDict, namedtuple
from logfiles import LogReader, LogWriter
import argparse
import pickle
from datasets import *
from helpers import *
class empty:
pass
def is_empty(val):
return val == inspect._empty or val == Parameter.empty or val == empty
class Parsable(Parameter):
"""
Data field storage for the logger.
positional arguments occurs in the order it appeared in the _field list
all non-positional arguments are grouped together at the end
required, keyword, positional, transient flags have to be treated
differently than default parameter or argparse meaning because
of the inclusion of structs
required
will raise a type error if they do not have a default
and are not provided (or if it is a struct, if its required
parameters are not provided)
positional
applies only to dirstring construction, the signature and
parser have no positional arguments. Positional arguments
get their own directory in the dirstring
keyword
if true, include the name in its string format (NAME-VALUE) instead of
alone (VALUE)
Optional extra parameters are stored in self.kwargs
"""
_expected_type = empty
def __init__(self, name, kind=Parameter.POSITIONAL_OR_KEYWORD,
required=False, keyword=True, positional=False,
constructor=empty, transient=False, constant=empty,
**kwargs):
default = kwargs.pop('default', empty)
self.kwargs = kwargs
self.init_kind = kind
self.required = required # does running the experiment require it
self.transient = transient # is it necessary for defining the results
self.positional = positional # is its value set by its position in dirstring
self.keyword = keyword # is its keyword included in the dirstring
self.constant = constant # is it a sentinel value
self.choices = kwargs.get('choices', None) # is the value restricted
self.nargs = kwargs.get('nargs', None)
# nargs might have implicitly determined required
if self.nargs:
if self.nargs == '+':
self.required = True
else:
self.required = False
self.constructor = constructor
super().__init__(name, kind=Parameter.POSITIONAL_OR_KEYWORD,
default=default, annotation=self.kwargs)
def is_valid(self, value):
"""
Return true if the passed value satisfies constraints on this
type of parsable. There are no constraints on the base parsable
"""
return True
def add_parser_arg(self, parser):
"""
Add yourself to the parser that is passed, even if not included in the dirstring
Include required, default, and type arguments as applicable
Optional kwargs let you customize/override the add_argument for special types
"""
# TODO: should make sure only valid add_argument keywords used
kwargs = self.kwargs.copy()
if not is_empty(self.default):
kwargs['default'] = self.default
kwargs.setdefault('required', self.required)
if not is_empty(self._expected_type):
# TODO: should check what types translate naturally --- add warning
# from docs
# type= can take any callable that takes a single string argument and returns the converted value:
kwargs['type'] = self._expected_type
parser.add_argument("--{}".format(self.name), **kwargs)
def stringify(self, value, code='-', remove_name=False):
"""
Return an appropriately formatted string for your parameter settings,
given the value passed
"""
if not isinstance(value, str):
value = clean_string(value)
if (self.keyword and not remove_name) or code=='arg':
parts = format_param(name=self.name, value=value, code=code)
else:
parts = format_param(value=value, code=code)
if isinstance(parts, str):
return parts
else:
return format_join(parts, code=code)
def de_string(self, value):
"""
Return an appropriate literal evaluation of the passed value
"""
return de_string(value)
def get_arg_string(self, instance):
"""
Return the command-line argument representation of the parameter
with the value taken from the object passed
"""
val = getattr(instance, self.name, empty)
if is_empty(val): # or val == self.default:
return ''
if not isinstance(val, str):
val = clean_string(val)
return self.stringify(val, code='arg')
def copy_self(self, instance, from_instance):
"""
Copy the relevant attribute from from_instance to instance
"""
if hasattr(from_instance, self.name):
self.set_self(instance, {k: getattr(from_instance, self.name)})
with suppress(TypeError, KeyError):
self.set_self(instance, {k: from_instance[self.name]})
def convert_val(self, val):
if not is_empty(self._expected_type) and not isinstance(val, self._expected_type):
try:
val = self._expected_type(val) #should override this for bools/None
except:
raise TypeError('Could not convert {} to {}'.format(val, self._expected_type))
elif not is_empty(self.constructor) and not isinstance(val, self.constructor):
val = self.constructor.from_wildcard(val)
"""
if val == 'None':
val = None
elif val == 'True':
val = True
elif val == 'False':
val = False
"""
if self.choices and val not in self.choices:
raise TypeError('{} must be one of {}'.format(self.name, self.choices))
return val
def set_self(self, instance, kwargs):
"""
Use this to set values with explicit type checking and all
Uses the kwarg dictionary to set self.name value on the given instance.
If no valid key in dictionary, uses own default.
If you don't have a default *and* no key is provided, you should complain. Also it shouldn't happen.
Might want to turn this into an official generator, except for the performance overhead.
"""
if self.name in kwargs:
val = kwargs[self.name]
elif not is_empty(self.constructor) and 'kwargs' in kwargs:
try:
val = self.constructor.from_wildcard(kwargs['kwargs'])
except TypeError as e:
if not self.required:
return
else:
raise e
elif not is_empty(self.default):
val = self.default
elif not self.required:
return
else:
raise TypeError('Missing required keyword {}'.format(self.name))
if not self.nargs:
val = self.convert_val(val)
elif self.nargs:
val = listify(val)
val = [self.convert_val(v) for v in val]
if isinstance(self.nargs, int):
if len(val) < self.nargs:
raise TypeError('{} requires {} values'.format(self.name, self.nargs))
elif len(val) > self.nargs:
val = val[:self.nargs]
print('Dropped excess arguments from {}'.format(self.name))
setattr(instance, self.name, val)
class String(Parsable):
_expected_type = str
class Integer(Parsable):
_expected_type = int
class Float(Parsable):
_expected_type = float
class Dir(Parsable):
_expected_type = str
# TODO: add set_parameter function to check directory
# TODO: might be able to have expected_type be something file specific
class Boolean(Parsable):
_expected_type = bool
"""
Deal with the fact that argparse doesn't properly handle boolean strings
In the parser set as
---ARG_off
or
---ARG
"""
# TODO: add add_parser_arg and set_parameter functions
def add_parser_arg(self, parser):
"""
Booleans require flags in the parser args, can't be strings
"""
bl = parser.add_mutually_exclusive_group(required=False)
bl.add_argument('--{}_off'.format(self.name), action='store_const',
dest=self.name, const=False, default=self.default)
bl.add_argument('--{}'.format(self.name), action='store_true', default=self.default)
def get_arg_string(self, instance):
val = getattr(instance, self.name)
if val:
return "--{}".format(self.name)
else:
return "--{}_off".format(self.name)
def set_parameter(self, instance, kwargs):
if self.name in kwargs and isinstance(kwargs[self.name], str):
kwargs[self.name] = ast.literal_eval(kwargs[self.name])
super().set_parameter(instance, kwargs)
class Listable(Parsable):
"""
Listable parameters can be passed by string or as a list of separate items
In the parser they take the form
---ARGs val1 val2 val3
or
---ARG val1-val2-val3
"""
def add_parser_arg(self, parser):
# TODO: should not fail silently if parameter has annotations
tmp = parser.add_mutually_exclusive_group(required=self.required)
tmp.add_argument('--{}'.format(self.name), dest=self.name)
tmp.add_argument('--{}s'.format(self.name), dest=self.name, nargs='+')
def set_self(self, instance, kwargs):
# TODO: straighten out Listable parameter definitions---consistency and parsability
if self.name in kwargs:
val = kwargs[self.name]
elif not is_empty(self.default):
val = self.default
elif self.required:
raise ValueError("Can't set {}, not available in kwargs".format(self.name))
else:
val = empty
if isinstance(val, str) and self.constructor != str:
setattr(instance, self.name, self.de_string(val))
else:
# might need to do some more processing here of the elements of the list
setattr(instance, self.name, listify(val))
def de_string(self, value):
val = listify(de_string(value, suppress_eval=True))
return [de_string(v) for v in val]
class Keyed(Parsable):
"""
Keyed parameters are passed by dictionaries
In the parser they take the form (like separate lists for each key with key as first element)
--ARG key1-val1-val2 key2-val1 key3-
"""
_expected_type = dict
def add_parser_arg(self, parser):
tmp = parser.add_mutually_exclusive_group(required=self.required)
tmp.add_argument('--{}'.format(self.name), nargs='+')
def stringify(self, value, code='-', remove_name=False):
if isinstance(value, str):
return super().stringify(value, code=code, remove_name=remove_name)
else:
return super().stringify(clean_string(value), code=code, remove_name=remove_name)
def de_string(self, value):
return split_params(value)
def set_self(self, instance, kwargs):
if self.name in kwargs:
val = kwargs[self.name]
elif not is_empty(self.default):
val = self.default
elif not self.required:
val = empty
else:
raise ValueError("No {} parameter in kwargs".format(self.name))
if isinstance(val, str):
val = split_params(val)
elif not isinstance(val, dict) and not is_empty(val):
with suppress(TypeError):
val = split_params('_'.join(val))
setattr(instance, self.name, val)
kwargs = Parameter('kwargs', kind=Parameter.VAR_KEYWORD)
def has_nonpos_nonkey(params):
possibilities = [k.name for k in params if isinstance(k, Parsable) and not k.positional and not k.keyword and not k.transient]
if len(possibilities) == 0:
return None
elif len(possibilities) > 1:
raise TypeError("Only one non-positional non-keyword parameter allowed per Structure")
else:
return possibilities[0]
class StructureMeta(type):
"""
Pulls the arguments from a _fields class variable
"""
def __new__(cls, clsname, bases, clsdict):
param_list = [f for f in clsdict.get('_fields', [])]
param_list.append(kwargs)
clsdict['__signature__'] = Signature(param_list)
return super().__new__(cls, clsname, bases, clsdict)
class Structure(metaclass=StructureMeta):
"""
A structure groups a bunch of Parsables and allows for
lossless conversion between instances, dictionaries, command line arguments, dirpaths, strings
_fields is a list of the Parsables it holds
_inherited is a list of variable names it copies from somewhere else
The dirpath structure:
- the final directory is always the non-positional parameters (if any), alphabetically sorted
- positional parameters each reside in their own directory in fixed order
- only one non-keyword non-positional argument can be recovered
- does not included any transient parameters
f
Inherited parameters are not stored as part of the immediate dirstring.
- but if they are there it shouldn't cause problems
To get access to direct saving/loading functions, define the log file
and either define or inherit the base_dir
@property
def log_file(self):
return "data.txt"
"""
_fields = []
_inherited = ['base_dir']
constructor = type(None)
log_data = False
def __init__(self, *args, **kwargs):
"""
Create an instance of this kind of structure according to its _fields
Try to set inherited instance variables according to extra keywords
"""
#print("calling init from", self, self._hello)
bargs = self.__signature__.bind(*args, **kwargs).arguments
for name, value in self.__signature__.parameters.items():
#if isinstance(value, Struct):
# value.set_self(self, kwargs)
if name != 'kwargs':
value.set_self(self, bargs)
# set inherited properties
self.set_inherited(complain=False, **kwargs)
def set_inherited(self, instance=None, complain=False, **kwargs):
"""
Set inherited values according to either the instance or
the kwargs passed
Checks kwargs first so you can use this to override instance values
"""
for name in self._inherited:
if kwargs and name in kwargs:
val = kwargs[name]
elif instance:
val = getattr(instance, name)
elif complain:
raise ValueError("Need to set {} inherited field from instance or kwargs".format(name))
else:
continue
setattr(self, name, val)
@property
def log_dir(self):
"""
Constructs log directory from the structure specifications
"""
return self.get_dir_string()
@property
def log_path(self):
return os.path.join(self.base_dir, self.log_dir)
@property
def log_file_path(self):
return os.path.join(self.log_path, self.log_file)
@property
def name(self):
return self.log_dir
def set_base_dir(self, base_dir):
self.base_dir = base_dir
@property
def log_exists(self):
"""
Check if log file exists already.
Could do more checking of the log here or let the child classes
overwrite to do their own checking
"""
if self.loggable:
return os.path.exists(self.log_file_path)
else:
return False
@property
def loggable(self):
"""
Find out it this Structure can log itself
"""
return self.log_data and hasattr(self, 'log_file')
@property
def data(self):
if hasattr(self, '_data'):
return self._data
elif self.log_exists:
return self.load_data()
else:
print("Attempting to automatically generate data")
return self.run()
#@profile
def load_data(self, base_dir=None, **kwargs):
"""
Load the data into self._data and return it
"""
if base_dir:
self.set_base_dir(base_dir)
if hasattr(self, '_data'):
return self._data
if not self.loggable:
raise TypeError("{} is not able to log".format(self.name))
if not self.log_exists:
raise TypeError("{} has not been logged".format(self.name))
kwargs.setdefault('sep', ' ')
if kwargs.get('squeeze', False):
kwargs.setdefault('header', None)
if not base_dir:
base_dir = self.base_path
self._data = LogReader.read_log(self.log_file,
base_dir=base_dir,
**kwargs)
return self._data
def save_data(self, data=None):
"""
If provided, overwrite internal data structure with passed data
TODO: probably should do some error checking here
TODO: make sure models and log files are handled the same way
Try to write _data to disk.
"""
if not self.loggable:
raise TypeError("{} cannot write logs.".format(self.name))
if data is None:
if not hasattr(self, '_data'):
raise TypeError("{} has no _data to write".format(self.name))
data = self._data
if self.log_exists:
print("Log exists, turn off safe mode to overwrite")
return False
os.makedirs(self.log_path, exist_ok=True)
with open(self.log_file_path, 'wt') as fp:
fp.write("# {}\n".format(self.log_file_path))
self._data.to_csv(fp, sep=' ', index=False)
return self.log_file_path
@classmethod
def _has_keyword(cls):
return any([(f.keyword and not f.transient and not isinstance(f, Struct)) \
for f in cls._iter_params()])
@classmethod
def _has_npnk(cls):
return has_nonpos_nonkey(cls._iter_params())
@classmethod
def from_wildcard(cls, wildcard):
if isinstance(wildcard, argparse.Namespace):
return cls.from_namespace(wildcard)
elif isinstance(wildcard, list):
return cls.from_args(wildcard)
elif isinstance(wildcard, str):
if '--' in wildcard:
return cls.from_string(wildcard)
else:
return cls.from_dir_string(wildcard)
elif isinstance(wildcard, dict):
return cls(**wildcard)
else:
raise TypeError("Do not know how to construct {} from {}".format(cls, type(
wildcard)))
@classmethod
def from_args(cls, args):
# TODO: the problem here is get_parser includes all args
parser = cls.get_parser()
# TODO: going to have to do this in a method call when args need
# to be determined at runtime
# we only care about known args
ns = parser.parse_known_args(args)[0]
return cls.from_namespace(ns)
@classmethod
def from_string(cls, string):
return cls.from_args(string.split(' '))
@classmethod
def from_namespace(cls, ns):
return cls(**vars(ns))
@classmethod
def from_dir_string(cls, dstring):
return cls(**cls.params_from_dir_string(dstring, deep_eval=True))
@classmethod
def params_from_dir_string(cls, dstring, deep_eval=False):
"""
Extract the parameters (in command-line passable form)
from the directory string
"""
parts = os.path.splitext(dstring)
istring = dstring
#TODO: figure out how to normalize a dirstring
if 'txt' in parts[1]:
dstring = os.path.dirname(dstring)
keyworded = []
npnk = None
dstring = os.path.normpath(dstring)
# get keyworded params from the end if necessary
if cls._has_keyword():
(dstring, kstring) = os.path.split(dstring)
if cls._has_npnk():
# this is a bit of a hack right now
kstring = '-'.join([cls._has_npnk(), kstring])
params = split_params(kstring, deep_eval=False)
keyworded = [k for k in params]
elif cls._has_npnk(): #no keyword, but a non-positional
(dstring, pstring) = os.path.split(dstring)
params = {cls._has_npnk(): pstring}
else:
params = {}
# find the positional parameters
for v in reversed(list(cls._iter_params())):
if v.transient:
continue
elif isinstance(v, Struct):
try:
sparams = v.constructor.params_from_dir_string(dstring,
deep_eval=deep_eval)
except ValueError as e:
if not v.required:
continue
else:
raise e
dstring = sparams.pop('base_dir')
params.update(sparams)
elif v.positional:
(dstring, name) = os.path.split(dstring)
if v.keyword:
# have to take off the keyword argument
parts = de_string(name)
if parts[0] != v.name:
if v.required:
raise ValueError("Dirstring {} has no value for {}".format(istring, v.name))
else:
# it was optional and needs to be parsed for the correct parsable
dstring = os.path.join(dstring, name)
continue
if len(parts) > 2:
params[v.name] = v.stringify(parts[1:])
else:
params[v.name] = parts[1]
else:
params[v.name] = name
elif v.keyword: # keyworded and not positional means it should have been in the keyword splitting
try:
keyworded.remove(v.name)
except ValueError as e:
# we don't need to complain unless it's a necessary value
# but somebody else might have added it too
if v.required and v.name not in params:
raise e
else:
continue
elif not v.transient: # this is our npnk case
npnk = v
if deep_eval and not v.transient and not isinstance(v, Struct) and not v._expected_type == str:
params[v.name] = v.de_string(params[v.name])
# check if it is a sentinel value
if not is_empty(v.constant) and params[v.name] != v.constant:
raise ValueError("Invalid value ({}) for {}".format(params[v.name], v.name))
# set the non-positional non-keyword parameter if necessary
if npnk and npnk.name not in params:
if len(keyworded) < 1:
raise ValueError("Dirstring {} has no value for {}".format(istring, npnk.name))
elif len(keyworded) > 1:
raise ValueError("Dirstring {} has too many values for {}".format(istring, npnk.name))
else:
val = params.pop(keyworded[0])
if deep_eval:
val = npnk.de_string(val)
params[npnk.name] = val
params['base_dir'] = dstring
return params
def pretty_print(self, ignored_keys=None, code='-'):
"""
Construct a nicely formatted descriptor of this Structure object
"""
ignored_keys = listify(ignored_keys)
parts = []
kwargs = {}
for v in self._iter_params():
if v.name in ignored_keys or v.transient:
continue
elif isinstance(v, Struct):
parts.append(getattr(self, v.name).pretty_print(ignored_keys=ignored_keys, code=code))
elif v.keyword:
kwargs[v.name] = getattr(self, v.name)
else:
parts.append(format_param(v.name, value=getattr(self, v.name), code=code))
parts.append(clean_string(kwargs))
return format_join(parts, code=code)
def get_nonpos_string(self):
args = []
npnk = None
for v in self._iter_params():
if v.transient or v.positional or isinstance(v, Struct):
continue
val = getattr(self, v.name, empty)
if is_empty(val):
if v.required:
raise TypeError("How did this get constructed without a required parameter?")
continue
# convert to string
if not v.keyword: #then we have the single allowed one
if npnk is not None:
raise TypeError("Structures cannot have multiple non-keyword params in dirstring")
npnk = v.stringify(val, code='-')
else:
args.append(v.stringify(val, code='-'))
if args:
args = sorted(args)
if npnk is not None:
args.insert(0, npnk)
if args:
return format_join(args, '_')
else:
return ''
def get_dir_string(self):
args = []
for v in self._iter_params():
if v.transient: # we don't care
continue
if not isinstance(v, Struct) and not v.positional: # grabbed by the other function
continue
val = getattr(self, v.name, empty)
if is_empty(val):
if v.required:
raise TypeError("How did this get constructed without a required parameter?")
continue
#if isinstance(v, Struct):
# s = val.get_dir_string()
# if v.keyword:
# args.append(v.name)
# args.append(s)
#else:
#try:
# val = val.get_dir_string()
#except:
# val = v.stringify(val, code='-')
val = v.stringify(val, code='-')
assert isinstance(val, str)
if v.positional or isinstance(v, Struct):
args.append(val)
else:
if npnk:
raise TypeError("Structures cannot have multiple non-keyword params in dirstring")
npnk = val
key_string = self.get_nonpos_string()
if key_string:
args.append(key_string)
if args:
return os.path.join(*args)
else:
return ''
def get_dir_string_old(self):
args = []
nonpos = []
npnk = None
for v in self._iter_params():
if v.transient:
continue
elif not hasattr(self, v.name) and not v.required:
continue
elif isinstance(v, Struct):
s = getattr(self, v.name).get_dir_string()
if v.keyword:
args.append(v.name)
args.append(s)
elif is_empty(getattr(self, v.name)):
continue
else:
val = getattr(self, v.name)
try:
val = val.get_dir_string()
except:
val = v.stringify(val, code='-')
assert isinstance(val, str)
if v.positional:
args.append(val)
elif not v.keyword:
npnk = val
else:
nonpos.append(val)
nonpos = sorted(nonpos)
if npnk:
nonpos.insert(0, npnk)
if nonpos:
nonpos_dir = format_join(nonpos, '_')
args.append(nonpos_dir)
if args:
return os.path.join(*args)
else:
return ''
def get_record(self):
"""
Return a Series object containing all the fields
of this object
"""
print("Deprecated")
return Series({v.name: getattr(self, v.name) \
for v in self._iter_params() \
if not v.transient})
def create(self):
"""
Return an instance of the thing you are supposed to make
"""
return self.constructor(**vars(self))
@classmethod
def get_parser(cls):
parser = argparse.ArgumentParser()
cls.add_parser_args(parser)
return parser
@classmethod
def add_parser_args(cls, parser):
for v in cls._iter_params():
v.add_parser_arg(parser)
@classmethod
def _iter_keys(cls):
for v in cls._iter_params():
yield v.name
@classmethod
def _iter_params(cls):
for v in cls._fields:
yield v
@classmethod
def _iter_base_params(cls):
for v in cls._fields:
if isinstance(v, Struct):
yield from v.constructor._iter_base_params()
elif not v.transient:
yield v
def matches_param(self, param, drop_vals=None, assert_vals=None):
key = param.name
val = param.stringify(getattr(self, key), remove_name=True)
if not drop_vals:
drop_vals = {}
if not assert_vals:
assert_vals = {}
# if this key is in the drop list and its value is one of the options, not a match
if key in drop_vals and val in listify(drop_vals[key]):
return False
# if this key is in the must-have list and its value is not given, not a match
if key in assert_vals and val not in listify(assert_vals[key]):
return False
if isinstance(param, Struct):
return getattr(self, key).matches(drop_vals=drop_vals, assert_vals=assert_vals)
return True
def get_parameter(self, key):
"""
Return the named parameter for this object (doing deep search)
If no such parameter is found, return a generic one.
"""
for k in self._iter_base_params():
if k.name == key:
return k
else:
return Parsable(key)
def matches(self, drop_vals=None, assert_vals=None):
if assert_vals is None:
assert_vals = {}
if drop_vals is None:
drop_vals = {}
for k in assert_vals:
if not hasattr(self, k):
return False
val = getattr(self, k)
strval = self.get_parameter(k).stringify(val)
if val not in listify(assert_vals[k]) and strval not in listify(assert_vals[k]):
return False
for k in drop_vals:
if not hasattr(self, k):
continue
val = self.get_parameter(k).stringify(getattr(self, k))
if val in listify(drop_vals[k]):
return False
#for v in self._iter_params():
# if not self.matches_param(v, drop_vals=drop_vals, assert_vals=assert_vals):
# return False
# make sure we have every asserted key
return True
def get_flat_record(self, coll_strings=False):
d = {}
for v in self._iter_base_params():
if coll_strings and (isinstance(v, Keyed) or isinstance(v, Listable)):
val = v.stringify(getattr(self, v.name), remove_name=True)
else:
val = getattr(self, v.name)
d[v.name] = val
return Series(d)
def items(self):
for v in self._iter_keys():
yield (v, getattr(self, v))
def get_arg_string(self):
return " ".join([p.get_arg_string(self) \
for p in self._fields])
#TODO: make model saver and log saver mixins
class Experiment(Structure):
"""
TODO: make this an abstract class so that call must be defined
TODO: move the data logging into here as well, maybe?
Consolidates the features that are useful in experiment structures
Allows for variable numbers of Structs
The __call__ function of each paticular experiment defines what happens
"""
log_data = True
log_model = False
def __enter__(self):
base_path = os.path.normpath(os.path.join(self.base_dir,
self.get_nonpos_string()))
self.input_dim = None
for s in self._iter_params():
if isinstance(s, Struct) and hasattr(self, s.name):
child = getattr(self, s.name)
if s.keyword:
base_path = os.path.normpath(os.path.join(base_path, s.name))
child.set_inherited(base_dir=base_path, instance=self)
with suppress(AttributeError):
child.__enter__()
base_path = child.log_path
with suppress(AttributeError):
self.input_dim = child.output_dim
self.get_inherited(s, complain=True)
# see if it needs to share anything
# this will not be true if it has its own positional/nonpos arguments
#assert base_path == self.log_path
return self
def __exit__(self, *args):
for s in self._iter_keys():
with suppress(AttributeError):
getattr(self, s).__exit__()
if self.log_model:
self.save_model()
def get_inherited(self, child, complain=False):
"""
Given a Structure object, pull in whatever inherited features
"""
if child.broadcast and hasattr(self, child.name):
content = getattr(self, child.name)
for att in child.broadcast:
#TODO: could use a dictionary here if we want to translate things
try:
setattr(self, att, getattr(content, att))
except AttributeError as e:
if complain:
raise e
# TODO: this is the batch approach, should probably have both incremental
# and batch standardized somehow
def run(self):
with self as setup:
return setup()
def __call__(self):
raise NotImplementedError("Must define each experiment's call method")
def load_model(self):
"""
Load the model if possible. If not, run the experiment to generate it
"""
if hasattr(self, '_model'):
return self._model