code: def tree_lasso_projection(v, n_features, idx, n_nodes):
      """
      This functions solves the following optimization problem min_w 1/2 ||w-v||_2^2 + \sum z_i||w_{G_{i}}||
      where w and v are of dimensions of n_features; z_i >=0, and G_{i} follows the tree structure
      """
      # test whether the first node is special
      if idx[0, 0] == -1 and idx[1, 0] == -1:
          w_projection = np.zeros(n_features)
          z = idx[2, 0]
          for j in range(n_features):
              if v[j] > z:
                  w_projection[j] = v[j] - z
              else:
                  if v[j] < -z:
                      w_projection[j] = v[j] + z
                  else:
                      w_projection[j] = 0
          i = 1
  
      else:
          w = v.copy()
          i = 0
  
      # sequentially process each node
      while i < n_nodes:
          # compute the L2 norm of this group
          two_norm = 0
          start_idx = int(idx[0, i] - 1)
          end_idx = int(idx[1, i])
          for j in range(start_idx, end_idx):
              two_norm += w_projection[j] * w_projection[j]
          two_norm = np.sqrt(two_norm)
          z = idx[2, i]
          if two_norm > z:
              ratio = (two_norm - z) / two_norm
              # shrinkage this group by ratio
              for j in range(start_idx, end_idx):
                  w_projection[j] *= ratio
          else:
              for j in range(start_idx, end_idx):
                  w_projection[j] = 0
          i += 1
      return w_projection
  
file_path: scikit-feature/skfeature/utility/sparse_learning.py
imports: import numpy
  from numpy import linalg
label_desc: Use image node_id 7 for calling a global function with example usage: tree_lasso_projection(v, n_features, idx, n_nodes) and returns: w_projection