Example #1
0
    def __init__(self, data_loader, dose_loader=None):
        """
        Prepare the class for evaluating dose distributions
        :param data_loader: a data loader object that loads data from the reference dataset
        :param dose_loader: a data loader object that loads a dose tensor from any dataset (e.g., predictions)
        """
        # Initialize objects
        self.data_loader = data_loader  # Loads data related to ground truth patient information
        self.dose_loader = dose_loader  # Loads the data for a benchmark dose

        # Initialize objects for later
        self.patient_list = None
        self.roi_mask = None
        self.new_dose = None
        self.reference_dose = None
        self.voxel_size = None
        self.possible_dose_mask = None

        # Set metrics to be evaluated
        self.oar_eval_metrics = ['D_0.1_cc', 'mean']
        self.tar_eval_metrics = ['D_99', 'D_95', 'D_1']

        # Name metrics for data frame
        oar_metrics = list(
            it_product(self.oar_eval_metrics, self.data_loader.rois['oars']))
        target_metrics = list(
            it_product(self.tar_eval_metrics,
                       self.data_loader.rois['targets']))

        # Make data frame to store dose metrics and the difference data frame
        self.metric_difference_df = pd.DataFrame(
            index=self.data_loader.patient_id_list,
            columns=[*oar_metrics, *target_metrics])
        self.reference_dose_metric_df = self.metric_difference_df.copy()
        self.new_dose_metric_df = self.metric_difference_df.copy()
Example #2
0
def combine_transitions(ctrl):
    """ Combine parallell transitions when they are independent of environment actions
    Parameters
    ----------
        ctrl:  mealy machine

    """
    ctrl_copy = ctrl.copy()

    for c_state in ctrl_copy.nodes():
        for post_s in ctrl_copy.states.post(c_state):
            logger.info('(' + str(c_state) + ')' + '(' + str(post_s) + ')')
            labels = [
                set(label.items())
                for (x, y,
                     label) in ctrl_copy.transitions.find({c_state}, {post_s})
            ]
            min_set = set.intersection(*labels)
            labels_mins = [lab - min_set for lab in labels]
            if set.union(*labels_mins) == set():
                continue

            list_in = [
                set(it_product({key}, values))
                for (key, values) in ctrl_copy.inputs.items()
                if (not values == {0, 1})
                & (set(it_product({key}, values)) <= set.union(*labels_mins))
            ] + [
                set(it_product({key}, {True, False}))
                for (key, values) in ctrl_copy.inputs.items() if
                ((values == {0, 1})
                 & (set(it_product({key}, values)) <= set.union(*labels_mins)))
            ]

            labels_updated = labels.copy()
            for list_el in list_in:
                for label in labels_updated:
                    label_gen = [(label - list_el) | {el_set}
                                 for el_set in list_el]
                    if all([
                            any([
                                label_gen_el == labels_el
                                for labels_el in labels_updated
                            ]) for label_gen_el in label_gen
                    ]):
                        labels_updated = set(
                            frozenset(labels_el)
                            for labels_el in labels_updated if not any([
                                label_gen_el == labels_el
                                for label_gen_el in label_gen
                            ]))
                        labels_updated |= {frozenset((label - list_el))}
            ctrl_copy.transitions.remove_from(
                ctrl_copy.transitions.find({c_state}, {post_s}))
            for labels_updated_el in labels_updated:
                ctrl_copy.transitions.add(c_state, post_s,
                                          dict(set(labels_updated_el)))
    return ctrl_copy
Example #3
0
 def pdq_maker(order_limit=2, s_order_limit=1, period=12):
     """generate all possible SARIMA orders for range"""
     p = d = q = range(0, order_limit)
     P = D = Q = range(0, s_order_limit)
     # Generate all different combinations of pdq/PDQ triplets
     pdq = list(it_product(p, d, q))
     seasonal_PDQ = [(x[0], x[1], x[2], period)
                     for x in list(it_product(p, d, q))]
     return (pdq, seasonal_PDQ)
Example #4
0
    def build(self, build_data):
        self.rects.clear()
        self.rects.append(
            Rect(1, 1, build_data.map.width - 2, build_data.map.height - 2))
        first_room = self.rects[0]
        self.add_subrects(first_room)

        rooms = list()
        rooms_copy = copy.deepcopy(self.rects)
        '''
        for room in rooms_copy:
            rooms.append(room)
            for y in range(room.y1, room.y2):
                for x in range(room.x1, room.x2):
                    idx = build_data.map.xy_idx(x, y)
                    if 0 < idx < ((build_data.map.width * build_data.map.height) - 1):
                        build_data.map.tiles[idx] = TileType.FLOOR
            build_data.take_snapshot()
        '''
        for room in rooms_copy:
            rooms.append(room)
            for x, y in it_product(range(room.x1, room.x2),
                                   range(room.y1, room.y2)):
                idx = build_data.map.xy_idx(x, y)
                if 0 < idx < (
                    (build_data.map.width * build_data.map.height) - 1):
                    build_data.map.tiles[idx] = TileType.FLOOR
            build_data.take_snapshot()

        build_data.rooms = rooms
Example #5
0
    def trials(self, period: int = 1) -> Iterator[ActionsTrial]:
        """
        Generate a sequence of ActionsTrial objects, representing all the
        different combinations of Action that could be tried, given the
        constraints of `allow_no_action` and `allow_multiple_actions`.

        :param period: The period in which the action is being tried.
        """
        for try_each_action in it_product(*[[False, True]
                                            for _ in range(self.num_actions)]):
            # dont allow zero-action trials if not allowed
            if not self.allow_no_action and all(
                [try_action is False for try_action in try_each_action]):
                continue
            # don't allow multi-action trials if not allowed
            if not self.allow_multiple_actions and sum(
                [try_action is True for try_action in try_each_action]) > 1:
                continue

            if (
                    # all actions can be tried
                    self.can_try_action is None
                    # or all actions we want to try can be tried
                    or all(
                        self.can_try_action(action, period) is True
                        or try_action is False for action, try_action in zip(
                            self.actions, try_each_action))):
                yield ActionsTrial(
                    *(AvailableAction(action=action, try_action=try_action)
                      for action, try_action in zip(self.actions,
                                                    try_each_action)))
            else:
                continue
Example #6
0
    def is_possible(self, rect, gmap, rooms):
        expanded = copy.deepcopy(rect)
        expanded.x1 -= 2
        expanded.x2 += 2
        expanded.y1 -= 2
        expanded.y2 += 2
        can_build = True

        for room in rooms:
            if room.intersect(rect):
                can_build = False
        '''
        for y in range(expanded.y1, expanded.y2):
            for x in range(expanded.x1, expanded.x2):
                if x > gmap.width - 2 or y > gmap.height - 2 or x < 1 or y < 1:
                    can_build = False

                if can_build:
                    idx = gmap.xy_idx(x, y)
                    if gmap.tiles[idx] != TileType.WALL:
                        can_build = False
        '''
        for x, y in it_product(range(expanded.x1, expanded.x2),
                               range(expanded.y1, expanded.y2)):
            if x > gmap.width - 2 or y > gmap.height - 2 or x < 1 or y < 1:
                can_build = False

            if can_build:
                idx = gmap.xy_idx(x, y)
                if gmap.tiles[idx] != TileType.WALL:
                    can_build = False

        return can_build
    def build_initial_map(self, build_data):
        '''
        for y in range(1, build_data.map.height - 2):
            for x in range(1, build_data.map.width - 2):
                rand = randint(1, 100)
                idx = build_data.map.xy_idx(x, y)
                if rand > 55:
                    build_data.map.tiles[idx] = TileType.FLOOR
                else:
                    build_data.map.tiles[idx] = TileType.WALL
        '''

        for x, y in it_product(range(1, build_data.map.width - 2),
                               range(1, build_data.map.height - 2)):
            rand = randint(1, 100)
            idx = build_data.map.xy_idx(x, y)
            if rand > 55:
                build_data.map.tiles[idx] = TileType.FLOOR
            else:
                build_data.map.tiles[idx] = TileType.WALL

        build_data.take_snapshot()

        for _i in range(0, 15):
            self.apply_iteration(build_data)
Example #8
0
def remove_aux_inputs(ctrl, inputs):

    #1. check whether you are allowed to remove the aux inputs. <= not done
    #2. remove aux. inputs.

    ctrl_new = transys.MealyMachine()
    ctrl_new.add_outputs(ctrl.outputs)

    # this needs to be changed to be a limited set
    inputs_dict = dict()
    for i in inputs:
        inputs_dict[i] = ctrl.inputs[i]
    ctrl_new.add_inputs(inputs_dict)

    # add nodes from original mealy
    ctrl_new.add_nodes_from(ctrl.nodes())
    block_pairs = it_product(ctrl, ctrl)
    for (b, c) in block_pairs:
        labels = {
            frozenset([(key, label[key]) for key in ctrl_new.inputs.keys()] +
                      [(output, label[output])
                       for output in ctrl_new.outputs.keys()])
            for (x, y, label) in ctrl.transitions.find(b, c)
        }
        for q in labels:
            ctrl_new.transitions.add(b, c, **dict(q))

    ctrl_new.states.initial.add_from(ctrl.states.initial)

    return ctrl_new
Example #9
0
def _add_tests():
    _pair = ['mem', 'dsk']

    _template = '''
def test_{1}_{2}_to_{3}{0}(self):
    Rd = {0}(self.{1}A, self.{2}B)
    self.assert_(isinstance(Rd, blaze.Array))
    self.assert_(Rd._data.deferred)
    p = _store('{3}Rd') if '{3}' == 'dsk' else None
    try:
        Rc = blaze.eval(Rd, storage=p)
        self.assert_(isinstance(Rc, blaze.Array))
        assert_allclose(np.array(dd_as_py(Rc._data)), self.npy{4})
        self.assert_(Rc._data.persistent if '{3}' == 'dsk'
                                         else not Rc._data.persistent)
    finally:
        if p is not None:
            blaze.drop(p)
'''
    frame = sys._getframe(1)
    for expr, ltr in zip(['_addition', '_expression'], ['R', 'Q']):
        for i in it_product(_pair, _pair, _pair):
            args = i + (ltr,)
            exec_(_template.format(expr,*args),
                  frame.f_globals,
                  frame.f_locals)
Example #10
0
def iterate_equiv(q_blocks, ctrl, outputs={'loc'}):
    """ Iterate the equivalence classes
    Parameters
    ----------
    q_blocks : equivalence classes
    ctrl :  mealy machine
    outputs :  Tells which outputs are critical and should be kept. Given as a set of strings.
    """
    dict__r = dict(
        sum([
            list(it_product(block, {i})) for (i, block) in enumerate(q_blocks)
        ], []))

    blocks = []
    # Determine the equivalence class for each element of the iterable.
    for y in ctrl.states():
        #  Each element y must be in *exactly one* equivalence class.
        #
        # Each block is guaranteed to be non-empty
        if y in ctrl.states.initial:
            blocks.append(
                [y]
            )  # We don't want to group in the initial state. Because that will give issues witht he autocoding.
        else:
            for block in blocks:
                x = next(iter(block))

                if len(ctrl[x]) != len(ctrl[y]):
                    #  print('unequal number')
                    continue

                # compute set of labels:
                labels_x = {
                    frozenset([(key, label[key])
                               for key in ctrl.inputs.keys()] +
                              [(output, label[output]) for output in outputs] +
                              [('Relx', dict__r[_x])] +
                              [('Rely', dict__r[_y])])
                    for (_x, _y, label) in ctrl.transitions.find({x})
                }
                labels_y = {
                    frozenset([(key, label[key])
                               for key in ctrl.inputs.keys()] +
                              [(output, label[output]) for output in outputs] +
                              [('Relx', dict__r[_x])] +
                              [('Rely', dict__r[_y])])
                    for (_x, _y, label) in ctrl.transitions.find({y})
                }

                if labels_x == labels_y:
                    block.append(y)
                    break
            else:
                # If the element y is not part of any known equivalence class, it
                # must be in its own, so we create a new singleton equivalence
                # class for it.
                blocks.append([y])
    return {frozenset(block) for block in blocks}
Example #11
0
def _add_tests():
    _pair = ['mem', 'dsk']
    frame = sys._getframe(1)
    for expr, ltr in zip([_addition, _expression], ['R', 'Q']):
        for i in it_product(_pair, _pair, _pair):
            args = i + (ltr, )
            f = _build_tst(expr, *args)
            f.__name__ = 'test_{1}_{2}_to_{3}{0}'.format(f.__name__, *args)
            frame.f_locals[f.__name__] = f
Example #12
0
def _add_tests():
    _pair = ['mem', 'dsk']
    frame = sys._getframe(1)
    for expr, ltr in zip([_addition, _expression], ['R', 'Q']):
        for i in it_product(_pair, _pair, _pair):
            args = i + (ltr,)
            f = _build_tst(expr, *args)
            f.__name__ = 'test_{1}_{2}_to_{3}{0}'.format(f.__name__, *args)
            frame.f_locals[f.__name__] = f
Example #13
0
def spawn_room(room, current_map, spawn_list):
    possible_targets = []
    for x, y in it_product(range(room.x1, room.x2 + 1),
                           range(room.y1, room.y2 + 1)):
        idx = current_map.xy_idx(x, y)
        if current_map.tiles[idx] == TileType.FLOOR:
            possible_targets.append(idx)

    spawn_region(possible_targets, current_map, spawn_list)
Example #14
0
def _chunk_split(dims, chunk_size):
    # returns for each chunk its "index" as well as its actual chunk size.
    if len(dims) > 0:
        helper_it = it_product(*([xrange(x) for x in dims[:-1]] +
                                 [_chunk_size_gen(dims[-1], chunk_size)]))

        for i in helper_it:
            yield i[:-1]+(i[-1][0],), i[-1][1]
    else:
        yield tuple(), 1
Example #15
0
 def check_for_greetings(sentence):
     greeting_words = self.bot_data.get_greetings()
     greeting_response = self.bot_data.get_greet_resp()
     for word, greet in it_product(
             str(sentence).split(" ")[:2], greeting_words):
         if word.startswith(greet):
             if len(str(sentence).split(" ")) < 2:
                 return 1.0, random.choice(greeting_response)
             else:
                 return 0.6, random.choice(greeting_response)
     return 0.0, None
Example #16
0
 def run_write(self, dd, chunk_size=1):
     f = self.cfunc
     arg_s = self.arg_structs
     r = self.readers
     dst = dd.element_writer(len(self.outer_dims))
     for element in it_product(*[xrange(x) for x in self.outer_dims]):
         for struct, reader, typ in izip(arg_s[:-1], r, self.c_types[:-1]):
             self._patch(struct,
                         reader.read_single(element, chunk_size), typ)
         with dst.buffered_ptr(element, chunk_size) as dst_buf:
             self._patch(arg_s[-1], dst_buf, self.c_types[-1])
             f(*arg_s)
Example #17
0
 def draw_rectangle(self, gmap, room):
     '''
     for y in range(room.y1, room.y2):
         for x in range(room.x1, room.x2):
             idx = gmap.xy_idx(x, y)
             if 0 < idx < ((gmap.width * gmap.height) - 1):
                 gmap.tiles[idx] = TileType.FLOOR
     '''
     for x, y in it_product(range(room.x1, room.x2),
                            range(room.y1, room.y2)):
         idx = gmap.xy_idx(x, y)
         if 0 < idx < ((gmap.width * gmap.height) - 1):
             gmap.tiles[idx] = TileType.FLOOR
Example #18
0
 def run_write(self, dd):
     f = self.cfunc
     arg_s = self.arg_structs
     r = self.readers
     dst = dd.element_writer(len(self.outer_dims))
     for element in it_product(*[xrange(x) for x in self.outer_dims]):
         for struct, reader in izip(arg_s[:-1], r):
             struct.e0 = ctypes.cast(reader.read_single(element),
                                     ctypes.POINTER(struct.e0._type_))
         with dst.buffered_ptr(element) as buf:
             arg_s[-1].e0 = ctypes.cast(buf,
                                        ctypes.POINTER(arg_s[-1].e0._type_))
             f(*arg_s)
Example #19
0
    def apply_sectionnal(self, build_data):
        # New section coords
        chunk_x = 0
        if type(self.template.placement[0]) is not HorizontalPlacement:
            print(
                f'placement should be a tuple, with Horizontal as first option'
            )
            raise SyntaxError
        elif self.template.placement[0] == HorizontalPlacement.LEFT:
            chunk_x = 0
        elif self.template.placement[0] == HorizontalPlacement.CENTER:
            chunk_x = (build_data.map.width // 2) - (self.template.width // 2)
        elif self.template.placement[0] == HorizontalPlacement.RIGHT:
            chunk_x = (build_data.map.width - 1) - self.template.width

        chunk_y = 0
        if type(self.template.placement[1]) is not VerticalPlacement:
            print(
                f'placement should be a tuple, with Horizontal as first option'
            )
            raise SyntaxError
        elif self.template.placement[1] == VerticalPlacement.TOP:
            chunk_y = 0
        elif self.template.placement[1] == VerticalPlacement.CENTER:
            chunk_y = (build_data.map.height // 2) - (self.template.height //
                                                      2)
        elif self.template.placement[1] == VerticalPlacement.BOTTOM:
            chunk_y = (build_data.map.height - 1) - self.template.height

        self.apply_previous_iteration(chunk_x, chunk_y, build_data)

        string_vec = self.template.template
        string_vec = string_vec.replace('\n', '').replace('\r', '')
        '''
        i = 0
        for y in range(0, self.template.height):
            for x in range(0, self.template.width):
                if x < build_data.map.width and y < build_data.map.height:
                    idx = build_data.map.xy_idx(x + chunk_x, y + chunk_y)
                    self.char_to_map(string_vec[i], idx, build_data)
                i += 1
        build_data.take_snapshot()
        '''
        i = 0
        for x, y in it_product(range(0, self.template.width),
                               range(0, self.template.height)):
            if x < build_data.map.width and y < build_data.map.height:
                idx = build_data.map.xy_idx(x + chunk_x, y + chunk_y)
                self.char_to_map(string_vec[i], idx, build_data)
            i += 1
        build_data.take_snapshot()
Example #20
0
def prune_init(ctrl, init_event=None):
    ctrl_s = synth.determinize_machine_init(ctrl)

    if init_event is not None:
        try:
            keys = list(set(key for (key, val) in list(init_event)))

            inputsb = {env_var: ctrl.inputs[env_var] for env_var in keys}
            # this allows you to give a subset of the inputs

            set_in = set.union(*[
                set(it_product({key}, values))
                for (key, values) in inputsb.items() if not values == {0, 1}
            ] + [
                set(it_product({key}, {True, False}))
                for (key, values) in inputsb.items() if values == {0, 1}
            ])
            if not init_event <= set_in:
                raise ValueError(
                    'The set of initial environment values does not'
                    ' belong to the set of inputs of the mealy machine')
            for s, to, label in ctrl_s.transitions.find({'Sinit'}):
                if not (set.intersection(set(label.items()),
                                         set_in)) <= init_event:
                    ctrl_s.transitions.remove(s, to, attr_dict=label)
            if ctrl_s['Sinit'] is None:
                raise ValueError(
                    'The set of initial environment values does not'
                    ' belong to the set of inputs of the mealy machine.\n'
                    ' All initial transitions were removed.')

        except ValueError as inst:
            print(inst.args)
            print('Determinized Mealy machine,'
                  ' initial transitions have not been pruned.(WARNING)')
            return synth.determinize_machine_init(ctrl)

    return ctrl_s
Example #21
0
def get_aoe_tiles(target, aoe_radius):
    blast_tiles_idx = list()
    current_map = World.fetch('current_map')

    target_x, target_y = target
    # idx = current_map.xy_idx(target_x, target_y)
    radius = aoe_radius // 2

    for x, y in it_product(range(-radius, radius + 1),
                           range(-radius, radius + 1)):
        radius_x = target_x + x
        radius_y = target_y + y
        new_idx = current_map.xy_idx(radius_x, radius_y)
        if not current_map.out_of_bound(new_idx):
            blast_tiles_idx.append(new_idx)

    return blast_tiles_idx
Example #22
0
 def draw_circle(self, gmap, room):
     radius = min(room.x2 - room.x1, room.y2 - room.y1) / 2.0
     center_x, center_y = room.center()
     '''
     for y in range(room.y1, room.y2):
         for x in range(room.x1, room.x2):
             idx = gmap.xy_idx(x, y)
             distance = distance_to(x, y, center_x, center_y)
             if 0 < idx < (gmap.width * gmap.height) - 1 and distance < radius:
                 gmap.tiles[idx] = TileType.FLOOR
     '''
     for x, y in it_product(range(room.x1, room.x2),
                            range(room.y1, room.y2)):
         idx = gmap.xy_idx(x, y)
         distance = distance_to(x, y, center_x, center_y)
         if 0 < idx < (gmap.width * gmap.height) - 1 and distance < radius:
             gmap.tiles[idx] = TileType.FLOOR
Example #23
0
    def build_meta_map(self, build_data):
        noise = tcod.noise.Noise(
            dimensions=2,
            algorithm=tcod.NOISE_SIMPLEX,
            implementation=tcod.noise.TURBULENCE,
            hurst=0.5,
            lacunarity=2.0,
            octaves=4,
            seed=None
        )

        '''
        noise_areas = dict()
        for y in range(0, build_data.map.height):
            for x in range(0, build_data.map.width):
                if build_data.map.tiles[build_data.map.xy_idx(x, y)] == TileType.FLOOR:
                    # score between 0.99 & 0.5 : 550 at >0.9, 1200 at >8, 0 at > 6 and 200 at < 6.
                    cell_value = noise.get_point(x, y)
                    cell_value_int = int(cell_value * 10)  # so we have enought for 10 areas.
                    if cell_value_int not in noise_areas:
                        noise_areas[cell_value_int] = list()
                    noise_areas[cell_value_int].append(build_data.map.xy_idx(x, y))
        '''
        noise_areas = dict()
        for x, y in it_product(range(0, build_data.map.width), range(0, build_data.map.height)):
            if build_data.map.tiles[build_data.map.xy_idx(x, y)] == TileType.FLOOR:
                # score between 0.99 & 0.5 : 550 at >0.9, 1200 at >8, 0 at > 6 and 200 at < 6.
                cell_value = noise.get_point(x, y)
                cell_value_int = int(cell_value * 10)  # so we have enought for 10 areas.
                if cell_value_int not in noise_areas:
                    noise_areas[cell_value_int] = list()
                noise_areas[cell_value_int].append(build_data.map.xy_idx(x, y))

        count = 0
        for key, value in noise_areas.items():
            print(f'area {key} - nb of points : {len(value)} idx')
            count += 1
        print(f'number of areas : {count}')

        for area in noise_areas:
            spawn_region(noise_areas[area], build_data.map, build_data.spawn_list)
Example #24
0
    def load_ascii_map(self, build_data):
        string_vec = self.template.template
        string_vec = string_vec.replace('\n', '').replace('\r', '')
        '''
        i = 0
        for y in range(0, self.template.height):
            for x in range(0, self.template.width):
                if x < build_data.map.width and y < build_data.map.height:
                    idx = build_data.map.xy_idx(x, y)
                    self.char_to_map(string_vec[i], idx, build_data)
                i += 1
            if randint(1, 10) == 1:
                build_data.take_snapshot()
        '''

        i = 0
        for x, y in it_product(range(0, self.template.width),
                               range(0, self.template.height)):
            if x < build_data.map.width and y < build_data.map.height:
                idx = build_data.map.xy_idx(x, y)
                self.char_to_map(string_vec[i], idx, build_data)
            i += 1
            if randint(1, 10) == 1:
                build_data.take_snapshot()
Example #25
0
    def apply_room_vaults(self, build_data):
        x = y = 0
        self.apply_previous_iteration(x, y, build_data)

        vault_roll = randint(1, 6)
        if vault_roll < 4:
            return

        # placeholders
        master_vault_list = [
            PrefabRoom(TOTALY_NOT_A_TRAP, 5, 5, 0, 100),
            PrefabRoom(SILLY_SMILE_MAP, 6, 6, 0, 100),
            PrefabRoom(CHECKERBOARD_MAP, 6, 6, 0, 100)
        ]

        # looking for a valid available vault
        possible_vaults = list()
        for vault in master_vault_list:
            if vault.first_depth < build_data.map.depth < vault.last_depth:
                possible_vaults.append(vault)

        used_tiles = dict(
        )  # to add the tiles we overlap with one room, so we dont add another on it.
        nb_vaults = randint(1, 3)
        for j in range(0, nb_vaults):
            if not possible_vaults:
                return
            if len(possible_vaults) == 1:
                vault_index = 0
            else:
                vault_index = randint(1, len(possible_vaults)) - 1
            vault = possible_vaults[vault_index]

            # looking for places to put the vault.
            vault_positions = list()
            idx = 0
            while True:
                x, y = build_data.map.index_to_point2d(idx)

                # in the map?
                if x > 1 and (
                        x +
                        vault.width) < build_data.map.width - 2 and y > 1 and (
                            y + vault.height) < build_data.map.height - 2:
                    possible = True
                    for vy in range(0, vault.height):
                        for vx in range(0, vault.width):
                            idx = build_data.map.xy_idx(vx + x, vy + y)
                            if build_data.map.tiles[idx] is not TileType.FLOOR:
                                possible = False
                            if used_tiles.get(idx):
                                possible = False

                    if possible:
                        vault_positions.append((x, y))

                idx += 1
                if idx >= len(build_data.map.tiles) - 1:
                    break

            # si des positions ont été trouvées
            if vault_positions:
                if len(vault_positions) == 1:
                    pos_index = 0
                else:
                    pos_index = randint(0, len(vault_positions)) - 1
                position = vault_positions[pos_index]
                chunk_x, chunk_y = position

                # char to map incoming
                if self.template.template:
                    print(f'prefab vault : {self.template}')
                    print(
                        f'prefab vault : template : {self.template.template}')
                    string_vec = self.template.template
                    string_vec = string_vec.replace('\n', '').replace('\r', '')
                else:
                    return
                '''
                i = 0
                for y in range(0, vault.height - 1):
                    for x in range(0, vault.width - 1):
                        idx = build_data.map.xy_idx(x + chunk_x, y + chunk_y)
                        self.char_to_map(string_vec[i], idx, build_data)
                        # print(f'string vec is {string_vec} and i - 1 is : {string_vec[i - 1]}')
                        used_tiles[idx] = True
                        i += 1
                build_data.take_snapshot()
                '''
                i = 0
                for x, y in it_product(range(0, vault.width - 1),
                                       range(0, vault.width - 1)):
                    idx = build_data.map.xy_idx(x + chunk_x, y + chunk_y)
                    self.char_to_map(string_vec[i], idx, build_data)
                    # print(f'string vec is {string_vec} and i - 1 is : {string_vec[i - 1]}')
                    used_tiles[idx] = True
                    i += 1
                build_data.take_snapshot()

                # remove spawn in spawn list where spawn in our vault
                spawn_list_to_keep = deepcopy(build_data.spawn_list)
                for i, (spawn_idx,
                        spawn_name) in enumerate(build_data.spawn_list):
                    x, y = build_data.map.index_to_point2d(spawn_idx)
                    # if x and y in vault
                    if chunk_x < x < chunk_x + vault.width and chunk_y < y < chunk_y + vault.height:
                        spawn_list_to_keep.remove(build_data.spawn_list[i])

                build_data.spawn_list = spawn_list_to_keep

            build_data.take_snapshot()
            possible_vaults.remove(vault)
Example #26
0
    def __init__(self, config, net, data_loader, dose_loader=None, TTA_shift_by=4, offset_lists=[np.arange(-3,4)], conv=True, load_cache=True, store_cache=True, cache_dir='', evalbs=128):
        """
        Prepare the class for evaluating dose distributions
        :param data_loader: a data loader object that loads data from the reference dataset
        :param dose_loader: a data loader object that loads a dose tensor from any dataset (e.g., predictions)
        """
        # Initialize objects
        self.config = config
        self.data_loader = data_loader  # Loads data related to ground truth patient information
        self.dose_loader = dose_loader  # Loads the data for a benchmark dose
        self.TTA_shift_by = TTA_shift_by

        # Initialize objects for later
        self.patient_list = None
        self.roi_mask = None
        self.new_dose = None
        self.reference_dose = None
        self.voxel_size = None
        self.possible_dose_mask = None

        # Set metrics to be evaluated
        self.oar_eval_metrics = ['D_0.1_cc', 'mean']
        self.tar_eval_metrics = ['D_99', 'D_95', 'D_1']

        # Name metrics for data frame
        oar_metrics = list(it_product(self.oar_eval_metrics, self.data_loader.dataset.defdataset.rois['oars']))
        target_metrics = list(it_product(self.tar_eval_metrics, self.data_loader.dataset.defdataset.rois['targets']))

        # Make data frame to store dose metrics and the difference data frame
        self.metric_difference_df = pd.DataFrame(index=self.data_loader.dataset.defdataset.patient_id_list,
                                                 columns=[*oar_metrics, *target_metrics])
        self.reference_dose_metric_df = self.metric_difference_df.copy()
        self.new_dose_metric_df = self.metric_difference_df.copy()
                
        if net is not None:
            net.eval()
        with torch.no_grad():
            self.preds = [np.zeros((1, 128, 128, 128)) for i in range(len(data_loader))]
            for off in offset_lists:
                print("Offset list: ", off)
                off_id = make_offset_id(off)
                CACHE_PATH = './preds/{}/{}{}'.format(config.exp_name, cache_dir, off_id)
                if store_cache and not os.path.exists(CACHE_PATH):
                    os.makedirs(CACHE_PATH)
                
                loaded = False
                if load_cache:
                    if len(glob.glob(os.path.join(CACHE_PATH, '*.npy'))) != len(data_loader):
                        print(CACHE_PATH)
                        print('Not found sufficient files for loading offset {}, predicting...'.format(off_id))
                    else:
                        print('Loading offset {} from cache...'.format(off_id))
                        for i in range(len(data_loader)):
                            pat_id = os.path.basename(data_loader.dataset.data_df.loc[i, 'Id'])
                            curpred = np.load(os.path.join(CACHE_PATH, '{}.npy'.format(pat_id))).astype('float32')
                            self.preds[i] += curpred/len(offset_lists)
                        loaded = True
                if not loaded:
                    evalds = EvalDataset(dose_loader.dataset, off)
                    evaldl = DataLoader(evalds, batch_size=1, shuffle=False, num_workers=2)
                    print('Making predictions from network...')
                    for i, img in enumerate(tqdm(evaldl)):
                        curpred = TTAflip(net, img[0], axis=self.config.axis, shift_by=self.TTA_shift_by, conv=conv, evalbs=evalbs)
                        if type(curpred) == torch.Tensor:
                            curpred = curpred.numpy()
                        curpred = np.moveaxis(curpred, 0, config.axis)  # (1, 128, 128, 128)
                        if conv:
                            print('conv...')
                            curpred = convolve(curpred[0], gau_filter3, mode='same')[None, :]
                        
                        if config.resample is not None:
                            voxel_sz = evaldl.dataset.ds.originalvoxelsz[i]
                            resampled_sz = config.resample.copy()
                            resampled_sz[2] = voxel_sz[0,2]
                            curpred = resample(curpred, np.array(resampled_sz)[None], voxel_sz[0])
                        
                        if store_cache:
                            curpredhalf = curpred.astype('float16')
                            pat_id = os.path.basename(dose_loader.dataset.data_df.loc[i, 'Id'])
                            np.save(os.path.join(CACHE_PATH, '{}.npy'.format(pat_id)), curpredhalf)
                        self.preds[i] += curpred/len(offset_lists)
        print('Done inference! Making metrics...')
    def apply_iteration(self, build_data):
        newtiles = deepcopy(build_data.map.tiles)
        '''
        for y in range(1, build_data.map.height - 2):
            for x in range(1, build_data.map.width - 2):
                idx = build_data.map.xy_idx(x, y)
                neighbors = 0
                if build_data.map.tiles[idx - 1] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx + 1] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx - build_data.map.width] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx + build_data.map.width] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx - (build_data.map.width + 1)] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx - (build_data.map.width - 1)] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx + (build_data.map.width + 1)] == TileType.WALL:
                    neighbors += 1
                if build_data.map.tiles[idx + (build_data.map.width - 1)] == TileType.WALL:
                    neighbors += 1

                if neighbors > 4 or neighbors == 0:
                    newtiles[idx] = TileType.WALL
                else:
                    newtiles[idx] = TileType.FLOOR
        '''

        for x, y in it_product(range(1, build_data.map.width - 2),
                               range(1, build_data.map.height - 2)):
            idx = build_data.map.xy_idx(x, y)
            neighbors = 0
            if build_data.map.tiles[idx - 1] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx + 1] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx -
                                    build_data.map.width] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx +
                                    build_data.map.width] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx - (build_data.map.width +
                                           1)] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx - (build_data.map.width -
                                           1)] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx + (build_data.map.width +
                                           1)] == TileType.WALL:
                neighbors += 1
            if build_data.map.tiles[idx + (build_data.map.width -
                                           1)] == TileType.WALL:
                neighbors += 1

            if neighbors > 4 or neighbors == 0:
                newtiles[idx] = TileType.WALL
            else:
                newtiles[idx] = TileType.FLOOR

        build_data.map.tiles = deepcopy(newtiles)
        build_data.take_snapshot()
Example #28
0
def quotient_mealy(mealy, node_relation=None, relabel=False, outputs={'loc'}):
    """Returns the quotient graph of ``G`` under the specified equivalence
    relation on nodes.

    Parameters
    ----------
    mealy : NetworkX graph
       The graph for which to return the quotient graph with the specified node
       relation.

    node_relation : Boolean function with two arguments
       This function must represent an equivalence relation on the nodes of
       ``G``. It must take two arguments *u* and *v* and return ``True``
       exactly when *u* and *v* are in the same equivalence class. The
       equivalence classes form the nodes in the returned graph.


        unlike the original networkx.quotient_graph selfloops are maintained  

    relabel : Boolean
        if true relabel nodes in the graph 

    outputs :  Tells which outputs are critical and should be kept. Given as a set of strings.

    """
    if node_relation is None:
        node_relation = lambda u, v: mealy.states.post(u) == mealy.states.post(
            v)

    q_mealy = transys.MealyMachine()
    q_mealy.add_inputs(mealy.inputs)
    q_mealy.add_outputs(mealy.outputs)
    # Compute the blocks of the partition on the nodes of G induced by the
    # equivalence relation R.
    if relabel:
        mapping = dict(
            (n, i)
            for (i, n) in enumerate(equivalence_classes(mealy, node_relation)))
        for (n, i) in mapping.items():
            if {'Sinit'} <= set(n):
                mapping[n] = 'Sinit'
        q_mealy.add_nodes_from({n for (i, n) in mapping.items()})
    else:
        q_mealy.add_nodes_from(equivalence_classes(mealy, node_relation))

    if relabel:
        block_pairs = it_product(mapping.keys(), mapping.keys())
        for (b, c) in block_pairs:
            labels = {
                frozenset([(key, label[key]) for key in mealy.inputs.keys()] +
                          [(output, label[output]) for output in outputs])
                for (x, y, label) in mealy.transitions.find(b, c)
            }
            for q in labels:
                q_mealy.transitions.add(mapping[b], mapping[c], **dict(q))
    else:
        block_pairs = it_product(q_mealy, q_mealy)
        for (b, c) in block_pairs:
            labels = {
                frozenset([(key, label[key]) for key in mealy.inputs.keys()] +
                          [(output, label[output]) for output in outputs])
                for (x, y, label) in mealy.transitions.find(b, c)
            }
            for q in labels:
                q_mealy.transitions.add(b, c, **dict(q))

    if relabel:
        for node_eq in mapping.keys():
            if any(init in node_eq for init in mealy.states.initial):
                q_mealy.states.initial.add(mapping[node_eq])
    else:  # only initializing  after relabel
        for node_eq in q_mealy.nodes():
            if any(init in node_eq for init in mealy.states.initial):
                q_mealy.states.initial.add(node_eq)

    return q_mealy
Example #29
0
def reduce_mealy(ctrl,
                 outputs={'ctrl'},
                 relabel=False,
                 prune_set=None,
                 full=True,
                 combine_trans=False,
                 verbose=True):
    """ reduce mealy machines by computing the quotient system of the maximal equivalence class
    Parameters
    ----------
    ctrl:  mealy machine
    outputs :  Tells which outputs are critical and should be kept. Given as a set of strings.
    relabel :  True/False = Relabels nodes (especially needed when ctrl comes with hash like names)
    prune_init :  if set => try 'prune' => remove all transitions that do not belong to the set of allowed initialisations
                    Else determinize

    """

    assert isinstance(prune_set,
                      set) or prune_set is None, 'prune_set is not a set'

    ctrl_s = prune_init(ctrl, init_event=prune_set)
    if verbose:
        print('Original number of states = ' + str(len(ctrl)) + '\n' +
              '    number of transitions = ' +
              str(len(ctrl.transitions.find())))
    it_beh = True
    len_last = len(ctrl_s)
    while it_beh:

        equiv_classes = equiv_alpha(ctrl_s, outputs)

        if verbose: print('Start iterating for maximally coarse bisimulation')
        it = True
        # now you should iterate for maximally coarse
        while it:
            if verbose: print('Number of states = ' + str(len(equiv_classes)))
            equiv_classes_new = iterate_equiv(equiv_classes,
                                              ctrl_s,
                                              outputs=outputs)
            it = (len(equiv_classes_new) != len(equiv_classes))
            equiv_classes = equiv_classes_new

        if verbose: print('Found equivalence classes')

        # now compute quotient system
        equiv_dict = dict(
            sum([
                list(it_product(block, {i}))
                for (i, block) in enumerate(equiv_classes)
            ], []))
        node_rel = lambda u, v: equiv_dict[u] == equiv_dict[
            v]  # the initial relation
        ctrl_s = quotient_mealy(ctrl_s,
                                node_relation=node_rel,
                                relabel=relabel,
                                outputs=outputs)

        if full:
            equiv_classes = reduce_guar_beh(ctrl_s, outputs=outputs)
            equiv_dict = dict(
                sum([
                    list(it_product(block, {i}))
                    for (i, block) in enumerate(equiv_classes)
                ], []))
            node_rel = lambda u, v: equiv_dict[u] == equiv_dict[
                v]  # the initial relation => groups of nodes that can
            # have equal next nodes
            ctrl_s = quotient_mealy(ctrl_s,
                                    node_relation=node_rel,
                                    relabel=relabel,
                                    outputs=outputs)

            if verbose:
                print('Behavioural equivalence reductions \n' +
                      '-  number of states = ' + str(len(ctrl_s)) + '\n' +
                      '-  number of transitions = ' +
                      str(len(ctrl_s.transitions.find())))

        it_beh = ((len(ctrl_s) != len_last) and full)
        len_last = len(ctrl_s)

    if combine_trans:
        ctrl_s = combine_transitions(ctrl_s)
        if verbose:
            print('Combine transitions \n' + '-  number of states = ' +
                  str(len(ctrl_s)) + '\n' + '-  number of transitions = ' +
                  str(len(ctrl_s.transitions.find())))

    return ctrl_s
Example #30
0
def transform2control(trans_sys1,
                      statevar='loc',
                      fullmodel=False,
                      noreach=True):
    """Transform a finite state transition system with labelled nodes to a Transition system whose nodes represent 
    the old edges. 
    Specifically the function translates TransSys1 to TransSys2: 
    TransSys1
        * Nodes =  represent locations or states, owned by environment
        * Edges = control actions by system ending in a new state

    TransSys2:
        * Nodes = Represents a move towards a specific state (owned by environment)
        * Edges = contains Arrival at state (dictated by environment) + decision to move to new state

    @param trans_sys1: FiniteTransitionSystem()
    @param statevar: string name of locations in trans_sys1
    @param fullmodel: give two models, the transformed and simplified and the full model

        """
    #  - - - - Check incoming arguments - - - - - - - -
    #   1 Check whether the given transition system is actually a transition system
    assert isinstance(trans_sys1, transys.FiniteTransitionSystem)
    from tulip.synth import _sprint_aps as print_aps

    # - - - - - Function Body - - - - - - - - - - - - -
    trans_sys2 = transys.FiniteTransitionSystem(
    )  # Define empty transition system
    if trans_sys1.name is None:
        trans_sys2.name = "FTS*"
    else:
        trans_sys2.name = trans_sys1.name + "*"
    trans_sys2.atomic_propositions.add_from({'env_s'})
    # trans_sys2.atomic_propositions.add_from({'env_s', 'ctrl_m'})
    # tagging to who nodes in the graph belong

    try:  # state names are integers
        for s in trans_sys1.states:
            trans_sys2.states.add('%d' % s, ap={'env_s'})
            if s in trans_sys1.states.initial:
                trans_sys2.states.initial |= {'%d' % s}
    except TypeError:
        try:  # state names are strings
            for s in trans_sys1.states:
                trans_sys2.states.add('%s' % s, ap={'env_s'})
                if s in trans_sys1.states.initial:
                    trans_sys2.states.initial |= {'%s' % s}
        except Exception:
            raise logger.debug('State naming gave a failure.')

    # create nodes for the existing transitions in original FTS
    # and add transitions in the new FTS
    for (s1, s2) in trans_sys1.transitions():
        tr12 = 'm%s_%s' % (str(s1), str(s2))
        trans_sys2.states.add(tr12)
        trans_sys2.transitions.add('%s' % str(s1), tr12)
        trans_sys2.transitions.add(tr12, '%s' % str(s2))
        if s1 in trans_sys1.states.initial:
            trans_sys2.states.initial |= {tr12}

    # simplify the obtained two player game (sys= env_s; input =ctrl_m)
    for (s_state,
         label_) in trans_sys2.states.find(with_attr_dict={'ap': {'env_s'}}):
        if trans_sys2.states.post(trans_sys2.states.pre(s_state)) == {s_state}:
            # check whether all states with transitions that enter s_state have only transitions to s_state
            logger.info('Contracting incoming transitions of state %s ' %
                        s_state)
            trans_sys2.states.add('m%s' %
                                  s_state)  # to replace all 'm%s_%s' states

            for node in trans_sys2.states.pre(s_state):
                # print('transition from %s to %s' % (str(trans_sys2.states.pre(node)), s_state))
                trans_sys2.transitions.add_comb(trans_sys2.states.pre(node),
                                                {'m%s' % s_state})
                if node in trans_sys2.states.initial:
                    trans_sys2.states.initial |= {'m%s' % s_state}
                trans_sys2.states.remove(node)
            # TODO : above block can be written more efficient

            trans_sys2.transitions.add('m%s' % s_state, s_state)

    # now the resulting FTS has
    # - states owned by the control system (ctrl_m)
    #    whose outgoing transitions are selected by the system in the environment
    # - states owned by the system(= environment system) (env_s)
    #    whose outgoing transitions are selected by the control system

    # ----------------------------------  Reduce  ----------------------------------
    # for reduced Transitions system create new copy (we do this to later on
    # have the option to use the original 2 play FTS)

    # This only works if the original transition were deterministic
    # We can check this as
    assert all([(len(trans_sys2.states.pre(str(s_state))) == 1)
                for s_state in trans_sys1.states()])

    if noreach:

        trans_red = trans_sys2.copy()
        sys_safe = list(
        )  # list of transitions to be added to the GR(1) specification
        sys_init = list()

        for s_state in trans_sys1.states():
            logger.info('removing state :' + str(s_state))
            labels = trans_sys1.states[s_state]
            _tmp = print_aps(labels, trans_sys1.ap)

            for (tr_state1,
                 tr_state2) in it_product(trans_red.states.pre(str(s_state)),
                                          trans_red.states.post(str(s_state))):
                trans_red.transitions.add(tr_state1, tr_state2)
            for tr_state in trans_red.states.pre(str(s_state)):
                sys_safe += [
                    '((' + statevar + ' = "' + tr_state + '") -> X (' + _tmp +
                    '))'
                ]
            if s_state in trans_sys1.states.initial:
                _goto = ' || '.join([
                    '(' + statevar + ' = "' + str(x) + '" )'
                    for x in trans_red.states.post(str(s_state))
                ])
                sys_init += [_tmp + ' && ' + ' ( ' + _goto + ' ) ']
            trans_red.states.remove(str(s_state))

        # Remove unneeded labels
        trans_red.ap.remove('env_s')

        # Create needed additional specifications
        # -   Environment variables and assumptions
        env_vars = list()
        env_init = list()
        env_safe = list()
        env_prog = list()  # ['(env_actions= "' + reach + '")']

        # -   System variables and requirements
        sys_vars = [x for x in trans_sys1.ap]  # we assign the labels
        #  to the control system (otherwise the GR1 synthesis will blow up the control synthesis)
        sys_prog = list()
        gr_sys = synth.sys_to_spec(trans_red, True, statevar)

        add_specs = gr_sys | spec.GRSpec(env_vars, sys_vars, env_init,
                                         sys_init, env_safe, sys_safe,
                                         env_prog, sys_prog)

    else:

        trans_red = trans_sys2.copy()
        reach = 'reach'
        stay = 'stay'
        sys_safe = list(
        )  # list of transitions to be added to the GR(1) specification
        sys_init = list()

        trans_red.actions['env_actions'] |= {
            reach, stay
        }  # these are the action of the environment
        for s_state in trans_sys1.states():
            logger.info('removing state :' + str(s_state))
            labels = trans_sys1.states[s_state]
            _tmp = print_aps(labels, trans_sys1.ap)

            for (tr_state1,
                 tr_state2) in it_product(trans_red.states.pre(str(s_state)),
                                          trans_red.states.post(str(s_state))):
                trans_red.transitions.add(tr_state1,
                                          tr_state2,
                                          env_actions=reach)
            for tr_state in trans_red.states.pre(str(s_state)):
                trans_red.transitions.add(tr_state, tr_state, env_actions=stay)
                sys_safe += [
                    '(((' + statevar + ' = "' + tr_state +
                    '") && X ( env_actions = "' + reach + '")) -> X (' + _tmp +
                    '))'
                ]
            if s_state in trans_sys1.states.initial:
                _goto = ' || '.join([
                    '(' + statevar + ' = "' + str(x) + '" )'
                    for x in trans_red.states.post(str(s_state))
                ])
                sys_init += [_tmp + ' && ' + ' ( ' + _goto + ' ) ']
            trans_red.states.remove(str(s_state))

        for x in trans_sys1.ap:
            sys_safe += [
                '( !' + str(x) + ' & X ( env_actions = "stay")) -> ( X !' +
                str(x) + ')'
            ]
            sys_safe += [
                '( ' + str(x) + ' & X ( env_actions = "stay")) -> ( X ' +
                str(x) + ')'
            ]

        # Remove unneeded labels
        trans_red.ap.remove('env_s')

        # Create needed additional specifications
        # -   Environment variables and assumptions
        env_vars = list()
        env_init = list()
        env_safe = list()
        env_prog = list()  # ['(env_actions= "' + reach + '")']

        # -   System variables and requirements
        sys_vars = [x for x in trans_sys1.ap]  # we assign the labels
        #  to the control system (otherwise the GR1 synthesis will blow up the control synthesis)
        sys_prog = list()

        add_specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init,
                                env_safe, sys_safe, env_prog, sys_prog)

    if fullmodel:
        return trans_red, add_specs, trans_sys2
    return trans_red, add_specs
Example #31
0

if __name__ == '__main__':
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
 
    operand_datashape = blaze.dshape('10, 10, 10, float64')

    op0 = blaze.empty(operand_datashape)
    op1 = blaze.empty(operand_datashape)

    shape = (a.val for a in operand_datashape.shape)

    from operator import add
    t = time.time()
    for el in it_product(*[xrange(i) for i in shape]):
        val = random.uniform(-math.pi, math.pi)
        factor = math.sqrt(reduce(add, [j * 10**i for i, j in enumerate(reversed(el))]))

        op0[el] = math.sin(val) * factor
        op1[el] = math.cos(val) * factor

    logging.info("initialization took %f seconds", (time.time()-t))

    expr = op0*op0 + op1*op1

    eval_in_mem(expr, 0, dump_result=True)   
    eval_in_mem(expr, 0, dump_result=True)
    eval_in_mem(expr, iter_dims=1, dump_result=True)
    eval_in_mem(expr, iter_dims=1, chunk=3, dump_result=True)
    eval_in_mem(expr, iter_dims=1, chunk=4, dump_result=True)