<commit_before>import unittest
import libpysal
import giddy.directional as directional
import numpy as np


class Rose_Tester(unittest.TestCase):
    def setUp(self):
        f = open(libpysal.examples.get_path("spi_download.csv"), 'r')
        lines = f.readlines()
        f.close()
        lines = [line.strip().split(",") for line in lines]
        names = [line[2] for line in lines[1:-5]]
        data = np.array([list(map(int, line[3:])) for line in lines[1:-5]])
        sids = list(range(60))
        out = ['"United States 3/"',
               '"Alaska 3/"',
               '"District of Columbia"',
               '"Hawaii 3/"',
               '"New England"',
               '"Mideast"',
               '"Great Lakes"',
               '"Plains"',
               '"Southeast"',
               '"Southwest"',
               '"Rocky Mountain"',
               '"Far West 3/"']
        snames = [name for name in names if name not in out]
        sids = [names.index(name) for name in snames]
        states = data[sids, :]
        us = data[0]
        years = np.arange(1969, 2009)
        rel = states / (us * 1.)
        gal = libpysal.open(libpysal.examples.get_path('states48.gal'))
        self.w = gal.read()
        self.w.transform = 'r'
        self.Y = rel[:, [0, -1]]

    def test_rose(self):
        k = 4
        np.random.seed(100)
        r4 = directional.Rose(self.Y, self.w, k)
        exp = [0., 1.57079633, 3.14159265, 4.71238898, 6.28318531]
        obs = list(r4.cuts)
        for i in range(k + 1):
            self.assertAlmostEqual(exp[i], obs[i])
        self.assertEqual(list(r4.counts), [32, 5, 9, 2])

<commit_msg>add tests for `.plot` and `.plot_vectors` methods<commit_after>import unittest
import libpysal
import giddy.directional as directional
import numpy as np


class Rose_Tester(unittest.TestCase):
    def setUp(self):
        f = open(libpysal.examples.get_path("spi_download.csv"), 'r')
        lines = f.readlines()
        f.close()
        lines = [line.strip().split(",") for line in lines]
        names = [line[2] for line in lines[1:-5]]
        data = np.array([list(map(int, line[3:])) for line in lines[1:-5]])
        sids = list(range(60))
        out = ['"United States 3/"',
               '"Alaska 3/"',
               '"District of Columbia"',
               '"Hawaii 3/"',
               '"New England"',
               '"Mideast"',
               '"Great Lakes"',
               '"Plains"',
               '"Southeast"',
               '"Southwest"',
               '"Rocky Mountain"',
               '"Far West 3/"']
        snames = [name for name in names if name not in out]
        sids = [names.index(name) for name in snames]
        states = data[sids, :]
        us = data[0]
        years = np.arange(1969, 2009)
        rel = states / (us * 1.)
        gal = libpysal.open(libpysal.examples.get_path('states48.gal'))
        self.w = gal.read()
        self.w.transform = 'r'
        self.Y = rel[:, [0, -1]]

    def test_rose(self):
        k = 4
        np.random.seed(100)
        r4 = directional.Rose(self.Y, self.w, k)
        exp = [0., 1.57079633, 3.14159265, 4.71238898, 6.28318531]
        obs = list(r4.cuts)
        for i in range(k + 1):
            self.assertAlmostEqual(exp[i], obs[i])
        self.assertEqual(list(r4.counts), [32, 5, 9, 2])


    def test_plot(self):
        import geopandas as gpd
        import pandas as pd
        import libpysal.api as lp
        from libpysal import examples
        import numpy as np
        import matplotlib.pyplot as plt
        from giddy.directional import Rose
        # get data, calc mean, merge
        shp_link = examples.get_path('us48.shp')
        df = gpd.read_file(shp_link)
        income_table = pd.read_csv(examples.get_path("usjoin.csv"))
        for year in range(1969, 2010):
            income_table[str(year) + '_rel'] = (
                income_table[str(year)] / income_table[str(year)].mean())
        gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name')
        # statistical analysis
        w = lp.Queen.from_dataframe(gdf)
        w.transform = 'r'
        y1 = gdf['1969_rel'].values
        y2 = gdf['2000_rel'].values
        Y = np.array([y1, y2]).T
        rose = Rose(Y, w, k=5)
        # plot
        fig, _ = rose.plot()
        plt.close(fig)
        # plot with atribute coloring
        fig, _ = rose.plot(attribute=y1)
        plt.close(fig)


    def test_plot_vectors(self):
        import geopandas as gpd
        import pandas as pd
        import libpysal.api as lp
        from libpysal import examples
        import numpy as np
        import matplotlib.pyplot as plt
        from giddy.directional import Rose
        # get data, calc mean, merge
        shp_link = examples.get_path('us48.shp')
        df = gpd.read_file(shp_link)
        income_table = pd.read_csv(examples.get_path("usjoin.csv"))
        for year in range(1969, 2010):
            income_table[str(year) + '_rel'] = (
                income_table[str(year)] / income_table[str(year)].mean())
        gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name')
        # statistical analysis
        w = lp.Queen.from_dataframe(gdf)
        w.transform = 'r'
        y1 = gdf['1969_rel'].values
        y2 = gdf['2000_rel'].values
        Y = np.array([y1, y2]).T
        rose = Rose(Y, w, k=5)
        # plot
        fig, _ = rose.plot_vectors()
        plt.close(fig)
        # customize plot TODO add arrow example when splot is released


{'fraction_non_alphanumeric': 0.1367010682600036, 'fraction_numerical': 0.036031142495020825, 'mean_word_length': 1.8921465968586388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4530416', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1792, 'n_words': 437}