Example #1
0
def load_using_bioformats(path,
                          c=None,
                          z=0,
                          t=0,
                          series=None,
                          index=None,
                          rescale=True,
                          wants_max_intensity=False,
                          channel_names=None):
    '''Load the given image file using the Bioformats library
    
    path: path to the file
    z: the frame index in the z (depth) dimension.
    t: the frame index in the time dimension.
    channel_names: None if you don't want them, a list which will be filled if you do
    
    Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image
    '''
    #
    # We loop as long as the user is willing to try logging
    # in after timeout.
    #
    while True:
        try:
            return __load_using_bioformats(path, c, z, t, series, index,
                                           rescale, wants_max_intensity,
                                           channel_names)
        except jutil.JavaException, e:
            je = e.throwable
            if jutil.is_instance_of(je, "loci/formats/FormatException"):
                je = jutil.call(je, "getCause", "()Ljava/lang/Throwable;")
            if jutil.is_instance_of(je, "Glacier2/PermissionDeniedException"):
                omero_logout()
                omero_login()
            else:
                import errno
                import exceptions
                import traceback
                logger.warn(e.message)
                for line in traceback.format_exc().split("\n"):
                    logger.warn(line)
                if jutil.is_instance_of(je, "java/io/FileNotFoundException"):
                    raise exceptions.IOError(
                        errno.ENOENT,
                        "The file, \"%s\", does not exist." % path, path)
                e2 = exceptions.IOError(
                    errno.EINVAL,
                    "Could not load the file as an image (see log for details)",
                    path.encode('utf-8'))
                raise e2
Example #2
0
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
Example #3
0
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
Example #4
0
def load_using_bioformats(path, c=None, z=0, t=0, series=None, index=None,
                          rescale = True,
                          wants_max_intensity = False,
                          channel_names = None):
    '''Load the given image file using the Bioformats library
    
    path: path to the file
    z: the frame index in the z (depth) dimension.
    t: the frame index in the time dimension.
    channel_names: None if you don't want them, a list which will be filled if you do
    
    Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image
    '''
    #
    # We loop as long as the user is willing to try logging
    # in after timeout.
    #
    while True:
        try:
            return __load_using_bioformats(
                path, c, z, t, series, index, rescale, wants_max_intensity,
                channel_names)
        except jutil.JavaException, e:
            je = e.throwable
            if jutil.is_instance_of(
                je, "loci/formats/FormatException"):
                je = jutil.call(je, "getCause", 
                                "()Ljava/lang/Throwable;")
            if jutil.is_instance_of(
                je, "Glacier2/PermissionDeniedException"):
                omero_logout()
                omero_login()
            else:
                import errno
                import exceptions
                import traceback
                logger.warn(e.message)
                for line in traceback.format_exc().split("\n"):
                    logger.warn(line)
                if jutil.is_instance_of(je, "java/io/FileNotFoundException"):
                                raise exceptions.IOError(
                                    errno.ENOENT, 
                                    "The file, \"%s\", does not exist." % path,
                                    path)
                e2 = exceptions.IOError(
                    errno.EINVAL, "Could not load the file as an image (see log for details)", path.encode('utf-8'))
                raise e2
Example #5
0
 def get_command_settings(self, command, d):
     '''Get the settings associated with the current command
     
     d - the dictionary that persists the setting. None = regular
     '''
     key = command.get_unicode_value()
     if not d.has_key(key):
         try:
             module_info = command.get_selected_leaf()[2]
         except cps.ValidationError:
             logger.info("Could not find command %s" % key)
             return []
         result = []
         inputs = module_info.getInputs()
         for module_item in inputs:
             field_type = module_item.getType()
             label = module_item.getLabel()
             if label is None:
                 label = module_item.getName()
             if module_item.isOutput():
                 # if both, qualify which is for input and which for output
                 label = "%s (Input)" % label
             minimum = module_item.getMinimumValue()
             maximum = module_item.getMaximumValue()
             default = module_item.loadValue()
             description = module_item.getDescription()
             if field_type == ij2.FT_BOOL:
                 value = (J.is_instance_of(default, 'java/lang/Boolean') and
                          J.call(default, "booleanValue", "()Z"))
                 setting = cps.Binary(
                     label,
                     value = value,
                     doc = description)
             elif field_type == ij2.FT_INTEGER:
                 if J.is_instance_of(default, 'java/lang/Number'):
                     value = J.call(default, "intValue", "()I")
                 elif minimum is not None:
                     value = minimum
                 elif maximum is not None:
                     value = maximum
                 else:
                     value = 0
                 setting = cps.Integer(
                     label,
                     value = value,
                     doc = description)
             elif field_type == ij2.FT_FLOAT:
                 if J.is_instance_of(default, 'java/lang/Number'):
                     value = J.call(default, "doubleValue", "()D")
                 elif minimum is not None:
                     value = minimum
                 elif maximum is not None:
                     value = maximum
                 else:
                     value = 0
                 setting = cps.Float(
                     label,
                     value=value,
                     doc = description)
             elif field_type == ij2.FT_STRING:
                 choices = module_item.getChoices()
                 value = J.to_string(default)
                 if choices is not None:
                     choices = J.get_collection_wrapper(choices)
                     setting = cps.Choice(
                         label, choices, value, doc = description)
                 else:
                     setting = cps.Text(
                         label, value, doc = description)
             elif field_type == ij2.FT_COLOR:
                 value = "#ffffff"
                 setting = cps.Color(label, value, doc = description)
             elif field_type == ij2.FT_IMAGE:
                 setting = cps.ImageNameSubscriber(
                     label, "InputImage",
                     doc = description)
             elif field_type == ij2.FT_TABLE:
                 setting = IJTableSubscriber(label, "InputTable",
                                             doc=description)
             elif field_type == ij2.FT_FILE:
                 setting = cps.FilenameText(
                     label, None, doc = description)
             else:
                 continue
             result.append((setting, module_item))
         for output in module_info.getOutputs():
             field_type = output.getType()
             label = output.getLabel()
             if label is None:
                 label = output.getName()
             if output.isInput():
                 # if both, qualify which is for input and which for output
                 label = "%s (Output)" % label
             if field_type == ij2.FT_IMAGE:
                 result.append((cps.ImageNameProvider(
                     label, "ImageJImage",
                     doc = description), output))
             elif field_type == ij2.FT_TABLE:
                 result.append((IJTableProvider(
                     label, "ImageJTable", doc=description), output))
         d[key] = result
     else:
         result = d[key]
     return [setting for setting, module_info in result]
Example #6
0
 def test_02_02_01_get_modules(self):
     svc = ij2.get_module_service(self.context)
     module_infos = svc.getModules()
     self.assertTrue(J.is_instance_of(module_infos[0].o,
                                      "imagej/module/ModuleInfo"))
Example #7
0
 def get_command_settings(self, command, d):
     '''Get the settings associated with the current command
     
     d - the dictionary that persists the setting. None = regular
     '''
     key = command.get_unicode_value()
     if not d.has_key(key):
         try:
             module_info = command.get_selected_leaf()[2]
         except cps.ValidationError:
             logger.info("Could not find command %s" % key)
             return []
         result = []
         inputs = module_info.getInputs()
         for module_item in inputs:
             field_type = module_item.getType()
             label = module_item.getLabel()
             if label is None:
                 label = module_item.getName()
             if module_item.isOutput():
                 # if both, qualify which is for input and which for output
                 label = "%s (Input)" % label
             minimum = module_item.getMinimumValue()
             maximum = module_item.getMaximumValue()
             default = module_item.loadValue()
             description = module_item.getDescription()
             if field_type == ij2.FT_BOOL:
                 value = (J.is_instance_of(default, 'java/lang/Boolean')
                          and J.call(default, "booleanValue", "()Z"))
                 setting = cps.Binary(label, value=value, doc=description)
             elif field_type == ij2.FT_INTEGER:
                 if J.is_instance_of(default, 'java/lang/Number'):
                     value = J.call(default, "intValue", "()I")
                 elif minimum is not None:
                     value = minimum
                 elif maximum is not None:
                     value = maximum
                 else:
                     value = 0
                 setting = cps.Integer(label, value=value, doc=description)
             elif field_type == ij2.FT_FLOAT:
                 if J.is_instance_of(default, 'java/lang/Number'):
                     value = J.call(default, "doubleValue", "()D")
                 elif minimum is not None:
                     value = minimum
                 elif maximum is not None:
                     value = maximum
                 else:
                     value = 0
                 setting = cps.Float(label, value=value, doc=description)
             elif field_type == ij2.FT_STRING:
                 choices = module_item.getChoices()
                 value = J.to_string(default)
                 if choices is not None:
                     choices = J.get_collection_wrapper(choices)
                     setting = cps.Choice(label,
                                          choices,
                                          value,
                                          doc=description)
                 else:
                     setting = cps.Text(label, value, doc=description)
             elif field_type == ij2.FT_COLOR:
                 value = "#ffffff"
                 setting = cps.Color(label, value, doc=description)
             elif field_type == ij2.FT_IMAGE:
                 setting = cps.ImageNameSubscriber(label,
                                                   "InputImage",
                                                   doc=description)
             elif field_type == ij2.FT_TABLE:
                 setting = IJTableSubscriber(label,
                                             "InputTable",
                                             doc=description)
             elif field_type == ij2.FT_FILE:
                 setting = cps.FilenameText(label, None, doc=description)
             else:
                 continue
             result.append((setting, module_item))
         for output in module_info.getOutputs():
             field_type = output.getType()
             label = output.getLabel()
             if label is None:
                 label = output.getName()
             if output.isInput():
                 # if both, qualify which is for input and which for output
                 label = "%s (Output)" % label
             if field_type == ij2.FT_IMAGE:
                 result.append(
                     (cps.ImageNameProvider(label,
                                            "ImageJImage",
                                            doc=description), output))
             elif field_type == ij2.FT_TABLE:
                 result.append((IJTableProvider(label,
                                                "ImageJTable",
                                                doc=description), output))
         d[key] = result
     else:
         result = d[key]
     return [setting for setting, module_info in result]