docstr: Tests getters and setters for padded/list representations.
docstr_tok: ["Tests","getters","and","setters","for","padded\/list","representations","."]
code: def test_feature_density_setters(self):
      """
      Tests getters and setters for padded/list representations.
      """
  
      device = torch.device("cuda:0")
      diff_device = torch.device("cpu")
  
      num_volumes = 30
      num_channels = 4
      K = 20
  
      densities = []
      features = []
      grid_sizes = []
      diff_grid_sizes = []
  
      for _ in range(num_volumes):
          grid_size = torch.randint(K - 1, size=(3,)).long() + 1
          densities.append(
              torch.rand(
                  (1, *grid_size), device=device, dtype=torch.float32
              )
          )
          features.append(
              torch.rand(
                  (num_channels, *grid_size),
                  device=device,
                  dtype=torch.float32,
              )
          )
          grid_sizes.append(grid_size)
  
          diff_grid_size = (
              copy.deepcopy(grid_size)
              + torch.randint(2, size=(3,)).long()
              + 1
          )
          diff_grid_sizes.append(diff_grid_size)
      grid_sizes = torch.stack(grid_sizes).to(device)
      diff_grid_sizes = torch.stack(diff_grid_sizes).to(device)
  
      volumes = Volumes(densities=densities, features=features)
      self.assertClose(volumes.get_grid_sizes(), grid_sizes)
  
      # test the getters
      features_padded = volumes.features()
      densities_padded = volumes.densities()
      features_list = volumes.features_list()
      densities_list = volumes.densities_list()
      for x_pad, x_list in zip(
          (
              densities_padded,
              features_padded,
              densities_padded,
              features_padded,
          ),
          (densities_list, features_list, densities, features),
      ):
          self._check_padded(x_pad, x_list, grid_sizes)
  
      # test feature setters
      features_new = [
          torch.rand(
              (num_channels, *grid_size),
              device=device,
              dtype=torch.float32,
          )
          for grid_size in grid_sizes
      ]
      volumes._set_features(features_new)
      features_new_list = volumes.features_list()
      features_new_padded = volumes.features()
      for x_pad, x_list in zip(
          (features_new_padded, features_new_padded),
          (features_new, features_new_list),
      ):
          self._check_padded(x_pad, x_list, grid_sizes)
  
      # wrong features to update
      bad_features_new = [
          [
              torch.rand(
                  (num_channels, *grid_size),
                  device=diff_device,
                  dtype=torch.float32,
              )
              for grid_size in diff_grid_sizes
          ],
          torch.rand(
              (num_volumes, num_channels, K + 1, K, K),
              device=device,
              dtype=torch.float32,
          ),
          None,
      ]
      for bad_features_new_ in bad_features_new:
          with self.assertRaises(ValueError):
              volumes._set_densities(bad_features_new_)
  
      # test density setters
      densities_new = [
          torch.rand(
              (1, *grid_size), device=device, dtype=torch.float32
          )
          for grid_size in grid_sizes
      ]
      volumes._set_densities(densities_new)
      densities_new_list = volumes.densities_list()
      densities_new_padded = volumes.densities()
      for x_pad, x_list in zip(
          (densities_new_padded, densities_new_padded),
          (densities_new, densities_new_list),
      ):
          self._check_padded(x_pad, x_list, grid_sizes)
  
      # wrong densities to update
      bad_densities_new = [
          [
              torch.rand(
                  (1, *grid_size),
                  device=diff_device,
                  dtype=torch.float32,
              )
              for grid_size in diff_grid_sizes
          ],
          torch.rand(
              (num_volumes, 1, K + 1, K, K),
              device=device,
              dtype=torch.float32,
          ),
          None,
      ]
      for bad_densities_new_ in bad_densities_new:
          with self.assertRaises(ValueError):
              volumes._set_densities(bad_densities_new_)
  
      # test update_padded
      volumes = Volumes(densities=densities, features=features)
      volumes_updated = volumes.update_padded(
          densities_new, new_features=features_new
      )
      densities_new_list = volumes_updated.densities_list()
      densities_new_padded = volumes_updated.densities()
      features_new_list = volumes_updated.features_list()
      features_new_padded = volumes_updated.features()
      for x_pad, x_list in zip(
          (
              densities_new_padded,
              densities_new_padded,
              features_new_padded,
              features_new_padded,
          ),
          (
              densities_new,
              densities_new_list,
              features_new,
              features_new_list,
          ),
      ):
          self._check_padded(x_pad, x_list, grid_sizes)
      self.assertIs(
          volumes.get_grid_sizes(), volumes_updated.get_grid_sizes()
      )
      self.assertIs(
          volumes.get_local_to_world_coords_transform(),
          volumes_updated.get_local_to_world_coords_transform(),
      )
      self.assertIs(volumes.device, volumes_updated.device)
  
imports: import copy
  import itertools
  import random
  import unittest
  import numpy
  import torch
  from pytorch3d.structures.volumes import VolumeLocator, Volumes
  from pytorch3d.transforms import Scale
  from .common_testing import TestCaseMixin
label_desc: Use image node_id 14 for calling the TestVolumes obj's underlying member method code with example usage: obj.test_feature_density_setters() without return types