Skip to content

UnmeltProxyModel

Base classes

Name Children Inherits
FlatProxyModel
prettyqt.itemmodels.proxies.flatproxymodel
Base class for proxies which reshape a source table into another flat table.

â‹” Inheritance diagram

graph TD
  3482035875["itemmodels.UnmeltProxyModel"]
  1752756326["itemmodels.FlatProxyModel"]
  445963150["itemmodels.FlatProxyMixin"]
  2751680898["builtins.object"]
  1274898529["core.AbstractProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  1752756326 --> 3482035875
  445963150 --> 1752756326
  2751680898 --> 445963150
  1274898529 --> 1752756326
  2431222121 --> 1274898529
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  2751680898 --> 2431222121
  3989152870 --> 1274898529
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: FlatProxyModel

Proxy model to pivot a table from long format to wide format.

Reverses the MeltProxyModel operation.

app = widgets.app()
data = {
    "ID": ["A", "A", "B", "B"] * 2,
    "Variable": ["height", "weight"] * 4,
    "Value": [5.5, 130, 6.0, 150, 5.7, 140, 5.9, 145],
}
model = gui.StandardItemModel.from_dict(data)
table = widgets.TableView()
table.set_model(model)
table.show()
app = widgets.app()
data = {
    "ID": ["A", "A", "B", "B"] * 2,
    "Variable": ["height", "weight"] * 4,
    "Value": [5.5, 130, 6.0, 150, 5.7, 140, 5.9, 145],
}
model = gui.StandardItemModel.from_dict(data)
table = widgets.TableView()
table.set_model(model)
table.proxifier.unmelt(id_columns=[0], variable_column=1, value_column=2)
table.show()
Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
class UnmeltProxyModel(FlatProxyModel):
    """Proxy model to pivot a table from long format to wide format.

    Reverses the MeltProxyModel operation.

    === "Without proxy"

        ```py
        app = widgets.app()
        data = {
            "ID": ["A", "A", "B", "B"] * 2,
            "Variable": ["height", "weight"] * 4,
            "Value": [5.5, 130, 6.0, 150, 5.7, 140, 5.9, 145],
        }
        model = gui.StandardItemModel.from_dict(data)
        table = widgets.TableView()
        table.set_model(model)
        table.show()
        ```

    === "With proxy"

        ```py
        app = widgets.app()
        data = {
            "ID": ["A", "A", "B", "B"] * 2,
            "Variable": ["height", "weight"] * 4,
            "Value": [5.5, 130, 6.0, 150, 5.7, 140, 5.9, 145],
        }
        model = gui.StandardItemModel.from_dict(data)
        table = widgets.TableView()
        table.set_model(model)
        table.proxifier.unmelt(id_columns=[0], variable_column=1, value_column=2)
        table.show()
        ```
    """

    ID = "unmelt"
    ICON = "mdi6.table-pivot"

    def __init__(
        self,
        id_columns: list[int],
        variable_column: int,
        value_column: int,
        **kwargs,
    ):
        """Initialize UnmeltProxyModel.

        Args:
            id_columns: List of column indices that identify unique rows
            variable_column: Column index containing variable names
            value_column: Column index containing values
            kwargs: Additional keyword arguments.
        """
        self._id_columns = id_columns
        self._variable_column = variable_column
        self._value_column = value_column
        self._unique_vars: list[str] = []
        self._unique_ids: list[tuple] = []
        # (id tuple, variable name) -> source row
        self._lookup: dict[tuple[tuple, str], int] = {}
        # id tuple -> first source row with these ids
        self._id_rows: dict[tuple, int] = {}
        super().__init__(**kwargs)

    def _get_ids(self, source_row: int) -> tuple:
        source = self.sourceModel()
        return tuple(source.index(source_row, col).data() for col in self._id_columns)

    def _update_cache(self):
        """Update internal cache of unique variables and IDs."""
        self._lookup.clear()
        self._id_rows.clear()
        source = self.sourceModel()
        if source is None:
            self._unique_vars, self._unique_ids = [], []
            return
        variables: dict[str, None] = {}
        for row in range(source.rowCount()):
            var = str(source.index(row, self._variable_column).data())
            ids = self._get_ids(row)
            variables[var] = None
            self._id_rows.setdefault(ids, row)
            self._lookup.setdefault((ids, var), row)
        self._unique_vars = sorted(variables)
        # keep order of first appearance (ids may not be sortable)
        self._unique_ids = list(self._id_rows)

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        """Return number of rows in the model."""
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self._unique_ids)

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        """Return number of columns in the model."""
        parent = parent or core.ModelIndex()
        if parent.isValid() or self.sourceModel() is None:
            return 0
        return len(self._id_columns) + len(self._unique_vars)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> Any:
        """Return header data for given section and orientation."""
        if orientation != constants.HORIZONTAL or role != constants.DISPLAY_ROLE:
            return None
        if section < len(self._id_columns):
            # Return original header for ID columns
            return self.sourceModel().headerData(
                self._id_columns[section], constants.HORIZONTAL, role
            )
        # Return variable name for value columns
        var_idx = section - len(self._id_columns)
        return self._unique_vars[var_idx] if var_idx < len(self._unique_vars) else None

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> Any:
        """Return data for given index and role."""
        source_index = self.mapToSource(index)
        return source_index.data(role) if source_index.isValid() else None

    def mapToSource(self, proxy_index: core.ModelIndex) -> core.ModelIndex:
        """Map proxy index to source index."""
        if not proxy_index.isValid() or self.sourceModel() is None:
            return core.ModelIndex()
        row, col = proxy_index.row(), proxy_index.column()
        ids = self._unique_ids[row]
        if col < len(self._id_columns):
            source_row = self._id_rows[ids]
            return self.sourceModel().index(source_row, self._id_columns[col])
        var_name = self._unique_vars[col - len(self._id_columns)]
        if (source_row := self._lookup.get((ids, var_name))) is None:
            return core.ModelIndex()
        return self.sourceModel().index(source_row, self._value_column)

    def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
        """Map source index to proxy index."""
        if not source_index.isValid() or source_index.parent().isValid():
            return core.ModelIndex()
        source_row, source_col = source_index.row(), source_index.column()
        ids = self._get_ids(source_row)
        if ids not in self._id_rows:
            return core.ModelIndex()
        proxy_row = self._unique_ids.index(ids)
        if source_col in self._id_columns:
            return self.index(proxy_row, self._id_columns.index(source_col))
        if source_col == self._value_column:
            var = str(self.sourceModel().index(source_row, self._variable_column).data())
            proxy_col = len(self._id_columns) + self._unique_vars.index(var)
            return self.index(proxy_row, proxy_col)
        return core.ModelIndex()

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        source_index = self.mapToSource(index)
        if not source_index.isValid():
            return constants.NO_FLAGS if index.isValid() else super().flags(index)
        return self.sourceModel().flags(source_index)

__init__(id_columns: list[int], variable_column: int, value_column: int, **kwargs)

Initialize UnmeltProxyModel.

Parameters:

Name Type Description Default
id_columns list[int]

List of column indices that identify unique rows

required
variable_column int

Column index containing variable names

required
value_column int

Column index containing values

required
kwargs

Additional keyword arguments.

{}
Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def __init__(
    self,
    id_columns: list[int],
    variable_column: int,
    value_column: int,
    **kwargs,
):
    """Initialize UnmeltProxyModel.

    Args:
        id_columns: List of column indices that identify unique rows
        variable_column: Column index containing variable names
        value_column: Column index containing values
        kwargs: Additional keyword arguments.
    """
    self._id_columns = id_columns
    self._variable_column = variable_column
    self._value_column = value_column
    self._unique_vars: list[str] = []
    self._unique_ids: list[tuple] = []
    # (id tuple, variable name) -> source row
    self._lookup: dict[tuple[tuple, str], int] = {}
    # id tuple -> first source row with these ids
    self._id_rows: dict[tuple, int] = {}
    super().__init__(**kwargs)

columnCount(parent: core.ModelIndex | None = None) -> int

Return number of columns in the model.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def columnCount(self, parent: core.ModelIndex | None = None) -> int:
    """Return number of columns in the model."""
    parent = parent or core.ModelIndex()
    if parent.isValid() or self.sourceModel() is None:
        return 0
    return len(self._id_columns) + len(self._unique_vars)

data(index: core.ModelIndex, role: constants.ItemDataRole = constants.DISPLAY_ROLE) -> Any

Return data for given index and role.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def data(
    self,
    index: core.ModelIndex,
    role: constants.ItemDataRole = constants.DISPLAY_ROLE,
) -> Any:
    """Return data for given index and role."""
    source_index = self.mapToSource(index)
    return source_index.data(role) if source_index.isValid() else None

headerData(section: int, orientation: constants.Orientation, role: constants.ItemDataRole = constants.DISPLAY_ROLE) -> Any

Return header data for given section and orientation.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def headerData(
    self,
    section: int,
    orientation: constants.Orientation,
    role: constants.ItemDataRole = constants.DISPLAY_ROLE,
) -> Any:
    """Return header data for given section and orientation."""
    if orientation != constants.HORIZONTAL or role != constants.DISPLAY_ROLE:
        return None
    if section < len(self._id_columns):
        # Return original header for ID columns
        return self.sourceModel().headerData(
            self._id_columns[section], constants.HORIZONTAL, role
        )
    # Return variable name for value columns
    var_idx = section - len(self._id_columns)
    return self._unique_vars[var_idx] if var_idx < len(self._unique_vars) else None

mapFromSource(source_index: core.ModelIndex) -> core.ModelIndex

Map source index to proxy index.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
    """Map source index to proxy index."""
    if not source_index.isValid() or source_index.parent().isValid():
        return core.ModelIndex()
    source_row, source_col = source_index.row(), source_index.column()
    ids = self._get_ids(source_row)
    if ids not in self._id_rows:
        return core.ModelIndex()
    proxy_row = self._unique_ids.index(ids)
    if source_col in self._id_columns:
        return self.index(proxy_row, self._id_columns.index(source_col))
    if source_col == self._value_column:
        var = str(self.sourceModel().index(source_row, self._variable_column).data())
        proxy_col = len(self._id_columns) + self._unique_vars.index(var)
        return self.index(proxy_row, proxy_col)
    return core.ModelIndex()

mapToSource(proxy_index: core.ModelIndex) -> core.ModelIndex

Map proxy index to source index.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def mapToSource(self, proxy_index: core.ModelIndex) -> core.ModelIndex:
    """Map proxy index to source index."""
    if not proxy_index.isValid() or self.sourceModel() is None:
        return core.ModelIndex()
    row, col = proxy_index.row(), proxy_index.column()
    ids = self._unique_ids[row]
    if col < len(self._id_columns):
        source_row = self._id_rows[ids]
        return self.sourceModel().index(source_row, self._id_columns[col])
    var_name = self._unique_vars[col - len(self._id_columns)]
    if (source_row := self._lookup.get((ids, var_name))) is None:
        return core.ModelIndex()
    return self.sourceModel().index(source_row, self._value_column)

rowCount(parent: core.ModelIndex | None = None) -> int

Return number of rows in the model.

Source code in prettyqt/itemmodels/proxies/unmeltproxymodel.py
def rowCount(self, parent: core.ModelIndex | None = None) -> int:
    """Return number of rows in the model."""
    parent = parent or core.ModelIndex()
    return 0 if parent.isValid() else len(self._unique_ids)

:fa-brands-github: Show source on GitHub