Workspace Group

A WorkspaceGroup is a group of workspaces. The WorkspaceGroup object does not hold any data itself, but instead holds a list of Workspace objects. They appear as an expandable list of workspaces in the MantidPlot interface (the list of workspaces is also called the ADS or AnalysisDataService). Thus, workspace groups add structure to the ADS and make it more readable and also allow algorithms to be executed over a list of workspaces contained within the group but passing the group to the algorithm.

Most algorithms can be passed a WorkspaceGroup in place of a normal workspace input, and will simply execute the algorithm on each workspace contained within the group.

Working with Event Workspaces in Python

Creating and splitting groups

Workspace groups can be created through the MantidPlot interface;

  • Select a few workspaces from the ADS in MantidPlot and click the “Group” button above the list of workspaces. The group will be named “NewGroup”.

Workspace groups can be created in a more flexible way in the Python script window using the Python API. Groups may be created via the GroupWorkspaces algorithm, This will place a workspace group directly into the ADS, and requires at least one workspace to be added to the group.

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
ws3 = CreateSampleWorkspace()


# Create a group workpace
wsList = [ws1,ws2,ws3]
wsGroup = GroupWorkspaces(wsList)
# or
wsGroup = GroupWorkspaces("ws1,ws2,ws3")

print(wsGroup.getNames())

# Remove the group
# The child workspaces will be preserved
UnGroupWorkspace(wsGroup)
# Using wsGroup now will cause a runtime error
# RuntimeError: Variable invalidated, data has been deleted.

Output:

['ws1','ws2','ws3']

To avoid interaction with the ADS, a WorkspaceGroup object can be instantiated using

import mantid.api as api

ws_group = api.WorkspaceGroup()

This will not be automatically added to the ADS, to do so, use the following line

AnalysisDataService.add("name", ws_group)

the group should then appear in the ADS with the given name. Using direct instantiation; groups can be added to the ADS and then workspaces added to the group via their name and the add method;

from mantid.api import WorkspaceGroup

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
ws3 = CreateSampleWorkspace()

# Create a group workspace and add to the ADS
ws_group = WorkspaceGroup()
mtd.add("group1", ws_group)

ws_group.add("ws1")
ws_group.add("ws2")
ws_group.add("ws3")

print(ws_group.getNames())

Output:

['ws1','ws2','ws3']

Alternatively, workspace group objects can be fed workspaces which are not in the ADS (in this case the addWorkspace method is used rather than add because add requires a name, and since the workspaces are not in the ADS they may not have a name)

from mantid.api import WorkspaceGroup

ws1 = WorkspaceFactory.create("Workspace2D", 2, 2, 2)
ws2 = WorkspaceFactory.create("Workspace2D", 2, 2, 2)
ws3 = WorkspaceFactory.create("Workspace2D", 2, 2, 2)

# Create a group workspace
ws_group = WorkspaceGroup()

ws_group.addWorkspace(ws1)
ws_group.addWorkspace(ws2)
ws_group.addWorkspace(ws3)

print(ws_group.getNames())

mtd.add("group1", ws_group)

print(ws_group.getNames())

Output:

['','','']
['group1_1','group1_2','group1_3']

when adding the group to the ADS, the workspaces will also be added, and given default names. It is not recommended to add workspace to groups in this way, as much of the functionality of groups depends on workspaces having names; for example the “in” keyword.

Accessing Workspace Groups

The methods for getting a variable to an EventWorkspace is the same as shown in the Workspace help page.

If you want to check if a variable points to something that is a Workspace Group you can use this:

from mantid.api import WorkspaceGroup

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
wsGroup = GroupWorkspaces("ws1,ws2")

if isinstance(wsGroup, WorkspaceGroup):
    print(wsGroup.name() + " is an " + wsGroup.id())

Output:

wsGroup is an WorkspaceGroup

Looping over all of the members of a group

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
wsGroup = GroupWorkspaces("ws1,ws2")

print("Number of members: " + str(wsGroup.getNumberOfEntries()))
print("List of names: " + str(wsGroup.getNames()))

# Get the member workspaces in a loop
for i in range(wsGroup.getNumberOfEntries()):
    wsLoop = wsGroup.getItem(i)
    print("Member {0} {1}".format(i, wsLoop.getName()))

Output:

Number of members: 2
List of names: ['ws1','ws2']
Member 0 ws1
Member 1 ws2

Using Workspace Groups in Algorithms

You can pass workspace groups into any algorithm and Mantid will run that algorithm for each member of the workspace group.

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
wsGroup = GroupWorkspaces("ws1,ws2")
wsGroup2 = GroupWorkspaces("ws2,ws1")

#  This will add the member workspaces in a pair like manner
wsGroup3 = wsGroup + wsGroup2

# Rebin all of wsGroup
wsRebinned = Rebin(wsGroup, Params=200)

# You can still of course refer to members of a group directly
ws1 = Rebin(ws1, Params=100)

Using Nested Workspace Groups

It is possible to have groups within groups, the inner group can simply be added to the outer group in the usual way. For example

# create the following structure
# group1/
#       ws1
#       ws2
#       group2/
#               ws3
#               ws4

ws1 = CreateSampleWorkspace()
ws2 = CreateSampleWorkspace()
ws3 = CreateSampleWorkspace()
ws4 = CreateSampleWorkspace()

group1 = WorkspaceGroup()
group2 = WorkspaceGroup()

mtd.add("group1", group1)
mtd.add("group2", group2)

group1.add("ws1")
group1.add("ws2")
group2.add("ws3")
group2.add("ws4")

group1.add("group2")

Be careful when creating nested groups; every single group and workspace must have a unique name, if not workspaces with the same name will be overwritten. Do not try to group workspaces which are already in a group using groupWorkspace as this will create duplicate named workspaces in two groups; this will result in data being deleted without warning. Similarly don’t try to create duplicate named workspaces and put them into different folders.

One final note; it is best to add all workspaces to the ADS before configuring the grouping structure (as in the above code); otherwise you will only be able to name the top level group when you add the structure to the ADS. All the sub-groups and workspaces not already in the ADS will be given default names which you will then have to change manually, it is much easier to name them as you go (and putting them in the ADS is the only way to name them).

Other Information on Workspaces

  • Workspace - Overview of workspaces, which include the following classes:
    • MatrixWorkspace - A base class that contains among others:
      • WorkspaceSingleValue - Holds a single number (and X & error value, if desired). Mainly used for workspace algebra, e.g. to divide all bins in a 2D workspace by a single value.
      • Workspace2D - A workspace for holding two dimensional data in memory, this is the most commonly used workspace.
      • EventWorkspace - A workspace that retains the individual neutron event data.
    • TableWorkspace - A workspace holding data in rows of columns having a particular type (e.g. text, integer, ...).
    • WorkspaceGroup - A container for a collection of workspaces. Algorithms given a group as input run sequentially on each member of the group.

Category: Concepts