Completed
Pull Request — master (#184)
by Martin
45s
created

LogFileWriter.log_writer_decorator()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
1
from .contextlibbackport import ContextDecorator
2
from .internal import ContextMethodDecorator
3
from ..optional import tensorflow
4
5
6
class LogFileWriter(ContextDecorator, ContextMethodDecorator):
7
    """
8
    Intercept ``logdir`` each time a new ``FileWriter`` instance is created.
9
10
    :param experiment: Tensorflow experiment.
11
12
    The state of the experiment must be running when entering the annotated
13
    function / the context manager.
14
15
    When creating ``FileWriters`` in Tensorflow, you might want to
16
    store the path to the produced log files in the sacred database.
17
18
    In the scope of ``LogFileWriter``, the corresponding log directory path
19
    is appended to a list in experiment.info["tensorflow"]["logdirs"].
20
21
    ``LogFileWriter`` can be used both as a context manager or as
22
     an annotation (decorator) on a function.
23
24
25
    Example usage as decorator::
26
27
        ex = Experiment("my experiment")
28
        @LogFileWriter(ex)
29
        def run_experiment(_run):
30
            with tf.Session() as s:
31
                swr = tf.summary.FileWriter("/tmp/1", s.graph)
32
                # _run.info["tensorflow"]["logdirs"] == ["/tmp/1"]
33
                swr2 tf.summary.FileWriter("./test", s.graph)
34
                #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
35
36
37
    Example usage as context manager::
38
39
        ex = Experiment("my experiment")
40
        def run_experiment(_run):
41
            with tf.Session() as s:
42
                with LogFileWriter(ex):
43
                    swr = tf.summary.FileWriter("/tmp/1", s.graph)
44
                    # _run.info["tensorflow"]["logdirs"] == ["/tmp/1"]
45
                    swr3 = tf.summary.FileWriter("./test", s.graph)
46
                    #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
47
                # This is called outside the scope and won't be captured
48
                swr3 = tf.summary.FileWriter("./nothing", s.graph)
49
                # Nothing has changed:
50
                #_run.info["tensorflow"]["logdirs"] == ["/tmp/1", "./test"]
51
52
    """
53
54
    def __init__(self, experiment):
55
        self.experiment = experiment
56
57
        def log_writer_decorator(instance, original_method, original_args,
58
                                 original_kwargs):
59
            result = original_method(instance, *original_args,
60
                                     **original_kwargs)
61
            if "logdir" in original_kwargs:
62
                logdir = original_kwargs["logdir"]
63
            else:
64
                logdir = original_args[0]
65
            self.experiment.info.setdefault("tensorflow", {}).setdefault(
66
                "logdirs", []).append(logdir)
67
            return result
68
69
        ContextMethodDecorator.__init__(self,
70
                                        tensorflow.summary.FileWriter,
71
                                        "__init__",
72
                                        log_writer_decorator)
73