mantidproject / mantid

Main repository for Mantid code
https://www.mantidproject.org
GNU General Public License v3.0
207 stars 122 forks source link

Simplify the process of creating table workspaces #37620

Open rboston628 opened 4 days ago

rboston628 commented 4 days ago

Is your feature request related to a problem? Please describe.

To create a table workspace with mantid's python API, it is currently necessary to perform the following steps:

# the info I want in the table
column1 = [1, 2, 3]
column2 = [4, 5, 6]

# getting it in the table
tab = CreateEmptyTableWorkspace()
tab.addColumn("column1")
tab.addColumn("column2")
for col1, col2 in zip(column1, column2):
    tab.addRow({"column1": col1, "column2": col2})

This is a lot of writing for something that should be fairly easy. Further, for extremely long tables (such as detector info tables), the process of inserting new rows one-by-one is expensive.

Describe the solution you'd like

The ITableWorkspace object already has a toDict() method. It should also have fromDict() method, so like:

# the info I want in the table
column1 = [1, 2, 3]
column2 = [4, 5, 6]

# getting it in the table
table_dict = {"column1": column1, "column2": column2}
tab = CreateEmptyTableWorkspace().fromDict(table_dict)

Or if not, an overload of the addColumn method, to work like so

# the info I want in the table
column1 = [1, 2, 3]
column2 = [4, 5, 6]

tab = CreateEmptyTableWorkspace()
# add entire columns at once
tab.addColumn("column1", column1)
tab.addColumn("column2", column2)

This would require updates inside the C++ code.

The C++ code suggests that the data inside a TableWorkspace is a vector m_columns of API::Column objects, which could be initialized with an entire vector of values, as suggested.

Describe alternatives you've considered

The "alternative" is to create table workspaces using the process currently accessible from the python API.

Possibly, exposing the API::Column object to the python API could offer a solution.

Additional context