예제 #1
0
    def test_add_multiple_chains(self):
        y = self.minimum[0]
        x = self.minimum[1:3]

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertCountEqual([], cluster.keys())

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_3 = self.stack2.get_chain(name="ChainName3",
                                        data_keys="Mar",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_4 = self.stack3.get_chain(name="ChainName4",
                                        data_keys="Apr",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])

        cluster.add_chain(chains=[chain_1, chain_2, chain_3, chain_4])
        self.assertCountEqual(
            ["ChainName1", "ChainName2", "ChainName3", "ChainName4"],
            cluster.keys())
예제 #2
0
    def test_add_multiple_chains(self):
        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([], cluster.keys())

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=['default'])
        chain_3 = self.stack2.get_chain(name="ChainName3",
                                        data_keys="Mar",
                                        x=x,
                                        y=y,
                                        views=['default'])
        chain_4 = self.stack3.get_chain(name="ChainName4",
                                        data_keys="Apr",
                                        x=x,
                                        y=y,
                                        views=['default'])

        cluster.add_chain(chains=[chain_1, chain_2, chain_3, chain_4])
        self.assertItemsEqual(
            ["ChainName1", "ChainName2", "ChainName3", "ChainName4"],
            cluster.keys())
예제 #3
0
    def test_save_cluster(self):
        # TO DO -- Cluster saving probably not working yet
        y = ['Wave']
        x = ['q2', 'q3']
        v = 'x|frequency||||counts'
        cluster = Cluster(name="ClusterName")

        self.assertIsInstance(cluster, Cluster)
        self.assertEqual([],cluster.keys())

        for i in xrange(3):
            chain = self.stack0.get_chain(name="ChainName{0}".format(i), data_keys="Jan", x=x, y=y, views=[v])
            cluster.add_chain(chains=chain)

        # Create a dictionary with the attribute structure of the cluster
        cluster_attributes = cluster.__dict__

        path_cluster = '{}test.cluster'.format(self.path)
        cluster.save(path_cluster)
        loaded_cluster = Cluster.load(path_cluster)

        # Create a dictionary with the attribute structure of the cluster
        loaded_cluster_attributes = loaded_cluster.__dict__

        # Ensure that we are not comparing the same variable (in memory)
        self.assertNotEqual(id(cluster), id(loaded_cluster))

        # Make sure that this is working by altering the loaded_stack_attributes
        # and comparing the result. (It should fail)

        # Change a 'value' in the dict
        loaded_cluster_attributes['name'] = "SomeOtherName"
        with self.assertRaises(AssertionError):
            self.assertEqual(cluster.name, loaded_cluster_attributes['name'])

        # reset the value
        loaded_cluster_attributes['name'] = cluster_attributes['name']
        self.assertEqual(cluster_attributes['name'], loaded_cluster_attributes['name'])

        # Change a 'key' in the dict
        del loaded_cluster_attributes['name']
        loaded_cluster_attributes['new_name'] = cluster_attributes['name']
        with self.assertRaises(AttributeError):
            self.assertEqual(cluster.name, loaded_cluster.name)

        # reset the value
        del loaded_cluster_attributes['new_name']
        loaded_cluster_attributes['name'] = cluster_attributes['name']
        self.assertEqual(cluster.name, loaded_cluster.name)

        # Remove a key/value pair
        del loaded_cluster_attributes['name']
        with self.assertRaises(AttributeError):
            self.assertEqual(cluster.name, loaded_cluster.name)

        # Cleanup
        if os.path.exists(path_cluster):
            os.remove(path_cluster)
예제 #4
0
    def test_add_chain(self):
        y = self.minimum[0]
        x = self.minimum[1:3]

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertCountEqual([], cluster.keys())

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_3 = self.stack2.get_chain(name="ChainName3",
                                        data_keys="Mar",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_4 = self.stack3.get_chain(name="ChainName4",
                                        data_keys="Apr",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])

        cluster.add_chain(chains=chain_1)
        cluster.add_chain(chains=chain_2)
        self.assertCountEqual(["ChainName1", "ChainName2"], cluster.keys())

        cluster.add_chain(chains=chain_3)
        cluster.add_chain(chains=chain_4)
        self.assertCountEqual([
            "ChainName1",
            "ChainName2",
            "ChainName3",
            "ChainName4",
        ], cluster.keys())

        #Check if the method incorrectly adds to the chain at the back of the list, along with / instead of saving over previous instance
        with self.assertRaises(IndexError):
            cluster.keys()[4]
예제 #5
0
    def test_add_multiple_chains_exceptions(self):
        y = self.minimum[0]
        x = self.minimum[1:3]

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertCountEqual([], cluster.keys())

        exception_message = "One or more of the supplied chains has an inappropriate type."

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_3 = self.stack2.get_chain(name="ChainName3",
                                        data_keys="Mar",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_4 = self.stack3.get_chain(name="ChainName4",
                                        data_keys="Apr",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])

        self.assertCountEqual([], cluster.keys())

        invalid_chains_list = [
            chain_1, chain_2, chain_3, chain_4, "This is not a chain"
        ]
        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=invalid_chains_list)
        self.assertEquals(cm.exception.message, exception_message)

        # Assert that no chains were added
        self.assertCountEqual([], cluster.keys())
예제 #6
0
    def test_add_chain_exceptions(self):

        y = self.minimum[0]
        x = self.minimum[1:3]

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertCountEqual([], cluster.keys())

        exception_message = (
            "You must pass either a Chain, a list of Chains or a"
            " banked chain definition (as a dict) into"
            " Cluster.add_chain().")

        # Test the exceptions in add_chain
        with self.assertRaises(TypeError) as cm:
            cluster.add_chain()
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=None)
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains="This is not a chain")
        self.assertEquals(cm.exception.message, exception_message)

        exception_message = "One or more of the supplied chains has an inappropriate type."

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=[COUNTS])

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=[chain_1, "This is not a chain", chain_2])
        self.assertEquals(cm.exception.message, exception_message)

        # Should succeed
        try:
            cluster.add_chain(chains=chain_1)
        except TypeError as cm:
            self.fail(
                'cluster.add_chain(chains=chain_1) should NOT raise a TypeError Exception.'
            )
        except:
            pass
예제 #7
0
    def test_add_chain_exceptions(self):

        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([], cluster.keys())

        exception_message = "You must pass either a Chain or a list of Chains to Cluster.add_chain()"

        # Test the exceptions in add_chain
        with self.assertRaises(TypeError) as cm:
            cluster.add_chain()
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=None)
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains="This is not a chain")
        self.assertEquals(cm.exception.message, exception_message)

        exception_message = "One or more of the supplied chains has an inappropriate type."

        chain_1 = self.stack0.get_chain(name="ChainName1",
                                        data_keys="Jan",
                                        x=x,
                                        y=y,
                                        views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2",
                                        data_keys="Feb",
                                        x=x,
                                        y=y,
                                        views=['default'])

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=[chain_1, "This is not a chain", chain_2])
        self.assertEquals(cm.exception.message, exception_message)

        # Should succeed
        try:
            cluster.add_chain(chains=chain_1)
        except TypeError as cm:
            self.fail(
                'cluster.add_chain(chains=chain_1) should NOT raise a TypeError Exception.'
            )
        except:
            pass
예제 #8
0
    def test_add_chain(self):
        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([],cluster.keys())

        chain_1 = self.stack0.get_chain(name="ChainName1", data_keys="Jan", x=x, y=y, views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2", data_keys="Feb", x=x, y=y, views=['default'])
        chain_3 = self.stack2.get_chain(name="ChainName3", data_keys="Mar", x=x, y=y, views=['default'])
        chain_4 = self.stack3.get_chain(name="ChainName4", data_keys="Apr", x=x, y=y, views=['default'])

        cluster.add_chain(chains=chain_1)
        cluster.add_chain(chains=chain_2)
        self.assertItemsEqual(["ChainName1", "ChainName2"], cluster.keys())

        cluster.add_chain(chains=chain_3)
        cluster.add_chain(chains=chain_4)
        self.assertItemsEqual(["ChainName1", "ChainName2", "ChainName3", "ChainName4",],cluster.keys())

        #Check if the method incorrectly adds to the chain at the back of the list, along with / instead of saving over previous instance
        with self.assertRaises(IndexError):
            cluster.keys()[4]
예제 #9
0
    def test_add_multiple_chains(self):
        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([],cluster.keys())

        chain_1 = self.stack0.get_chain(name="ChainName1", data_keys="Jan", x=x, y=y, views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2", data_keys="Feb", x=x, y=y, views=['default'])
        chain_3 = self.stack2.get_chain(name="ChainName3", data_keys="Mar", x=x, y=y, views=['default'])
        chain_4 = self.stack3.get_chain(name="ChainName4", data_keys="Apr", x=x, y=y, views=['default'])

        cluster.add_chain(chains=[chain_1, chain_2, chain_3, chain_4])
        self.assertItemsEqual(["ChainName1", "ChainName2", "ChainName3", "ChainName4"], cluster.keys())
예제 #10
0
    def test_add_multiple_chains_exceptions(self):
        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([],cluster.keys())

        exception_message = "One or more of the supplied chains has an inappropriate type."

        chain_1 = self.stack0.get_chain(name="ChainName1", data_keys="Jan", x=x, y=y, views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2", data_keys="Feb", x=x, y=y, views=['default'])
        chain_3 = self.stack2.get_chain(name="ChainName3", data_keys="Mar", x=x, y=y, views=['default'])
        chain_4 = self.stack3.get_chain(name="ChainName4", data_keys="Apr", x=x, y=y, views=['default'])

        self.assertItemsEqual([], cluster.keys())

        invalid_chains_list = [chain_1, chain_2, chain_3, chain_4, "This is not a chain"]
        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=invalid_chains_list)
        self.assertEquals(cm.exception.message, exception_message)

        # Assert that no chains were added
        self.assertItemsEqual([], cluster.keys())
예제 #11
0
    def test_add_dataframe(self):

        df = self.example_data_A_data[self.one_of_each]

        cluster = Cluster('DataFrame')
        cluster.add_chain(df)

        #check that we have a cluster
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual('DataFrame', cluster.name)

        #check the cluster keys
        self.assertItemsEqual(['_'.join(self.one_of_each)], cluster.keys())

        #cluster contents
        for item in cluster.values():
            self.assertIsInstance(item, pd.DataFrame)
예제 #12
0
    def test_add_dataframe(self):

        df = self.example_data_A_data[self.one_of_each]

        cluster = Cluster('DataFrame')
        cluster.add_chain(df)

        #check that we have a cluster
        self.assertIsInstance(cluster, Cluster)
        self.assertCountEqual('DataFrame', cluster.name)

        #check the cluster keys
        self.assertCountEqual(['_'.join(self.one_of_each)], cluster.keys())

        #cluster contents
        for item in cluster.values():
            self.assertIsInstance(item, pd.DataFrame)
예제 #13
0
    def test_add_chain_exceptions(self):

        y = ['Gender']
        x = ['Animal', 'Region']

        cluster = Cluster(name="ClusterName")
        self.assertIsInstance(cluster, Cluster)
        self.assertItemsEqual([],cluster.keys())

        exception_message = "You must pass either a Chain or a list of Chains to Cluster.add_chain()"

        # Test the exceptions in add_chain
        with self.assertRaises(TypeError) as cm:
            cluster.add_chain()
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=None)
        self.assertEquals(cm.exception.message, exception_message)

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains="This is not a chain")
        self.assertEquals(cm.exception.message, exception_message)

        exception_message = "One or more of the supplied chains has an inappropriate type."

        chain_1 = self.stack0.get_chain(name="ChainName1", data_keys="Jan", x=x, y=y, views=['default'])
        chain_2 = self.stack1.get_chain(name="ChainName2", data_keys="Feb", x=x, y=y, views=['default'])

        with self.assertRaises(TypeError) as cm:
            cluster.add_chain(chains=[chain_1, "This is not a chain", chain_2])
        self.assertEquals(cm.exception.message, exception_message)

        # Should succeed
        try:
            cluster.add_chain(chains=chain_1)
        except TypeError as cm:
            self.fail('cluster.add_chain(chains=chain_1) should NOT raise a TypeError Exception.')
        except:
            pass