test_parallelbiflow.py :  » Math » Modular-toolkit-for-Data-Processing » MDP-2.6 » bimdp » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » Modular toolkit for Data Processing 
Modular toolkit for Data Processing » MDP 2.6 » bimdp » test » test_parallelbiflow.py

import unittest
import numpy as n

import mdp

from bimdp.nodes import SFABiNode,SFA2BiNode
from bimdp import BiFlow
from bimdp.parallel import ParallelBiFlow,ParallelBiFlowNode

# TODO: maybe test the helper classes as well, e.g. the new callable

class TestParallelBiNode(unittest.TestCase):
    
    def test_stop_message_attribute(self):
        """Test that the stop_result attribute is present in forked node."""
        stop_result = ({"test": "blabla"}, "node123")
        x = n.random.random([100,10])
        node = SFABiNode(stop_result=stop_result)
        try:
            mdp.activate_extension("parallel")
            node2 = node.fork()
            node2.train(x)
            forked_result = node2.stop_training()
            self.assert_(forked_result == stop_result)
            # same with derived sfa2 node
            node = SFA2BiNode(stop_result=stop_result)
            mdp.activate_extension("parallel")
            node2 = node.fork()
            node2.train(x)
            forked_result = node2.stop_training()
            self.assert_(forked_result == stop_result)
        finally:
            mdp.deactivate_extension("parallel")
        
        
class TestParallelBiFlow(unittest.TestCase):
    
    def test_nonparallel_flow(self):
        """Test a ParallelBiFlow with standard nodes."""
        flow = ParallelBiFlow([mdp.nodes.SFANode(output_dim=5),
                               mdp.nodes.PolynomialExpansionNode(degree=3),
                               mdp.nodes.SFANode(output_dim=20)])
        data_iterables = [[n.random.random((20,10)) for _ in range(6)], 
                          None, 
                          [n.random.random((20,10)) for _ in range(6)]]
        scheduler = mdp.parallel.Scheduler()
        flow.train(data_iterables, scheduler=scheduler)
        x = n.random.random([100,10])
        flow.execute(x)
        iterator = [n.random.random((20,10)) for _ in range(6)]
        flow.execute(iterator, scheduler=scheduler)
        scheduler.shutdown()
        
    def test_mixed_parallel_flow(self):
        """Test a ParallelBiFlow with both standard and BiNodes."""
        flow = ParallelBiFlow([mdp.nodes.PCANode(output_dim=8),
                               SFABiNode(output_dim=5),
                               SFA2BiNode(output_dim=20)])
        data_iterables = [[n.random.random((20,10)) for _ in range(6)]] * 3
        scheduler = mdp.parallel.Scheduler()
        flow.train(data_iterables, scheduler=scheduler)
        x = n.random.random([100,10])
        flow.execute(x)
        iterator = [n.random.random((20,10)) for _ in range(6)]
        flow.execute(iterator, scheduler=scheduler)
        scheduler.shutdown()

    def test_parallel_multiphase(self):
        """Test training and execution with multiple training phases.
        
        The node with multiple training phases is a hinet.FlowNode. 
        """
        sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
        sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
        flownode = ParallelBiFlowNode(BiFlow([sfa_node, sfa2_node]))
        flow = ParallelBiFlow([flownode,
                               mdp.nodes.PolynomialExpansionNode(degree=2),
                               mdp.nodes.SFANode(output_dim=5)])
        data_iterables = [[n.random.random((30,10)) for _ in range(6)], 
                          None, 
                          [n.random.random((30,10)) for _ in range(6)]]
        scheduler = mdp.parallel.Scheduler()
        flow.train(data_iterables, scheduler=scheduler)
        x = n.random.random([100,10])
        flow.execute([x for _ in range(4)], scheduler=scheduler)
        flow.execute(x)
        iterator = [n.random.random((20,10)) for _ in range(6)]
        flow.execute(iterator, scheduler=scheduler)
        scheduler.shutdown()
        
    def test_parallel_process(self):
        """Test training and execution with multiple training phases.
        
        The node with multiple training phases is a hinet.FlowNode. 
        """
        sfa_node = mdp.nodes.SFANode(input_dim=10, output_dim=8)
        sfa2_node = mdp.nodes.SFA2Node(input_dim=8, output_dim=6)
        flow = ParallelBiFlow([sfa_node, sfa2_node])
        data_iterables = [[n.random.random((30,10)) for _ in range(6)], 
                          [n.random.random((30,10)) for _ in range(7)]]
        scheduler = mdp.parallel.ProcessScheduler(n_processes=2)
        flow.train(data_iterables, scheduler=scheduler)
        flow.execute(data_iterables[1], scheduler=scheduler)
        x = n.random.random([100,10])
        flow.execute(x)
        iterator = [n.random.random((20,10)) for _ in range(6)]
        flow.execute(iterator, scheduler=scheduler)
        scheduler.shutdown()
        

def get_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestParallelBiNode))
    suite.addTest(unittest.makeSuite(TestParallelBiFlow))
    return suite
            
if __name__ == '__main__':
    unittest.main() 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.