コード例 #1
0
ファイル: formatreader.py プロジェクト: sammac/CellProfiler
        def allowOpenToCheckType(self, allow):
            '''Allow the "isThisType" function to open files
            
            For the cluster, you want to tell potential file formats
            not to open the image file to test if it's their format.
            '''
            if not hasattr(self, "allowOpenToCheckType_method"):
                self.allowOpenToCheckType_method = None
                class_wrapper = jutil.get_class_wrapper(self.o)
                methods = class_wrapper.getMethods()
                for method in jutil.get_env().get_object_array_elements(
                        methods):
                    m = jutil.get_method_wrapper(method)
                    if m.getName() in ('allowOpenToCheckType',
                                       'setAllowOpenFiles'):
                        self.allowOpenToCheckType_method = m
            if self.allowOpenToCheckType_method is not None:
                object_class = env.find_class('java/lang/Object')
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)

                boolean_value = jutil.make_instance('java/lang/Boolean',
                                                    '(Z)V', allow)
                args = jutil.get_env().make_object_array(1, object_class)
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)
                jutil.get_env().set_object_array_element(
                    args, 0, boolean_value)
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)
                self.allowOpenToCheckType_method.invoke(self.o, args)
コード例 #2
0
ファイル: images.py プロジェクト: crschmidt/CellProfiler
def evaluate_url(filtr, url):
    '''Evaluate a URL using a setting's filter
    
    '''
    global filter_class, filter_method_id
    env = J.get_env()
    if filter_class is None:
        filter_class = env.find_class(
            "org/cellprofiler/imageset/filter/Filter")
        if filter_class is None:
            jexception = get_env.exception_occurred()
            raise J.JavaException(jexception)
        filter_method_id = env.get_static_method_id(
            filter_class, "filter", "(Ljava/lang/String;Ljava/lang/String;)Z")
        if filter_method_id is None:
            raise JavaError(
                "Could not find static method, org.cellprofiler.imageset.filter.Filter.filter(String, String)"
            )
    try:
        expression = filtr.value_text
        if isinstance(expression, unicode):
            expression = expression.encode("utf-8")
        if isinstance(url, unicode):
            url = url.encode("utf-8")
        return env.call_static_method(filter_class, filter_method_id,
                                      env.new_string_utf(expression),
                                      env.new_string_utf(url))
    except J.JavaException as e:
        if J.is_instance_of(
                e.throwable,
                "org/cellprofiler/imageset/filter/Filter$BadFilterExpressionException"
        ):
            raise
        return False
コード例 #3
0
ファイル: metadata.py プロジェクト: crschmidt/CellProfiler
    def prepare_run(self, workspace):
        '''Initialize the pipeline's metadata'''
        if workspace.pipeline.in_batch_mode():
            return True

        file_list = workspace.file_list
        pipeline = workspace.pipeline
        ipds = pipeline.get_filtered_image_plane_details(workspace)
        extractor = self.build_extractor()
        max_series = 0
        max_index = 0
        for ipd in ipds:
            if ipd.series is not None:
                max_series = max(max_series, ipd.series)
            if ipd.index is not None:
                max_index = max(max_index, ipd.index)
        if max_series > 0:
            series_digits = int(np.log10(max_series)) + 1
        else:
            series_digits = 1
        if max_index > 0:
            index_digits = int(np.log10(max_index)) + 1
        else:
            index_digits = 1
        if max_series > 0 or max_index > 0:
            script = """
            importPackage(Packages.org.cellprofiler.imageset);
            extractor.addImagePlaneExtractor(new SeriesIndexMetadataExtractor(
                seriesDigits, indexDigits));
            """
            J.run_script(
                script,
                dict(extractor=extractor,
                     seriesDigits=series_digits,
                     indexDigits=index_digits))
        env = J.get_env()
        entry_set_class = env.find_class("java/util/Map$Entry")
        get_key_id = env.get_method_id(entry_set_class, "getKey",
                                       "()Ljava/lang/Object;")
        get_value_id = env.get_method_id(entry_set_class, "getValue",
                                         "()Ljava/lang/Object;")

        def wrap_entry_set(o):
            return (env.get_string_utf(env.call_method(o, get_key_id)),
                    env.get_string_utf(env.call_method(o, get_value_id)))

        #
        # Much of what appears below is optimized to avoid the cost of
        # "getting nice arguments" for the Java bridge. The IPDs should be
        # in alphabetical order which means that, for stacks, we can
        # save the results of OME-XML parsing in the Java ImageFile object.
        #
        extractor_class = env.find_class(
            "org/cellprofiler/imageset/ImagePlaneMetadataExtractor")
        extract_metadata_id = env.get_method_id(
            extractor_class, "extractMetadata",
            "(Ljava/lang/String;IILjava/lang/String;"
            "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;"
            "[Lorg/cellprofiler/imageset/ImageFile;"
            ")Ljava/util/Iterator;")
        extract_metadata_if_id = env.get_method_id(
            extractor_class, "extractMetadata",
            "(Lorg/cellprofiler/imageset/ImageFile;II"
            "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;"
            ")Ljava/util/Iterator;")
        ipd_class = env.find_class(
            "org/cellprofiler/imageset/filter/ImagePlaneDetails")
        if_class = env.find_class("org/cellprofiler/imageset/ImageFile")
        clear_xml_document_id = env.get_method_id(if_class, "clearXMLDocument",
                                                  "()V")
        pIPD = env.make_object_array(1, ipd_class)
        pIF = env.make_object_array(1, if_class)

        last_url = None
        last_if = None
        if_has_metadata = False
        for ipd in ipds:
            series, index = [
                x if x is not None else 0 for x in ipd.series, ipd.index
            ]
            if ipd.url != last_url:
                if if_has_metadata:
                    env.call_method(last_if, clear_xml_document_id)
                    x = env.exception_occurred()
                    if x is not None:
                        raise J.JavaException(x)
                    if_has_metadata = False
                xmlmetadata = file_list.get_metadata(ipd.url)
                if xmlmetadata is not None:
                    xmlmetadata = env.new_string_utf(xmlmetadata)
                    if_has_metadata = True
                metadata = env.call_method(extractor, extract_metadata_id,
                                           env.new_string_utf(ipd.url),
                                           int(series), int(index),
                                           xmlmetadata, pIPD, pIF)
                x = env.exception_occurred()
                if x is not None:
                    raise J.JavaException(x)
                last_url = ipd.url
                last_if = env.get_object_array_elements(pIF)[0]
            else:
                metadata = env.call_method(extractor,
                                           extract_metadata_if_id, last_if,
                                           int(series), int(index), pIPD)
                x = env.exception_occurred()
                if x is not None:
                    raise J.JavaException(x)

            ipd.metadata.update(J.iterate_java(metadata, wrap_entry_set))
            ipd.jipd = env.get_object_array_elements(pIPD)[0]
        if if_has_metadata:
            env.call_method(last_if, clear_xml_document_id)
            x = env.exception_occurred()
            if x is not None:
                raise J.JavaException(x)
        return True