def __onCopyLut(self, ev): """Called when the user presses the *Copy LUT* button. Prompts the user to enter a name (via :func:`promptForLutName`), and then creates and registers a new :class:`.LookupTable` instance which is initialised with the same labels as the previously selected ``LookupTable``. Updates this ``LookupTablePanel`` via the :meth:`__updateLutChoices` and :meth:`__setLut` methods. """ oldKey = self.__selectedLut.key oldName = self.__selectedLut.name newKey, newName = promptForLutName('{} copy'.format(oldName)) if newKey is None: return log.debug('Creating and registering new ' 'LookupTable {} (copied from {})'.format(newKey, oldKey)) lut = fslcmaps.LookupTable(key=newKey, name=newName) for label in self.__selectedLut.labels(): lut.insert(label.value, name=label.name, colour=label.colour, enabled=label.enabled) fslcmaps.registerLookupTable(lut, self.overlayList, self.displayCtx) self.__updateLutChoices() self.__setLut(lut)
def __onNewLut(self, ev): """Called when the user presses the *New LUT* button. Prompts the user to enter a name (via :func:`promptForLutName`), and then creates and registers a new :class:`.LookupTable` instance. Updates this ``LookupTablePanel`` via the :meth:`__updateLutChoices` and :meth:`__setLut` methods. """ key, name = promptForLutName() if key is None: return log.debug('Creating and registering new ' 'LookupTable: {}'.format(key)) lut = fslcmaps.LookupTable(key=key, name=name) fslcmaps.registerLookupTable(lut, self.overlayList, self.displayCtx) self.__updateLutChoices() self.__setLut(lut)
def test_LookupTable(): lut = tw.dedent(""" 1 0 0 0 Label 1 2 0 0 1 Label 2 3 0 1 1 Label 3 4 1 1 1 Label 4 """).strip() colours = [(0, 0, 0, 1), (0, 0, 1, 1), (0, 1, 1, 1), (1, 1, 1, 1)] with tempdir(): with open('lut.txt', 'wt') as f: f.write(lut) lut = fslcm.LookupTable('mylut', 'My LUT', 'lut.txt') assert lut.key == 'mylut' assert lut.name == 'My LUT' assert str( lut) == 'My LUT' assert repr(lut) == 'My LUT' assert len(lut) == 4 for i in range(3): assert lut[i].value == i + 1 assert lut.index(i + 1) == i assert lut.max() == 4 assert lut.saved for i in range(4): val = i + 1 lbl = lut.get(val) name = 'Label {}'.format(val) assert lbl.value == val assert lbl.name == name assert lbl.internalName == name.lower() assert tuple(lbl.colour) == colours[i] assert lut.getByName(name) == lbl assert list(lut.labels())[i] == lbl repr(lbl) hash(lbl) called = {} def removed(lt, top, args): called['removed'] = args def added(lt, top, args): called['added'] = args def saved(lt, top, args): called['saved'] = True def label(lt, top, args): called['label'] = args lut.register('l1', removed, topic='removed') lut.register('l2', added, topic='added') lut.register('l3', saved, topic='saved') lut.register('l4', label, topic='label') lbl0 = list(lut.labels())[0] lbl0.name = 'My Label 1!' assert called['saved'] assert called['label'] == (lbl0, 0) assert not lut.saved called.pop('saved') lut.save('newfile.lut') assert called['saved'] assert lut.saved called.pop('saved') lut.delete(4) assert len(lut) == 3 assert lut.max() == 3 assert not lut.saved assert called['saved'] called.pop('saved') lut.save('newfile.lut') assert lut.saved lbl = lut.new('New big label') assert lbl.value == 4 assert lut.max() == 4 assert len(lut) == 4 assert not lut.saved assert called['added'] == (lbl, 3) called.pop('saved') lut.save('newfile.lut') assert lut.saved lbl = lut.insert(7, name='New huge label') assert lbl.value == 7 assert lut.max() == 7 assert len(lut) == 5 assert not lut.saved assert called['added'] == (lbl, 4)