コード例 #1
0
    def run(self):
        logger.info("gensei generator running")
        from gensei.base import Config, Object
        from gensei import Objects, Box
        from gensei.utils import get_suffix

        conf = {
            'objects': default_objects,
            'box': default_box,
            'options': default_options
        }

        config = Config.from_conf(conf,
                                  required=['objects', 'box'],
                                  optional=['options'])

        if isinstance(config.box['dims'], str):
            config.box['dims'] = eval(config.box['dims'])
        if isinstance(config.box['resolution'], str):
            aux = tuple([int(r) for r in config.box['resolution'].split('x')])
            config.box['resolution'] = aux

        # config.box["resolution"] = 1/self.voxelsize_mm[1:]
        # config.box['dims'] = self.area_shape
        config.box["resolution"] = tuple(self.resolution.astype(int).tolist())
        config.box['dims'] = self.dims.astype(int).tolist()
        config.box['n_slice'] = int(self.n_slice)
        box = Box(**config.box)
        options = Object(name='options', **config.options)

        output(box)
        output(options)

        object_classes = Objects.from_conf(config.objects, box)
        objects = object_classes.place_objects(box, options)

        self.objects = objects
        self.box = box
        self.options = options
        pass
コード例 #2
0
    def place_objects(self, box, options):
        """Generate non-intersecting objects fully contained in the specimen's
        block.
        """
        objs = Objects(is_placed=True)
        objs.box = box
        objs.init_trait('n_object_requested', self.n_object_requested)

        stats_per_class = {}
        for key in self.names:
            obj_class = self[key]

            stats = Object(volume=0.0, surface=0.0, length=0.0)
            stats_per_class[key] = stats
            
            for ii in xrange(self.n_object_requested[key]):
                output(('*** %s: %d ' % (key, ii)) + 50*'*')

                obj = obj_class.copy(deep=True)
                obj.init_trait('obj_class', key)
                
                t0 = time.clock()
                ok = True

                while 1:
                    if (time.clock() - t0) > options.timeout:
                        output('timeout -> try reducing object size!')
                        ok = False
                        break

                    obj.setup_orientation()

                    bbox = obj.get_origin_bounding_box()
                    ## This "fixes" the bounding box until exact bounding boxes
                    ## for all kinds of objects are implemented.
                    r = obj.get_radius()
                    bbox[:,1] = np.minimum(r, bbox[:,1])
                    assert_((bbox[:,1] < (0.5 * box.dims)).all())
                    
                    centre = get_random(bbox[:,1], box.dims - bbox[:,1])
                    obj.set_centre(centre)
                    obj.is_placed = True
                    
                    for ip, prev in enumerate(objs.itervalues()):
                        bad = prev.intersects(obj)
                        ## print '%d. intersects: %d' % (ip, bad)
                        if bad:
                            break
                    else:
                        ## print 'ok'
                        break

                if ok:
                    output('accepted:', obj)
                    obj_class.accepted()

                    obj.name = key.replace('class', 'object') + ('_%d' % ii)
                    objs[key + ' ' + obj.name] = obj

                    obj.update_stats(stats)

                else:
                    break

        objs.init_trait('n_object',
                        {}.fromkeys(self.n_object_requested.keys(), 0))
        for key in self.names:
            obj_class = self[key]
            # This was updated in obj_class.accepted() call above.
            objs.n_object[key] = obj_class.conf.n_object

        object_volume = object_surface = object_length = 0.0
        for stats in stats_per_class.itervalues():
            object_volume += stats.volume
            object_surface += stats.surface
            object_length += stats.length

        objs.stats_per_class = stats_per_class
        objs.init_trait('total_object_volume', object_volume)
        objs.init_trait('total_object_volume_fraction',
                        object_volume / box.volume)
        objs.init_trait('total_object_surface', object_surface)
        objs.init_trait('total_object_surface_fraction',
                        object_surface / box.volume)
        objs.init_trait('total_object_length', object_length)
        objs.init_trait('total_object_length_density',
                        object_length / box.volume)
        objs.section_volumes = {}
        objs.section_surfaces = {}

        return objs