def test_distances_by_groups(self):
        """distances_by_groups should return correct result.
        """
        exp = [\
            ['Control','Fast',array([[0.729,  0.8  ,  0.721, 0.765],
                                    [0.776,  0.744,  0.749, 0.677],
                                    [0.734,  0.777,  0.733, 0.724],
                                    [0.696,  0.675,  0.654, 0.696],
                                    [0.731,  0.758,  0.738, 0.737]])],\
            ['Control','Control', array([0.625,  0.623,  0.61 ,  0.577, 0.615,
                                        0.642,  0.673, 0.682,  0.737, 0.704])],\
            ['Fast','Fast', array([0.718,  0.666, 0.727, 0.6, 0.578, 0.623])]
        ]

        obs = distances_by_groups(self.distance_header,self.dmat,\
            self.treatment_groups)
        self.assertEqual(obs, exp)
 def test_distances_by_groups(self):
     """distances_by_groups should return correct result.
     """
     exp = [\
         ['Control','Fast',array([[0.729,  0.8  ,  0.721, 0.765],
                                 [0.776,  0.744,  0.749, 0.677],
                                 [0.734,  0.777,  0.733, 0.724],
                                 [0.696,  0.675,  0.654, 0.696],
                                 [0.731,  0.758,  0.738, 0.737]])],\
         ['Control','Control', array([0.625,  0.623,  0.61 ,  0.577, 0.615,
                                     0.642,  0.673, 0.682,  0.737, 0.704])],\
         ['Fast','Fast', array([0.718,  0.666, 0.727, 0.6, 0.578, 0.623])]
     ]
     
     obs = distances_by_groups(self.distance_header,self.dmat,\
         self.treatment_groups)
     self.assertEqual(obs,exp)
    def setUp(self):
        """setup data function for DistanceHistogramsTests."""
        self.working_dir = '/tmp/distance_histogram_tests/'
        try:
            mkdir(self.working_dir)
        except OSError:  #except already exisits
            pass

        self.histogram_dir = self.working_dir + 'histograms/'
        try:
            mkdir(self.histogram_dir)
        except OSError:  #except already exisits remove it and make a new one
            pass

        #Create distance matrix file
        self.dmat_file = self.working_dir + 'dmat.txt'
        dmat_out = open(self.dmat_file, 'w')
        dmat_out.write(DISTANCE_MATRIX_STRING)
        dmat_out.close()

        self.distance_header, self.dmat = \
            parse_distmat(open(self.dmat_file,'U'))

        #Create mapping file
        self.map_file = self.working_dir + 'map.txt'
        map_out = open(self.map_file, 'w')
        map_out.write(MAPPING_STRING)
        map_out.close()

        mapping, header, comments = parse_mapping_file(open(
            self.map_file, 'U'))
        header[0] = '#' + header[0]
        header = [header]
        header.extend(mapping)
        self.mapping = header

        #Create prefs file
        self.prefs_file = self.working_dir + 'prefs.txt'
        prefs_out = open(self.prefs_file, 'w')
        prefs_out.write(str(PREFS))
        prefs_out.close()

        #Build single field dict for 'Treatment' field.
        self.single_field_treatment = defaultdict(dict)
        self.treatment_groups = group_by_field(self.mapping, 'Treatment')
        self.single_field_treatment['Treatment'] = \
            distances_by_groups(self.distance_header,self.dmat,\
                self.treatment_groups)
        self.paired_field_treatment = {'Treatment_to_Treatment':[\
            [('Control','Control'),('Fast','Fast'),\
                             array([[0.729,  0.8  ,  0.721, 0.765],
                                    [0.776,  0.744,  0.749, 0.677],
                                    [0.734,  0.777,  0.733, 0.724],
                                    [0.696,  0.675,  0.654, 0.696],
                                    [0.731,  0.758,  0.738, 0.737]])],\
            [('Control','Control'),('Control','Control'),\
                                 array([0.625,  0.623,  0.61 ,  0.577, 0.615,
                                        0.642,  0.673, 0.682,  0.737, 0.704])],\
            [('Fast','Fast'),('Fast','Fast'),\
                             array([0.718,  0.666, 0.727, 0.6, 0.578, 0.623])]
        ]}

        self.distances_file = self.working_dir + 'distances_out.txt'
        dist_out = open(self.distances_file, 'w')
        dist_out.write(DISTANCES_OUT)
        dist_out.close()
    def setUp(self):
        """setup data function for DistanceHistogramsTests."""
        self.working_dir = '/tmp/distance_histogram_tests/'
        try:
            mkdir(self.working_dir)
        except OSError: #except already exisits
            pass
        
        self.histogram_dir = path.join(self.working_dir,'histograms')
        try:
            mkdir(self.histogram_dir)
        except OSError: #except already exisits remove it and make a new one
            pass
            
        #Create distance matrix file
        self.dmat_file = self.working_dir+'dmat.txt'
        dmat_out = open(self.dmat_file,'w')
        dmat_out.write(DISTANCE_MATRIX_STRING)
        dmat_out.close()

        self.distance_header, self.dmat = \
            parse_distmat(open(self.dmat_file,'U'))
        
        #Create mapping file
        self.map_file = self.working_dir+'map.txt'
        map_out = open(self.map_file,'w')
        map_out.write(MAPPING_STRING)
        map_out.close()
        
        mapping, header, comments = parse_mapping_file(open(self.map_file,'U'))
        header[0] = '#'+header[0]
        header = [header]
        header.extend(mapping)
        self.mapping=header
        
        #Create prefs file
        self.prefs_file = self.working_dir+'prefs.txt'
        prefs_out = open(self.prefs_file,'w')
        prefs_out.write(str(PREFS))
        prefs_out.close()
        
        #Build single field dict for 'Treatment' field.
        self.single_field_treatment = defaultdict(dict)
        self.treatment_groups = group_by_field(self.mapping, 'Treatment')
        self.single_field_treatment['Treatment'] = \
            distances_by_groups(self.distance_header,self.dmat,\
                self.treatment_groups)
        self.paired_field_treatment = {'Treatment_to_Treatment':[\
            [('Control','Control'),('Fast','Fast'),\
                             array([[0.729,  0.8  ,  0.721, 0.765],
                                    [0.776,  0.744,  0.749, 0.677],
                                    [0.734,  0.777,  0.733, 0.724],
                                    [0.696,  0.675,  0.654, 0.696],
                                    [0.731,  0.758,  0.738, 0.737]])],\
            [('Control','Control'),('Control','Control'),\
                                 array([0.625,  0.623,  0.61 ,  0.577, 0.615,
                                        0.642,  0.673, 0.682,  0.737, 0.704])],\
            [('Fast','Fast'),('Fast','Fast'),\
                             array([0.718,  0.666, 0.727, 0.6, 0.578, 0.623])]
        ]}
        
        self.distances_file = self.working_dir+'distances_out.txt'
        dist_out = open(self.distances_file,'w')
        dist_out.write(DISTANCES_OUT)
        dist_out.close()