def __init__(self, names=None, objs=None, is_placed=False): if names is None: names, objs = [], [] dict.__init__(self, zip(names, objs)) Object.__init__(self, is_placed=is_placed) self.names = sorted(self.keys()) self.n_class = len(self.names)
def report(self, filename): fd = self.fd_open(filename) if self.is_placed: Object.report(self, fd, header=False) fd.write('intersections per axis:\n') for axis, ints in ordered_iteritems(self.intersection_counters): fd.write(' %s (%d): %s\n' % (axis, len(ints), ints)) else: fd.write(format_dict(self.conf.get_dict(), raw=self.requested_conf.get_dict()) + '\n') self.fd_close()
def __init__(self, dims=None, units=None, resolution=None, n_object=None, n_slice=None): Object.__init__(self, name='box', dims=np.array(dims, dtype=np.float64), units=units, resolution=resolution, n_object=n_object, n_slice=n_slice) if not isinstance(self.n_slice, dict): self.n_slice = {'z' : int(self.n_slice)} self.init_trait('volume', np.prod(self.dims)) self._axis_map = {'x' : [1, 2, 0], 'y' : [2, 0, 1], 'z' : [0, 1, 2]}
def report(self, filename): fd = self.fd_open(filename) if self.is_placed: Object.report(self, fd, header=False) fd.write('intersections per axis:\n') for axis, ints in ordered_iteritems(self.intersection_counters): fd.write(' %s (%d): %s\n' % (axis, len(ints), ints)) else: fd.write( format_dict(self.conf.get_dict(), raw=self.requested_conf.get_dict()) + '\n') self.fd_close()
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
def from_conf(conf, box): objs = Objects(conf.keys(), conf.values(), is_placed=False) n_object = objs.init_counts(box.n_object) objs.init_trait('n_object_requested', n_object) for key, val in conf.iteritems(): output(('*** %s ' % key) + 50*'*') obj_conf = Object.objects_from_dict(val, name='obj_conf', flag=(1,)) obj_conf.init_trait('n_object', objs.n_object_requested[key]) obj_conf0 = obj_conf.copy(deep=True) obj = reduce_to_fit(obj_conf, box) obj.set_conf(obj_conf, obj_conf0) obj.name = key output(obj._format(mode='set_only')) ## print obj.conf ## print obj.requested_conf objs[key] = obj return objs
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