def update_import_op(self, op): trait_to_dtype = { "Str": "category", "Float": "float", "Bool": "bool", "Int": "int" } conditions = {} for trait_name, trait in list(self.tube_traits.items()): if not trait.condition: continue trait_type = trait.__class__.__name__ conditions[trait_name] = trait_to_dtype[trait_type] tubes = [] for tube in self.tubes: op_tube = CytoflowTube(file=tube.file, conditions=tube.trait_get(condition=True), conditions_list=[ x for x in list(self.tube_traits.keys()) if self.tube_traits[x].condition ]) tubes.append(op_tube) op.conditions = conditions op.tubes = tubes
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ file_dialog = FileDialog() file_dialog.wildcard = "Flow cytometry files (*.fcs *.lmd)|*.fcs *.lmd|" file_dialog.action = 'open files' file_dialog.open() if file_dialog.return_code != PyfaceOK: return for path in file_dialog.paths: try: metadata, _ = parse_tube(path, metadata_only=True) except Exception as e: raise RuntimeError("FCS reader threw an error on tube {0}: {1}"\ .format(path, e.value)) # if we're the first tube loaded, create a dummy experiment # and setup default metadata columns if not self.model.dummy_experiment: self.model.dummy_experiment = \ ImportOp(tubes = [CytoflowTube(file = path)]).apply(metadata_only = True) # check the next tube against the dummy experiment try: check_tube(path, self.model.dummy_experiment) except util.CytoflowError as e: error(None, e.__str__(), "Error importing tube") return metadata['CF_File'] = Path(path).stem tube = Tube(file=path, parent=self.model, metadata=sanitize_metadata(metadata)) self.model.tubes.append(tube) for trait in self.model.tube_traits: if trait.type != 'metadata' and trait.name: tube.on_trait_change(self._try_multiedit, trait.name)
def update_import_op(self, import_op): if not self.tubes: return assert self.dummy_experiment is not None conditions = { trait.name: trait.type for trait in self.tube_traits if trait.type != 'metadata' } tubes = [] events = 0 for tube in self.tubes: op_tube = CytoflowTube(file=tube.file, conditions=tube.trait_get( list(conditions.keys()))) tubes.append(op_tube) events += tube.metadata['TOT'] import_op.ret_events = events import_op.conditions = conditions import_op.tubes = tubes import_op.original_channels = channels = self.dummy_experiment.channels all_present = len(import_op.channels_list) > 0 if len(import_op.channels_list) > 0: for c in import_op.channels_list: if c.name not in channels: all_present = False if not all_present: warning( None, "Some of the operation's channels weren't found in " "these FCS files. Resetting all channel names.", "Resetting channel names") if not all_present: import_op.reset_channels()
def update_import_op(self, op): trait_to_dtype = { "Str": "category", "Float": "float", "LogFloat": "log", "Bool": "bool", "Int": "int" } op.conditions.clear() for trait_name, trait in self.tube_traits.items(): if not trait.condition: continue trait_type = trait.__class__.__name__ op.conditions[trait_name] = trait_to_dtype[trait_type] for tube in self.tubes: op_tube = CytoflowTube(source=tube.Source, tube=tube.Tube, file=tube._file) if "Row" in tube.trait_names(): op_tube.row = tube.Row if "Col" in tube.trait_names(): op_tube.col = tube.Col op_tube.conditions = tube.trait_get(condition=True) # AFAICT this is going to cause the op to reload THE ENTIRE # SET each time a tube is added. >.> # TODO - FIX THIS. need a general strategy for only updating # if there's a "pause" in the action. op.tubes.append(op_tube)
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ file_dialog = FileDialog() file_dialog.wildcard = "Flow cytometry files (*.fcs)|*.fcs|" file_dialog.action = 'open files' file_dialog.open() if file_dialog.return_code != PyfaceOK: return for path in file_dialog.paths: try: tube_meta = fcsparser.parse(path, meta_data_only = True, reformat_meta = True) #tube_channels = tube_meta["_channels_"].set_index("$PnN") except Exception as e: raise RuntimeError("FCS reader threw an error on tube {0}: {1}"\ .format(path, e.value)) # if we're the first tube loaded, create a dummy experiment if not self.model.dummy_experiment: self.model.dummy_experiment = ImportOp(tubes = [CytoflowTube(file = path)], coarse_events = 1).apply() # check the next tube against the dummy experiment try: check_tube(path, self.model.dummy_experiment) except util.CytoflowError as e: error(None, e.__str__(), "Error importing tube") return tube = Tube() for trait_name, trait in self.model.tube_traits.items(): tube.add_trait(trait_name, trait) # this magic makes sure the trait is actually defined # in tube.__dict__, so it shows up in trait_names etc. tube.trait_set(**{trait_name : trait.default_value}) if trait.condition: tube.on_trait_change(self._try_multiedit, trait_name) tube.trait_set(file = path, parent = self.model) if '$SRC' in tube_meta: self._add_metadata("$SRC", "$SRC", Str(condition = False)) tube.trait_set(**{"$SRC" : tube_meta['$SRC']}) if 'TUBE NAME' in tube_meta: self._add_metadata("TUBE NAME", "TUBE NAME", Str(condition = False)) tube.trait_set(**{"TUBE NAME" : tube_meta['TUBE NAME']}) if '$SMNO' in tube_meta: self._add_metadata("$SMNO", "$SMNO", Str(condition = False)) tube.trait_set(**{"$SMNO" : tube_meta['$SMNO']}) if 'WELL ID' in tube_meta: self._add_metadata("Row", "Row", Str(condition = False)) self._add_metadata("Col", "Col", Int(condition = False)) pos = tube_meta['WELL ID'] row = pos[0] col = int(pos[1:3]) tube.trait_set(**{"Row" : row, "Col" : col}) self.model.tubes.append(tube)