Skip to content

Commit af382b5

Browse files
committed
Fixing csv generation
1 parent e96166a commit af382b5

File tree

8 files changed

+141
-13
lines changed

8 files changed

+141
-13
lines changed
File renamed without changes.

experiments/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ExperimentAdmin(admin.ModelAdmin):
3333
readonly_fields = ('created_at', 'updated_at',)
3434
list_display = ('description', 'created_at', 'updated_at')
3535

36+
3637
class TaskAdmin(admin.ModelAdmin):
3738
inlines = [ExecutionInline,]
3839
list_filter = ('created_at', 'experiment',)
@@ -47,6 +48,7 @@ class ParticipantAdmin(admin.ModelAdmin):
4748
readonly_fields = ('created_at', 'updated_at',)
4849
list_display = ('name', 'email', 'created_at', 'updated_at')
4950

51+
5052
class ExecutionAdmin(admin.ModelAdmin):
5153
list_filter = ('participant', 'task', 'start', 'end', 'created_at', 'updated_at',)
5254
readonly_fields = ('start', 'end', 'created_at', 'updated_at',)
@@ -58,6 +60,7 @@ class PointAdmin(admin.ModelAdmin):
5860
list_display = ('x', 'y', 'datetime',)
5961
list_filter = ('datetime',)
6062

63+
6164
class LatinSquareAdmin(admin.ModelAdmin):
6265
list_filter = ('experiment', 'row1', 'row2',)
6366
readonly_fields = ('row1', 'row2',)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import csv
4+
from django.core.management.base import BaseCommand
5+
from django.db.models import Q
6+
from experiments.models import LatinSquare, Execution
7+
8+
9+
class Command(BaseCommand):
10+
help = 'Generate a new CSV file to be used as dataset on rstudio script'
11+
12+
def handle(self, *args, **options):
13+
executions = Execution.objects\
14+
.filter(task__experiment_id=2)\
15+
.exclude(participant_id=18).order_by('-id') # O eye tracker não funcionou na aplicação da Lettícia
16+
17+
18+
with open('/Users/fernandooliveira/workspaces/mestrado/Experimento/masters_experiment_analysis/datasetatoms3.csv', 'wb') as csvfile:
19+
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
20+
filewriter.writerow(["", "Replica", "Id", "Student", "SetOfTasks", "Tasks", "Technique", "Trials", "Time", "Minutes"])
21+
22+
for index, execution in enumerate(executions, start=1):
23+
if hasattr(execution.participant.row_participant, 'row1_latin_square'):
24+
latin_square = execution.participant.row_participant.row1_latin_square
25+
idline = 1
26+
else:
27+
latin_square = execution.participant.row_participant.row2_latin_square
28+
idline = 2
29+
30+
set_of_tasks = "ST1" if execution.task.frame in [1,3] else "ST2"
31+
32+
if execution.task.description.startswith("AV1"):
33+
tasks = 1
34+
elif execution.task.description.startswith("CO1"):
35+
tasks = 2
36+
elif execution.task.description.startswith("DE1"):
37+
tasks = 3
38+
elif execution.task.description.startswith("AV2"):
39+
tasks = 4
40+
elif execution.task.description.startswith("CO2"):
41+
tasks = 5
42+
else:
43+
tasks = 6
44+
45+
if execution.task.description.endswith(".1"):
46+
technique = "With Atom"
47+
else:
48+
technique = "Without Atom"
49+
50+
filewriter.writerow(
51+
[
52+
index,
53+
latin_square.id,
54+
execution.participant_id,# idline,
55+
execution.participant.name.encode('utf-8'),
56+
set_of_tasks,
57+
tasks,
58+
technique,
59+
execution.answer_set.all().count(),
60+
int(execution.duration_in_minutes() * 60000),
61+
execution.duration_in_minutes()
62+
]
63+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.20 on 2019-06-25 04:23
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('experiments', '0023_auto_20190408_0617'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='latinsquare',
18+
name='row1',
19+
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='row1_latin_square', to='experiments.LatinSquareRow'),
20+
),
21+
migrations.AlterField(
22+
model_name='latinsquare',
23+
name='row2',
24+
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='row2_latin_square', to='experiments.LatinSquareRow'),
25+
),
26+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.20 on 2019-06-25 04:25
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('experiments', '0024_auto_20190625_0423'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='latinsquarerow',
18+
name='participant',
19+
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='row_participant', to='experiments.Participant'),
20+
),
21+
]

experiments/models.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
from datetime import datetime
4+
from datetime import datetime, timedelta
55

66
from django.contrib.postgres.fields import ArrayField
77
from django.db import models
@@ -29,14 +29,14 @@ class Participant(models.Model):
2929
def finish_last_pause_for_each_execution(self):
3030
for execution in self.execution_set.all():
3131
last_pause = Pause.objects.filter(execution=execution).order_by('-pk')
32-
32+
3333
if len(last_pause) > 0:
3434
last_pause = last_pause[0]
3535
if last_pause.end_time is None:
3636
last_pause.end_time = datetime.now()
3737
last_pause.save()
3838

39-
#def finish_all_pauses(self):
39+
# def finish_all_pauses(self):
4040
# for execution in self.execution_set.all():
4141
# execution.pause_set.all().update(end_time=datetime.now())
4242

@@ -91,13 +91,13 @@ def execution_total_duration(self):
9191

9292
return (self.end - self.start).total_seconds()
9393

94-
def duration_in_seconds(self):
94+
def duration_in_minutes(self):
9595
duration_in_s = self.execution_total_duration() - self.pauses_duration()
96-
97-
# duration_in_s = (self.end - self.start).total_seconds()
9896

99-
return divmod(duration_in_s, 60)[0] # return in minutes
97+
# duration_in_s = (self.end - self.start).total_seconds()
10098

99+
# return divmod(duration_in_s, 60)[0] # return in minutes
100+
return duration_in_s / 60
101101

102102
class Point(models.Model):
103103
x = models.CharField(max_length=200)
@@ -117,19 +117,19 @@ class LatinSquareRow(models.Model):
117117
related_name='first_cell')
118118
cell2 = models.ForeignKey(LatinSquareCell, on_delete=models.CASCADE, blank=True, null=True,
119119
related_name='second_cell')
120-
participant = models.ForeignKey(Participant, on_delete=models.CASCADE, blank=True, null=True,
121-
related_name='row_participant')
120+
participant = models.OneToOneField(Participant, on_delete=models.CASCADE, blank=True, null=True,
121+
related_name='row_participant')
122122

123123
def __unicode__(self):
124124
return "%d" % self.id
125125

126126

127127
class LatinSquare(models.Model):
128128
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, blank=True, null=True)
129-
row1 = models.ForeignKey(LatinSquareRow, on_delete=models.CASCADE, blank=True, null=True,
130-
related_name='row1_latin_square')
131-
row2 = models.ForeignKey(LatinSquareRow, on_delete=models.CASCADE, blank=True, null=True,
132-
related_name='row2_latin_square')
129+
row1 = models.OneToOneField(LatinSquareRow, on_delete=models.CASCADE, blank=True, null=True,
130+
related_name='row1_latin_square')
131+
row2 = models.OneToOneField(LatinSquareRow, on_delete=models.CASCADE, blank=True, null=True,
132+
related_name='row2_latin_square')
133133
frame_sequence = ArrayField(models.IntegerField(), null=True, blank=True, default=[1, 2, 3, 4])
134134

135135
objects = LatinSquareManager()

manage.py

100644100755
File mode changed.

test.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from datetime import timedelta
2+
from experiments.models import Point, Execution
3+
4+
5+
participants_ids = [54,71,4,34,41,52,43,32,10,45,38,79,6,81,77,36,30,49,60,47,66,13,18,64,2,62,75,56,73,58,8]
6+
executions = Execution.objects.filter(participant_id__in=participants_ids)
7+
8+
for execution in executions:
9+
if (execution.start is None) or (execution.end is None):
10+
print("Execution %d has no start %s or end %s" % (execution.id, execution.start, execution.end,))
11+
continue
12+
start_time = execution.start + timedelta(hours=3)
13+
end_time = execution.end + timedelta(hours=3)
14+
points_number = Point.objects.filter(datetime__range=(start_time, end_time)).count()
15+
print("Execution %d: Participant: %d %d points" % (execution.id, execution.participant_id, points_number))

0 commit comments

Comments
 (0)