pydial.py 53 KB
Newer Older
Carel van Niekerk's avatar
Init  
Carel van Niekerk committed
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
#! /usr/bin/env python

###############################################################################
# PyDial: Multi-domain Statistical Spoken Dialogue System Software
###############################################################################
#
# Copyright 2015 - 2019
# Cambridge University Engineering Department Dialogue Systems Group
#
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################


import os
from scriptine import run, path, log, command
import re
import numpy as np

# Uncomment for mac os users
import matplotlib
# matplotlib.use('TkAgg')
# matplotlib.use('Agg')

import matplotlib.pyplot as plt

#Uncomment for 4k screens
# matplotlib.rcParams.update({'font.size': 22})

# PyDial modules
import Simulate
import Texthub
from utils import Settings
from utils import ContextLogger
from ontology import Ontology
import utils.ContextLogger as clog
import pprint
pp = pprint.PrettyPrinter(indent=4)

# Remove tensorflow deprecation warnings
#import tensorflow.python.util.deprecation as deprecation
#deprecation._PRINT_DEPRECATION_WARNINGS = False

logger = None
tracedialog = 2
policy_dir = ""
conf_dir = ""
log_dir = ""
logfile = ""

gnumtrainbatches = 0
gtraindialogsperbatch = 0
gnumbatchtestdialogs = 0
gnumtestdialogs = 0
gtrainerrorrate = 0
gtesterrorrate = 0
gtrainsourceiteration = 0
gtesteverybatch = False

gpscale = 1

gplotnum = 1

gbatchnum = 0

isSingleDomain = False
taskID = ""
domain = ""
domains = []
policytype = "hdc"

policytypes = {}


def help_command():
    """ Provide an overview of pydial functionality
    """
    print("\n pydial - command line interface to PyDial")
    print('""""""""""""""""""""""""""""""""""""""""""""')
    print(' o Runs simulator to train and test policies')
    print(' o Plots learning rates and performance vs error rate')
    print(' o Runs texthub in multi-domain chat mode\n')
    print('Basic usage:')
    print('  a) Make pydial.py executable and add a symbolic link to it (eg pydial) from your')
    print('     local bin directory.  Create a directory called ID and cd into it.\n')
    print("  b) create a config file and add an exec_config section eg:\n")
    print('     [exec_config]')
    print('     domain = CamRestaurants     # specific train/test domain')
    print('     policytype = gp             # type of policy to train/test')
    print('     configdir = cfgdir          # folder to store configs')
    print('     logfiledir = logdir         # folder to store logfiles')
    print('     numtrainbatches = 2         # num training batches (iterations)')
    print('     traindialogsperbatch = 10   # num dialogs per batch')
    print('     numbatchtestdialogs =  20   # num dialogs to eval each batch')
    print('     trainsourceiteration = 0    # index of initial source policy to update')
    print('     testiteration = 1           # policy iteration to test')
    print('     numtestdialogs =  10        # num dialogs per test')
    print('     trainerrorrate = 0          # train error rate in %')
    print('     testerrorrate  = 0          # test error rate in %')
    print('     testeverybatch = True       # enable batch testing\n')
    print('     by convention the config file name for training and testing should be of the')
    print('     form ID-policytype-domain.cfg where ID is a user-defined id.')
    print('     (There is more detail on naming conventions below.)')
    print('     Also unless the current directory is the same as the PyDial root')
    print('     make sure that [GENERAL]root points to root of the PyDial source tree.\n')
    print('  c) to train a policy as specified in the config file, type')
    print('       > pydial train config')
    print('     if trainsourceiteration=0 this creates a new policy in n batches where')
    print('     n=numtrainbatches, otherwise an existing policy is trained further.\n')
    print('  d) to test a policy as specified in the config file, type')
    print('       > pydial test config\n')
    print('     texthub.py can be invoked to interact with a policy from the keyboard by:')
    print('       > pydial chat config')
    print('     Note that train and test must refer to a specific domain as per [exec_config] domain')
    print('     whereas chat mode can specify multiple domains via the [GENERAL]domains variable.\n')
    print('  e) for convenience, many config parameters can be overridden on the command line, eg')
    print('       > pydial train config --trainerrorrate=20')
    print('       > pydial test config --iteration=4 --trainerrorrate=20 --testerrorrate=50')
    print('     to train a policy at 20% error rate and test the 4th iteration at 50% error rate.')
    print('     A range of test error rates can be specified as a triple (stErr,enErr,stepSize), eg')
    print("       > pydial test config --iteration=4 --trainerrorrate=20 --testerrorrate='(0,50,10)'")
    print('     to test a policy at 0%, 10%, 20%, 30%, 40%, and 50% error rates.\n')
    print('  f) logfiles for each train/test run are stored in logfiledir.')
    print('     The plot command scans one or more logfiles and extract info to plot eg')
    print('       > pydial plot logdir/*train*')
    print('     Setting the option --printtab, also tabulates the performance data.\n')
    print('  All policy information is stored in the policydir specified in the corresponding ')
    print('  config file section with name [policy_domain]. Since pydial overrides some config')
    print('  params, the actual configs used for each run are recorded in configdir.\n')
    print('  Derived file naming convention:')
    print("     Policyname: ID-poltype-domain-TrainErrRate               eg S0-gp-CamRestaurants-20")
    print("     Policy: ID-poltype-domain-TrainErrRate.Iteration         eg S0-gp-CamRestaurants-20.3")
    print("     Policyfile: ID-poltype-domain-TrainErrRate.Iteration.ext eg S0-gp-CamRestaurants-20.3.dct")
    print("     TrainLogfiles: PolicyName.IterationRange.train.log       eg S0-gp-CamRestaurants-20.1-3.train.log")
    print("     EvalLogfiles:  Policy.eval.ErrorRange.eval.log           eg S0-gp-CamRestaurants-20.3.eval.00-50.log\n")
    print("To get further help:")
    print("  pydial             list of available commands")
    print("  pydial help        this overview")
    print("  pydial cmd --help  help for a specific command\n")


def conventionCheck(name):
    global taskID, domain, policytype
    try:
        if name.find('-') < 0:
            raise Exception('no separators')
        (taskID,p,d)=name.split('-')
        if p != policytype:
            raise Exception('policytype != config param')
        if d != domain:
            raise Exception('domain name != config param')
    except Exception as x:
        pass#log.warn("Non-standard config name [%s] (preferred format ID-policytype-domain.cfg)", x.args[0])


def getConfigId(configFileName):
    i = configFileName.rfind('.')
    if i < 0 or configFileName[i+1:] != 'cfg':
        print(("Config file %s does not have required .cfg extension" % configFileName))
        exit(0)

    cfg = path(configFileName)
    if not cfg.isfile():
        print(("Config file %s does not exist" % configFileName))
        exit(0)
    id = configFileName[:i]
    j = id.rfind('/')
    if j >= 0: id = id[j+1:]
    return id


def getOptionalConfigVar(configvarname, default='', section='exec_config'):
    value = default
    if Settings.config.has_option(section, configvarname):
        value = Settings.config.get(section, configvarname)
    return value


def getRequiredDirectory(directoryname, section='exec_config'):
    assert Settings.config.has_option(section, directoryname),\
        "Value {} in section {} is missing.".format(directoryname, section)
    dir = Settings.config.get(section, directoryname)
    if dir[-1] != '/': dir = dir+'/'
    return dir


def getOptionalConfigInt(configvarname, default='0',section='exec_config'):
    value = default
    if Settings.config.has_option(section, configvarname):
        try:
            value = Settings.config.getint(section, configvarname)
        except ValueError:
            value = Settings.config.get(section, configvarname)



    return value


def getOptionalConfigBool(configvarname, default='False', section='exec_config'):
    value = default
    if Settings.config.has_option(section, configvarname):
        value = Settings.config.getboolean(section, configvarname)
    return value


def initialise(configId, config_file, seed, mode, trainerrorrate=None, trainsourceiteration=None,
               numtrainbatches=None, traindialogsperbatch=None, numtestdialogs=None,
               testerrorrate=None, testenderrorrate=None, iteration=None, traindomains=None, testdomains=None,
               dbprefix=None):
    global logger, logfile, traceDialog, isSingleDomain
    global policy_dir, conf_dir, log_dir
    global gnumtrainbatches, gtraindialogsperbatch, gnumbatchtestdialogs, gnumtestdialogs
    global gtrainerrorrate, gtesterrorrate, gtrainsourceiteration
    global taskID, domain, domains, policytype, gtesteverybatch, gpscale
    global gdeleteprevpolicy, isSingleModel
    global policytypes

    if seed is not None:
        seed = int(seed)
    seed = Settings.init(config_file, seed)
    taskID = 'ID'

    isSingleDomain = getOptionalConfigBool("singledomain", isSingleDomain, "GENERAL")
    isSingleModel = getOptionalConfigBool("singlemodel", False, "policycommittee")
    traceDialog    = getOptionalConfigInt("tracedialog", tracedialog, "GENERAL")
    domain         = getOptionalConfigVar("domains", '', "GENERAL")
    if len(domain.split(',')) > 1 and isSingleDomain:
        logger.error('It cannot be singledomain and have several domains defined, Check config file.')
    if isSingleDomain:
        if Settings.config.has_section('policy_' + domain):
            policytype = getOptionalConfigVar('policytype', policytype, 'policy_' + domain)
        else:
            policytype = getOptionalConfigVar('policytype', policytype, 'policy')
        conventionCheck(configId)
    else:
        domains = getOptionalConfigVar("domains", "", "GENERAL").split(',')
        policytypes = {}
        for domain in domains:
            if Settings.config.has_section('policy_' + domain):
                policytypes[domain] = getOptionalConfigVar('policytype', policytype, 'policy_' + domain)
            else:
                policytypes[domain] = getOptionalConfigVar('policytype', policytype, 'policy')

    # if gp, make sure to save required scale before potentially overriding
    if isSingleDomain:
        if policytype == "gp":
            if Settings.config.has_section("gpsarsa_" + domain):
                try:
                    gpscale = Settings.config.getint("gpsarsa_" + domain, "scale")
                except ValueError:
                    gpscale = Settings.config.get("gpsarsa_" + domain, "scale")
            else:
                try:
                    gpscale = Settings.config.getint("gpsarsa", "scale")
                except ValueError:
                    gpscale = Settings.config.get("gpsarsa", "scale")
    else:
        gpscales = {}
        for domain in domains:
            if policytypes[domain] == "gp":
                if Settings.config.has_section("gpsarsa_" + domain):
                    try:
                        gpscales[domain] = Settings.config.getint("gpsarsa_"+ domain, "scale")
                    except ValueError:
                        gpscales[domain] = Settings.config.get("gpsarsa_"+ domain, "scale")

                else:
                    try:
                        gpscales[domain] = Settings.config.getint("gpsarsa", "scale")
                    except ValueError:
                        gpscales[domain] = Settings.config.get("gpsarsa", "scale")

    # if deep-rl model, make sure to set the correct n_in
    if isSingleDomain:
        if Settings.config.has_section("dqnpolicy"):
            if domain == 'CamRestaurants':
                Settings.config.set("dqnpolicy", 'n_in', '268')
            elif domain == 'SFRestaurants':
                Settings.config.set("dqnpolicy", 'n_in', '636')
            elif domain == 'Laptops11':
                Settings.config.set("dqnpolicy", 'n_in', '257')
                # TODO: set rest of environments and multidomain

    # Get required folders and create if necessary
    log_dir    = getRequiredDirectory("logfiledir")
    conf_dir   = getRequiredDirectory("configdir")
    if isSingleDomain:
        if policytype != 'hdc':
            if Settings.config.has_section("policy_"+domain):
                policy_dir = getRequiredDirectory("policydir", "policy_"+domain)
            else:
                policy_dir = getRequiredDirectory("policydir", "policy")
            pd = path(policy_dir)
            if not pd.isdir():
                print("Policy dir %s does not exist, creating it" % policy_dir)
                pd.mkdir()
    else:
        for domain in domains:
            if policytypes[domain] != 'hdc':
                if Settings.config.has_section("policy_" + domain):
                    policy_dir = getRequiredDirectory("policydir", "policy_" + domain)
                else:
                    policy_dir = getRequiredDirectory("policydir", "policy")
                pd = path(policy_dir)
                if not pd.isdir():
                    print("Policy dir %s does not exist, creating it" % policy_dir)
                    pd.mkdir()

    cd = path(conf_dir)
    if not cd.isdir():
        print("Config dir %s does not exist, creating it" % conf_dir)
        cd.mkdir()
    ld = path(log_dir)
    if not ld.isdir():
        print("Log dir %s does not exist, creating it" % log_dir)
        ld.mkdir()


    # optional config settings
    if numtrainbatches:
        gnumtrainbatches = int(numtrainbatches)
    else:
        gnumtrainbatches = getOptionalConfigInt("numtrainbatches", 1)
    if traindialogsperbatch:
        gtraindialogsperbatch = int(traindialogsperbatch)
    else:
        gtraindialogsperbatch = getOptionalConfigInt("traindialogsperbatch", 100)
    if trainerrorrate:
        gtrainerrorrate = int(trainerrorrate)
    else:
        gtrainerrorrate = getOptionalConfigInt("trainerrorrate", 0)
    if testerrorrate:
        gtesterrorrate = int(testerrorrate)
    else:
        gtesterrorrate = getOptionalConfigInt("testerrorrate",0)
    if trainsourceiteration:
        gtrainsourceiteration = int(trainsourceiteration)
    else:
        gtrainsourceiteration = getOptionalConfigInt("trainsourceiteration",0)
    if numtestdialogs:
        gnumtestdialogs = int(numtestdialogs)
    else:
        gnumtestdialogs = getOptionalConfigInt("numtestdialogs", 50)

    gnumbatchtestdialogs = getOptionalConfigInt("numbatchtestdialogs", 20)
    gtesteverybatch = getOptionalConfigBool("testeverybatch",True)
    gdeleteprevpolicy = getOptionalConfigBool("deleteprevpolicy", False)
    if seed is not None and not 'seed' in configId:
        if seed >= 100 and seed < 200:
            seed_string = 'seed{}-'.format(seed - 100)
        else:
            seed_string = 'seed{}-'.format(seed)
    else:
        seed_string = ''
    if mode == "train":
        if gnumtrainbatches>1:
            enditeration = gtrainsourceiteration+gnumtrainbatches
            logfile = "%s-%s%02d.%d-%d.train.log" % (configId, seed_string,gtrainerrorrate,gtrainsourceiteration+1,enditeration)
        else:
            logfile = "%s-%s%02d.%d.train.log" % (configId, seed_string, gtrainerrorrate, gtrainsourceiteration + 1)
    elif mode == "eval":
        if testenderrorrate:
            logfile = "%s-%s%02d.%d.eval.%02d-%02d.log" % (configId, seed_string,gtrainerrorrate,iteration,
                                                         gtesterrorrate,testenderrorrate)
        else:
            if type(iteration) == str:
                logfile = "{}_vs_{}-{}.eval.log".format(configId, iteration, seed_string[:-1])
            else:
                logfile = "%s-%s%02d.%d.eval.%02d.log" % (configId, seed_string, gtrainerrorrate, iteration, gtesterrorrate)
    elif mode == "chat":
        logfile = "%s-%s%02d.%d.chat.log" % (configId, seed_string, gtrainerrorrate, gtrainsourceiteration)
    else:
        print("Unknown initialisation mode:",mode)
        exit(0)
    print('*** logfile: {} ***'.format(logfile))
    Settings.config.set("logging", "file", log_dir + logfile)
    if traindomains:
        Settings.config.set("GENERAL", "traindomains", traindomains)
    if testdomains:
        Settings.config.set("GENERAL", "testdomains", testdomains)
    if dbprefix:
        Settings.config.set("exec_config", "dbprefix", dbprefix)
    if not Ontology.global_ontology:
        ContextLogger.createLoggingHandlers(config=Settings.config)
        logger = ContextLogger.getLogger('')
        Ontology.init_global_ontology()
    else:
        ContextLogger.resetLoggingHandlers()
        ContextLogger.createLoggingHandlers(config=Settings.config)
        logger = ContextLogger.getLogger('')

    Settings.random.seed(int(seed))
    if Settings.root == '':
        Settings.root = os.getcwd()
    logger.info("Seed = %d", seed)
    logger.info("Root = %s", Settings.root)


def setupPolicy(domain, configId, trainerr, source_iteration, target_iteration, seed=None):
    if Settings.config.has_section("policy_" + domain):
        policy_section = "policy_" + domain
    else:
        policy_section = "policy"
    if not str(source_iteration).isdigit():
        inpolicyfile = source_iteration
        outpolicyfile = source_iteration
    elif seed is not None:
        inpolicyfile = "%s-seed%s-%02d.%d" % (configId, seed, trainerr, source_iteration)
        outpolicyfile = "%s-seed%s-%02d.%d" % (configId, seed, trainerr, target_iteration)
    else:
        inpolicyfile = "%s-%02d.%d" % (configId, trainerr, source_iteration)
        outpolicyfile = "%s-%02d.%d" % (configId, trainerr, target_iteration)
    if isSingleDomain:
        Settings.config.set(policy_section, "inpolicyfile", policy_dir + inpolicyfile)
        Settings.config.set(policy_section, "outpolicyfile", policy_dir + outpolicyfile)
    else:
        multi_policy_dir = policy_dir + domain
        pd = path(multi_policy_dir)
        if not pd.isdir():
            print("Policy dir %s does not exist, creating it" % multi_policy_dir)
            pd.mkdir()
        Settings.config.set(policy_section, "inpolicyfile", multi_policy_dir + inpolicyfile)
        Settings.config.set(policy_section, "outpolicyfile", multi_policy_dir + outpolicyfile)
    return (inpolicyfile, outpolicyfile)


def trainBatch(domain, configId, trainerr, ndialogs, source_iteration, seed=None):
    if isSingleDomain:
        (inpolicy, outpolicy) = setupPolicy(domain, configId, trainerr, source_iteration, source_iteration + 1, seed=seed)
        mess = "*** Training Iteration %s->%s: iter=%d, error-rate=%d, num-dialogs=%d ***" % (
            inpolicy, outpolicy, source_iteration, trainerr, ndialogs)
        if tracedialog > 0: print(mess)
        logger.results(mess)
        # make sure that learning is switched on
        if Settings.config.has_section("policy_" + domain):
            Settings.config.set("policy_" + domain, "learning", 'True')
        else:
            Settings.config.set("policy", "learning", 'True')
        # if gp, make sure to reset scale to config setting
        if policytype == "gp":
            if Settings.config.has_section("gpsarsa_" + domain):
                Settings.config.set("gpsarsa_" + domain, "scale", str(gpscale))
            else:
                Settings.config.set("gpsarsa", "scale", str(gpscale))
        # Define the config file for this iteration
        confsavefile = conf_dir + outpolicy + ".train.cfg"
    else:
        mess = "*** Training Iteration: iter=%d, error-rate=%d, num-dialogs=%d ***" % (
            source_iteration, trainerr, ndialogs)
        if tracedialog > 0: print(mess)
        logger.results(mess)
        for dom in domain:
            setupPolicy(dom, configId, trainerr, source_iteration, source_iteration + 1, seed=seed)
            # make sure that learning is switched on
            if Settings.config.has_section("policy_" + dom):
                Settings.config.set("policy_" + dom, "learning", 'True')
            else:
                Settings.config.set("policy", "learning", 'True')
            # if gp, make sure to reset scale to config setting
            if policytype == "gp":
                Settings.config.set("gpsarsa_" + dom, "scale", str(gpscale))
        # Define the config file for this iteration
        multipolicy = "%s-%02d.%d" % (configId, trainerr, source_iteration + 1)
        confsavefile = conf_dir + multipolicy + ".train.cfg"

    # Save the config file for this iteration
    cf = open(confsavefile, 'w')
    Settings.config.write(cf)
    error = float(trainerr) / 100.0
    # run the system
    simulator = Simulate.SimulationSystem(error_rate=error)
    simulator.run_dialogs(ndialogs)
    if gdeleteprevpolicy:
        if isSingleDomain:
            if inpolicy[-1] != '0':
                if Settings.config.has_section("policy_" + domain):
                    for f in os.listdir(Settings.config.get('policy_{}'.format(domain), 'policydir')):
                        if re.search(inpolicy, f):
                            os.remove(os.path.join(Settings.config.get('policy_{}'.format(domain), 'policydir'), f))
                else:
                    for f in os.listdir(Settings.config.get('policy', 'policydir')):
                        if re.search(inpolicy, f):
                            os.remove(os.path.join(Settings.config.get('policy', 'policydir'), f))


def setEvalConfig(domain, configId, evalerr, ndialogs, iteration, seed=None):
    (_, policy) = setupPolicy(domain, configId, gtrainerrorrate, iteration, iteration, seed=seed)
    if isSingleDomain:
        mess = "*** Evaluating %s: error-rate=%d num-dialogs=%d ***" % (policy, evalerr, ndialogs)
    else:
        mess = "*** Evaluating %s: error-rate=%d num-dialogs=%d ***" % (policy.replace('Multidomain', domain),
                                                                        evalerr, ndialogs)
    if tracedialog > 0: print(mess)
    logger.results(mess)
    # make sure that learning is switched off
    if Settings.config.has_section("policy_" + domain):
        Settings.config.set("policy_" + domain, "learning", 'False')
    else:
        Settings.config.set("policy", "learning", 'False')
    # if gp, make sure to reset scale to 1 for evaluation
    if policytype == "gp":
        if Settings.config.has_section("gpsarsa_" + domain):
            Settings.config.set("gpsarsa_" + domain, "scale", "1")
        else:
            Settings.config.set("gpsarsa", "scale", "1")
    # Save a copy of config file
    confsavefile = conf_dir + "%s.eval.%02d.cfg" % (policy, evalerr)
    cf = open(confsavefile, 'w')
    Settings.config.write(cf)


def evalPolicy(domain, configId, evalerr, ndialogs, iteration, seed=None):
    if isSingleDomain:
        setEvalConfig(domain, configId, evalerr, ndialogs, iteration, seed=seed)
    else:
        for dom in domains:
            setEvalConfig(dom, configId, evalerr, ndialogs, iteration, seed=seed)

    error = float(evalerr) / 100.0
    # finally run the system
    simulator = Simulate.SimulationSystem(error_rate=error)
    simulator.run_dialogs(ndialogs)


def getIntParam(line, key):
    m = re.search(" %s *= *(\d+)" % (key), line) #what is this parenthesisi placement here and below???
    if m is None:
        print("Cant find int %s in %s" % (key, line))
        exit(0)
    return int(m.group(1))


def getFloatRange(line,key):
    m = re.search(" %s *= *(\-?\d+\.\d+) *\+- *(\d+\.\d+)" % (key), line)
    if m==None:
        print("Cant find float %s in %s" % (key, line))
        exit(0)
    return (float(m.group(1)),float(m.group(2)))


def getDomainFromLog(l):
    return l.split()[-1].split(',')


def extractEvalData(lines):
    evalData = {}
    training = False
    domain_list = []
    #domain_list = []#['SFRestaurants','SFHotels','Laptops11']
    #for dom in domain_list:
    #    evalData[dom] = {}
    cur_domain = None
    for l in lines:
        if l.find('List of domains:') >= 0:
            # get the list of domains from the log by reading the lines where the ontologies are loaded
            doms = getDomainFromLog(l)
            for domain in doms:
                if domain not in domain_list:
                    domain_list.append(domain)
                    evalData[domain] = {}
        if l.find('*** Training Iteration') >= 0:
            iteration = getIntParam(l, 'iter')+1
            if iteration in list(evalData.keys()):
                print("Duplicate iteration %d" % iteration)
                exit(0)
            for domain in domain_list:
                evalData[domain][iteration] = {}
                evalData[domain][iteration]['erate'] = getIntParam(l, 'error-rate')
                evalData[domain][iteration]['ndialogs'] = getIntParam(l, 'num-dialogs')
            training = True
        elif l.find('*** Evaluating')>=0 and not training:
            l = l.replace('CR', 'CamRestaurants') 
            erate = getIntParam(l, 'error-rate')
            ll = l[l.find('*** Evaluating') + len('*** Evaluating')+1:]
            (ll,x) = ll.split(':')
            for domain in domain_list:
                if domain in ll:
                    evalData[domain][erate] = {}
                    evalData[domain][erate]['policy'] = ll
                    evalData[domain][erate]['ndialogs'] = getIntParam(l, 'num-dialogs')
        elif l.find('Results for domain:') >= 0:
            cur_domain = l.split('Results for domain:')[1].split('--')[0].strip()
        elif l.find('Average reward') >= 0:
            if training:
                evalData[cur_domain][iteration]['reward'] = getFloatRange(l, 'Average reward')
            else:
                evalData[cur_domain][erate]['reward'] = getFloatRange(l, 'Average reward')
        elif l.find('Average success') >= 0:
            if training:
                evalData[cur_domain][iteration]['success'] = getFloatRange(l, 'Average success')
            else:
                evalData[cur_domain][erate]['success'] = getFloatRange(l, 'Average success')

        elif l.find('Average turns') >= 0:
            if training:
                evalData[cur_domain][iteration]['turns'] = getFloatRange(l, 'Average turns')
            else:
                evalData[cur_domain][erate]['turns'] = getFloatRange(l, 'Average turns')
    return evalData


def plotTrain(dname, rtab, stab, block=True, saveplot=False):
    font = {
            'weight': 'bold',
            'size': 20}
    plt.rc('font', **font)

    global gplotnum
    policylist = sorted(rtab.keys())
    ncurves = len(policylist)
    plt.figure(gplotnum)

    gplotnum += 1
    for policy in policylist:
        tab = rtab[policy]
        plt.subplot(211)
        # plt.xlim((800, 4200))
        if len(tab['x']) < 2:
            plt.axhline(y=tab['y'][0], linestyle='--')
        else:
            plt.errorbar(tab['x'],tab['y'], yerr=tab['var'], label=policy)
            # plt.errorbar(tab['x'], tab['y'], label=policy)
        tab = stab[policy]
        plt.subplot(212)
        # plt.xlim((800, 4200))
        if len(tab['x']) < 2:
            plt.axhline(y=tab['y'][0], linestyle='--')
        else:
            plt.errorbar(tab['x'],tab['y'],yerr=tab['var'],label=policy)
            # plt.errorbar(tab['x'], tab['y'], label=policy)
    plt.subplot(211)
    plt.grid()
    plt.legend(loc='lower right', fontsize=14)  # loc='lower right', best,
    plt.title("Performance vs Num Train Dialogues")
    plt.ylabel('Reward')
    plt.subplot(212)
    plt.grid()
    plt.legend(loc='lower right', fontsize=14)
    plt.xlabel('Num Dialogues')
    plt.ylabel('Success')
    if saveplot:
        if not os.path.exists('_plots'):
            os.mkdir('_plots')
        plt.savefig('_plots/' + dname + '.png', bbox_inches='tight')
        print('plot saved as', dname)
    else:
        plt.show(block=block)


def plotTest(dname, rtab, stab, block=True, saveplot=False):
    global gplotnum
    policylist = sorted(rtab.keys())
    ncurves = len(policylist)
    plt.figure(gplotnum)
    gplotnum += 1
    for policy in policylist:
        tab = rtab[policy]
        plt.subplot(211)
        plt.errorbar(tab['x'], tab['y'], yerr=tab['var'], label=policy)
        tab = stab[policy]
        plt.subplot(212)
        plt.errorbar(tab['x'], tab['y'], yerr=tab['var'], label=policy)
    plt.subplot(211)
    plt.grid()
    plt.legend(loc='lower left', fontsize=12-ncurves)
    plt.title(dname+" Performance vs Error Rate")
    plt.ylabel('Reward')
    plt.subplot(212)
    plt.grid()
    plt.legend(loc='lower left', fontsize=12-ncurves)
    plt.xlabel('Error Rate')
    plt.ylabel('Success')
    # plt.show(block=block)
    plt.show()


def printTable(title, tab):
    firstrow = True
    policylist = sorted(tab.keys())
    for policy in policylist:
        xvals = tab[policy]['x']
        if firstrow:
            s = "%-20s" % title
            for i in range(0, len(xvals)): s += "%13d" % xvals[i]
            print(s)
            firstrow = False
        s = "%-18s :" % policy
        for i in range(0,len(xvals)):
            s+= "%6.1f +-%4.1f" % (tab[policy]['y'][i],tab[policy]['var'][i])
        print(s)
    print("")


def tabulateTrain(dataSet):
    #pp.pprint(dataSet)
    rtab = {}
    stab = {}
    ttab = {}
    oldx = []
    for policy in list(dataSet.keys()):
        yvals = []
        xvals = []
        dialogsum = 0
        for iteration in list(dataSet[policy].keys()):
            d = dataSet[policy][iteration]
            (yr, yrv) = d['reward']
            (ys, ysv) = d['success']
            (yt, ytv) = d['turns']
            ndialogs = d['ndialogs']
            dialogsum += ndialogs
            yvals.append((yr, yrv, ys, ysv, yt, ytv))
            xvals.append(dialogsum)
        yvals = [yy for (xx, yy) in sorted(zip(xvals, yvals))]
        x = [xx for (xx, yy) in sorted(zip(xvals, yvals))]
        #if oldx != [] and oldx != x:
        #    print "Policy %s has inconsistent batch sizes" % policy
        oldx = x
        yrew = [yr for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        yrerr = [yrv for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        ysucc = [ys for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        yserr = [ysv for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        yturn = [yt for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        yterr = [ytv for (yr, yrv, ys, ysv, yt, ytv) in yvals]
        if not (policy in list(rtab.keys())): rtab[policy] = {}
        rtab[policy]['y'] = yrew
        rtab[policy]['var'] = yrerr
        rtab[policy]['x'] = x
        if not (policy in list(stab.keys())): stab[policy] = {}
        stab[policy]['y'] = ysucc
        stab[policy]['var'] = yserr
        stab[policy]['x'] = x
        if not (policy in list(ttab.keys())): ttab[policy] = {}
        ttab[policy]['y'] = yturn
        ttab[policy]['var'] = yterr
        ttab[policy]['x'] = x
    # average results over seeds
    averaged_result_list = []
    for result in [rtab, stab, ttab]:
        averaged_result = {}
        n_seeds = {}
        for policy_key in result:
            if "seed" in policy_key:
                seed_n = policy_key[policy_key.find("seed"):]
                seed_n = seed_n.split('-')[0]
                general_policy_key = policy_key.replace(seed_n + '-', '')
            else:
                general_policy_key = policy_key
            if not general_policy_key in averaged_result:
                averaged_result[general_policy_key] = {}
                n_seeds[general_policy_key] = 1
            else:
                n_seeds[general_policy_key] += 1
            for key in result[policy_key]:
                if not key in averaged_result[general_policy_key]:
                    averaged_result[general_policy_key][key] = np.array(result[policy_key][key])
                else:
                    averaged_result[general_policy_key][key] += np.array(result[policy_key][key])
        for policy_key in averaged_result:
            for key in averaged_result[policy_key]:
                averaged_result[policy_key][key] = averaged_result[policy_key][key]/n_seeds[policy_key]
        averaged_result_list.append(averaged_result)

    return averaged_result_list


def tabulateTest(dataSet):
    #pp.pprint(dataSet)
    rtab = {}
    stab = {}
    ttab = {}
    oldx = []
    for policy in list(dataSet.keys()):
        yvals = []
        xvals = []
        for erate in list(dataSet[policy].keys()):
            d = dataSet[policy][erate]
            (yr,yrv) = d['reward']
            (ys,ysv) = d['success']
            (yt,ytv) = d['turns']
            yvals.append((yr, yrv, ys, ysv, yt, ytv))
            xvals.append(erate)
        yvals = [yy for (xx, yy) in sorted(zip(xvals, yvals))]
        x = [xx for (xx,yy) in sorted(zip(xvals, yvals))]
        if oldx != [] and oldx != x:
            print("Policy %s has inconsistent range of error rates" % policy)
            exit(0)
        oldx = x
        yrew = [yr for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        yrerr = [yrv for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        ysucc = [ys for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        yserr = [ysv for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        yturn = [yt for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        yterr = [ytv for (yr,yrv,ys,ysv,yt,ytv) in yvals]
        if not (policy in list(rtab.keys())): rtab[policy]={}
        rtab[policy]['y'] = yrew
        rtab[policy]['var'] = yrerr
        rtab[policy]['x'] = x
        if not (policy in list(stab.keys())): stab[policy] = {}
        stab[policy]['y'] = ysucc
        stab[policy]['var'] = yserr
        stab[policy]['x'] = x
        if not (policy in list(ttab.keys())): ttab[policy] = {}
        ttab[policy]['y'] = yturn
        ttab[policy]['var'] = yterr
        ttab[policy]['x'] = x
    return rtab, stab, ttab


def train_command(configfile, seed=None, trainerrorrate=None,trainsourceiteration=None,
                  numtrainbatches=None, traindialogsperbatch=None, traindomains=None, dbprefix=None):
    """ Train a policy according to the supplied configfile.
        Results are stored in the directories specified in the [exec_config] section of the config file.
        Optional parameters over-ride the corresponding config parameters of the same name.
    """

    try:
        if seed and seed.startswith('('):
            seeds = seed.replace('(', '').replace(')', '').split(',')
            if len(seeds) == 2 and int(seeds[0]) < int(seeds[1]):
                seeds = [str(x) for x in range(int(seeds[0]), 1+int(seeds[1]))]
            for seed in seeds:
                print('*** Seed {} ***'.format(seed))
                train_command(configfile, seed=seed, trainerrorrate=trainerrorrate,
                              trainsourceiteration=trainsourceiteration,
                              numtrainbatches=numtrainbatches, traindialogsperbatch=traindialogsperbatch,
                              traindomains=traindomains, dbprefix=dbprefix)

        else:
            configId = getConfigId(configfile)
            if seed:
                seed = int(seed)
            initialise(configId,configfile,seed,"train",trainerrorrate=trainerrorrate,
                       trainsourceiteration=trainsourceiteration,numtrainbatches=numtrainbatches,
                       traindialogsperbatch=traindialogsperbatch,traindomains=traindomains,dbprefix=dbprefix)
            for i in range(gtrainsourceiteration, gtrainsourceiteration+gnumtrainbatches):
                Settings.global_numiter = i + 1
                if isSingleDomain:
                    logger.results('List of domains: {}'.format(domain))
                    trainBatch(domain, configId, gtrainerrorrate, gtraindialogsperbatch, i, seed=seed)
                else:
                    logger.results('List of domains: {}'.format(','.join(domains)))
                    trainBatch(domains, configId, gtrainerrorrate, gtraindialogsperbatch, i, seed=seed)
                if gtesteverybatch and gnumbatchtestdialogs>0 and i+1 < gtrainsourceiteration+gnumtrainbatches:
                    if isSingleDomain:
                        evalPolicy(domain, configId, gtrainerrorrate, gnumbatchtestdialogs, i + 1, seed=seed)
                    else:
                        evalPolicy(domains, configId, gtrainerrorrate, gnumbatchtestdialogs, i + 1, seed=seed)
            if gnumbatchtestdialogs > 0:
                if isSingleDomain:
                    logger.results('List of domains: {}'.format(domain))
                    evalPolicy(domain, configId, gtrainerrorrate, gnumbatchtestdialogs, i + 1, seed=seed)
                else:
                    logger.results('List of domains: {}'.format(','.join(domains)))
                    evalPolicy(domains, configId, gtrainerrorrate, gnumbatchtestdialogs, i + 1, seed=seed)

            logger.results("*** Training complete. log: %s - final policy is %s-%02d-%02d" % (logfile, configId, gtrainerrorrate, i+1))
    except clog.ExceptionRaisedByLogger:
        print("Command Aborted - see Log file for error:", logfile)
        exit(0)
    except KeyboardInterrupt:
        print("\nCommand Aborted from Keyboard")


def test_command(configfile, iteration, seed=None, testerrorrate=None, trainerrorrate=None,
                 numtestdialogs=None, testdomains=None, dbprefix=None, inputpolicy=None):
    """ Test a specific policy iteration trained at a specific error rate according to the supplied configfile.
        Results are embedded in the logfile specified in the config file.
        Optional parameters over-ride the corresponding config parameters of the same name.
        The testerrorrate can also be specified as a triple (e1,e2,stepsize).  This will repeat the test
        over a range of error rates from e1 to e2.  NB the tuple must be quoted on the command line.
    """
    try:
        if seed and seed.startswith('('):
            seeds = seed.replace('(','').replace(')','').split(',')
            if len(seeds) == 2 and int(seeds[0]) < int(seeds[1]):
                seeds = [str(x) for x in range(int(seeds[0]), 1 + int(seeds[1]))]
            for seed in seeds:
                print('*** Seed {} ***'.format(seed))
                test_command(configfile, iteration, seed=seed, testerrorrate=testerrorrate,
                              trainerrorrate=trainerrorrate, numtestdialogs=numtestdialogs, testdomains=testdomains,
                              dbprefix=dbprefix, inputpolicy=inputpolicy)

        else:
            errStepping = False
            enErr = None
            if testerrorrate and testerrorrate[0] == '(':
                if testerrorrate[-1] != ')':
                    print("Missing closing parenthesis in error range %s" % testerrorrate)
                    exit(0)
                errRange = eval(testerrorrate)
                if len(errRange) != 3:
                    print("Ill-formed error range %s" % testerrorrate)
                    exit(0)
                (stErr, enErr, stepErr) = errRange
                if enErr < stErr or stepErr <= 0:
                    print("Ill-formed test error range [%d,%d,%d]" % testerrorrate)
                    exit(0)
                errStepping = True
                testerrorrate = stErr
            if iteration.isdigit():
                i = int(iteration)
            else:
                i = iteration
            #if i < 1:
            #    print 'iteration must be > 0'
            #    exit(0)
            configId = getConfigId(configfile)
            orig_seed = '0'
            if seed:
                orig_seed = seed
                seed = int(seed) + 100  # To have a different seed during training and testing
            initialise(configId, configfile, seed, "eval", iteration=i, testerrorrate=testerrorrate,
                       testenderrorrate=enErr, trainerrorrate=trainerrorrate,
                       numtestdialogs=numtestdialogs,testdomains=testdomains, dbprefix=dbprefix)
            if type(i) == str:
                policyname = i
                if not 'seed' in policyname:
                    ps= policyname.split('-')
                    policyname = '-'.join(ps[:-1] + ['seed{}'.format(orig_seed)] + [ps[-1]])
            else:
                policyname = "%s-%02d.%d" % (configId, gtrainerrorrate, i)
Carel van Niekerk's avatar
Carel van Niekerk committed
934
935
936
                if not 'seed' in policyname:
                    ps= policyname.split('-')
                    policyname = '-'.join(ps[:-1] + ['seed{}'.format(orig_seed)] + [ps[-1]])
Carel van Niekerk's avatar
Init  
Carel van Niekerk committed
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
            poldirpath = path(policy_dir)
            if poldirpath.isdir():
                policyfiles = poldirpath.files()
                policynamelist = [p.namebase for p in policyfiles]
                if isSingleDomain:
                    logger.results('List of domains: {}'.format(domain))
                    if policyname in policynamelist:
                        if errStepping:
                            while stErr <= enErr:
                                evalPolicy(domain, configId, stErr, gnumtestdialogs, i, seed=seed)
                                stErr += stepErr
                        else:
                            evalPolicy(domain, configId, gtesterrorrate, gnumtestdialogs, i, seed=seed)
                        logger.results("*** Testing complete. logfile: %s - policy %s evaluated" % (logfile, policyname))
                    else:
                        print("Cannot find policy iteration %s in %s" % (policyname, policy_dir))
                else:
                    allPolicyFiles = True
                    logger.results('List of domains: {}'.format(','.join(domains)))
                    for dom in domains:
                        multi_policyname = dom+policyname
                        if isSingleModel:
                            multi_policyname = 'singlemodel'+policyname
                        if not multi_policyname in policynamelist:
                            print("Cannot find policy iteration %s in %s" % (multi_policyname, policy_dir))
                            allPolicyFiles = False
                    if allPolicyFiles:
                        if errStepping:
                            while stErr <= enErr:
                                evalPolicy(domain, configId, stErr, gnumtestdialogs, i)
                                stErr += stepErr
                        else:
                            evalPolicy(domain, configId, gtesterrorrate, gnumtestdialogs, i)
                        logger.results("*** Testing complete. logfile: %s - policy %s evaluated" % (logfile, policyname))
            else:
                print("Policy folder %s does not exist" % policy_dir)
    except clog.ExceptionRaisedByLogger:
        print("Command Aborted - see Log file for error:", logfile)
        exit(0)
    except KeyboardInterrupt:
        print("\nCommand Aborted from Keyboard")


def plotTrainLogs(logfilelist, printtab, noplot, saveplot, datasetname, block=True):
    """
        Extract data from given log files and display.
    """
    try:
        resultset = {}
        ncurves = 0
        domains = None

        if len(logfilelist) < 1:
            print("No log files specified")
            exit(0)
        for fname in logfilelist:
            fn = open(fname, "r")
            if fn:
                logName = path(fname).namebase
                if 'epsil0.' in logName:
                    logName = logName.replace('epsil0.', 'epsil0')
                i = logName.find('.')
                if i < 0:
                    print("No index info in train log file name")
                    exit(0)
                curveName = logName[:i]
                if datasetname == '':
                    i = curveName.find('-')
                    if i >= 0:
                        datasetname=curveName[:i]
                lines = fn.read().splitlines()
                results = extractEvalData(lines)
                npoints = len(results[list(results.keys())[0]])
                if npoints == 0:
                    print("Log file %s has no plotable data" % fname)
                else:
                    if len(resultset) == 0:
                        # the list of domains needs to be read from the logfile
                        domains = list(results.keys())
                        for domain in domains:
                            resultset[domain] = {}
                    else:
                        domains_1 = list(resultset.keys()).sort()
                        domains_2 = list(results.keys()).sort()
                        assert domains_1 == domains_2, 'The logfiles have different domains'
                    ncurves += 1
                    for domain in domains:
                        if curveName in list(resultset[domain].keys()):
                            curve = resultset[domain][curveName]
                            for iteration in list(results[domain].keys()):
                                curve[iteration] = results[domain][iteration]
                        else:
                            resultset[domain][curveName] = results[domain]
            else:
                print(("Cannot find logfile %s" % fname))
        if ncurves > 0:
            average_results = [[], [], []]
            for domain in domains:
                (rtab, stab, ttab) = tabulateTrain(resultset[domain])
                average_results[0].append(rtab)
                average_results[1].append(stab)
                average_results[2].append(ttab)
                if printtab:
                    print("\n%s-%s: Performance vs Num Dialogs\n" % (datasetname, domain))
                    printTable('Reward', rtab)
                    printTable('Success', stab)
                    printTable('Turns', ttab)
                    '''for key in rtab:
                        print key
                        if len(stab[key]['y']) == 1:
                            print '1K', stab[key]['y'][0], rtab[key]['y'][0]
                        else:
                            print '1K', stab[key]['y'][5], rtab[key]['y'][5]
                            print '4K', stab[key]['y'][-1], rtab[key]['y'][-1]'''
                    #print rtab
                    #print stab

                if not noplot:
                    plotTrain(datasetname+'-'+domain,rtab,stab,block=block,saveplot=saveplot)
            # Print average for all domains
            if len(domains) > 1:
                av_rtab, av_stab, av_ttab = getAverageResults(average_results)
                plotTrain(datasetname+'-mean', av_rtab, av_stab, block=block,saveplot=saveplot)
        else:
            print("No plotable train data found")
    except clog.ExceptionRaisedByLogger:
        print("Command Aborted - see Log file for error:")


def getAverageResults(average_result_list):
    averaged_results = []
    for tab_list in average_result_list:
        n_domains = len(tab_list)
        tab_av_results = {}
        for domain_rtab in tab_list:
            for policy_key in domain_rtab:
                if not policy_key in tab_av_results:
                    tab_av_results[policy_key] = {}
                for key in domain_rtab[policy_key]:
                    if not key in tab_av_results[policy_key]:
                        if key == 'var':
                            tab_av_results[policy_key][key] = np.sqrt(np.array(domain_rtab[policy_key][key]))
                        else:
                            tab_av_results[policy_key][key] = np.array(domain_rtab[policy_key][key])
                    else:
                        if key == 'var':
                            tab_av_results[policy_key][key] += np.sqrt(np.array(domain_rtab[policy_key][key]))
                        else:
                            tab_av_results[policy_key][key] += np.array(domain_rtab[policy_key][key])
        #normalise
        for policy_key in tab_av_results:
            for key in tab_av_results[policy_key]:
                tab_av_results[policy_key][key] /= n_domains
                if key == 'var':
                    tab_av_results[policy_key][key] = np.square(tab_av_results[policy_key][key])
        averaged_results.append(tab_av_results)
    return averaged_results


def plotTestLogs(logfilelist,printtab,noplot,datasetname,block=True):
    """
        Extract data from given eval log files and display performance
        as a function of error rate
    """
    try:
        resultset = {}
        domains = None
        for fname in logfilelist:
            fn = open(fname,"r")
            if fn:
                lines = fn.read().splitlines()
                results = extractEvalData(lines)
                if results:
                    domains = list(results.keys())
                    for domain in domains:
                        if not domain in list(resultset.keys()): 
                            resultset[domain] = {}
                        akey = list(results[domain].keys())[0]
                        aresult = results[domain][akey]
                        if 'policy' in list(aresult.keys()):
                            policyname = results[domain][akey]['policy']
                            if datasetname == '':
                                i = policyname.find('-')
                                if i >= 0:
                                    datasetname=policyname[:i]
                            if not policyname in resultset[domain]: resultset[domain][policyname]={}
                            for erate in list(results[domain].keys()):
                                resultset[domain][policyname][erate] = results[domain][erate]
                        else:
                            print('Format error in log file',fname)
                            exit(0)
            else:
                print("Cannot find logfile %s" % fname)
                exit(0)
        for domain in domains:
            if len(list(resultset[domain].keys()))>0:
                (rtab,stab,ttab) = tabulateTest(resultset[domain])
                if printtab:
                    print("\n%s-%s: Performance vs Error Rate\n" % (datasetname, domain))
                    printTable('Reward', rtab)
                    printTable('Success', stab)
                    printTable('Turns', ttab)
                if not noplot:
                    plotTest('%s-%s'%(datasetname, domain),rtab,stab,block=block)
            else:
                print("No data found")
    except clog.ExceptionRaisedByLogger:
        print("Command Aborted - see Log file for error:")


@command.fetch_all('args')
def plot_command(args="", printtab=False, noplot=False, saveplot=False, datasetname=''):
    """ Call plot with a list of log files and it will print train and test curves.
        For train log files it plots performance vs num dialogs.
        For test log files it plots performance vs error rate.
        Set the printtab option to print a table of results.
        A name can be given to plot via dataset name.
    """
    trainlogs = []
    testlogs = []
    for fname in args:
        if fname.find('train') >= 0:
            trainlogs.append(fname)
        elif fname.find('eval') >= 0:
            testlogs.append(fname)
    block = True
    # if testlogs: block = False
    if noplot: printtab = True    # otherwise no point!
    if trainlogs:
        plotTrainLogs(trainlogs, printtab, noplot, saveplot, datasetname, block)
    if testlogs:
        plotTestLogs(testlogs, printtab, noplot, saveplot, datasetname)


def chat_command(configfile, seed=None, trainerrorrate=None, trainsourceiteration=None):
        """ Run the texthub according to the supplied configfile.
        """
        try:
            configId = getConfigId(configfile)
            initialise(configId, configfile, seed, "chat", trainerrorrate=trainerrorrate,
                       trainsourceiteration=trainsourceiteration)
            for dom in domains:
                if policytypes[dom] != 'hdc':
                    setupPolicy(dom, configId, gtrainerrorrate,
                                gtrainsourceiteration, gtrainsourceiteration)
                    # make sure that learning is switched off
                    if Settings.config.has_section("policy_" + dom):
                        Settings.config.set("policy_" + dom, "learning", 'False')
                    else:
                        Settings.config.set("policy", "learning", 'False')
                    # if gp, make sure to reset scale to 1 for evaluation
                    if policytypes[dom] == "gp":
                        if Settings.config.has_section("gpsarsa_" + dom):
                            Settings.config.set("gpsarsa_" + dom, "scale", "1")
                        else:
                            Settings.config.set("gpsarsa", "scale", "1")
            mess = "*** Chatting with policies %s: ***" % str(domains)
            if tracedialog > 0: print(mess)
            logger.dial(mess)

            # create text hub and run it
            hub = Texthub.ConsoleHub()
            hub.run()
            logger.dial("*** Chat complete")
            # Save a copy of config file
            confsavefile = conf_dir + configId + ".chat.cfg"
            cf = open(confsavefile, 'w')
            Settings.config.write(cf)
        except clog.ExceptionRaisedByLogger:
            print("Command Aborted - see Log file for error:", logfile)
            exit(0)
        except KeyboardInterrupt:
            print("\nCommand Aborted from Keyboard")


# class addInfo(object):
#     def __init__(self, gbatchnum):
#         self.batch_number = gbatchnum

run()