code: def get_gradient_matrix(w_size, mask):
      """Return gradient matrix.
  
      Given a number of features and a mask (which has the property
      mask[mask==True].size == w_size) computes a matrix G such that for
      a w vector we have np.dot(G, w) == gradient(w_masked)[mask]
      """
      grad_matrix = np.zeros((mask.ndim * w_size, w_size))
      grad_mask = np.array([mask for _ in range(mask.ndim)])
      image_buffer = np.zeros(mask.shape)
  
      for i in range(w_size):
          base_vector = np.zeros(w_size)
          base_vector[i] = 1
          image_buffer[mask] = base_vector
          gradient_column = gradient(image_buffer)[grad_mask]
          grad_matrix[:, i] = gradient_column
  
      return grad_matrix
  
imports: import numpy
  import pytest
  import scipy
  from nibabel import Nifti1Image
  from numpy.testing import assert_almost_equal
  from scipy import linalg
  from nilearn.decoding._objective_functions import divergence, gradient
  from nilearn.decoding.space_net import BaseSpaceNet
  from nilearn.decoding.space_net_solvers import _graph_net_adjoint_data_function, _graph_net_data_function, _logistic_data_loss_and_spatial_grad, _logistic_data_loss_and_spatial_grad_derivative, _logistic_derivative_lipschitz_constant, _squared_loss_and_spatial_grad, _squared_loss_and_spatial_grad_derivative, _squared_loss_derivative_lipschitz_constant, mfista
  from nilearn.decoding.tests._testing import create_graph_net_simulation_data
  from .test_same_api import to_niimgs
label_desc: Use image node_id 2 for calling a global function with example usage: get_gradient_matrix(w_size, mask) and returns: grad_matrix