6
$\begingroup$

Mathematica includes RegisterImport to setup custom functions which read and translate file data for Import, effectively allowing it to be extended for any format. One can get a list of information that Import can return for a file type like so:

Import["ExampleData/lena.tif", {"TIFF", "Elements"}]
(* {Animation,Author,BitDepth,CameraTopOrientation,<<22>>,Thumbnail,ThumbnailList,TileSize} *)

This listing behavior is set up automatically by RegisterImport for correctly specified elements.

Options for functions registered to Import can be set up in the standard way and passed through the options given to the Import command:

Import["ExampleData/lena.tif", {"TIFF", "Image"}, ImageSize -> Tiny]

There's a similar syntax to see available options specific to the specified file format:

Import["ExampleData/lena.tif", {"TIFF", "Options"}]
(* {"BitDepth" -> 8, "ColorSpace" -> RGBColor, "Comments" -> None, "ImageSize" -> {150, 116}} *)

What needs to be done so that options for user-specified functions registered to Import appear in this listing?

So far I haven't been able to find any help docs or discussion on this topic.

$\endgroup$
  • $\begingroup$ Interesting question. It appears that the element "Options" is itself reserved, i.e. returning element rules like {"dummy" -> 1, "Options" -> Options[foo`import]} does not work, whereas {"dummy" -> 1, "Opts" -> Options[foo`import]} works for "Opts". $\endgroup$ – Mr.Wizard Dec 12 '16 at 10:03
  • $\begingroup$ @Mr.Wizard Yes, the call to RegisterImport seems to set up "Rules", "Elements", and "Options" on it own based on input. I think "Rules" comes from the default function (specified in the call to RegisterImport), but I don't see where "Options" come in. $\endgroup$ – dionys Dec 12 '16 at 12:06
1
$\begingroup$

Options isn't used very often, and doesn't do exactly what it sounds like. You are correct the Import framework has a few elements that it knows how to do for every format: All, "Rules", "Options", "Elements". Rules is a list of rules with elementName -> data for every element. All is a list of the data without the element name. "Options" allows you to specify at registration a list of elements that would be returned if you as for "Options", importing that subset of elements.

In[33]:= ImportExport`RegisterImport["fmt", 
    {"Data" -> ("Data" -> 1 &), 
    "Foo" -> ("Foo" -> 2 &), 
    Bar" -> ("Bar" -> 3 &)
    }, 
    "DefaultElement" -> "Data", 
    "Options" -> {"Data", "Bar"}]

In[34]:= ImportString["", "fmt"]

Out[34]= 1

In[35]:= ImportString["", {"fmt", "Foo"}]

Out[35]= 2

In[36]:= ImportString["", {"fmt", "Options"}]

Out[36]= {"Bar" -> 3, "Data" -> 1}

"Options" does not give a list of options supported by the format. I'm not sure why one would want to do this, but that's how it works. There is currently no way to get a list of options that a format accepts, maybe in future versions. As far as creating options that work with a custom format, it is done like this:

ImportExport`RegisterImport["fmt", myFunc, "DefaultElement" -> "Data"]
Options[myFunc] = {"opt" -> "val", ..}

Now you could call:

Import[.., "fmt", "opt" -> "newval"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.