Ejemplo n.º 1
0
 def test_1_d_interpolate(self):
     # arrange
     cm = Colormap.__new__(Colormap)
     cm._Colormap__data = self.normalized_color_data_1d
     # act
     color_1 = cm._1d_interpolate(-1)
     color_2 = cm._1d_interpolate(-0.1)
     color_3 = cm._1d_interpolate(0.0)
     color_4 = cm._1d_interpolate(0.1)
     color_5 = cm._1d_interpolate(0.11)
     color_6 = cm._1d_interpolate(1)
     color_7 = cm._1d_interpolate(1.2)
     color_8 = cm._1d_interpolate(5)
     # assert
     for color, result in [
         (color_1, (0.19607843137254902, 0.0392156862745098,
                    0.0784313725490196)),
         (color_2, (0.19607843137254902, 0.0392156862745098,
                    0.0784313725490196)),
         (color_3, (0.19607843137254902, 0.0392156862745098,
                    0.0784313725490196)),
         (color_4, (0.9019607843137255, 0.39215686274509803,
                    0.19607843137254902)),
         (color_5, (0.8911764705882352, 0.39215686274509803,
                    0.21617647058823528)),
         (color_6, (0.13445378151260506, 0.8263305322128852, 1.0)),
         (color_7, (0.0, 1.0, 1.0)),
         (color_8, (0.0, 1.0, 1.0)),
     ]:
         for color_i, result_i in zip(color, result):
             self.assertAlmostEqual(color_i, result_i)
Ejemplo n.º 2
0
    def test_rendering_color_space(self):
        """
        This test saves an image of color space
        """
        # prepare directory
        if not os.path.exists("./research/color spaces benchmarks/"):
            os.makedirs("./research/color spaces benchmarks/")

        for name, colordata in zip(
            ["color", "black_white"],
            [self.color_data_2d, self.black_white_data_2d]):
            # make roughly unique hash of code version by computing common
            # bytes sum times alternating sum
            with open("./pkwscraper/lib/visualizer.py", "rb") as f:
                code = f.read()
            code_hash = sum(code) * sum(c ^ 0b01010101 for c in code)

            # compose filepath
            filepath = (f"./research/color spaces benchmarks"
                        f"/_benchmark_{name}_{code_hash}.png")

            # make color space
            size = 50  # 250
            cm = Colormap(colordata)
            space = np.array(
                [[cm([j, i]) for j in np.linspace(0, 1, size + 1)]
                 for i in np.linspace(0, 1, size + 1)])
            plt.imshow(space, origin='lower')
            plt.savefig(filepath)
            #plt.show()
            plt.close()
Ejemplo n.º 3
0
    def test_init_matplotlib_colormap_name(self):
        cm = Colormap("ocean")

        self.assertIs(cm._Colormap__data, ocean)
        self.assertIsInstance(cm._Colormap__data, LinearSegmentedColormap)
        self.assertEqual(cm.interpolation, "linear")
        self.assertIsNone(cm._vdim)
Ejemplo n.º 4
0
    def test_init(self):
        with self.assertRaises(ValueError):
            Colormap(self.color_data_1d, interpolation="quadratic")

        cm = Colormap(self.color_data_1d)

        self.assertDictEqual(
            cm._Colormap__data, {
                0.0:
                (0.19607843137254902, 0.0392156862745098, 0.0784313725490196),
                0.1:
                (0.9019607843137255, 0.39215686274509803, 0.19607843137254902),
                0.5: (0.47058823529411764, 0.39215686274509803, 1.0),
                1.2: (0.0, 1.0, 1.0)
            })
        self.assertEqual(cm.interpolation, "linear")
        self.assertIsNone(cm._vdim)
Ejemplo n.º 5
0
    def test_with_colormap(self):
        # prepare data
        values = [(0.2, 0.5), (0.4, 0.1), (0.3, 1.0)]
        colormap = Colormap(self.color_data_2d)

        # create visualizer
        vis = Visualizer(self.regions,
                         values,
                         colormap,
                         contours=[self.whole_region],
                         title="Test plot")

        # call preparations
        vis.normalize_values()
        vis.render_colors()
Ejemplo n.º 6
0
 def test_call_colormap(self):
     # arrange
     mock_cm = Colormap.__new__(Colormap)
     mock_cm._Colormap__data = ocean
     mock_cm._1d_interpolate = MagicMock()
     mock_cm._nd_interpolate = MagicMock()
     # act
     result = mock_cm(0.5)
     result_2 = mock_cm(2)
     # assert
     self.assertTupleEqual(
         result, (0.0, 0.2529411764705882, 0.5019607843137255, 1.0))
     self.assertTupleEqual(result_2, (1, 1, 1, 1))
     mock_cm._1d_interpolate.assert_not_called()
     mock_cm._nd_interpolate.assert_not_called()
Ejemplo n.º 7
0
 def test_call_vector(self):
     # arrange
     mock_cm = Colormap.__new__(Colormap)
     mock_cm._Colormap__data = MagicMock()
     mock_cm._Colormap__data.return_value = (0, 0.5, 0.5)
     mock_cm._1d_interpolate = MagicMock()
     mock_cm._1d_interpolate.return_value = (1, 1, 1)
     mock_cm._nd_interpolate = MagicMock()
     mock_cm._nd_interpolate.return_value = (0, 0, 0)
     # act
     result = mock_cm((3, 0.2))
     # assert
     self.assertTupleEqual(result, (0, 0, 0))
     mock_cm._Colormap__data.assert_not_called()
     mock_cm._1d_interpolate.assert_not_called()
     mock_cm._nd_interpolate.assert_called_once_with((3, 0.2))
Ejemplo n.º 8
0
 def test_n_d_interpolate(self):
     # arrange
     cm = Colormap.__new__(Colormap)
     cm._Colormap__data = self.color_data_2d
     cm._vdim = 2
     # act
     color_1 = cm._nd_interpolate((0.3, 0.8))
     color_2 = cm._nd_interpolate((0.0, 1.0))
     color_3 = cm._nd_interpolate((0.5, 0.2))
     # assert
     for color, result in [
         (color_1, (0.31914294589583, 0.725615282464, 0.770649196451)),
         (color_2, (0.01619069672298, 0.987632500101, 0.987632500101)),
         (color_3, (0.5, 0.5, 0.272459202136)),
     ]:
         for color_i, result_i in zip(color, result):
             self.assertAlmostEqual(color_i, result_i)
Ejemplo n.º 9
0
                try:
                    non_party_votes += int(result[cand_id])
                except KeyError:
                    pass
            for cand_id in political_party_candidates:
                try:
                    political_party_votes += int(result[cand_id])
                except KeyError:
                    pass

    # return non-party result
    return non_party_votes / (non_party_votes + political_party_votes)


colormap = Colormap(color_data={
    0.00: (0.7, 0.8, 0.8, 0.82),
    1.00: (1.0, 0.1, 0.1, 0.82),
})


def main():
    grans = ["communes", "districts", "constituencies", "voivodships"]
    names = ["1comm", "2distr", "3const", "4voivod"]

    for gran, name in zip(grans, names):
        print(f"processing {gran}...")
        out_gran = "voivodships" if gran == "voivodships" else "constituencies"
        ctrl_i = Controller(("Sejm", 2015),
                            function,
                            colormap,
                            granularity=gran,
                            outlines_granularity=out_gran,
Ejemplo n.º 10
0
    def length(name):
        if name is None:
            return 22
        return len(name)

    names_lenghts = list(map(length, commission_names))
    average = np.average(names_lenghts)
    dispersion = np.std(names_lenghts) / average
    return average, dispersion


colormap = Colormap({
    (0.0, 0.0): (0, 255, 0, 220),
    (0.4, 0.0): (100, 50, 0, 220),
    (0.9, 0.0): (255, 0, 0, 220),
    (0.0, 0.6): (100, 255, 220, 200),
    (1.0, 0.6): (255, 220, 220, 200),
    (-0.1, 1.1): (220, 250, 220, 190),
    (0.5, 1.1): (230, 220, 220, 190),
    (1.1, 1.1): (250, 220, 220, 190),
})


def main():
    ctrl = Controller(("Sejm", 2015),
                      function,
                      colormap,
                      granularity="communes",
                      outlines_granularity="voivodships",
                      normalization=True,
                      output_filename="commission_names_length.png")
    ctrl.run()
Ejemplo n.º 11
0
                    female_votes += int(result[cand_id])
                except KeyError:
                    pass
            for cand_id in male_ids:
                try:
                    male_votes += int(result[cand_id])
                except KeyError:
                    pass

    # return females result
    return female_votes / (female_votes + male_votes)


colormap = Colormap(
    color_data={
        0.0: (0.0, 0.6, 0.9, 0.82),
        0.5: (1.0, 1.0, 1.0, 0.82),
        1.0: (1.0, 0.6, 0.7, 0.82),
    })


def main():
    grans = ["communes", "districts", "constituencies", "voivodships"]
    names = ["1comm", "2distr", "3const", "4voivod"]

    for gran, name in zip(grans, names):
        print(f"processing {gran}...")
        out_gran = "voivodships" if gran == "voivodships" else "constituencies"
        ctrl_i = Controller(("Sejm", 2015),
                            function,
                            colormap,
                            granularity=gran,
Ejemplo n.º 12
0
        list_result = list_votes / votes_valid
        turnout = votes_valid / voters
        # add measures to lists
        list_results.append(list_result)
        turnouts.append(turnout)

    # compute value of correlation
    my_rho = np.corrcoef(list_results, turnouts)
    # return the value
    return my_rho[0][1]


colormap = Colormap(
    color_data={
        -5: (20, 30, 100, 230),
        -1: (0, 0, 255, 200),
        0: (255, 255, 255, 255),
        1: (255, 0, 0, 200),
        5: (100, 50, 30, 230),
    })


def get_whole_country_lists():
    # load source DB
    temp_ctrl = Controller(("Sejm", 2015),
                           None,
                           None,
                           granularity="communes",
                           outlines_granularity="constituencies")
    temp_ctrl._load_db()

    # find list id and constituency for each valid candidate
Ejemplo n.º 13
0
    def test_init_matplotlib_colormap(self):
        cm = Colormap(ocean)

        self.assertIs(cm._Colormap__data, ocean)
        self.assertEqual(cm.interpolation, "linear")
        self.assertIsNone(cm._vdim)
Ejemplo n.º 14
0
    def test_init_vector_values(self):
        cm = Colormap(self.color_data_2d, "logarithmic")

        self.assertDictEqual(cm._Colormap__data, self.color_data_2d)
        self.assertEqual(cm.interpolation, "logarithmic")
        self.assertEqual(cm._vdim, 2)