|
| 1 | +.. _select_files: |
| 2 | + |
| 3 | +========================== |
| 4 | +The SelectFiles Interfaces |
| 5 | +========================== |
| 6 | + |
| 7 | +Nipype 0.9 introduces a new interface for intelligently finding files on the |
| 8 | +disk and feeding them into your workflows: :ref:`SelectFiles |
| 9 | +<nipype.interfaces.io.SelectFiles>`. SelectFiles is intended as a simpler |
| 10 | +alternative to the :ref:`DataGrabber <nipype.intefaces.io.DataGrabber>` |
| 11 | +interface that was discussed previously in :doc:`grabbing_and_sinking`. |
| 12 | + |
| 13 | +SelectFiles is built on Python `format strings |
| 14 | +<http://docs.python.org/2/library/string.html#format-string-syntax>`_, which |
| 15 | +are similar to the Python string interpolation feature you are likely already |
| 16 | +familiar with, but advantageous in several respects. Format strings allow you |
| 17 | +to replace named sections of template strings set off by curly braces (`{}`), |
| 18 | +possibly filtered through a set of functions that control how the values are |
| 19 | +rendered into the string. As a very basic example, we could write |
| 20 | + |
| 21 | +:: |
| 22 | + |
| 23 | + msg = "This workflow uses {package}" |
| 24 | + |
| 25 | +and then format it with keyword arguments:: |
| 26 | + |
| 27 | + print msg.format(package="FSL") |
| 28 | + |
| 29 | +SelectFiles only requires that you provide templates that can be used to find |
| 30 | +your data; the actual formatting happens behind the scenes. |
| 31 | + |
| 32 | +Consider a basic example in which you want to select a T1 image and multple |
| 33 | +functional images for a number of subjects. Invoking SelectFiles in this case |
| 34 | +is quite straightforward:: |
| 35 | + |
| 36 | + from nipype import SelectFiles |
| 37 | + templates = dict(T1="data/{subject_id}/struct/T1.nii", |
| 38 | + epi="data/{subject_id}/func/epi_run*.nii") |
| 39 | + sf = SelectFiles(templates) |
| 40 | + |
| 41 | +SelectFiles will take the `templates` dictionary and parse it to determine its |
| 42 | +own inputs and oututs. Specifically, each name used in the format spec (here |
| 43 | +just `subject_id`) will become an interface input, and each key in the |
| 44 | +dictionary (here `T1` and `epi`) will become interface outputs. The `templates` |
| 45 | +dictionary thus succinctly links the node inputs to the appropriate outputs. |
| 46 | +You'll also note that, as was the case with DataGrabber, you can use basic |
| 47 | +`glob <http://docs.python.org/2.7/library/glob.html>`_ syntax to match multiple |
| 48 | +files for a given output field. Additionally, any of the conversions outlined in the Python documentation for format strings can be used in the templates. |
| 49 | + |
| 50 | +There are a few other options that help make SelectFiles flexible enough to |
| 51 | +deal with any situation where you need to collect data. Like DataGrabber, |
| 52 | +SelectFiles has a `base_directory` parameter that allows you to specify a path |
| 53 | +that is common to all of the values in the `templates` dictionary. |
| 54 | +Additionally, as `glob` does not return a sorted list, there is also a |
| 55 | +`sort_filelist` option, taking a boolean, to control whether sorting should be |
| 56 | +applied (it is True by default). |
| 57 | + |
| 58 | +The final input is `force_lists`, which controls how SelectFiles behaves in |
| 59 | +cases where only a single file matches the template. The default behavior is |
| 60 | +that when a template matches multiple files they are returned as a list, while |
| 61 | +a single file is returned as a string. There may be situations where you want |
| 62 | +to force the outputs to always be returned as a list (for example, you are |
| 63 | +writing a workflow that expects to operate on several runs of data, but some of |
| 64 | +your subjects only have a single run). In this case, `force_lists` can be used |
| 65 | +to tune the outputs of the interface. You can either use a boolean value, which |
| 66 | +will be applied to every output the interface has, or you can provide a list of |
| 67 | +the output fields that should be coerced to a list. Returning to our basic |
| 68 | +example, you may want to ensure that the `epi` files are returned as a list, |
| 69 | +but you only ever will have a single `T1` file. In this case, you would do |
| 70 | + |
| 71 | +:: |
| 72 | + |
| 73 | + sf = SelectFiles(templates, force_lists=["epi"]) |
| 74 | + |
| 75 | +.. include:: ../links_names.txt |
0 commit comments