示例#1
0
文件: ga.py 项目: fdurupinar/sOED
    def __init__(self, max_generations, population_size, antibody_cnt, nc_cnt, c_cnt, markers_list,  cell_cnt,
                 mu_list, sigma_list):

        self.max_generations = max_generations
        self.population_size = population_size
        self.antibody_cnt = antibody_cnt
        self.nc_cnt = nc_cnt
        self.c_cnt = c_cnt

        # fill with integers
        self.population = np.full((max_generations, population_size, 4), 0,  dtype=np.int)

        self.fitness = {}

        self.total_population = population_size

        self.score_handler = ScoreHandler(antibody_cnt, nc_cnt, c_cnt, markers_list, cell_cnt,
                                          mu_list, sigma_list)

        self.cross_over_indices_2_point = StaticMethods.generate_cross_over_indices_2_point(4)
        self.cross_over_indices_1_point = StaticMethods.generate_cross_over_indices_1_point(4)

        self.max_possible_fitness = self.score_handler.compute_max_possible_precision(MARKERS_LIST[0])

        self.plot_methods = PlotMethods(self)
        print "Maximum possible fitness value is:"
        print self.max_possible_fitness
示例#2
0
    def test_get_ab_key(self):

        key = StaticMethods.get_ab_key(np.array([1, 2]))
        self.assertEqual(key, "1-2")

        key = StaticMethods.get_ab_key(np.array([]))
        self.assertEqual(key, "")

        key = StaticMethods.get_ab_key(np.array([1, 3, 4]))
        self.assertEqual(key, "1-3-4")
示例#3
0
    def _draw_intersection_ratio(self, patient, marker_cnt_arr):
        """
        Draw an intersection value from a gaussian distribution
        :param marker_ratios:
        :return:
        """

        pdf = StaticMethods.create_intersection_distribution(marker_cnt_arr, patient.cell_cnt)
        val = float(StaticMethods.draw_intersection_value(pdf)) / patient.cell_cnt
        return val
示例#4
0
    def test_find_intersection_probability(self):

        val0 = StaticMethods.find_intersection_probability(70, 80, 40, 100)
        val1 = StaticMethods.find_intersection_probability(70, 80, 50, 100)
        val2 = StaticMethods.find_intersection_probability(70, 80, 70, 100)
        val3 = StaticMethods.find_intersection_probability(70, 80, 60, 100)

        self.assertEqual(val0, 0)
        self.assertGreater(val1, 0)
        self.assertEqual(val2, 1.0)
        self.assertGreater(val3, 0)
        self.assertLess(val3, 1.0)
示例#5
0
文件: ga.py 项目: fdurupinar/sOED
    def get_fitness_value(self, child):
        """
        Return fitness value for child
        :param child: sorted np array of 4
        :return:
        """
        val = 0

        if StaticMethods.get_ab_key(child) in self.fitness:
            val = self.fitness[StaticMethods.get_ab_key(child)]

        return val
示例#6
0
    def get_marker_count(self, ab_list, to_be_recorded):
        """
        For all the cells, count the ones with all the markers in ab_seq and return their ratios
        :param ab_list: Is in format [A,B,C] : means antibodies A, B, and C are present
        :param to_be_recorded:
        :return:
        """

        key = StaticMethods.get_ab_key(ab_list)
        if key in self.marker_cnt:
            return self.marker_cnt[key]

        containing_cell_cnt = 0
        for c in self.cells:
            cnt_pres = 0
            for ab in ab_list:
                if int(c[ab]) == 1:
                    cnt_pres += 1
            if cnt_pres >= len(ab_list):
                containing_cell_cnt += 1

        cnt = float(containing_cell_cnt)

        if to_be_recorded:
            self.marker_cnt[key] = cnt
        return cnt
示例#7
0
    def compute_max_precision_for_ab_combination(self, ab_arr):
        """
        Compute all the scores for 16 combinations and find the maximum
        :param ab_arr: 4 antibodies listed in an unsorted way
        :return: precision of ab combination
        """

        # TODO updated
        # ab_combinations = StaticMethods.get_unique_combinations(ab_arr)
        #
        # max_prec = -1000
        # for comb in ab_combinations:
        #     comb = np.sort(comb)
        #     prec = self._compute_precision_for_ab_list(comb)
        #     if prec > max_prec:
        #         max_prec = prec
        # return max_prec
        #
        ab_combinations = StaticMethods.get_unique_combinations(ab_arr)

        # total_prec = 0
        # prec_cnt = 0
        max_prec = -1000
        for comb in ab_combinations:
            comb = np.sort(comb)
            prec = self._compute_precision_for_ab_list(comb)
            if prec > max_prec:
                max_prec = prec

        return max_prec
示例#8
0
    def is_measured(self, ab_arr):
        """
        Check of ab_arr is measured already
        :param ab_arr: sorted antibodies np array. Can be any length.
        :return:
        """

        key = StaticMethods.get_ab_key(ab_arr)
        return key in self.measured_dict
示例#9
0
文件: ga.py 项目: fdurupinar/sOED
    def survive_n_fittest(self, generation, n):
        """Find and survive the n fittest combinations in the population-- will overwrite the rest"""

        # fitness = np.zeros(len(self.population[generation]))
        sorted_fitness = sorted(self.fitness.items(), key=operator.itemgetter(1))
        len_fitness = len(sorted_fitness) - 1

        for i in range(n):
            self.population[generation + 1][i] = StaticMethods.key_to_ab(sorted_fitness[len_fitness - i][0])
示例#10
0
    def _add_measured(self, ab_arr):
        """
        After sorting it, add ab_arr to the measured list if it doesn't exist already
        :param ab_arr: Can be of any length and unsorted
        :return:
        """

        ab_arr = np.sort(ab_arr)

        key = StaticMethods.get_ab_key(ab_arr)
        self.measured_dict[key] = 1
示例#11
0
    def compute_is_precise_for_ab_combination(self, ab_arr):
        ab_combinations = StaticMethods.get_unique_combinations(ab_arr)

        is_prec = 0
        for comb in ab_combinations:
            comb = np.sort(comb)
            if self.compute_is_precise_for_ab_list(comb):
                is_prec = 1


        return is_prec
示例#12
0
文件: ga.py 项目: fdurupinar/sOED
    def update_fitness(self, child):
        """
        Compute the fitness of antibody quadruple based on existing values
        :param child:
        :return:
        """

        score = self.score_handler.compute_max_precision_for_ab_combination(child)

        # print str(child) + " " + str(score)
        key = StaticMethods.get_ab_key(child)

        self.fitness[key] = score
示例#13
0
    def test_get_unique_combinations(self):

        ab_arr = np.array([4, 2, 1, 3])

        combs = StaticMethods.get_unique_combinations(ab_arr)

        # there are 16 combinations of an array of length 4
        self.assertEqual(len(combs), 16)

        self.assertTrue([1, 2] in combs)
        self.assertTrue([1, 2, 3, 4] in combs)
        self.assertTrue([] in combs)
        self.assertTrue([1, 3] in combs)
        self.assertTrue([1] in combs)
        self.assertTrue([2, 3, 4] in combs)

        self.assertFalse([2, 1] in combs, "should be sorted")
示例#14
0
    def compute_max_possible_precision(self, ab_arr):
        ab_combinations = StaticMethods.get_unique_combinations(ab_arr)

        print "Marker precisions"

        max_prec = -1000
        # total_prec = 0
        # prec_cnt = 0
        for comb in ab_combinations:
            comb = np.sort(comb)
            prec = self._compute_unmeasured_precision_for_ab_list(comb)
            if prec > max_prec:
                max_prec = prec
            # total_prec += prec
            # if prec > 0:
            #     prec_cnt += 1

            str_comb = ', '.join(str(i) for i in comb)
            print '[' + str_comb + "] " + str(prec)
            # if prec > max_prec:
            #     max_prec = prec
        return max_prec
示例#15
0
    def test_generate_cross_over_indices_1_point(self):

        inds = StaticMethods.generate_cross_over_indices_1_point(4)

        self.assertEqual(len(inds), 16)