Ejemplo n.º 1
0
    def setup(self, ctx):
        self.choice = None
        self.is_first_run = True

        filepaths = ctx.database.get(datatype="spreadsheet")

        self.is_missing = True

        self.choice = None
        if filepaths is not None and len(filepaths) > 0:
            self.is_missing = False

            self._append_view(TextView("Select the covariates/group data spreadsheet file"))

            self.add_file_str = "Add spreadsheet file"

            dsp_values = [f'"{value}"' for value in filepaths]
            dsp_values = [*dsp_values, self.add_file_str]

            self.filepath_by_str = dict(zip(dsp_values, filepaths))

            self.input_view = SingleChoiceInputView(dsp_values, isVertical=True)

            self._append_view(self.input_view)
            self._append_view(SpacerView(1))
Ejemplo n.º 2
0
 def setup(self, ctx):
     self._append_view(TextView("Specify the analysis type"))
     self.input_view = SingleChoiceInputView(
         list(self.options.keys()), isVertical=self.is_vertical
     )
     self._append_view(self.input_view)
     self._append_view(SpacerView(1))
Ejemplo n.º 3
0
    def setup(self, ctx):
        self.is_first_run = True
        self.should_run = True
        self.choice = None

        if not hasattr(ctx.spec.files[-1], "metadata") or ctx.spec.files[-1].metadata is None:
            ctx.spec.files[-1].metadata = dict()

        if ctx.spec.files[-1].metadata.get("variables") is None:
            ctx.spec.files[-1].metadata["variables"] = []

        if any(
            variable["type"] == "id"
            for variable in ctx.spec.files[-1].metadata["variables"]
        ):
            self.should_run = False

        if self.should_run:
            self._append_view(TextView("Specify the column containing subject names"))

            already_used = set(v["name"] for v in ctx.spec.files[-1].metadata["variables"])

            df = loadspreadsheet(ctx.spec.files[-1].path)
            columns = [column for column in df if column not in already_used]
            options = [format_column(column) for column in columns]

            self.varname_by_str = dict(zip(options, columns))

            self.input_view = SingleChoiceInputView(options, isVertical=True)

            self._append_view(self.input_view)
            self._append_view(SpacerView(1))
Ejemplo n.º 4
0
    def setup(self, ctx):
        self._append_view(
            TextView("Specify the categorical variable for this contrast"))

        self.variables = ctx.database.metadata(ctx.spec.models[-1].spreadsheet,
                                               "variables")
        self.variables = apply_filters_to_variables(
            ctx.spec.models[-1].filters, self.variables)
        contrastvariables = set(
            ravel(contrast["variable"]
                  for contrast in ctx.spec.models[-1].contrasts
                  if contrast.get("type") == "infer")
        )  # names of all variables added to the model in the previous step
        self.variables = [
            variable for variable in self.variables
            if variable["type"] == "categorical"
            and variable["name"] in contrastvariables
        ]

        varnames = [variable["name"] for variable in self.variables]
        options = [format_column(varname) for varname in varnames]

        self.varname_by_str = dict(zip(options, varnames))

        self.input_view = SingleChoiceInputView(options)
        self._append_view(self.input_view)
        self._append_view(SpacerView(1))
Ejemplo n.º 5
0
    def setup(self, ctx):
        self._append_view(TextView("Specify the feature type"))

        self.input_view = SingleChoiceInputView(list(self.options.keys()),
                                                isVertical=True)

        self._append_view(self.input_view)
        self._append_view(SpacerView(1))
Ejemplo n.º 6
0
 def setup(self, ctx):
     if hasattr(self, "header_str") and self.header_str is not None:
         self._append_view(TextView(self.header_str))
     self.input_view = SingleChoiceInputView(
         list(self.options.keys()), isVertical=self.is_vertical
     )
     self._append_view(self.input_view)
     self._append_view(SpacerView(1))
Ejemplo n.º 7
0
 def setup(self, ctx):
     self.is_first_run = True
     self.existing_spec = loadspec(ctx.workdir,
                                   logger=logging.getLogger("pipeline.ui"))
     self.choice = None
     if self.existing_spec is not None:
         self._append_view(TextView("Found spec file in working directory"))
         self.input_view = SingleChoiceInputView(self.options,
                                                 isVertical=True)
         self._append_view(self.input_view)
         self._append_view(SpacerView(1))
Ejemplo n.º 8
0
 def setup(self, ctx):
     self._append_view(TextView("Specify the categorical variable for this contrast"))
     self.varname_by_str = {
         _format_column(variable.name): variable.name
         for variable in ctx.spec.analyses[-1].variables
         if variable.type == "categorical"
     }
     self.options = list(self.varname_by_str.keys())
     self.input_view = SingleChoiceInputView(self.options)
     self._append_view(self.input_view)
     self._append_view(SpacerView(1))
Ejemplo n.º 9
0
    def setup(self, ctx):
        self.choice = None
        self.is_first_run = True

        self.should_run = self._should_run(ctx)

        if self.should_run:
            if hasattr(self, "header_str") and self.header_str is not None:
                self._append_view(TextView(self.header_str))

            self.input_view = SingleChoiceInputView(
                list(self.options.keys()), isVertical=self.is_vertical
            )

            self._append_view(self.input_view)
            self._append_view(SpacerView(1))
Ejemplo n.º 10
0
 def setup(self, ctx):
     self.is_first_run = True
     self.is_missing = True
     if any(variable.type == "id" for variable in ctx.spec.analyses[-1].variables):
         self.is_missing = False
     if self.is_missing:
         self._append_view(TextView("Specify the column containing subject names"))
         if ctx.spec.analyses[-1].variables is None:
             ctx.spec.analyses[-1].variables = []
         already_used = set(variable.name for variable in ctx.spec.analyses[-1].variables)
         self.varname_by_str = {
             _format_column(column): column for column in self.df if column not in already_used
         }
         options = list(self.varname_by_str.keys())
         self.input_view = SingleChoiceInputView(options, isVertical=True)
         self._append_view(self.input_view)
         self._append_view(SpacerView(1))
Ejemplo n.º 11
0
    def setup(self, ctx):
        self.is_first_run = True
        self.existing_spec = loadspec(ctx.workdir,
                                      logger=logging.getLogger("halfpipe.ui"))
        self.choice = None
        if self.existing_spec is not None:
            self._append_view(TextView("Found spec file in working directory"))

            options = self.options[:3]

            if len(self.existing_spec.features) > 0:
                options.append(self.options[3])
            if len(self.existing_spec.models) > 0:
                options.append(self.options[4])

            self.input_view = SingleChoiceInputView(options, isVertical=True)
            self._append_view(self.input_view)
            self._append_view(SpacerView(1))
Ejemplo n.º 12
0
 def setup(self, ctx):
     self.is_first_run = True
     if self.header_str is not None:
         self._append_view(TextView(self.header_str))
     self.values = ctx.database.get_tagvaldict(self.entity)
     self.is_missing = True
     self.choice = None
     if self.values is not None and len(self.values) > 0:
         self.is_missing = False
         self.add_file_str = f"Add {self.filetype_str} file"
         dsp_values = []
         if len(self.values) > 1:
             dsp_values.append(self.all_str)
         dsp_values.extend(self._make_tagval_strs())
         dsp_values.append(self.add_file_str)
         self.input_view = SingleChoiceInputView(dsp_values,
                                                 isVertical=True)
         self._append_view(self.input_view)
         self._append_view(SpacerView(1))
Ejemplo n.º 13
0
    def setup(self, ctx):
        humankey = display_str(self.key).lower()

        unit = _get_unit(self.schema, self.key)
        field = self.field

        header_str = f"Specify {humankey}{self.appendstr}"
        if unit is not None and self.key != "slice_timing":
            header_str += f" in {unit}"

        self._append_view(TextView(header_str))

        self.aliases = {}

        if field.validate is not None and hasattr(field.validate, "choices") or self.key == "slice_timing":
            if self.key == "slice_timing":
                choices = [*slice_order_strs, "import from file"]
            else:
                choices = [*field.validate.choices]
            if set(space_codes).issubset(choices):
                choices = [*space_codes]
                if self.key == "slice_encoding_direction":
                    choices = list(reversed(choices))
                display_choices = [
                    display_str(direction_code_str(choice, None)) for choice in choices
                ]
            else:
                display_choices = [display_str(choice) for choice in choices]

            self.aliases = dict(zip(display_choices, choices))

            self.input_view = SingleChoiceInputView(display_choices, isVertical=True)

        elif isinstance(field, fields.Float):
            self.input_view = NumberInputView()

        else:
            raise ValueError(f'Unsupported metadata field "{field}"')

        self._append_view(self.input_view)
        self._append_view(SpacerView(1))
Ejemplo n.º 14
0
    def setup(self, ctx):
        self.is_first_run = True
        self.should_skip = self._should_skip(ctx)
        self.choice = None

        if self.should_skip:
            self.is_missing = True
            return

        humankey = display_str(self.key).lower()

        if self.filters is None:
            filepaths = [
                fileobj.path for fileobj in ctx.database.fromspecfileobj(ctx.spec.files[-1])
            ]
        else:
            filepaths = [*ctx.database.get(**self.filters)]

        ctx.database.fillmetadata(self.key, filepaths)

        vals = [ctx.database.metadata(filepath, self.key) for filepath in filepaths]
        self.suggestion = None

        if self.key == "phase_encoding_direction" or self.key == "slice_encoding_direction":
            for i, val in enumerate(vals):
                if val is not None:
                    vals[i] = direction_code_str(val, filepaths[i])

        elif self.key == "slice_timing":
            for i, val in enumerate(vals):
                if val is not None:
                    sts = slice_timing_str(val)
                    if sts == "unknown":
                        val = np.asarray(val)
                        sts = np.array2string(val, max_line_width=16384)
                    else:
                        sts = humanize(sts)
                    vals[i] = sts

        if any(val is None for val in vals):
            self.is_missing = True
            self._append_view(TextView(f"Missing {humankey} values"))

            vals = [val if val is not None else "missing" for val in vals]
        else:
            self.is_missing = False
            self._append_view(TextView(f"Check {humankey} values{self.appendstr}"))

        uniquevals, counts = np.unique(vals, return_counts=True)
        order = np.argsort(counts)

        column1 = []
        for i in range(min(10, len(order))):
            column1.append(f"{counts[i]} images")
        column1width = max(len(s) for s in column1)

        unit = _get_unit(self.schema, self.key)
        if unit is None:
            unit = ""

        if self.key == "slice_timing":
            unit = ""

        for i in range(min(10, len(order))):
            display = display_str(f"{uniquevals[i]}")
            if self.suggestion is None:
                self.suggestion = display
            tablerow = f" {column1[i]:>{column1width}} - {display}"
            if uniquevals[i] != "missing":
                tablerow = f"{tablerow} {unit}"
            self._append_view(TextView(tablerow))

        if len(order) > 10:
            self._append_view(TextView("..."))

        if self.is_missing is False:
            self._append_view(TextView("Proceed with these values?"))
            self.input_view = SingleChoiceInputView(["Yes", "No"], isVertical=False)
            self._append_view(self.input_view)

        self._append_view(SpacerView(1))