Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Timo Heikkilä
PET-rating
Commits
a67841eb
Commit
a67841eb
authored
May 22, 2020
by
Ossi Laine
Browse files
Refactoring
parent
3940ede0
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
app/experiment/views.py
View file @
a67841eb
This diff is collapsed.
Click to expand it.
app/routes.py
View file @
a67841eb
...
...
@@ -21,7 +21,7 @@ from app.models import background_question_option
from
app.models
import
answer_set
,
answer
,
forced_id
from
app.models
import
user
,
trial_randomization
from
app.forms
import
LoginForm
,
RegisterForm
,
StartWithIdForm
from
app.utils
import
saved_data_as_file
from
app.utils
import
saved_data_as_file
,
map_answers_to_questions
# Stimuli upload folder setting
APP_ROOT
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
...
...
@@ -402,7 +402,20 @@ def download_csv():
.
order_by
(
answer
.
page_idpage
)
\
.
all
()
answers_list
=
[
str
(
a
.
answer
).
strip
()
for
a
in
slider_answers
]
pages_and_questions
=
{}
for
p
in
pages
:
questions_list
=
[(
p
.
idpage
,
a
.
idquestion
)
for
a
in
questions
]
pages_and_questions
[
p
.
idpage
]
=
questions_list
_questions
=
[
item
for
sublist
in
pages_and_questions
.
values
()
for
item
in
sublist
]
answers_list
=
map_answers_to_questions
(
slider_answers
,
_questions
)
# typecast elemnts to string
answers_list
=
[
str
(
a
).
strip
()
for
a
in
answers_list
]
answer_row
+=
';'
.
join
(
answers_list
)
+
\
';'
if
slider_answers
else
len
(
questions
)
*
len
(
pages
)
*
';'
...
...
app/utils.py
View file @
a67841eb
...
...
@@ -5,11 +5,12 @@ from flask import send_file
def
map_values_to_int
(
values
:
dict
):
values
=
[
map
(
int
,
i
)
for
i
in
list
(
values
.
values
())]
return
zip_longest
(
*
values
,
fillvalue
=
None
)
#
values = [map(int, i) for i in list(values.values())]
return
zip_longest
(
*
values
.
values
()
,
fillvalue
=
None
)
def
calculate_mean
(
values
:
list
)
->
float
:
print
(
values
)
n_answers
=
sum
(
x
is
not
None
for
x
in
values
)
sum_of_answers
=
float
(
sum
(
filter
(
None
,
values
)))
mean
=
sum_of_answers
/
n_answers
...
...
@@ -34,3 +35,27 @@ def saved_data_as_file(filename, data):
attachment_filename
=
filename
)
finally
:
os
.
remove
(
path
)
def
get_values_from_list_of_answers
(
page_question
,
answers
):
page_id
=
page_question
[
0
]
question_id
=
page_question
[
1
]
for
_answer
in
answers
:
if
_answer
.
question_idquestion
==
question_id
and
\
_answer
.
page_idpage
==
page_id
:
return
int
(
_answer
.
answer
)
return
None
def
map_answers_to_questions
(
answers
,
questions
):
'''
questions = [(4, 1), (4, 2), (5, 1), (5, 2), (6, 1), (6, 2)]
+
answers = [{p:6, q:1, a:100}, {p:6, q:2, a:99}]
->
partial_answer = [None, None, None, None, 100, 99]
'''
return
list
(
map
(
lambda
x
:
get_values_from_list_of_answers
(
x
,
answers
),
questions
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment