Exemplo n.º 1
0
    def run_create_webpage(self, image_paths, thumb_paths=None,
                           metadata=None, alter_fn=None):
        '''Run the create_webpage module, returning the resulting HTML document

        image_paths - list of path / filename tuples. The function will
                      write an image to each of these and put images and
                      measurements into the workspace for each.
        thumb_paths - if present a list of path / filename tuples. Same as above
        metadata    - a dictionary of feature / string values
        alter_fn    - function taking a CreateWebPage module, for you to
                      alter the module's settings
        '''

        np.random.seed(0)
        module = C.CreateWebPage()
        module.module_num = 1
        module.orig_image_name.value = IMAGE_NAME
        module.web_page_file_name.value = DEFAULT_HTML_FILE
        if alter_fn is not None:
            alter_fn(module)
        pipeline = cpp.Pipeline()

        def callback(caller, event):
            self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

        pipeline.add_listener(callback)
        pipeline.add_module(module)

        images = [(IMAGE_NAME, image_paths)]
        if thumb_paths:
            images += [(THUMB_NAME, thumb_paths)]
            self.assertEqual(len(image_paths), len(thumb_paths))
            module.wants_thumbnails.value = True
            module.thumbnail_image_name.value = THUMB_NAME
        else:
            module.wants_thumbnails.value = False

        measurements = cpmeas.Measurements()

        workspace = cpw.Workspace(pipeline, module,
                                  measurements, None, measurements,
                                  None, None)
        for i in range(len(image_paths)):
            image_number = i + 1
            if metadata is not None:
                for key in metadata.keys():
                    values = metadata[key]
                    feature = cpmeas.C_METADATA + "_" + key
                    measurements[cpmeas.IMAGE, feature, image_number] = values[i]

            for image_name, paths in images:
                pixel_data = np.random.uniform(size=(10, 13))
                path_name, file_name = paths[i]
                if path_name is None:
                    path_name = cpprefs.get_default_image_directory()
                    is_file = True
                elif path_name.lower().startswith("http"):
                    is_file = False
                    url = path_name + "/" + file_name
                    if "?" in file_name:
                        file_name = file_name.split("?", 1)[0]
                if is_file:
                    full_path = os.path.abspath(os.path.join(
                            self.directory, path_name, file_name))
                    url = pathname2url(full_path)
                    path = os.path.split(full_path)[0]
                else:
                    full_path = url
                    path = path_name
                if is_file:
                    write_image(full_path, pixel_data, PT_UINT8)
                path_feature = '_'.join((C_PATH_NAME, image_name))
                file_feature = '_'.join((C_FILE_NAME, image_name))
                url_feature = '_'.join((C_URL, image_name))
                measurements[cpmeas.IMAGE, path_feature, image_number] = \
                    path
                measurements[cpmeas.IMAGE, file_feature, image_number] = \
                    file_name
                measurements[cpmeas.IMAGE, url_feature, image_number] = url

        module.post_run(workspace)
        return measurements
Exemplo n.º 2
0
    def run_create_webpage(self,
                           image_paths,
                           thumb_paths=None,
                           metadata=None,
                           alter_fn=None):
        '''Run the create_webpage module, returning the resulting HTML document
        
        image_paths - list of path / filename tuples. The function will
                      write an image to each of these and put images and
                      measurements into the workspace for each.
        thumb_paths - if present a list of path / filename tuples. Same as above
        metadata    - a dictionary of feature / string values
        alter_fn    - function taking a CreateWebPage module, for you to
                      alter the module's settings
        '''

        np.random.seed(0)
        module = C.CreateWebPage()
        module.module_num = 1
        module.orig_image_name.value = IMAGE_NAME
        module.web_page_file_name.value = DEFAULT_HTML_FILE
        if alter_fn is not None:
            alter_fn(module)
        pipeline = cpp.Pipeline()

        def callback(caller, event):
            self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

        pipeline.add_listener(callback)
        pipeline.add_module(module)

        images = [(IMAGE_NAME, image_paths)]
        if thumb_paths:
            images += [(THUMB_NAME, thumb_paths)]
            self.assertEqual(len(image_paths), len(thumb_paths))
            module.wants_thumbnails.value = True
            module.thumbnail_image_name.value = THUMB_NAME
        else:
            module.wants_thumbnails.value = False

        measurements = cpmeas.Measurements()
        image_set_list = cpi.ImageSetList()

        workspace = cpw.Workspace(pipeline, module, None, None, measurements,
                                  image_set_list, None)
        self.assertTrue(module.prepare_run(workspace))
        module.prepare_group(workspace, {}, np.arange(1, len(image_paths) + 1))
        for i in range(len(image_paths)):
            image_set = image_set_list.get_image_set(i)
            if metadata is not None:
                for key in metadata.keys():
                    values = metadata[key]
                    feature = cpmeas.C_METADATA + "_" + key
                    measurements.add_image_measurement(feature, values[i])

            for image_name, paths in images:
                pixel_data = np.random.uniform(size=(10, 13))
                img = cpi.Image(pixel_data)
                image_set.add(image_name, img)
                path_name, file_name = paths[i]
                if path_name is None:
                    path_name = cpprefs.get_default_image_directory()
                full_path = os.path.abspath(
                    os.path.join(self.directory, path_name, file_name))
                imsave(full_path, pixel_data)
                path_feature = '_'.join((C_PATH_NAME, image_name))
                file_feature = '_'.join((C_FILE_NAME, image_name))
                measurements.add_image_measurement(path_feature,
                                                   os.path.split(full_path)[0])
                measurements.add_image_measurement(file_feature, file_name)
            workspace = cpw.Workspace(pipeline, module, image_set,
                                      cpo.ObjectSet(), measurements,
                                      image_set_list)
            module.run(workspace)
            if i < len(image_paths) - 1:
                measurements.next_image_set()
        module.post_group(workspace, {})
        module.post_run(workspace)