def _on_import(self): """ Import format: CSV, first column is filename, path relative to CSV. others are conditions, type is autodetected. first row is header with names. """ file_dialog = FileDialog() file_dialog.wildcard = "CSV files (*.csv)|*.csv|" file_dialog.action = 'open' file_dialog.open() if file_dialog.return_code != PyfaceOK: return csv = pandas.read_csv(file_dialog.path) csv_folder = Path(file_dialog.path).parent if self.model.tubes or self.model.tube_traits: if confirm( parent=None, message="This will clear the current conditions and tubes! " "Are you sure you want to continue?", title="Clear tubes and conditions?") != YES: return for col in csv.columns[1:]: self.model.tube_traits.append( TubeTrait(model=self.model, name=util.sanitize_identifier(col), type='category')) for _, row in csv.iterrows(): filename = csv_folder / row[0] try: metadata, _ = parse_tube(str(filename), metadata_only=True) except Exception as e: warning( None, "Had trouble loading file {}: {}".format(filename, str(e))) continue metadata['CF_File'] = Path(filename).stem new_tube = Tube(file=str(filename), parent=self.model, metadata=sanitize_metadata(metadata)) self.model.tubes.append(new_tube) for col in csv.columns[1:]: new_tube.trait_set(**{util.sanitize_identifier(col): row[col]})
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ # TODO - adding a set of files, then a condition, then another # set doesn't work. 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)) tube = Tube() for trait_name, trait in self.model.tube_traits.items(): # TODO - do we still need to check for transient? 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(Source = tube_meta['$SRC'], _file = path, _parent = self.model) if 'TUBE NAME' in tube_meta: tube.Tube = tube_meta['TUBE NAME'] elif '$SMNO' in tube_meta: tube.Tube = tube_meta['$SMNO'] self.model.tubes.append(tube)
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ # TODO - adding a set of files, then a condition, then another # set doesn't work. 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)) tube = Tube() for trait_name, trait in self.model.tube_traits.items(): # TODO - do we still need to check for transient? 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(Source=tube_meta['$SRC'], _file=path, _parent=self.model) if 'TUBE NAME' in tube_meta: tube.Tube = tube_meta['TUBE NAME'] elif '$SMNO' in tube_meta: tube.Tube = tube_meta['$SMNO'] self.model.tubes.append(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 *.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 click ( self ): """ Handles the user left clicking on the feature image. """ # Create the file dialog: fd = FileDialog() # Set up the default path based on the current value (if any): default_path = getattr( self.dock_control.object, self.name, None ) if default_path is not None: fd.default_path = default_path # Set up the appropriate extension filters (if any): if len( self.extensions ) > 0: fd.wildcard = '\n'.join([ FileDialog.create_wildcard('', '*' + ext) for ext in self.extensions ]) # Display the file dialog, and if successful, set the new file name: if fd.open() == OK: self.drop( fd.path )
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ # TODO - adding a set of files, then a condition, then another # set doesn't work. 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: fcs = FCMeasurement(ID='new tube', datafile = path) tube = Tube() for trait_name, trait in self.model.tube_traits.items(): # TODO - do we still need to check for transient? 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(Source = fcs.meta['$SRC'], _file = path, _parent = self.model) if 'TUBE NAME' in fcs.meta: tube.Tube = fcs.meta['TUBE NAME'] elif '$SMNO' in fcs.meta: tube.Tube = fcs.meta['$SMNO'] self.model.tubes.append(tube)
def click(self): """ Handles the user left clicking on the feature image. """ # Create the file dialog: fd = FileDialog() # Set up the default path based on the current value (if any): default_path = getattr(self.dock_control.object, self.name, None) if default_path is not None: fd.default_path = default_path # Set up the appropriate extension filters (if any): if len(self.extensions) > 0: fd.wildcard = '\n'.join([ FileDialog.create_wildcard('', '*' + ext) for ext in self.extensions ]) # Display the file dialog, and if successful, set the new file name: if fd.open() == OK: self.drop(fd.path)
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)
def _on_add_tubes(self): """ Handle "Add tubes..." button. Add tubes to the experiment. """ # TODO - adding a set of files, then a condition, then another # set doesn't work. 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(): # TODO - do we still need to check for transient? 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"]}) self.model.tubes.append(tube) self.btn_add_cond.setEnabled(True)