Esempio n. 1
0
 def post(self, request, *args, **kwargs):
     categories = dict(request.POST)['categories[]']
     categories = QuestionCategory.objects.filter(id__in=categories)
     builder = MatrixBuilder()
     builder.set_electors_categories(categories)
     r = builder.get_result_as_array()
     return JsonResponse(json.dumps(r), safe=False)
 def test_get_matrix_of_candidates_and_positions_and_right_positions(self):
     builder = MatrixBuilder()
     matrix = builder.get_candidates_right_positions_matrix()
     self.assertEquals(matrix.shape, (3, 3))
     self.assertEquals(matrix[0][0], 2)
     self.assertEquals(matrix[0][1], 2)
     self.assertEquals(matrix[0][2], 0)
    def test_get_result_as_dict(self):
        coaligacao = Coaligacao.objects.create(name=u"Coaligacao a",
                                               initials='CA',
                                               number='1234')
        Partido.objects.create(name=u"Partido de los trabalhadores",
                               initials='PT',
                               number='12345',
                               mark=3.5,
                               coaligacao=coaligacao)
        peta = Partido.objects.create(name=u"Petronila",
                                      initials='PeTa',
                                      number='1232',
                                      mark=4.5,
                                      coaligacao=coaligacao)

        self.c1.partido = peta
        self.c1.save()
        builder = MatrixBuilder()
        builder.set_electors_categories([self.cat1, self.cat2])
        r = builder.get_result_as_array()
        self.assertEquals(r[0]['candidato'], self.c1)
        self.assertEquals(r[0]['nota'], 48)
        self.assertEquals(r[1]['candidato'], self.c2)
        self.assertEquals(r[1]['nota'], 3)
        self.assertEquals(r[2]['candidato'], self.c3)
        self.assertEquals(r[2]['nota'], 9)
 def test_get_matrix_of_positions_and_categories(self):
     builder = MatrixBuilder()
     matrix = builder.get_matrix_positions_and_categories()
     expected_mat = np.array([[1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
                              [0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
                              [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]).T
     assert_equal(matrix, expected_mat)
 def test_get_matrix_of_candidates_with_positions(self):
     builder = MatrixBuilder()
     matrix = builder.get_matrix_positions_and_candidates()
     expected_mat = np.array([[2, 0, 0, 2, 1, 0, 1, 0, 1, 0],
                              [0, 1, 1, 0, 0, 2, 0, 2, 1, 0],
                              [1, 0, 1, 0, 0, 1, 1, 0, 2, 0]])
     assert_equal(matrix, expected_mat)
 def post(self, request, *args, **kwargs):
     categories = dict(request.POST)['categories[]']
     categories = QuestionCategory.objects.filter(id__in=categories)
     builder = MatrixBuilder()
     builder.set_electors_categories(categories)
     r = builder.get_result_as_array()
     return JsonResponse(json.dumps(r), safe=False)
 def test_set_electors_categories(self):
     builder = MatrixBuilder()
     builder.set_electors_categories([self.cat1, self.cat2])
     electors_choices = builder.electors_categories
     self.assertEquals(electors_choices.shape, (3,))
     self.assertEquals(electors_choices[0], 3)
     self.assertEquals(electors_choices[1], 3)
     self.assertEquals(electors_choices[2], 1)
 def handle(self, *args, **options):
     time = options['time'][0]
     cache.set('merepresenta_matrix_builder_setup_', None)
     cache.set('candidates_right_positions', None)
     builder = MatrixBuilder(time=time)
     r = builder.get_candidates_result()
     shape = r.shape
     self.stdout.write("Saved matrix of %d candidates with answers, for %d minutes" % (shape[0], time/60))
 def test_get_candidates_full_answer_including_partido(self):
     coaligacao = Coaligacao.objects.create(name=u"Coaligacao a", initials='CA', number='1234')
     Partido.objects.create(name=u"Partido de los trabalhadores",
                            initials='PT',
                            number='12345',
                            mark=3.5,
                            coaligacao=coaligacao)
     peta = Partido.objects.create(name=u"Petronila",
                                   initials='PeTa',
                                   number='1232',
                                   mark=4.5,
                                   coaligacao=coaligacao)
     self.c1.partido = peta
     self.c1.save()
     builder = MatrixBuilder()
     builder.set_electors_categories([self.cat1, self.cat2])
     r = builder.get_result()
     self.assertEquals(r.shape, (Candidate.objects.count(), ))
     self.assertEquals(r[0], 56)
     self.assertEquals(r[1], 4)
     self.assertEquals(r[2], 10)
    def test_get_result_as_dict(self):
        coaligacao = Coaligacao.objects.create(name=u"Coaligacao a", initials='CA', number='1234')
        Partido.objects.create(name=u"Partido de los trabalhadores",
                               initials='PT',
                               number='12345',
                               mark=3.5,
                               coaligacao=coaligacao)
        peta = Partido.objects.create(name=u"Petronila",
                                      initials='PeTa',
                                      number='1232',
                                      mark=4.5,
                                      coaligacao=coaligacao)

        self.c1.partido = peta
        self.c1.save()
        builder = MatrixBuilder()
        builder.set_electors_categories([self.cat1, self.cat2])
        r = builder.get_result_as_array()
        self.assertEquals(r[0]['id'], self.c1.id)
        self.assertEquals(r[0]['nota'], 0.56)
        self.assertEquals(r[1]['id'], self.c2.id)
        self.assertEquals(r[1]['nota'], 0.04)
        self.assertEquals(r[2]['id'], self.c3.id)
        self.assertEquals(r[2]['nota'], 0.10)
    def test_get_positions_vector_of_categories(self):
        '''
        Aqui o que eu quero lograr é um vector de dimensiones (Nao sei falar portugues)
        Dx1 onde D é a quantidade de possivels respostas, asim:

        yes1 | 1 |
        no1  | 0 |
        yes2 | 1 |
        no2  | 0 |
        yes3 | 0 |
        no3  | 0 |
        yes4 | 0 |
        no4  | 0 |
        yes5 | 0 |
        no5  | 0 |

        Más que só tem os dados do Tema
        '''
        builder = MatrixBuilder()
        vector = builder.get_positions_vector_for_category(self.cat1)
        self.assertEquals(vector.shape, (Position.objects.count(), ))
        expected_vector = np.array([1, 0, 1, 0, 0, 0, 0, 0, 0, 0])

        assert_equal(vector, expected_vector)
 def test_get_candidates_right_answers_vs_electors(self):
     builder = MatrixBuilder()
     builder.set_electors_categories([self.cat1, self.cat2])
     r = builder.get_candidates_result()
     self.assertEquals(r.shape, (Candidate.objects.count(), ))
 def test_get_position_vector_respect_with_candidate(self):
     builder = MatrixBuilder()
     vector = builder.get_positions_vector_for_candidate(self.c1)
     expected_vector = np.array([2, 0, 0, 2, 1, 0, 1, 0, 1, 0])
     assert_equal(vector, expected_vector)
 def test_get_zeros_if_not_right_answers_selected(self):
     builder = MatrixBuilder()
     vector = builder.get_positions_vector_for_category(self.cat1)
     RightAnswer.objects.all().delete()
     expected_vector = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
 def test_get_desprivilegios_vector(self):
     builder = MatrixBuilder()
     v = builder.get_desprivilegios(Candidate.objects.all())
     self.assertEquals(v.shape, (Candidate.objects.count(), ))
     self.assertTrue(v[0])
Esempio n. 16
0
 def handle(self, *args, **options):
     time = options['time'][0]
     builder = MatrixBuilder(time=time)
     all_ = builder.set_cache()
     shape = all_[-1].shape
     self.stdout.write("Saved matrix of %d candidates with answers, for %d minutes" % (shape[0], time/60))