How to Programmatically Remove Data Transformations in Spotfire using IronPython

How to Programmatically Remove Data Transformations in Spotfire using IronPython

book

Article ID: KB0137747

calendar_today

Updated On:

Products Versions
Spotfire Analyst All

Description

When automating data manipulation within Spotfire, developers often need to manage existing data transformations applied to a data table. A common approach involves retrieving the transformations via methods like SourceView.GetAllOperations<DataOperation>(). Upon inspecting the returned collection object, development environments or introspection tools might display methods such as Clear() or Remove(), which are typically associated with collections that can be directly modified.

However, attempts to use these methods to alter the transformation list will result in a "SystemError: Collection is read-only" error. This occurs because the object returned by "GetAllOperations" is a static, immutable snapshot of the current transformations. Direct modification of the data lineage, including the addition or removal of transformations, is not performed on this snapshot. Instead, these operations must be executed directly on the "SourceView" object itself, which acts as the central manager for a data table's lineage. Understanding this distinction is critical for implementing effective and error-free Spotfire automation scripts.

Resolution

To successfully remove data transformations, interact directly with the "SourceView" object using its dedicated methods. Follow these steps to programmatically remove data transformations:

1. Access the Target Data Table's SourceView: First, obtain the SourceView instance for the data table whose transformations you intend to modify. 

# Replace "YourTableName" with the actual name of your data table

my_table = Document.Data.Tables["YourTableName"]

current_sourceview = my_table.GenerateSourceView()

2. Identify and Remove Specific Transformations: Iterate through the transformations in the "SourceView". For each transformation you wish to remove, verify that it can be removed using "SourceView.CanRemoveOperation()" before calling "SourceView.RemoveOperation()". To prevent issues that can arise from modifying a collection while iterating over it, it is a best practice to iterate over a copy of the operations list.

# Example: Removing all DataTransformationsOperation instances

# Create a copy of the list of operations to iterate safely

operations_to_remove = list(current_sourceview.GetAllOperations[DataTransformationsOperation]())

# For optimal performance when removing multiple operations, 

# process them in reverse order. This minimizes the number of times 

# subsequent operations need to be re-evaluated.

for op in reversed(operations_to_remove): 

    if current_sourceview.CanRemoveOperation(op):

        current_sourceview.RemoveOperation(op)

3. Regenerate the "SourceView": After any modification (addition or removal) of transformations, you must generate a new "SourceView" object. This new "SourceView" reflects the updated data lineage and is necessary for any subsequent operations or analyses to correctly interact with the modified data table.

# After transformations are removed, generate a new SourceView

# to ensure all subsequent operations reflect the changes.

updated_sourceview = my_table.GenerateSourceView()

Issue/Introduction

This article outlines the correct method for programmatically removing individual data transformations using IronPython. It clarifies that while some API introspection may suggest mutability, data transformation collections are read-only and require specific "SourceView" methods for modification.

Additional Information