

try:
    import numpy as np
except ImportError:
    np = None


def sanitize_np_types(value):
    if isinstance(value, (int, float, complex, type(None))):
        return value
    if np.isnan(value):
        return None
    if np and isinstance(value, np.integer):
        return int(value)
    if np and isinstance(value, np.floating):
        return float(value)
    return value


def to_np(value):
    if isinstance(value, np.ndarray):
        return value
    if np.isscalar(value):
        return np.array([value])


def calculate_scale_factor(tensor):
    converted = tensor.numpy() if not isinstance(tensor, np.ndarray) else tensor
    return 1 if converted.dtype == np.uint8 else 255


import math

try:
    import numpy as np
except ImportError:
    np = None


def sanitize_np_types(value):
    if math.isnan(value):
        return None
    if isinstance(value, (int, float, complex, type(None))):
        return value
    if np and np.isnan(value):
        return None
    if np and isinstance(value, np.integer):
        return int(value)
    if np and isinstance(value, np.floating):
        return float(value)
    return value


def to_np(value):
    if isinstance(value, np.ndarray):
        return value
    if np.isscalar(value):
        return np.array([value])


def calculate_scale_factor(tensor):
    converted = tensor.numpy() if not isinstance(tensor, np.ndarray) else tensor
    return 1 if converted.dtype == np.uint8 else 255

Add check for nan values
Add check for nan values

  
+ import math
  
  try:
      import numpy as np
  except ImportError:
      np = None
  
  
  def sanitize_np_types(value):
+     if math.isnan(value):
+         return None
      if isinstance(value, (int, float, complex, type(None))):
          return value
-     if np.isnan(value):
+     if np and np.isnan(value):
          return None
      if np and isinstance(value, np.integer):
          return int(value)
      if np and isinstance(value, np.floating):
          return float(value)
      return value
  
  
  def to_np(value):
      if isinstance(value, np.ndarray):
          return value
      if np.isscalar(value):
          return np.array([value])
  
  
  def calculate_scale_factor(tensor):
      converted = tensor.numpy() if not isinstance(tensor, np.ndarray) else tensor
      return 1 if converted.dtype == np.uint8 else 255
  
Add check for nan values
## Code Before:


try:
    import numpy as np
except ImportError:
    np = None


def sanitize_np_types(value):
    if isinstance(value, (int, float, complex, type(None))):
        return value
    if np.isnan(value):
        return None
    if np and isinstance(value, np.integer):
        return int(value)
    if np and isinstance(value, np.floating):
        return float(value)
    return value


def to_np(value):
    if isinstance(value, np.ndarray):
        return value
    if np.isscalar(value):
        return np.array([value])


def calculate_scale_factor(tensor):
    converted = tensor.numpy() if not isinstance(tensor, np.ndarray) else tensor
    return 1 if converted.dtype == np.uint8 else 255

## Instruction:
Add check for nan values
## Code After:

import math

try:
    import numpy as np
except ImportError:
    np = None


def sanitize_np_types(value):
    if math.isnan(value):
        return None
    if isinstance(value, (int, float, complex, type(None))):
        return value
    if np and np.isnan(value):
        return None
    if np and isinstance(value, np.integer):
        return int(value)
    if np and isinstance(value, np.floating):
        return float(value)
    return value


def to_np(value):
    if isinstance(value, np.ndarray):
        return value
    if np.isscalar(value):
        return np.array([value])


def calculate_scale_factor(tensor):
    converted = tensor.numpy() if not isinstance(tensor, np.ndarray) else tensor
    return 1 if converted.dtype == np.uint8 else 255
