-
Notifications
You must be signed in to change notification settings - Fork 14
/
classifier.js
379 lines (339 loc) · 12.8 KB
/
classifier.js
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
import apiClient from 'panoptes-client/lib/api-client'
import R from 'ramda'
import { setState } from '../actions/index'
import { Actions } from 'react-native-router-flux'
import { Alert, Platform, Image} from 'react-native'
import { getAuthUser } from '../actions/auth'
import { saveTutorialAsComplete, setUserProjectData } from '../actions/user';
import * as ActionConstants from '../constants/actions'
import getSubjectLocations from '../utils/get-subject-location'
import {
constructDrawingAnnotations
} from '../utils/annotationUtils'
import { clearShapes } from './drawing'
export function addSubjectsForWorklow(workflowId) {
return dispatch => {
return apiClient.type('subjects').get({workflow_id: workflowId, sort: 'queued'}).then((subjects) => {
subjects.forEach((subject) => subject.displays = getSubjectLocations(subject))
dispatch({
type: ActionConstants.APPEND_SUBJECTS_TO_WORKFLOW,
workflowId,
subjects
})
})
}
}
export function startNewClassification(workflow, project) {
return dispatch => {
dispatch(clearSubjectsFromWorkflow(workflow.id))
Promise.all([
dispatch(requestClassifierData),
dispatch(setState('loadingText', 'Loading Workflow...')),
dispatch(addSubjectsForWorklow(workflow.id)),
dispatch(setupProjectPreferences(workflow.id, project)),
dispatch(fetchFieldGuide(workflow.id, project.id)),
dispatch(fetchTutorials(workflow.id)).then(() => dispatch(setNeedsTutorial(workflow.id, project.id))),
dispatch(setSubjectStartTimeForWorkflow(workflow.id))
])
.then(() => {
dispatch(setSubjectForWorkflow(workflow.id))
dispatch(classifierDataSuccess)
})
.catch((error) => {
Alert.alert('Error', `Sorry, the following error occured when loading this workflow. ${error}`,
[{text: 'Go Back', onPress: () => { Actions.pop()}}]
)
})
}
}
export function saveClassification(workflow, subject, displayDimensions) {
return (dispatch, getState) => {
const classifier = getState().classifier
const subjectStartTime = classifier.subjectStartTime[workflow.id]
const subjectCompletionTime = (new Date).toISOString()
const annotations = R.map(a => ({task: a[0], value: a[1]}), R.toPairs(classifier.annotations[workflow.id]))
dispatch(setSubjectSeenThisSession(workflow.id, subject.id))
dispatch(setSubjectStartTimeForWorkflow(workflow.id))
dispatch(initializeAnnotation(workflow.id))
dispatch(setSubjectForWorkflow(workflow.id))
// If we are in preview mode, we skip reporting classifications
if (classifier.inPreviewMode) {
return
}
// Report classification
let subjectDimensions = []
const sizePromises = subject.displays.map(({src}) => {
return new Promise((resolve, reject) => {
Image.getSize(src, (naturalWidth, naturalHeight) => {
const aspectRatio = Math.min(displayDimensions.height/naturalHeight, displayDimensions.width/naturalWidth)
const subjectDimensions = {
naturalWidth,
naturalHeight,
clientWidth: displayDimensions.width * aspectRatio,
clientHeight: displayDimensions.height * aspectRatio
}
resolve(subjectDimensions)
}, reject)
})
})
Promise.all(sizePromises).then((imageDimensions) => {
subjectDimensions = imageDimensions
}).finally(() => {
apiClient.type('classifications').create({
completed: true,
annotations,
metadata: {
workflow_version: workflow.version,
started_at: subjectStartTime,
finished_at: subjectCompletionTime,
user_agent: `${Platform.OS} Mobile App`,
user_language: 'en',
utc_offset: ((new Date).getTimezoneOffset() * 60).toString(),
subject_dimensions: subjectDimensions,
viewport: { width: getState().app.device.width, height: getState().app.device.height },
session: getState().main.session.id
},
links: {
project: workflow.links.project,
workflow: workflow.id,
subjects: [subject.id]
}
}).save()
// Add more subjects if we are getting close to running out
const subjectList = classifier.subjectLists[workflow.id] || []
const subjectsSeenThisSession = classifier.seenThisSession[workflow.id] || []
const usableSubjects = subjectList.filter(subject => !subjectsSeenThisSession.includes(subject.id))
if (usableSubjects.length < 5) {
dispatch(addSubjectsForWorklow(workflow.id))
}
})
}
}
export function submitDrawingClassification(shapes, workflow, subject, {clientHeight, clientWidth}) {
return (dispatch, getState) => {
const { classifier } = getState()
const subjectStartTime = classifier.subjectStartTime[workflow.id]
const subjectCompletionTime = (new Date).toISOString()
const firstTask = workflow.first_task
const tools = workflow.tasks[firstTask].tools
const annotations = constructDrawingAnnotations(shapes, tools, firstTask)
apiClient.type('classifications').create({
completed: true,
annotations,
metadata: {
workflow_version: workflow.version,
started_at: subjectStartTime,
finished_at: subjectCompletionTime,
user_agent: `${Platform.OS} Mobile App`,
user_language: 'en',
utc_offset: ((new Date).getTimezoneOffset() * 60).toString(),
subject_dimensions: { ...classifier.subjectDimensions[subject.id], clientHeight, clientWidth },
viewport: { width: getState().app.device.width, height: getState().app.device.height },
session: getState().main.session.id
},
links: {
project: workflow.links.project,
workflow: workflow.id,
subjects: [subject.id]
}
}).save()
// Add more subjects if we are getting close to running out
const subjectList = classifier.subjectLists[workflow.id] || []
const subjectsSeenThisSession = classifier.seenThisSession[workflow.id] || []
const usableSubjects = subjectList.filter(subject => !subjectsSeenThisSession.includes(subject.id))
if (usableSubjects.length < 3) {
dispatch(addSubjectsForWorklow(workflow.id))
}
// Mark the subject as completed and move on to the next
dispatch(setSubjectSeenThisSession(workflow.id, subject.id))
dispatch(setSubjectForWorkflow(workflow.id))
dispatch(clearShapes())
}
}
const setSubjectForWorkflow = (workflowId) => ({
type: ActionConstants.SET_SUBJECT_FOR_WORKFLOW,
workflowId
})
export function fetchFieldGuide(workflowId, projectId) {
return (dispatch) => {
return new Promise ((resolve) => {
apiClient.type('field_guides').get({project_id: projectId}).then(([guide]) => {
if (R.isEmpty(guide.items)) { //no items (clicked add but didn't add anything)
return resolve()
} else {
let icons = {}
guide.get('attached_images').then((images) => {
R.forEach((image) => icons[image.id] = image, images)
guide.icons = icons
}).finally(() => {
dispatch(setGuideForWorkflow(workflowId, guide))
return resolve()
})
}
}).catch(() => {
return resolve()
})
})
}
}
export function fetchTutorials(workflowID) {
return dispatch => {
return new Promise ((resolve) => {
apiClient.type('tutorials').get({workflow_id: workflowID}).then(([tutorial]) => {
let tutorialResource = tutorial
let mediaByID = {}
tutorialResource.get('attached_images').then((mediaResources) => {
R.forEach((mediaResource) => mediaByID[mediaResource.id] = mediaResource, mediaResources)
}).finally(() => {
tutorialResource.mediaResources = mediaByID
dispatch(addTutorial(workflowID, tutorialResource))
resolve()
})
}).catch(() => { //does not exist for this project, that is OK
dispatch(addTutorial(workflowID, {}))
resolve()
})
})
}
}
export function setupProjectPreferences(workflowID, project) {
return (dispatch, getState) => {
return new Promise ((resolve, reject) => {
if (getState().user.isGuestUser){
return resolve()
}
getAuthUser().then((userResource)=> {
userResource.get('project_preferences', {project_id: project.id}).then (([projectPreferences]) => {
//Before being able to classify on a project, the user needs to have their preference created if it doesn't exist
if (projectPreferences) {
return resolve()
}
const projectPreference = {
links: { project: project.id },
preferences: {}
}
apiClient.type('project_preferences').create(projectPreference).save().then(() => {
const projectData = {
name: project.display_name,
slug: project.slug,
activity_count: 0,
sort_order: '',
tutorials_completed_at: {}
};
dispatch(setUserProjectData(project.id, projectData));
return resolve()
}).catch(() => {
return reject()
})
})
})
})
}
}
export function setNeedsTutorial(workflowId, projectId) {
return (dispatch, getState) => {
return new Promise ((resolve) => {
if (R.isEmpty(getState().classifier.tutorial[workflowId])) {
dispatch(setNeedsTutorialAction(workflowId, false))
return resolve()
}
const tutorialID = getState().classifier.tutorial[workflowId].id
let needsTutorial = getState().classifier.needsTutorial[workflowId] !== undefined ? getState().classifier.needsTutorial[workflowId] : true
if ((!getState().user.isGuestUser) && (getState().user.projects[projectId])) {
needsTutorial = !getState().user.projects[projectId]['tutorials_completed_at'][tutorialID]
}
dispatch(setNeedsTutorialAction(workflowId, needsTutorial))
return resolve()
})
}
}
export function setTutorialCompleted(workflowId, projectId) {
return (dispatch, getState) => {
dispatch(setNeedsTutorialAction(workflowId, false))
if (getState().user.isGuestUser) {
return
}
const now = new Date().toISOString()
const tutorialId = getState().classifier.tutorial[workflowId].id
getAuthUser().then((userResourse) => {
userResourse.get('project_preferences', {project_id: projectId}).then (([projectPreferences]) => {
if (!projectPreferences.preferences.tutorials_completed_at) {
projectPreferences.preferences.tutorials_completed_at = {}
}
projectPreferences.update({[`preferences.tutorials_completed_at.${tutorialId}`]: now}).save()
dispatch(saveTutorialAsComplete(projectId, tutorialId, now));
})
})
}
}
export const addAnnotationToTask = (workflowId, task, annotation, asList) => ({
type: ActionConstants.ADD_ANNOTATION_TO_TASK,
workflowId,
task,
annotation,
asList
})
export const removeAnnotationFromTask = (workflowId, task, annotation) => ({
type: ActionConstants.REMOVE_ANNOTATION_FROM_TASK,
workflowId,
task,
annotation,
})
const setSubjectSeenThisSession = (workflowId, subjectId) => ({
type: ActionConstants.SET_SUBJECT_SEEN_THIS_SESSION,
workflowId,
subjectId
})
export const setQuestionContainerHeight = (workflowId, questionContainerHeight) => ({
type: ActionConstants.SET_QUESTION_CONTAINER_HEIGHT,
workflowId,
questionContainerHeight
})
export const clearClassifierData = () => ({
type: ActionConstants.CLEAR_CLASSIFIER_DATA
})
export const setClassifierTestMode = (isTestMode) => ({
type: ActionConstants.SET_CLASSIFIER_TEST_MODE,
isTestMode
})
const clearSubjectsFromWorkflow = (workflowId) => ({
type: ActionConstants.CLEAR_SUBJECTS_FROM_WORKFLOW,
workflowId,
})
const setSubjectStartTimeForWorkflow = (workflowId) => ({
type: ActionConstants.SET_SUBJECT_START_TIME,
workflowId
})
export const setSubjectSizeInWorkflow = (subjectId, {width, height}) => ({
type: ActionConstants.SET_SUBJECT_DIMENSIONS,
subjectId,
subjectDimensions: {naturalWidth: width, naturalHeight: height}
})
const addTutorial = (workflowId, tutorial) => ({
type: ActionConstants.ADD_CLASSIFIER_TUTORIAL,
workflowId,
tutorial
})
const setNeedsTutorialAction = (workflowId, needsTutorial) => ({
type: ActionConstants.ADD_WORKFLOW_NEEDS_TUTORIAL,
workflowId,
needsTutorial
})
const setGuideForWorkflow = (workflowId, guide) => ({
type: ActionConstants.SET_CLASSIFIER_GUIDE,
workflowId,
guide
})
const initializeAnnotation = (workflowId) => ({
type: ActionConstants.INITIALIZE_ANNOTATION,
workflowId,
})
const requestClassifierData = {
type: ActionConstants.REQUEST_CLASSIFIER_DATA
}
const classifierDataSuccess = {
type: ActionConstants.CLASSIFIER_DATA_SUCCESS
}
const classifierDataFailure = {
type: ActionConstants.CLASSIFIER_DATA_FAILURE
}