datasetplugin.py 67.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- coding: utf-8 -*-

#    Copyright (C) 2010 Jeremy S. Sanders
#    Email: Jeremy Sanders <jeremy@jeremysanders.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##############################################################################

"""Plugins for creating datasets."""

23
from __future__ import division
24
import numpy as N
25
from . import field
26

27 28
from ..compat import czip, citems, cstr
from .. import utils
29
try:
30
    from ..helpers import qtloops
31 32
except ImportError:
    pass
33 34 35 36 37
from .. import qtall as qt4

def _(text, disambiguation=None, context='DatasetPlugin'):
    """Translate text."""
    return qt4.QCoreApplication.translate(context, text, disambiguation)
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
# add an instance of your class to this list to be registered
datasetpluginregistry = []

class DatasetPluginException(RuntimeError):
    """Raise this to report an error.
    """
    pass

def numpyCopyOrNone(data):
    """If data is None return None
    Otherwise return a numpy array corresponding to data."""
    if data is None:
        return None
    return N.array(data, dtype=N.float64)

# these classes are returned from dataset plugins
class Dataset1D(object):
    """1D dataset for ImportPlugin or DatasetPlugin."""
    def __init__(self, name, data=[], serr=None, perr=None, nerr=None):
        """1D dataset
        name: name of dataset
        data: data in dataset: list of floats or numpy 1D array
        serr: (optional) symmetric errors on data: list or numpy array
        perr: (optional) positive errors on data: list or numpy array
        nerr: (optional) negative errors on data: list or numpy array

        If errors are returned for data give serr or nerr and perr.
        nerr should be negative values if used.
        perr should be positive values if used.
        """
        self.name = name
        self.update(data=data, serr=serr, perr=perr, nerr=nerr)

    def update(self, data=[], serr=None, perr=None, nerr=None):
        """Update values to those given."""
        self.data = numpyCopyOrNone(data)
        self.serr = numpyCopyOrNone(serr)
        self.perr = numpyCopyOrNone(perr)
        self.nerr = numpyCopyOrNone(nerr)

    def _null(self):
        """Empty data contents."""
        self.data = N.array([])
        self.serr = self.perr = self.nerr = None

    def _makeVeuszDataset(self, manager):
        """Make a Veusz dataset from the plugin dataset."""
        # need to do the import here as otherwise we get a loop
87
        from .. import document
88 89 90 91
        return document.Dataset1DPlugin(manager, self)

class Dataset2D(object):
    """2D dataset for ImportPlugin or DatasetPlugin."""
92 93 94
    def __init__(self, name, data=[[]], rangex=None, rangey=None,
                 xedge=None, yedge=None,
                 xcent=None, ycent=None):
95 96 97 98 99
        """2D dataset.
        name: name of dataset
        data: 2D numpy array of values or list of lists of floats
        rangex: optional tuple with X range of data (min, max)
        rangey: optional tuple with Y range of data (min, max)
100 101 102 103
        xedge: x values for grid (instead of rangex)
        yedge: y values for grid (instead of rangey)
        xcent: x values for pixel centres (instead of rangex)
        ycent: y values for pixel centres (instead of rangey)
104 105
        """
        self.name = name
106 107 108
        self.update(data=data, rangex=rangex, rangey=rangey,
                    xedge=xedge, yedge=yedge,
                    xcent=xcent, ycent=ycent)
109

110 111 112
    def update(self, data=[[]], rangex=None, rangey=None,
               xedge=None, yedge=None,
               xcent=None, ycent=None):
113 114 115
        self.data = N.array(data, dtype=N.float64)
        self.rangex = rangex
        self.rangey = rangey
116 117 118 119
        self.xedge = xedge
        self.yedge = yedge
        self.xcent = xcent
        self.ycent = ycent
120 121 122 123 124

    def _null(self):
        """Empty data contents."""
        self.data = N.array([[]])
        self.rangex = self.rangey = (0, 1)
125
        self.xedge = self.yedge = self.xcent = self.ycent = None
126 127 128

    def _makeVeuszDataset(self, manager):
        """Make a Veusz dataset from the plugin dataset."""
129
        from .. import document
130 131
        return document.Dataset2DPlugin(manager, self)

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
class DatasetDateTime(object):
    """Date-time dataset for ImportPlugin or DatasetPlugin."""

    def __init__(self, name, data=[]):
        """A date dataset
        name: name of dataset
        data: list of datetime objects
        """
        self.name = name
        self.update(data=data)

    def update(self, data=[]):
        self.data = N.array(data)

    @staticmethod
    def datetimeToFloat(datetimeval):
        """Return a python datetime object to the required float type."""
        return utils.datetimeToFloat(datetimeval)

    @staticmethod
    def dateStringToFloat(text):
        """Try to convert an iso or local date time to the float type."""
        return utils.dateStringToDate(text)

    @staticmethod
    def floatToDateTime(val):
        """Convert float format datetime to Python datetime."""
        return utils.floatToDateTime(val)

    def _null(self):
        """Empty data contents."""
        self.data = N.array([])

    def _makeVeuszDataset(self, manager):
        """Make a Veusz dataset from the plugin dataset."""
167 168
        from .. import document
        return document.DatasetDateTimePlugin(manager, self)
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
class DatasetText(object):
    """Text dataset for ImportPlugin or DatasetPlugin."""
    def __init__(self, name, data=[]):
        """A text dataset
        name: name of dataset
        data: data in dataset: list of strings
        """
        self.name = name
        self.update(data=data)

    def update(self, data=[]):
        self.data = list(data)

    def _null(self):
        """Empty data contents."""
        self.data = []

    def _makeVeuszDataset(self, manager):
        """Make a Veusz dataset from the plugin dataset."""
189
        from .. import document
190 191
        return document.DatasetTextPlugin(manager, self)

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
class Constant(object):
    """Dataset to return to set a Veusz constant after import.
    This is only useful in an ImportPlugin, not a DatasetPlugin
    """
    def __init__(self, name, val):
        """Map string value val to name.
        Convert float vals to strings first!"""
        self.name = name
        self.val = val

class Function(object):
    """Dataset to return to set a Veusz function after import."""
    def __init__(self, name, val):
        """Map string value val to name.
        name is "funcname(param,...)", val is a text expression of param.
        This is only useful in an ImportPlugin, not a DatasetPlugin
        """
        self.name = name
        self.val = val

212 213 214 215 216 217 218 219 220 221 222
# class to pass to plugin to give parameters
class DatasetPluginHelper(object):
    """Helpers to get existing datasets for plugins."""
    
    def __init__(self, doc):
        """Construct helper object to pass to DatasetPlugins."""
        self._doc = doc

    @property
    def datasets1d(self):
        """Return list of existing 1D numeric datasets"""
223
        return [name for name, ds in citems(self._doc.data) if
224 225 226 227 228
                (ds.dimensions == 1 and ds.datatype == 'numeric')]

    @property
    def datasets2d(self):
        """Return list of existing 2D numeric datasets"""
229
        return [name for name, ds in citems(self._doc.data) if
230 231 232 233 234
                (ds.dimensions == 2 and ds.datatype == 'numeric')]

    @property
    def datasetstext(self):
        """Return list of existing 1D text datasets"""
235
        return [name for name, ds in citems(self._doc.data) if
236 237
                (ds.dimensions == 1 and ds.datatype == 'text')]

238 239 240
    @property
    def datasetsdatetime(self):
        """Return list of existing date-time datesets"""
241 242
        from .. import document
        return [name for name, ds in citems(self._doc.data) if
243 244
                isinstance(ds, document.DatasetDateTime)]

245 246 247 248 249
    @property
    def locale(self):
        """Return Qt locale."""
        return self._doc.locale

250 251 252 253
    def evaluateExpression(self, expr, part='data'):
        """Return results of evaluating a 1D dataset expression.
        part is 'data', 'serr', 'perr' or 'nerr' - these are the
        dataset parts which are evaluated by the expression
254 255

        Returns None if expression could not be evaluated.
256
        """
257 258 259 260 261
        ds = self._doc.evalDatasetExpression(expr, part=part)
        if ds is not None:
            return ds.data
        else:
            return None
262 263 264 265 266 267 268 269 270 271 272

    def getDataset(self, name, dimensions=1):
        """Return numerical dataset object for name given.
        Please make sure that dataset data are not modified.

        name: name of dataset
        dimensions: number of dimensions dataset requires

        name not found: raise a DatasetPluginException
        dimensions not right: raise a DatasetPluginException
        """
273
        from .. import document
274 275 276
        try:
            ds = self._doc.data[name]
        except KeyError:
277
            raise DatasetPluginException(_("Unknown dataset '%s'") % name)
278 279 280

        if ds.dimensions != dimensions:
            raise DatasetPluginException(
281 282
                _("Dataset '%s' does not have %i dimensions") % (
                    name, dimensions))
283 284
        if ds.datatype != 'numeric':
            raise DatasetPluginException(
285
                _("Dataset '%s' is not a numerical dataset") % name)
286

287 288 289
        if isinstance(ds, document.DatasetDateTime):
            return DatasetDateTime(name, data=ds.data)
        elif ds.dimensions == 1:
290 291 292 293
            return Dataset1D(name, data=ds.data, serr=ds.serr, 
                             perr=ds.perr, nerr=ds.nerr)
        elif ds.dimensions == 2:
            return Dataset2D(name, ds.data,
294 295 296
                             xrange=ds.xrange, yrange=ds.yrange,
                             xedge=ds.xedge, yedge=ds.yedge,
                             xcent=ds.xcent, ycent=ds.ycent)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        else:
            raise RuntimeError("Invalid number of dimensions in dataset")

    def getDatasets(self, names, dimensions=1):
        """Get a list of numerical datasets (of the dimension given)."""
        return [ self.getDataset(n, dimensions=dimensions) for n in names ]

    def getTextDataset(self, name):
        """Return a text dataset with name given.
        Do not modify this dataset.

        name not found: raise a DatasetPluginException
        """

        try:
            ds = self._doc.data[name]
        except KeyError:
314
            raise DatasetPluginException(_("Unknown dataset '%s'") % name)
315 316
        if ds.datatype == 'text':
            return DatasetText(name, ds.data)
317
        raise DatasetPluginException(_("Dataset '%s' is not a text datset") % name)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

# internal object to synchronise datasets created by a plugin
class DatasetPluginManager(object):
    """Manage datasets generated by plugin."""

    def __init__(self, plugin, doc, fields):
        """Construct manager object.

        plugin - instance of plugin class
        doc - document instance
        fields - fields to pass to plugin
        """
        
        self.plugin = plugin
        self.document = doc
        self.helper = DatasetPluginHelper(doc)
334
        self.fields = dict(fields)
335 336
        self.changeset = -1

337
        self.fixMissingFields()
338 339
        self.setupDatasets()

340 341 342 343 344 345
    def fixMissingFields(self):
        """If fields are missing, use defaults."""
        for pluginfield in self.plugin.fields:
            if pluginfield.name not in self.fields:
                self.fields[pluginfield.name] = pluginfield.default

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    def setupDatasets(self):
        """Do initial construction of datasets."""

        self.datasetnames = []
        self.datasets = []
        self.veuszdatasets = []
        self.datasets = self.plugin.getDatasets(self.fields)
        for ds in self.datasets:
            self.datasetnames.append(ds.name)
            veuszds = ds._makeVeuszDataset(self)
            veuszds.document = self.document
            self.veuszdatasets.append(veuszds)

    def nullDatasets(self):
        """Clear out contents of datasets."""
        for ds in self.datasets:
            ds._null()

    def saveToFile(self, fileobj):
        """Save command to load in plugin and parameters."""

        args = [ repr(self.plugin.name), repr(self.fields) ]

        # look for renamed or deleted datasets
        names = {}
371
        for ds, dsname in czip( self.veuszdatasets, self.datasetnames ):
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            try:
                currentname = self.document.datasetName(ds)
            except ValueError:
                # deleted
                currentname = None

            if currentname != dsname:
                names[dsname] = currentname

        if names:
            args.append( "datasetnames="+repr(names) )

        fileobj.write( 'DatasetPlugin(%s)\n' % (', '.join(args)) )

    def update(self, raiseerrors=False):
        """Update created datasets.

        if raiseerrors is True, raise an exception if there is an exeception
        when updating the dataset
        """

        if self.document.changeset == self.changeset:
            return
        self.changeset = self.document.changeset

        # run the plugin with its parameters
        try:
            self.plugin.updateDatasets(self.fields, self.helper)
400
        except DatasetPluginException as ex:
401 402 403 404 405
            # this is for immediate notification
            if raiseerrors:
                raise

            # otherwise if there's an error, then log and null outputs
406
            self.document.log( cstr(ex) )
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            self.nullDatasets()

class DatasetPlugin(object):
    """Base class for defining dataset plugins."""

    # the plugin will get inserted into the menu in a hierarchy based on
    # the elements of this tuple
    menu = ('Base plugin',)
    name = 'Base plugin'

    author = ''
    description_short = ''
    description_full = ''

    # if the plugin takes no parameters, set this to False
    has_parameters = True

    def __init__(self):
        """Override this to declare a list of input fields if required."""
        self.fields = []

    def getDatasets(self, fields):
        """Override this to return a list of (empty) Dataset1D,
        Dataset2D and DatasetText objects to provide the initial names
        and type of datasets.

        These should be saved for updating in updateDatasets.

        fields: dict of results to the field objects given in self.fields
        raise a DatasetPluginException if there is a problem with fields
        """
        return []

    def updateDatasets(self, fields, helper):
        """Override this to update the dataset objects provided by this plugin.

        fields: dict of field results (also provided to setup)
        helper: DatasetPluginHelper object, to get other datasets in document

        raise a DatasetPluginException if there is a problem
        """

class _OneOutputDatasetPlugin(DatasetPlugin):
    """Simplify plugins which create one output with field ds_out."""

    def getDatasets(self, fields):
        """Returns single output dataset (self.dsout)."""
        if fields['ds_out'] == '':
455
            raise DatasetPluginException(_('Invalid output dataset name'))
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
        self.dsout = Dataset1D(fields['ds_out'])
        return [self.dsout]

def errorBarType(ds):
    """Return type of error bars in list of datasets.
    'none', 'symmetric', 'asymmetric'
    """

    symerr = False
    for d in ds:
        if d.serr is not None:
            symerr = True
        elif d.perr is not None or d.nerr is not None:
            return 'asymmetric'
    if symerr:
        return 'symmetric'
    return 'none'

def combineAddedErrors(inds, length):
    """Combine error bars from list of input dataset, adding
    errors squared (suitable for adding/subtracting)."""

    errortype = errorBarType(inds)
    serr = perr = nerr = None
    if errortype == 'symmetric':
        serr = N.zeros(length, dtype=N.float64)
    elif errortype == 'asymmetric':
        perr = N.zeros(length, dtype=N.float64)
        nerr = N.zeros(length, dtype=N.float64)

    for d in inds:
        f = N.isfinite(d.data)

        if errortype == 'symmetric' and d.serr is not None:
            serr[f] += d.serr[f]**2 
        elif errortype == 'asymmetric':
            if d.serr is not None:
                v = (d.serr[f])**2
                perr[f] += v
                nerr[f] += v
            if d.perr is not None:
                perr[f] += (d.perr[f])**2
            if d.nerr is not None:
                nerr[f] += (d.nerr[f])**2

    if serr is not None: serr = N.sqrt(serr)
    if perr is not None: perr = N.sqrt(perr)
    if nerr is not None: nerr = -N.sqrt(nerr)
    return serr, perr, nerr

def combineMultipliedErrors(inds, length, data):
    """Combine error bars from list of input dataset, adding
    fractional errors squared (suitable for multipling/dividing)."""

    errortype = errorBarType(inds)
    serr = perr = nerr = None
    if errortype == 'symmetric':
        serr = N.zeros(length, dtype=N.float64)
    elif errortype == 'asymmetric':
        perr = N.zeros(length, dtype=N.float64)
        nerr = N.zeros(length, dtype=N.float64)

    for d in inds:
        f = N.isfinite(d.data)
        if len(f) > length:
            f = f[:length]

        if errortype == 'symmetric' and d.serr is not None:
            serr[f] += (d.serr[f]/d.data[f])**2 
        elif errortype == 'asymmetric':
            if d.serr is not None:
                v = (d.serr[f]/d.data[f])**2
                perr[f] += v
                nerr[f] += v
            if d.perr is not None:
                perr[f] += (d.perr[f]/d.data[f])**2
            if d.nerr is not None:
                nerr[f] += (d.nerr[f]/d.data[f])**2

    if serr is not None: serr = N.abs(N.sqrt(serr) * data)
    if perr is not None: perr = N.abs(N.sqrt(perr) * data)
    if nerr is not None: nerr = -N.abs(N.sqrt(nerr) * data)
    return serr, perr, nerr

###########################################################################
## Real plugins are below

class MultiplyDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to scale a dataset."""

546
    menu = (_('Multiply'), _('By constant'),)
547
    name = 'Multiply'
548 549 550
    description_short = _('Multiply dataset by a constant')
    description_full = _('Multiply a dataset by a factor. '
                         'Error bars are also scaled.')
551 552 553 554
    
    def __init__(self):
        """Define fields."""
        self.fields = [
555 556 557
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldFloat('factor', _('Factor'), default=1.),
            field.FieldDataset('ds_out', _('Output dataset name')),
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
            ]

    def updateDatasets(self, fields, helper):
        """Do scaling of dataset."""

        ds_in = helper.getDataset(fields['ds_in'])
        f = fields['factor']

        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr
        data = data * f
        if serr is not None: serr = serr * f
        if perr is not None: perr = perr * f
        if nerr is not None: nerr = nerr * f

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class AddDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to add a constant to a dataset."""

577
    menu = (_('Add'), _('Constant'),)
578
    name = 'Add'
579 580 581
    description_short = _('Add a constant to a dataset')
    description_full = _('Add a dataset by adding a value. '
                         'Error bars remain the same.')
582 583 584 585
    
    def __init__(self):
        """Define fields."""
        self.fields = [
586 587 588
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldFloat('value', _('Add value'), default=0.),
            field.FieldDataset('ds_out', _('Output dataset name')),
589 590 591 592 593 594 595 596 597 598 599
            ]

    def updateDatasets(self, fields, helper):
        """Do shifting of dataset."""
        ds_in = helper.getDataset(fields['ds_in'])
        self.dsout.update(data = ds_in.data + fields['value'],
                          serr=ds_in.serr, perr=ds_in.perr, nerr=ds_in.nerr)

class ConcatenateDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to concatenate datasets."""

600
    menu = (_('Join'), _('Concatenate'),)
601
    name = 'Concatenate'
602 603 604
    description_short = _('Concatenate datasets')
    description_full = _('Concatenate datasets into single dataset.\n'
                         'Error bars are merged.')
605 606 607 608

    def __init__(self):
        """Define fields."""
        self.fields = [
609 610
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldDataset('ds_out', _('Output dataset name')),
611 612 613 614 615 616 617
            ]

    def updateDatasets(self, fields, helper):
        """Do concatenation of dataset."""

        dsin = helper.getDatasets(fields['ds_in'])
        if len(dsin) == 0:
618
            raise DatasetPluginException(_('Requires one or more input datasets'))
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

        # concatenate main data
        dstack = N.hstack([d.data for d in dsin])
        sstack = pstack = nstack = None

        # what sort of error bars do we need?
        errortype = errorBarType(dsin)
        if errortype == 'symmetric':
            # symmetric and not asymmetric error bars
            sstack = []
            for d in dsin:
                if d.serr is not None:
                    sstack.append(d.serr)
                else:
                    sstack.append(N.zeros(d.data.shape, dtype=N.float64))
            sstack = N.hstack(sstack)
        elif errortype == 'asymmetric':
            # asymmetric error bars
            pstack = []
            nstack = []
            for d in dsin:
                p = n = N.zeros(d.data.shape, dtype=N.float64)
                if d.serr is not None:
                    p, n = d.serr, -d.serr
                else:
                    if d.perr is not None: p = d.perr
                    if d.nerr is not None: n = d.nerr
                pstack.append(p)
                nstack.append(n)
            pstack = N.hstack(pstack)
            nstack = N.hstack(nstack)

        self.dsout.update(data=dstack, serr=sstack, perr=pstack, nerr=nstack)

class InterleaveDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to interleave datasets."""

656
    menu = (_('Join'), _('Element by element'),)
657
    name = 'Interleave'
658 659 660
    description_short = _('Join datasets, interleaving element by element')
    description_full = _('Join datasets, interleaving element by element.\n'
                         'Error bars are merged.')
661 662 663 664

    def __init__(self):
        """Define fields."""
        self.fields = [
665 666
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldDataset('ds_out', _('Output dataset name')),
667 668 669 670 671 672 673
            ]

    def updateDatasets(self, fields, helper):
        """Do concatenation of dataset."""

        dsin = helper.getDatasets(fields['ds_in'])
        if len(dsin) == 0:
674
            raise DatasetPluginException(_('Requires one or more input datasets'))
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

        maxlength = max( [len(d.data) for d in dsin] )

        def interleave(datasets):
            """This is complex to account for different length datasets."""
            # stick in columns
            ds = [ N.hstack( (d, N.zeros(maxlength-len(d))) )
                   for d in datasets ]
            # which elements are valid
            good = [ N.hstack( (N.ones(len(d), dtype=N.bool),
                                N.zeros(maxlength-len(d), dtype=N.bool)) )
                     for d in datasets ]

            intl = N.column_stack(ds).reshape(maxlength*len(datasets))
            goodintl = N.column_stack(good).reshape(maxlength*len(datasets))
            return intl[goodintl]

        # do interleaving
        data = interleave([d.data for d in dsin])

        # interleave error bars
        errortype = errorBarType(dsin)
        serr = perr = nerr = None
        if errortype == 'symmetric':
            slist = []
            for ds in dsin:
                if ds.serr is None:
                    slist.append(N.zeros_like(ds.data))
                else:
                    slist.append(ds.serr)
            serr = interleave(slist)
        elif errortype == 'asymmetric':
            plist = []
            nlist = []
            for ds in dsin:
                if ds.serr is not None:
                    plist.append(ds.serr)
                    nlist.append(-ds.serr)
                else:
                    if ds.perr is not None:
                        plist.append(ds.perr)
                    else:
                        plist.append(N.zeros_like(ds.data))
                    if ds.nerr is not None:
                        nlist.append(ds.nerr)
                    else:
                        nlist.append(N.zeros_like(ds.data))
            perr = interleave(plist)
            nerr = interleave(nlist)

        # finally update
        self.dsout.update(data=data, serr=serr, nerr=nerr, perr=perr)

class ChopDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to chop datasets."""

731
    menu = (_('Split'), _('Chop'),)
732
    name = 'Chop'
733 734 735
    description_short = _('Chop dataset part into new dataset')
    description_full = _('Chop out a section of a dataset. Give starting '
                         'index of data and number of datapoints to take.')
736 737 738 739

    def __init__(self):
        """Define fields."""
        self.fields = [
740 741 742 743
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldInt('start', _('Starting index (from 1)'), default=1),
            field.FieldInt('num', _('Maximum number of datapoints'), default=1),
            field.FieldDataset('ds_out', _('Output dataset name')),
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
            ]

    def updateDatasets(self, fields, helper):
        """Do chopping of dataset."""

        ds_in = helper.getDataset(fields['ds_in'])
        start = fields['start']
        num = fields['num']

        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr

        # chop the data
        data = data[start-1:start-1+num]
        if serr is not None: serr = serr[start-1:start-1+num]
        if perr is not None: perr = perr[start-1:start-1+num]
        if nerr is not None: nerr = nerr[start-1:start-1+num]

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class PartsDatasetPlugin(DatasetPlugin):
    """Dataset plugin to split datasets into parts."""

766
    menu = (_('Split'), _('Parts'),)
767
    name = 'Parts'
768 769 770 771
    description_short = _('Split dataset into equal-size parts')
    description_full = _('Split dataset into equal-size parts. '
                         'The parts will differ in size if the dataset '
                         'cannot be split equally.')
772 773 774 775

    def __init__(self):
        """Define fields."""
        self.fields = [
776 777
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldDatasetMulti('ds_out', _('Output datasets')),
778 779 780 781 782 783 784 785 786
            ]

    def getDatasets(self, fields):
        """Get output datasets."""
        self.dsout = []
        for d in fields['ds_out']:
            if d.strip() != '':
                self.dsout.append( Dataset1D(d.strip()) )
        if len(self.dsout) == 0:
787
            raise DatasetPluginException(_('Needs at least one output dataset'))
788 789 790 791 792 793 794 795 796

        return self.dsout

    def updateDatasets(self, fields, helper):
        """Do chopping of dataset."""

        ds_in = helper.getDataset(fields['ds_in'])
        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr

797
        plen = len(data) / len(self.dsout)
798 799 800 801 802 803 804 805 806 807 808 809
        for i, ds in enumerate(self.dsout):
            minv, maxv = int(plen*i), int(plen*(i+1))
            pserr = pperr = pnerr = None
            pdata = data[minv:maxv]
            if serr is not None: pserr = serr[minv:maxv]
            if perr is not None: pperr = perr[minv:maxv]
            if nerr is not None: pnerr = nerr[minv:maxv]
            ds.update(data=pdata, serr=pserr, perr=pperr, nerr=pnerr)

class ThinDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to thin datasets."""

810
    menu = (_('Split'), _('Thin'),)
811
    name = 'Thin'
812 813 814
    description_short = _('Select data points at intervals from dataset')
    description_full = _('Select data points at intervals from dataset '
                         'to create new dataset')
815 816 817 818

    def __init__(self):
        """Define fields."""
        self.fields = [
819 820 821 822
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldInt('start', _('Starting index (from 1)'), default=1),
            field.FieldInt('interval', _('Interval between data points'), default=1),
            field.FieldDataset('ds_out', _('Output dataset name')),
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
            ]

    def updateDatasets(self, fields, helper):
        """Do thinning of dataset."""

        ds_in = helper.getDataset(fields['ds_in'])
        start = fields['start']
        interval = fields['interval']

        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr

        data = data[start-1::interval]
        if serr is not None: serr = serr[start-1::interval]
        if perr is not None: perr = perr[start-1::interval]
        if nerr is not None: nerr = nerr[start-1::interval]

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class MeanDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to mean datasets together."""

844
    menu = (_('Compute'), _('Mean of datasets'),)
845
    name = 'Mean'
846 847 848
    description_short = _('Compute mean of datasets')
    description_full = _('Compute mean of multiple datasets to create '
                         'a single dataset.')
849 850 851 852

    def __init__(self):
        """Define fields."""
        self.fields = [
853 854
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldDataset('ds_out', _('Output dataset name')),
855 856 857 858 859 860 861
            ]

    def updateDatasets(self, fields, helper):
        """Compute means of dataset."""

        inds = helper.getDatasets(fields['ds_in'])
        if len(inds) == 0:
862
            raise DatasetPluginException(_('Requires one or more input datasets'))
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
        maxlength = max( [len(d.data) for d in inds] )

        # mean data (only use finite values)
        tot = N.zeros(maxlength, dtype=N.float64)
        num = N.zeros(maxlength, dtype=N.int)
        for d in inds:
            f = N.isfinite(d.data)
            tot[f] += d.data[f]
            num[f] += 1
        data = tot / num

        def averageError(errtype, fallback=None):
            """Get average for an error value."""
            tot = N.zeros(maxlength, dtype=N.float64)
            num = N.zeros(maxlength, dtype=N.int)
            for d in inds:
                vals = getattr(d, errtype)
                if vals is None and fallback:
                    vals = getattr(d, fallback)

                # add values if not missing
                if vals is not None:
                    f = N.isfinite(vals)
                    tot[f] += (vals[f]) ** 2
                    num[f] += 1
                else:
                    # treat as zero errors if missing errors
                    num[:len(d.data)] += 1
            return N.sqrt(tot) / num

        # do error bar handling
        serr = perr = nerr = None
        errortype = errorBarType(inds)
        if errortype == 'symmetric':
            serr = averageError('serr')
        elif errortype == 'asymmetric':
            perr = averageError('perr', fallback='serr')
            nerr = -averageError('nerr', fallback='serr')

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class AddDatasetsPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to mean datasets together."""

907
    menu = (_('Add'), _('Datasets'),)
908
    name = 'Add Datasets'
909 910 911
    description_short = _('Add two or more datasets together')
    description_full = _('Add datasets together to make a single dataset. '
                         'Error bars are combined.')
912 913 914 915

    def __init__(self):
        """Define fields."""
        self.fields = [
916 917
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldDataset('ds_out', _('Output dataset name')),
918 919 920 921 922 923 924
            ]

    def updateDatasets(self, fields, helper):
        """Compute means of dataset."""

        inds = helper.getDatasets(fields['ds_in'])
        if len(inds) == 0:
925
            raise DatasetPluginException(_('Requires one or more input datasets'))
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
        maxlength = max( [len(d.data) for d in inds] )

        # add data where finite
        data = N.zeros(maxlength, dtype=N.float64)
        anyfinite = N.zeros(maxlength, dtype=N.bool)
        for d in inds:
            f = N.isfinite(d.data)
            data[f] += d.data[f]
            anyfinite[f] = True
        data[N.logical_not(anyfinite)] = N.nan

        # handle error bars
        serr, perr, nerr = combineAddedErrors(inds, maxlength)

        # update output dataset
        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class SubtractDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to subtract two datasets."""

946
    menu = (_('Subtract'), _('Datasets'),)
947
    name = 'Subtract Datasets'
948 949 950
    description_short = _('Subtract two datasets')
    description_full = _('Subtract two datasets. '
                         'Combined error bars are also calculated.')
951 952 953 954
    
    def __init__(self):
        """Define fields."""
        self.fields = [
955 956 957
            field.FieldDataset('ds_in1', _('Input dataset 1')),
            field.FieldDataset('ds_in2', _('Input dataset 2')),
            field.FieldDataset('ds_out', _('Output dataset name')),
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
            ]

    def updateDatasets(self, fields, helper):
        """Do scaling of dataset."""

        dsin1 = helper.getDataset(fields['ds_in1'])
        dsin2 = helper.getDataset(fields['ds_in2'])

        minlength = min( len(dsin1.data), len(dsin2.data) )
        data = dsin1.data[:minlength] - dsin2.data[:minlength]

        # computing error bars is non trivial!
        serr = perr = nerr = None
        errortype = errorBarType([dsin1, dsin2])
        if errortype == 'symmetric':
            serr1 = serr2 = 0
            if dsin1.serr is not None:
                serr1 = dsin1.serr[:minlength]
            if dsin2.serr is not None:
                serr2 = dsin2.serr[:minlength]
            serr = N.sqrt(serr1**2 + serr2**2)
        elif errortype == 'asymmetric':
            perr1 = perr2 = nerr1 = nerr2 = 0
            if dsin1.serr is not None:
                perr1 = nerr1 = dsin1.serr[:minlength]
            else:
                if dsin1.perr is not None: perr1 = dsin1.perr[:minlength]
                if dsin1.nerr is not None: nerr1 = dsin1.nerr[:minlength]
            if dsin2.serr is not None:
                perr2 = nerr2 = dsin2.serr[:minlength]
            else:
                if dsin2.perr is not None: perr2 = dsin2.perr[:minlength]
                if dsin2.nerr is not None: nerr2 = dsin2.nerr[:minlength]
            perr = N.sqrt(perr1**2 + nerr2**2)
            nerr = -N.sqrt(nerr1**2 + perr2**2)

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

996 997 998
class SubtractMeanDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to subtract mean from dataset."""

999
    menu = (_('Subtract'), _('Mean'),)
1000
    name = 'Subtract Mean'
1001 1002 1003
    description_short = _('Subtract mean from dataset')
    description_full = _('Subtract mean from dataset,'
                         ' optionally dividing by standard deviation.')
1004 1005 1006 1007

    def __init__(self):
        """Define fields."""
        self.fields = [
1008 1009 1010
            field.FieldDataset('ds_in', _('Input dataset 1')),
            field.FieldBool('divstddev', _('Divide by standard deviation')),
            field.FieldDataset('ds_out', _('Output dataset name')),
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
            ]

    def updateDatasets(self, fields, helper):
        """Do scaling of dataset."""

        dsin = helper.getDataset(fields['ds_in'])

        vals = dsin.data
        mean = vals[N.isfinite(vals)].mean()
        vals = vals - mean

        if fields['divstddev']:
            vals /= vals[N.isfinite(vals)].std()

        self.dsout.update(
            data=vals, serr=dsin.serr, perr=dsin.perr, nerr=dsin.nerr)

class SubtractMinimumDatasetPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to subtract minimum from dataset."""

1031
    menu = (_('Subtract'), _('Minimum'),)
1032
    name = 'Subtract Minimum'
1033 1034
    description_short = _('Subtract minimum from dataset')
    description_full = _('Subtract the minimum value from a dataset')
1035 1036 1037 1038

    def __init__(self):
        """Define fields."""
        self.fields = [
1039 1040
            field.FieldDataset('ds_in', _('Input dataset 1')),
            field.FieldDataset('ds_out', _('Output dataset name')),
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
            ]

    def updateDatasets(self, fields, helper):
        """Do scaling of dataset."""

        dsin = helper.getDataset(fields['ds_in'])

        vals = dsin.data
        minval = vals[N.isfinite(vals)].min()
        vals = vals - minval

        self.dsout.update(
            data=vals, serr=dsin.serr, perr=dsin.perr, nerr=dsin.nerr)

1055 1056 1057
class MultiplyDatasetsPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to multiply two or more datasets."""

1058
    menu = (_('Multiply'), _('Datasets'),)
1059
    name = 'Multiply Datasets'
1060 1061 1062
    description_short = _('Multiply two or more datasets')
    description_full = _('Multiply two or more datasets. '
                         'Combined error bars are also calculated.')
1063 1064 1065 1066
    
    def __init__(self):
        """Define fields."""
        self.fields = [
1067 1068
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldDataset('ds_out', _('Output dataset name')),
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
            ]

    def updateDatasets(self, fields, helper):
        """Multiply the datasets."""

        names = fields['ds_in']
        inds = [ helper.getDataset(d) for d in names ]
        maxlength = max( [d.data.shape[0] for d in inds] )

        # output data and where data is finite
        data = N.ones(maxlength, dtype=N.float64)
        anyfinite = N.zeros(maxlength, dtype=N.bool)
        for d in inds:
            f = N.isfinite(d.data)
            anyfinite[f] = True
            data[f] *= d.data[f]

        # where always NaN, make NaN
        data[N.logical_not(anyfinite)] = N.nan

        # get error bars
        serr, perr, nerr = combineMultipliedErrors(inds, maxlength, data)

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class DivideDatasetsPlugin(_OneOutputDatasetPlugin):
    """Dataset plugin to divide two datasets."""

1097
    menu = (_('Divide'), _('Datasets'),)
1098
    name = 'Divide Datasets'
1099 1100 1101
    description_short = _('Compute ratio or fractional difference'
                          ' between two datasets')
    description_full = _('Divide or compute fractional difference'
1102 1103 1104 1105 1106
                         ' between two datasets')
    
    def __init__(self):
        """Define fields."""
        self.fields = [
1107 1108 1109
            field.FieldDataset('ds_in1', _('Input dataset 1')),
            field.FieldDataset('ds_in2', _('Input dataset 2')),
            field.FieldBool('frac', _('Compute fractional difference'),
1110
                            default=False),
1111
            field.FieldDataset('ds_out', _('Output dataset name')),
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
            ]

    def updateDatasets(self, fields, helper):
        """Compute ratio."""

        inds1 = helper.getDataset( fields['ds_in1'] )
        inds2 = helper.getDataset( fields['ds_in2'] )
        length = min( len(inds1.data), len(inds2.data) )

        # compute ratio
        data = inds1.data[:length] / inds2.data[:length]

        # get error bars
        serr, perr, nerr = combineMultipliedErrors([inds1, inds2], length, data)

        # convert to fractional difference (if reqd)
        if fields['frac']:
            data -= 1

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
class DivideMaxPlugin(_OneOutputDatasetPlugin):
    """Plugin to divide by maximum of dataset."""

    menu = (_('Divide'), _('By maximum'),)
    name = 'Divide Maximum'
    description_short = description_full = _('Divide dataset by its maximum')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldDataset('ds_out', _('Output dataset name')),
            ]

    def updateDatasets(self, fields, helper):

        inds = helper.getDataset( fields['ds_in'] )
        maxval = N.nanmax( inds.data )

        # divide data
        data = inds.data / maxval
        # divide error bars
        serr = perr = nerr = None
        if inds.serr: serr = inds.serr / maxval
        if inds.perr: perr = inds.perr / maxval
        if inds.nerr: nerr = inds.nerr / maxval

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

class DivideNormalizePlugin(_OneOutputDatasetPlugin):
    """Plugin to normalize dataset."""

    menu = (_('Divide'), _('Normalize'),)
    name = 'Normalize'
    description_short = description_full = _(
        'Divide dataset by its sum of values')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldDataset('ds_out', _('Output dataset name')),
            ]

    def updateDatasets(self, fields, helper):

        inds = helper.getDataset( fields['ds_in'] )
        tot = N.nansum( inds.data )

        # divide data
        data = inds.data / tot
        # divide error bars
        serr = perr = nerr = None
        if inds.serr: serr = inds.serr / tot
        if inds.perr: perr = inds.perr / tot
        if inds.nerr: nerr = inds.nerr / tot

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

1192 1193 1194
class ExtremesDatasetPlugin(DatasetPlugin):
    """Dataset plugin to get extremes of dataset."""

1195
    menu = (_('Compute'), _('Dataset extremes'),)
1196
    name = 'Extremes'
1197 1198 1199
    description_short = _('Compute extreme values of input datasets')
    description_full = _('Compute extreme values of input datasets. Creates '
                         'minimum and maximum datasets.')
1200 1201 1202 1203

    def __init__(self):
        """Define fields."""
        self.fields = [
1204 1205 1206 1207 1208 1209
            field.FieldDatasetMulti('ds_in', _('Input datasets')),
            field.FieldBool('errorbars', _('Include error bars')),
            field.FieldDataset('ds_min', _('Output minimum dataset (optional)')),
            field.FieldDataset('ds_max', _('Output maximum dataset (optional)')),
            field.FieldDataset('ds_errorbar', _('Output range as error bars '
                                                'in dataset (optional)')),
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
            ]

    def getDatasets(self, fields):
        """Returns output dataset."""
        dsout = []
        self.dsmin = self.dsmax = self.dserror = None
        if fields['ds_min'] != '':
            self.dsmin = Dataset1D(fields['ds_min'])
            dsout.append(self.dsmin)
        if fields['ds_max'] != '':
            self.dsmax = Dataset1D(fields['ds_max'])
            dsout.append(self.dsmax)
        if fields['ds_errorbar'] != '':
            self.dserror = Dataset1D(fields['ds_errorbar'])
            dsout.append(self.dserror)
        if not dsout:
1226
            raise DatasetPluginException(_('Provide at least one output dataset'))
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
        return dsout

    def updateDatasets(self, fields, helper):
        """Compute extremes of datasets."""

        names = fields['ds_in']
        inds = [ helper.getDataset(d) for d in names ]
        maxlength = max( [d.data.shape[0] for d in inds] )

        minvals = N.zeros(maxlength, dtype=N.float64) + 1e100
        maxvals = N.zeros(maxlength, dtype=N.float64) - 1e100
        anyfinite = N.zeros(maxlength, dtype=N.bool)
        for d in inds:
            f = N.isfinite(d.data)
            anyfinite[f] = True

            v = d.data
            if fields['errorbars']:
                if d.serr is not None:
                    v = v - d.serr
                elif d.nerr is not None:
                    v = v + d.nerr
            minvals[f] = N.min( (minvals[f], v[f]), axis=0 )

            v = d.data
            if fields['errorbars']:
                if d.serr is not None:
                    v = v + d.serr
                elif d.perr is not None:
                    v = v + d.perr
            maxvals[f] = N.max( (maxvals[f], v[f]), axis=0 )

        minvals[N.logical_not(anyfinite)] = N.nan
        maxvals[N.logical_not(anyfinite)] = N.nan

        if self.dsmin is not None:
            self.dsmin.update(data=minvals)
        if self.dsmax is not None:
            self.dsmax.update(data=maxvals)
        if self.dserror is not None:
            # compute mean and look at differences from it
            tot = N.zeros(maxlength, dtype=N.float64)
            num = N.zeros(maxlength, dtype=N.int)
            for d in inds:
                f = N.isfinite(d.data)
                tot[f] += d.data[f]
                num[f] += 1
            mean = tot / num
            self.dserror.update(data=mean, nerr=minvals-mean, perr=maxvals-mean)

1277 1278 1279
class CumulativePlugin(_OneOutputDatasetPlugin):
    """Compute cumulative values."""

1280
    menu = (_('Compute'), _('Cumulative value'),)
1281
    name = 'Cumulative'
1282 1283 1284 1285
    description_short = _('Compute the cumulative value of a dataset')
    description_full = _('Compute the cumulative value of a dataset. '
                         ' Error bars are combined.\n'
                         'Default behaviour is to accumulate from start.')
1286 1287 1288 1289

    def __init__(self):
        """Define fields."""
        self.fields = [
1290 1291 1292
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldBool('fromend', _('Compute cumulative value from end')),
            field.FieldDataset('ds_out', _('Output dataset')),
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
            ]

    def updateDatasets(self, fields, helper):
        """Do accumulation."""

        ds_in = helper.getDataset(fields['ds_in'])
        fromend = fields['fromend']

        def cumsum(v):
            """Compute cumulative, handing nans and reverse."""
            v = N.array(v)
            if fromend: v = v[::-1]
            v[ N.logical_not(N.isfinite(v)) ] = 0.
            c = N.cumsum(v)
            if fromend: c = c[::-1]
            return c

        # compute cumulative values
        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr
        data = cumsum(data)
        if serr is not None: serr = N.sqrt( cumsum(serr**2) )
        if perr is not None: perr = N.sqrt( cumsum(perr**2) )
        if nerr is not None: nerr = -N.sqrt( cumsum(nerr**2) )
        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

1318 1319 1320
class DemultiplexPlugin(DatasetPlugin):
    """Dataset plugin to split a dataset into multiple datasets, element-by-element."""

1321
    menu = (_('Split'), _('Element by element'),)
1322
    name = 'Demultiplex'
1323 1324 1325 1326 1327
    description_short = _('Split dataset into multiple datasets element-by-element')
    description_full = _('Split dataset into multiple datasets on an '
                         'element-by-element basis.\n'
                         'e.g. 1, 2, 3, 4, 5, 6 could be converted to '
                         '1, 3, 5 and 2, 4, 6.')
1328 1329 1330 1331

    def __init__(self):
        """Define fields."""
        self.fields = [
1332 1333
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldDatasetMulti('ds_out', _('Output datasets')),
1334 1335 1336 1337 1338 1339
            ]

    def getDatasets(self, fields):
        """Returns demuxed output datasets."""
        names = [n.strip() for n in fields['ds_out'] if n.strip() != '']
        if len(names) == 0:
1340
            raise DatasetPluginException(_('Requires at least one output dataset'))
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

        self.ds_out = [ Dataset1D(n) for n in names ]
        return self.ds_out

    def updateDatasets(self, fields, helper):
        """Compute means of dataset."""

        ds_in = helper.getDataset( fields['ds_in'] )

        num = len(self.ds_out)
        for i, ds in enumerate(self.ds_out):
            data = ds_in.data[i::num]
            serr = nerr = perr = None
            if ds_in.serr is not None:
                serr = ds_in.serr[i::num]
            if ds_in.perr is not None:
                perr = ds_in.perr[i::num]
            if ds_in.nerr is not None:
                nerr = ds_in.nerr[i::num]
            ds.update(data=data, serr=serr, perr=perr, nerr=nerr)

class PolarToCartesianPlugin(DatasetPlugin):
    """Convert from r,theta to x,y coordinates."""

1365
    menu = (_('Convert'), _('Polar to Cartesian'),)
1366
    name = 'PolarToCartesian'
1367 1368 1369
    description_short = _('Convert r,theta coordinates to x,y coordinates')
    description_full = _('Convert r,theta coordinates to x,y coordinates.\n'
                         'Error bars are ignored.')
1370 1371 1372 1373

    def __init__(self):
        """Define fields."""
        self.fields = [
1374 1375 1376
            field.FieldDataset('r_in', _('Input dataset (r)')),
            field.FieldDataset('theta_in', _('Input dataset (theta)')),
            field.FieldCombo('units', _('Angular units'),
1377 1378
                             items=('radians', 'degrees'),
                             editable=False),
1379 1380
            field.FieldDataset('x_out', _('Output dataset (x)')),
            field.FieldDataset('y_out', _('Output dataset (y)')),
1381 1382 1383 1384 1385
            ]

    def getDatasets(self, fields):
        """Returns x and y output datasets."""
        if fields['x_out'] == '':
1386
            raise DatasetPluginException(_('Invalid output x dataset name'))
1387
        if fields['y_out'] == '':
1388
            raise DatasetPluginException(_('Invalid output y dataset name'))
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        self.x_out = Dataset1D(fields['x_out'])
        self.y_out = Dataset1D(fields['y_out'])
        return [self.x_out, self.y_out]

    def updateDatasets(self, fields, helper):
        """Compute means of dataset."""

        ds_r = helper.getDataset( fields['r_in'] ).data
        ds_theta = helper.getDataset( fields['theta_in'] ).data
        if fields['units'] == 'degrees':
            # convert to radians
            ds_theta = ds_theta * (N.pi / 180.)

        x = ds_r * N.cos(ds_theta)
        y = ds_r * N.sin(ds_theta)
        self.x_out.update(data=x)
        self.y_out.update(data=y)

class FilterDatasetPlugin(_OneOutputDatasetPlugin):
1408
    """Dataset plugin to filter a dataset using an expression."""
1409

1410
    menu = (_('Filter'), _('Expression'),)
1411
    name = 'FilterExpression'
1412 1413 1414
    description_short = _('Filter a dataset using an expression')
    description_full = _('Filter a dataset using an expression, '
                         'e.g. "x>10" or "(x>1) & (y<2)"')
1415 1416 1417 1418
    
    def __init__(self):
        """Define fields."""
        self.fields = [
1419 1420 1421 1422
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldText('filter', _('Filter expression')),
            field.FieldBool('replacenan', _('Replace excluded points by NaN\n'
                                            '(indicate missing points)'),
1423
                            default=False),
1424
            field.FieldDataset('ds_out', _('Output dataset')),
1425 1426 1427 1428 1429 1430
            ]

    def updateDatasets(self, fields, helper):
        """Do shifting of dataset."""
        ds_in = helper.getDataset(fields['ds_in'])
        filt = helper.evaluateExpression(fields['filter'])
1431

1432 1433
        data, serr, perr, nerr = ds_in.data, ds_in.serr, ds_in.perr, ds_in.nerr

1434 1435 1436 1437 1438 1439 1440
        if filt is None:
            # select nothing
            filt = N.zeros(data.shape, dtype=N.bool)
        else:
            # filter must have int/bool type
            filt = N.array(filt, dtype=N.bool)

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
        try:
            if fields['replacenan']:
                # replace bad points with nan
                data = data.copy()
                data[N.logical_not(filt)] = N.nan
            else:
                # just select good points
                data = data[filt]
                if serr is not None: serr = serr[filt]
                if perr is not None: perr = perr[filt]
                if nerr is not None: nerr = nerr[filt]
1452 1453 1454
        except (ValueError, IndexError) as e:
            raise DatasetPluginException(_("Error filtering dataset: '%s')") %
                                         cstr(e))
1455 1456 1457

        self.dsout.update(data=data, serr=serr, perr=perr, nerr=nerr)

1458 1459 1460
class MovingAveragePlugin(_OneOutputDatasetPlugin):
    """Compute moving average for dataset."""

1461
    menu = (_('Filtering'), _('Moving Average'),)
1462
    name = 'MovingAverage'
1463 1464 1465 1466
    description_short = _('Compute moving average for regularly spaced data')
    description_full = _('Compute moving average for regularly spaced data.'
                         'Average is computed either\nside of each data point '
                         'by number of points given.')
1467 1468 1469 1470
    
    def __init__(self):
        """Define fields."""
        self.fields = [
1471 1472
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldInt('width', _('Points either side of point to average'),
1473
                           default=1, minval=0),
1474
            field.FieldBool('weighterrors', _('Weight by error bars'),
1475
                            default=True),
1476
            field.FieldDataset('ds_out', _('Output dataset')),
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
            ]
    
    def updateDatasets(self, fields, helper):
        """Do shifting of dataset."""
        ds_in = helper.getDataset(fields['ds_in'])
        weights = None
        if fields['weighterrors']:
            if ds_in.serr is not None:
                weights = 1. / ds_in.serr**2
            elif ds_in.perr is not None and ds_in.nerr is not None:
                weights = 1. / ( (ds_in.perr**2+ds_in.nerr**2)/2. )
        width = fields['width']
        data = qtloops.rollingAverage(ds_in.data, weights, width)
        self.dsout.update(data=data)

class LinearInterpolatePlugin(_OneOutputDatasetPlugin):
    """Do linear interpolation of data."""

1495
    menu = (_('Filtering'), _('Linear interpolation'),)
1496
    name = 'LinearInterpolation'
1497 1498 1499 1500 1501
    description_short = _('Linear interpolation of x,y data')
    description_full = _("Compute linear interpolation of x,y data.\n"
                         "Given datasets for y = f(x), compute y' = f(x'), "
                         "using linear interpolation.\n"
                         "Assumes x dataset increases in value.")
1502 1503 1504 1505

    def __init__(self):
        """Define fields."""
        self.fields = [
1506 1507 1508 1509 1510
            field.FieldDataset('ds_x', _('Input dataset x')),
            field.FieldDataset('ds_y', _('Input dataset y')),
            field.FieldDataset('ds_xprime', _("Input dataset x'")),
            field.FieldBool('edgenan', _('Use nan for values outside x range')),
            field.FieldDataset('ds_out', _("Output dataset y'")),
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
            ]

    def updateDatasets(self, fields, helper):
        """Do shifting of dataset."""
        ds_x = helper.getDataset(fields['ds_x']).data
        ds_y = helper.getDataset(fields['ds_y']).data
        ds_xprime = helper.getDataset(fields['ds_xprime']).data

        minlenin = min( len(ds_x), len(ds_y) )
        pad = None
        if fields['edgenan']:
            pad = N.nan

        interpol = N.interp(ds_xprime,
                            ds_x[:minlenin], ds_y[:minlenin],
                            left=pad, right=pad)

        self.dsout.update(data=interpol)

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
class ReBinXYPlugin(DatasetPlugin):
    """Bin-up data by factor given."""

    menu = (_('Filtering'), _('Bin X,Y'))
    name = 'RebinXY'
    description_short = 'Bin every N datapoints'
    description_full = ('Given dataset Y (and optionally X), for every N '
                        'datapoints calculate the binned value. For '
                        'dataset Y this is the sum or mean of every N '
                        'datapoints. For X this is the midpoint of the '
                        'datapoints (using error bars to give the range.')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_y', _('Input dataset Y')),
            field.FieldDataset('ds_x', _('Input dataset X (optional)')),
            field.FieldInt('binsize', _('Bin size (N)'),
                           minval=1, default=2),
            field.FieldCombo('mode', _('Mode of binning'),
                             items=('sum', 'average'),
                             editable=False),
            field.FieldDataset('ds_yout', _("Output Y'")),
            field.FieldDataset('ds_xout', _("Output X' (optional)")),
            ]

    def getDatasets(self, fields):
        """Return output datasets"""
        if fields['ds_yout'] == '':
1559
            raise DatasetPluginException(_('Invalid output Y dataset name'))
1560
        if fields['ds_x'] != '' and fields['ds_xout'] == '':
1561
            raise DatasetPluginException(_('Invalid output X dataset name'))
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

        self.dssout = out = [ Dataset1D(fields['ds_yout']) ]
        if fields['ds_xout'] != '':
            out.append(Dataset1D(fields['ds_xout']))
        return out

    def updateDatasets(self, fields, helper):
        """Do binning."""

        binsize = fields['binsize']
        average = fields['mode'] == 'average'

        def binerr(err):
            """Compute binned error."""
            if err is None:
                return None
            err2 = qtloops.binData(err**2, binsize, False)
            cts = qtloops.binData(N.ones(err.shape), binsize, False)
            return N.sqrt(err2) / cts

        # bin up data and calculate errors (if any)
        dsy = helper.getDataset(fields['ds_y'])
        binydata = qtloops.binData(dsy.data, binsize, average)
        binyserr = binerr(dsy.serr)
        binyperr = binerr(dsy.perr)
        binynerr = binerr(dsy.nerr)

        self.dssout[0].update(data=binydata, serr=binyserr, perr=binyperr, nerr=binynerr)

        if len(self.dssout) == 2:
            # x datasets
            dsx = helper.getDataset(fields['ds_x'])

            # Calculate ranges between adjacent binned points.  This
            # is horribly messy - we have to account for the fact
            # there might not be error bars and calculate the midpoint
            # to the previous/next point.
            minvals = N.array(dsx.data)
            if dsx.serr is not None:
                minvals -= dsx.serr
            elif dsx.nerr is not None:
                minvals += dsx.nerr
            else:
                minvals = 0.5*(dsx.data[1:] + dsx.data[:-1])
                if len(dsx.data) > 2:
                    # assume +ve error bar on last point is as big as its -ve error
                    minvals = N.insert(minvals, 0, dsx.data[0] - 0.5*(
                            dsx.data[1] - dsx.data[0]))
                elif len(dsx.data) != 0:
                    # no previous point so we assume 0 error
                    minvals = N.insert(minvals, 0, dsx.data[0])

            maxvals = N.array(dsx.data)
            if dsx.serr is not None:
                maxvals += dsx.serr
            elif dsx.perr is not None:
                maxvals += dsx.perr
            else:
                maxvals = 0.5*(dsx.data[1:] + dsx.data[:-1])
                if len(dsx.data) > 2:
                    maxvals = N.append(maxvals, dsx.data[-1] + 0.5*(
                            dsx.data[-1] - dsx.data[-2]))
                elif len(dsx.data) != 0:
                    maxvals = N.append(maxvals, dsx.data[-1])

            minbin = minvals[::binsize]
            maxbin = maxvals[binsize-1::binsize]
            if len(minbin) > len(maxbin):
                # not an even number of bin size
                maxbin = N.append(maxbin, maxvals[-1])

            self.dssout[1].update(data=0.5*(minbin+maxbin),
                                  serr=0.5*(maxbin-minbin))

class SortPlugin(_OneOutputDatasetPlugin):
    """Sort a dataset."""

    menu = (_('Compute'), _('Sorted'),)
    name = 'Sort'
    description_short = description_full = _('Sort a dataset')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldDataset('ds_sort', _('Sort by (optional)')),
            field.FieldBool('reverse', _('Reverse')),
            field.FieldDataset('ds_out', _('Output dataset')),
            ]

    def updateDatasets(self, fields, helper):
        """Do sorting of dataset."""

        ds_sort = ds = helper.getDataset(fields['ds_in'])
        if fields['ds_sort'].strip():
            ds_sort = helper.getDataset(fields['ds_sort'])

        idxs = N.argsort(ds_sort.data)
        if fields['reverse']:
            idxs = idxs[::-1]

        out = { 'data': ds.data[idxs] }
        if ds.serr is not None: out['serr'] = ds.serr[idxs]
        if ds.perr is not None: out['perr'] = ds.perr[idxs]
        if ds.nerr is not None: out['nerr'] = ds.nerr[idxs]

        self.dsout.update(**out)

class SortTextPlugin(_OneOutputDatasetPlugin):
    """Sort a text dataset."""

    menu = (_('Compute'), _('Sorted Text'),)
    name = 'Sort Text'
    description_short = description_full = _('Sort a text dataset')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_in', _('Input dataset'), datatype='text'),
            field.FieldDataset('ds_sort', _('Sort by (optional)')),
            field.FieldBool('reverse', _('Reverse')),
            field.FieldDataset('ds_out', _('Output dataset'), datatype='text'),
            ]

    def getDatasets(self, fields):
        """Returns single output dataset (self.dsout)."""
        if fields['ds_out'] == '':
1689
            raise DatasetPluginException(_('Invalid output dataset name'))
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
        self.dsout = DatasetText(fields['ds_out'])
        return [self.dsout]

    def updateDatasets(self, fields, helper):
        """Do sorting of dataset."""

        ds = helper.getTextDataset(fields['ds_in']).data
        if fields['ds_sort'].strip():
            ds_sort = helper.getDataset(fields['ds_sort'])
            idxs = N.argsort(ds_sort.data)
            dout = []
            for i in idxs:
                dout.append(ds[i])
        else:
            dout = list(ds)
            dout.sort()

        if fields['reverse']:
            dout = dout[::1]
        self.dsout.update(dout)

class Histogram2D(DatasetPlugin):
    """Compute 2D histogram for two 1D dataset.

    Algorithm: Count up values in boxes. Sort. Compute probability
    working downwards.
    """

    menu = (_('Compute'), _('2D histogram'),)
    name = 'Histogram 2D'
    description_short = _('Compute 2D histogram.')
    description_full = _('Given two 1D datasets, compute a 2D histogram. '
                         'Can optionally compute a probability distribution.')

    def __init__(self):
        """Input fields."""
        self.fields = [
            field.FieldDataset('ds_inx', _('Input dataset x')),
            field.FieldFloatOrAuto('minx', _('Minimum value for dataset x')),
            field.FieldFloatOrAuto('maxx', _('Maximum value for dataset x')),
            field.FieldInt('binsx', _('Number of bins for dataset x'),
                           default=10, minval=2),

            field.FieldDataset('ds_iny', _('Input dataset y')),
            field.FieldFloatOrAuto('miny', _('Minimum value for dataset y')),
            field.FieldFloatOrAuto('maxy', _('Maximum value for dataset y')),
            field.FieldInt('binsy', _('Number of bins for dataset y'),
                           default=10, minval=2),

            field.FieldCombo('mode', _('Mode'),
                             items=('Count',
                                    'Fraction',
                                    'CumulativeProbability',
                                    'CumulativeProbabilityInverse'),
                             default='Count', editable=False),

            field.FieldDataset('ds_out', _('Output 2D dataset'), dims=2),
            ]

    def probabilityCalculator(self, histo):
        """Convert an image of counts to a cumulative probability
        distribution.
        """

        # get sorted pixel values
        pixvals = N.ravel(histo)
        sortpix = N.sort(pixvals)

        # cumulative sum of values
        probs = N.cumsum(sortpix)
        probs = probs * (1./probs[-1])

        # values in pixvals which are unique
        unique = N.concatenate( (sortpix[:-1] != sortpix[1:], [True]) )

        # now we have the pixel values and probabilities
        pixaxis = sortpix[unique]
        probaxis = probs[unique]

        # use linear interpolation to map the pixvals -> cumulative probability
        probvals = N.interp(pixvals, pixaxis, probaxis)
        probvals = probvals.reshape(histo.shape)
        return probvals

    def updateDatasets(self, fields, helper):
        """Calculate values of output dataset."""

        dsy = helper.getDataset(fields['ds_iny']).data
        dsx = helper.getDataset(fields['ds_inx']).data

        # use range of data or specified parameters
        miny = fields['miny']
        if miny == 'Auto': miny = N.nanmin(dsy)
        maxy = fields['maxy']
        if maxy == 'Auto': maxy = N.nanmax(dsy)
        minx = fields['minx']
        if minx == 'Auto': minx = N.nanmin(dsx)
        maxx = fields['maxx']
        if maxx == 'Auto': maxx = N.nanmax(dsx)

        # compute counts in each bin
        histo, xedge, yedge = N.histogram2d(
            dsy, dsx, bins=[fields['binsy'], fields['binsx']],
            range=[[miny,maxy], [minx,maxx]], normed=False)

        m = fields['mode']
        if m == 'Count':
            out = histo
        elif m == 'Fraction':
            out = histo * (1./N.sum(histo))
        elif m == 'CumulativeProbability':
            out = self.probabilityCalculator(histo)
        elif m == 'CumulativeProbabilityInverse':
            out = 1. - self.probabilityCalculator(histo)

        # update output dataset
        self.dsout.update(out, rangey=(miny, maxy), rangex=(minx, maxx))

    def getDatasets(self, fields):
        """Returns single output dataset (self.dsout)."""
        if fields['ds_out'] == '':
1811
            raise DatasetPluginException(_('Invalid output dataset name'))
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
        self.dsout = Dataset2D(fields['ds_out'])
        return [self.dsout]

class ConvertNumbersToText(DatasetPlugin):
    """Convert a set of numbers to text."""

    menu = (_('Convert'), _('Numbers to Text'),)
    name = 'NumbersToText'
    description_short = _('Convert numeric dataset to text')
    description_full = _('Given a 1D numeric dataset, create a text dataset '
                         'by applying formatting. Format string is in standard '
                         'Veusz-extended C formatting, e.g.\n'
                         ' "%Vg" - general,'
                         ' "%Ve" - scientific,'
                         ' "%VE" - engineering suffix,'
                         ' "%.2f" - two decimal places and'
                         ' "%e" - C-style scientific')

    def __init__(self):
        """Define fields."""
        self.fields = [
            field.FieldDataset('ds_in', _('Input dataset')),
            field.FieldText('format', _('Format'), default='%Vg'),
            field.FieldDataset('ds_out', _('Output dataset name')),
            ]

    def getDatasets(self, fields):
        if fields['ds_out'] == '':
1840
            raise DatasetPluginException(_('Invalid output dataset name'))
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
        self.dsout = DatasetText(fields['ds_out'])
        return [self.dsout]

    def updateDatasets(self, fields, helper):
        """Convert dataset."""
        ds_in = helper.getDataset(fields['ds_in'])
        f = fields['format']

        data = [ utils.formatNumber(n, f, locale=helper.locale)
                 for n in ds_in.data ]

        self.dsout.update(data=data)

1854 1855 1856 1857
datasetpluginregistry += [
    AddDatasetPlugin,
    AddDatasetsPlugin,
    SubtractDatasetPlugin,
1858 1859
    SubtractMeanDatasetPlugin,
    SubtractMinimumDatasetPlugin,
1860 1861 1862
    MultiplyDatasetPlugin,
    MultiplyDatasetsPlugin,
    DivideDatasetsPlugin,
1863 1864
    DivideMaxPlugin,
    DivideNormalizePlugin,
1865
    MeanDatasetPlugin,
1866
    ExtremesDatasetPlugin,
1867
    CumulativePlugin,
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877

    ConcatenateDatasetPlugin,
    InterleaveDatasetPlugin,

    ChopDatasetPlugin,
    PartsDatasetPlugin,
    DemultiplexPlugin,
    ThinDatasetPlugin,

    PolarToCartesianPlugin,
1878
    ConvertNumbersToText,
1879 1880

    FilterDatasetPlugin,
1881 1882 1883

    MovingAveragePlugin,
    LinearInterpolatePlugin,
1884 1885 1886 1887 1888 1889
    ReBinXYPlugin,

    SortPlugin,
    SortTextPlugin,

    Histogram2D,
1890
    ]