Survivalplot.vue 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<template>
    <chart v-on:resize="resize">
        <control-panel class="fjs-control-panel" name="Survivalplot Panel">
            <data-box class="fjs-data-box"
                      header="Duration [numerical]"
                      :data-types="['numerical']"
                      v-on:update="updateDurationVariable">
            </data-box>
            <data-box class="fjs-data-box"
                      header="Groups (optional) [categorical]"
                      :data-types="['categorical']"
                      v-on:update="updateGroupVariable">
            </data-box>
            <data-box class="fjs-data-box"
                      header="Observed (optional) [categorical]"
                      :data-types="['categorical']"
                      v-on:update="updateObservedVariable">
            </data-box>
Sascha Herzinger's avatar
Sascha Herzinger committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
            <hr class="fjs-seperator"/>
            <fieldset class="fjs-fieldset">
                <legend>Estimator</legend>
                <div v-for="method in estimators">
                    <label>
                        <input type="radio" :value="method" v-model="estimator">
                        {{ method }}
                    </label>
                </div>
            </fieldset>
            <div>
                <label>
                    Ignore Subsets
                    <input type="checkbox" v-model="ignoreSubsets"/>
                </label>

            </div>
36
37
38
        </control-panel>
        <svg :height="height" :width="width">
            <g :transform="`translate(${margin.left}, ${margin.top})`">
39
40
41
42
                <g class="fjs-axis" ref="yAxis2" :transform="`translate(${padded.width}, 0)`"></g>
                <g class="fjs-axis" ref="xAxis2"></g>
                <g class="fjs-axis" ref="xAxis1" :transform="`translate(0, ${padded.height})`"></g>
                <g class="fjs-axis" ref="yAxis1"></g>
Sascha Herzinger's avatar
Sascha Herzinger committed
43
                <crosshair :width="padded.width" :height="padded.height"/>
44
45
46
47
48
49
50
51
52
53
                <g class="fjs-paths">
                    <path class="fjs-estimate-path"
                          :style="{stroke: path.color}"
                          :d="path.d" v-for="path in paths">
                    </path>
                    <path class="fjs-ci-path"
                          :style="{fill: path.color}"
                          :d="path.d" v-for="path in ciPaths">
                    </path>
                </g>
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            </g>
        </svg>
    </chart>
</template>

<script>
  import ControlPanel from '../components/ControlPanel.vue'
  import Chart from '../components/Chart.vue'
  import DataBox from '../components/DataBox.vue'
  import RunAnalysis from '../mixins/run-analysis'
  import store from '../../store/store'
  import deepFreeze from 'deep-freeze-strict'
  import * as d3 from 'd3'
  import Crosshair from '../components/Crosshair.vue'
  export default {
    name: 'survivalplot',
70
    components: {Crosshair, DataBox, Chart, ControlPanel},
71
72
73
74
75
76
77
78
79
    mixins: [RunAnalysis],
    data () {
      return {
        height: 0,
        width: 0,
        durationVariables: [],
        groupVariables: [],
        observedVariables: [],
        estimator: 'KaplanMeier',
Sascha Herzinger's avatar
Sascha Herzinger committed
80
81
        estimators: ['KaplanMeier', 'NelsonAalen'],
        ignoreSubsets: false,
82
83
84
85
86
87
        groupColors: d3.schemeCategory10,
        results: {
          subsets: [],
          categories: [],
          stats: {}
        }
88
89
90
91
92
93
94
95
96
      }
    },
    computed: {
      args () {
        return {
          durations: this.durationVariables,
          categories: this.groupVariables,
          event_observed: this.observedVariables,
          estimator: this.estimator,
97
          id_filter: store.getters.filter('ids').value,
Sascha Herzinger's avatar
Sascha Herzinger committed
98
          subsets: this.ignoreSubsets ? [] : store.getters.subsets
99
100
101
        }
      },
      validArgs () {
102
        return this.durationVariables.length === 1
103
104
105
106
107
108
109
110
111
112
113
114
      },
      margin () {
        const left = this.width / 15
        const top = this.height / 15
        const right = this.width / 15
        const bottom = this.height / 15
        return {left, top, right, bottom}
      },
      padded () {
        const width = this.width - this.margin.left - this.margin.right
        const height = this.height - this.margin.top - this.margin.bottom
        return {width, height}
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
      },
      dataRanges () {
        let timelineGlobalMin = Number.MAX_SAFE_INTEGER
        let timelineGlobalMax = Number.MIN_SAFE_INTEGER
        let estimateGlobalMin = Number.MAX_SAFE_INTEGER
        let estimateGlobalMax = Number.MIN_SAFE_INTEGER
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            const [localTimelineMin, localTimelineMax] = d3.extent(this.results.stats[category][subset].timeline)
            timelineGlobalMin = localTimelineMin < timelineGlobalMin ? localTimelineMin : timelineGlobalMin
            timelineGlobalMax = localTimelineMax > timelineGlobalMax ? localTimelineMax : timelineGlobalMax
            const [localEstimateMin, localEstimateMax] = d3.extent(this.results.stats[category][subset].estimate)
            estimateGlobalMin = localEstimateMin < estimateGlobalMin ? localEstimateMin : estimateGlobalMin
            estimateGlobalMax = localEstimateMax > estimateGlobalMax ? localEstimateMax : estimateGlobalMax
          })
        })
        return { timelineGlobalMin, timelineGlobalMax, estimateGlobalMin, estimateGlobalMax }
      },
      scales () {
        const x = d3.scaleLinear()
          .domain([this.dataRanges.timelineGlobalMin, this.dataRanges.timelineGlobalMax])
          .range([0, this.padded.width])
        const y = d3.scaleLinear()
          .domain([this.dataRanges.estimateGlobalMin, this.dataRanges.estimateGlobalMax])
          .range([this.padded.height, 0])
        return { x, y }
      },
      axis () {
Sascha Herzinger's avatar
Sascha Herzinger committed
143
144
        const x1 = d3.axisBottom(this.scales.x)
        const y1 = d3.axisLeft(this.scales.y)
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
        const x2 = d3.axisBottom(this.scales.x)
          .tickSizeInner(this.padded.height)
          .tickFormat('')
        const y2 = d3.axisLeft(this.scales.y)
          .tickSizeInner(this.padded.width)
          .tickFormat('')
        return { x1, x2, y1, y2 }
      },
      groups () {
        const groups = {}
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            groups[this.getGroupName(category, subset)] = {}
          })
        })
        Object.keys(groups).forEach((key, i) => {
          groups[key].color = this.groupColors[i % this.groupColors.length]
        })
        return groups
      },
      paths () {
        const paths = []
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            let path = ''
            this.results.stats[category][subset].estimate.forEach((d, i, arr) => {
              const stats = this.results.stats[category][subset]
              const x = this.scales.x(stats.timeline[i])
              if (i === 0) {
                path += `M ${x} ${this.scales.y(d)}`
              } else {
                path += `L ${x} ${this.scales.y(arr[i - 1])}`
                path += `L ${x} ${this.scales.y(d)}`
              }
            })
            paths.push({
              d: path,
              color: this.groups[this.getGroupName(category, subset)].color
            })
          })
        })
        return paths
      },
      ciPaths () {
        const paths = []
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
Sascha Herzinger's avatar
Sascha Herzinger committed
192
            const stats = this.results.stats[category][subset]
193
194
            let path = ''
            let backpath = ' Z '
Sascha Herzinger's avatar
Sascha Herzinger committed
195
            this.results.stats[category][subset].ci_upper.forEach((_, i) => {
196
197
198
199
              const x = this.scales.x(stats.timeline[i])
              if (i === 0) {
                return true
              } else if (i === 1) {
Sascha Herzinger's avatar
Sascha Herzinger committed
200
201
                path += `M ${x} ${this.scales.y(stats.ci_upper[i])} `
                backpath = ` L ${x} ${this.scales.y(stats.ci_lower[i])}` + backpath
202
              } else {
Sascha Herzinger's avatar
Sascha Herzinger committed
203
204
                path += `L ${x} ${this.scales.y(stats.ci_upper[i - 1])} `
                path += `L ${x} ${this.scales.y(stats.ci_upper[i])} `
205
206
207
208
209
210
211
212
213
214
215
                backpath = ` L ${x} ${this.scales.y(stats.ci_lower[i - 1])}` + backpath
                backpath = ` L ${x} ${this.scales.y(stats.ci_lower[i])}` + backpath
              }
            })
            paths.push({
              d: path + backpath,
              color: this.groups[this.getGroupName(category, subset)].color
            })
          })
        })
        return paths
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
      }
    },
    methods: {
      updateDurationVariable (ids) {
        this.durationVariables = ids
      },
      updateGroupVariable (ids) {
        this.groupVariables = ids
      },
      updateObservedVariable (ids) {
        this.observedVariables = ids
      },
      resize (width, height) {
        this.width = width
        this.height = height
      },
      runAnalysisWrapper (args) {
        this.runAnalysis('survival-analysis', args)
          .then(response => {
            const results = JSON.parse(response)
            deepFreeze(results) // massively improve performance by telling Vue that the objects properties won't change
            this.results = results
          })
          .catch(error => console.error(error))
240
241
242
      },
      getGroupName (category, subset) {
        return `${category} [s${subset + 1}]`
243
244
245
246
247
248
249
250
251
      }
    },
    watch: {
      'args': {
        handler: function (newArgs) {
          if (this.validArgs) {
            this.runAnalysisWrapper(newArgs)
          }
        }
252
253
254
255
256
257
258
259
260
261
      },
      'axis': {
        handler: function (newAxis) {
          this.$nextTick(() => {
            d3.select(this.$refs.xAxis1).call(newAxis.x1)
            d3.select(this.$refs.yAxis1).call(newAxis.y1)
            d3.select(this.$refs.yAxis2).call(newAxis.y2)
            d3.select(this.$refs.xAxis2).call(newAxis.x2)
          })
        }
262
263
264
265
266
267
268
269
      }
    }
  }
</script>

<style lang="sass" scoped>
    @import '~assets/base.sass'

270
271
272
273
274
275
276
277
278
    svg
        .fjs-paths
            path
                shape-rendering: crispEdges
            .fjs-estimate-path
                stroke-width: 2px
                fill: none
            .fjs-ci-path
                opacity: 0.4
279
280
281
282
</style>

<!--CSS for dynamically created components-->
<style lang="sass">
283
    @import '~assets/d3.sass'
284

285
286
287
288
289
290
291
292
293
294
    .fjs-axis
        shape-rendering: crispEdges
        .tick
            shape-rendering: crispEdges
            line
                stroke: #E2E2E2
            text
                font-size: 0.75em
        path
            stroke: none
295
</style>