code: def gen_kernel(
      k_size=np.array([15, 15]),
      scale_factor=np.array([4, 4]),
      min_var=0.6,
      max_var=10.0,
      noise_level=0,
  ):
      """ "
      # modified version of https://github.com/assafshocher/BlindSR_dataset_generator
      # Kai Zhang
      # min_var = 0.175 * sf  # variance of the gaussian kernel will be sampled between min_var and max_var
      # max_var = 2.5 * sf
      """
      # Set random eigen-vals (lambdas) and angle (theta) for COV matrix
      lambda_1 = min_var + np.random.rand() * (max_var - min_var)
      lambda_2 = min_var + np.random.rand() * (max_var - min_var)
      theta = np.random.rand() * np.pi  # random theta
      noise = -noise_level + np.random.rand(*k_size) * noise_level * 2
  
      # Set COV matrix using Lambdas and Theta
      LAMBDA = np.diag([lambda_1, lambda_2])
      Q = np.array(
          [
              [np.cos(theta), -np.sin(theta)],
              [np.sin(theta), np.cos(theta)],
          ]
      )
      SIGMA = Q @ LAMBDA @ Q.T
      INV_SIGMA = np.linalg.inv(SIGMA)[None, None, :, :]
  
      # Set expectation position (shifting kernel for aligned image)
      MU = k_size // 2 - 0.5 * (
          scale_factor - 1
      )  # - 0.5 * (scale_factor - k_size % 2)
      MU = MU[None, None, :, None]
  
      # Create meshgrid for Gaussian
      [X, Y] = np.meshgrid(range(k_size[0]), range(k_size[1]))
      Z = np.stack([X, Y], 2)[:, :, :, None]
  
      # Calcualte Gaussian for every pixel of the kernel
      ZZ = Z - MU
      ZZ_t = ZZ.transpose(0, 1, 3, 2)
      raw_kernel = np.exp(-0.5 * np.squeeze(ZZ_t @ INV_SIGMA @ ZZ)) * (
          1 + noise
      )
  
      # shift the kernel so it will be centered
      # raw_kernel_centered = kernel_shift(raw_kernel, scale_factor)
  
      # Normalize the kernel and return
      # kernel = raw_kernel_centered / np.sum(raw_kernel_centered)
      kernel = raw_kernel / np.sum(raw_kernel)
      return kernel
  
imports: import random
  from functools import partial
  import albumentations
  import cv2
  import ldm.modules.image_degradation.utils_image
  import numpy
  import scipy
  import scipy.stats
  import torch
  from scipy import ndimage
  from scipy.interpolate import interp2d
  from scipy.linalg import orth
label_desc: Use image node_id 7 for calling a global function with example usage: gen_kernel(k_size, scale_factor, min_var, max_var, noise_level) and returns: kernel