vkit.mechanism.distortion_policy.photometric.noise
1# Copyright 2022 vkit-x Administrator. All Rights Reserved. 2# 3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses. 4# 5# The commercial license gives you the full rights to create and distribute software 6# on your own terms without any SSPL license obligations. For more information, 7# please see the "LICENSE_COMMERCIAL.txt" file. 8# 9# This project is also available under Server Side Public License (SSPL). 10# The SSPL licensing is ideal for use cases such as open source projects with 11# SSPL distribution, student/academic purposes, hobby projects, internal research 12# projects without external distribution, or other projects where all SSPL 13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file. 14from typing import Tuple 15 16import attrs 17from numpy.random import Generator as RandomGenerator 18 19from vkit.mechanism import distortion 20from ..type import DistortionConfigGenerator, DistortionPolicyFactory 21from ..opt import sample_float 22 23 24@attrs.define 25class GaussionNoiseConfigGeneratorConfig: 26 std_min: float = 1.0 27 std_max: float = 35.0 28 29 30class GaussionNoiseConfigGenerator( 31 DistortionConfigGenerator[ 32 GaussionNoiseConfigGeneratorConfig, 33 distortion.GaussionNoiseConfig, 34 ] 35): # yapf: disable 36 37 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 38 std = sample_float( 39 level=self.level, 40 value_min=self.config.std_min, 41 value_max=self.config.std_max, 42 prob_reciprocal=None, 43 rng=rng, 44 ) 45 46 return distortion.GaussionNoiseConfig(std=std) 47 48 49gaussion_noise_policy_factory = DistortionPolicyFactory( 50 distortion.gaussion_noise, 51 GaussionNoiseConfigGenerator, 52) 53 54 55@attrs.define 56class PoissonNoiseConfigGeneratorConfig: 57 pass 58 59 60class PoissonNoiseConfigGenerator( 61 DistortionConfigGenerator[ 62 PoissonNoiseConfigGeneratorConfig, 63 distortion.PoissonNoiseConfig, 64 ] 65): # yapf: disable 66 67 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 68 return distortion.PoissonNoiseConfig() 69 70 71poisson_noise_policy_factory = DistortionPolicyFactory( 72 distortion.poisson_noise, 73 PoissonNoiseConfigGenerator, 74) 75 76 77@attrs.define 78class ImpulseNoiseConfigGeneratorConfig: 79 prob_presv_min: float = 0.95 80 prob_presv_max: float = 1.0 81 82 83class ImpulseNoiseConfigGenerator( 84 DistortionConfigGenerator[ 85 ImpulseNoiseConfigGeneratorConfig, 86 distortion.ImpulseNoiseConfig, 87 ] 88): # yapf: disable 89 90 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 91 prob_presv = sample_float( 92 level=self.level, 93 value_min=self.config.prob_presv_min, 94 value_max=self.config.prob_presv_max, 95 prob_reciprocal=None, 96 rng=rng, 97 inverse_level=True, 98 ) 99 prob_not_presv = 1 - prob_presv 100 101 salt_ratio = rng.uniform() 102 prob_salt = prob_not_presv * salt_ratio 103 prob_pepper = prob_not_presv - prob_salt 104 105 return distortion.ImpulseNoiseConfig( 106 prob_salt=prob_salt, 107 prob_pepper=prob_pepper, 108 ) 109 110 111impulse_noise_policy_factory = DistortionPolicyFactory( 112 distortion.impulse_noise, 113 ImpulseNoiseConfigGenerator, 114) 115 116 117@attrs.define 118class SpeckleNoiseConfigGeneratorConfig: 119 std_min: float = 0.0 120 std_max: float = 0.3 121 122 123class SpeckleNoiseConfigGenerator( 124 DistortionConfigGenerator[ 125 SpeckleNoiseConfigGeneratorConfig, 126 distortion.SpeckleNoiseConfig, 127 ] 128): # yapf: disable 129 130 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 131 std = sample_float( 132 level=self.level, 133 value_min=self.config.std_min, 134 value_max=self.config.std_max, 135 prob_reciprocal=None, 136 rng=rng, 137 ) 138 139 return distortion.SpeckleNoiseConfig(std=std) 140 141 142speckle_noise_policy_factory = DistortionPolicyFactory( 143 distortion.speckle_noise, 144 SpeckleNoiseConfigGenerator, 145)
31class GaussionNoiseConfigGenerator( 32 DistortionConfigGenerator[ 33 GaussionNoiseConfigGeneratorConfig, 34 distortion.GaussionNoiseConfig, 35 ] 36): # yapf: disable 37 38 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 39 std = sample_float( 40 level=self.level, 41 value_min=self.config.std_min, 42 value_max=self.config.std_max, 43 prob_reciprocal=None, 44 rng=rng, 45 ) 46 47 return distortion.GaussionNoiseConfig(std=std)
Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows::
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default
61class PoissonNoiseConfigGenerator( 62 DistortionConfigGenerator[ 63 PoissonNoiseConfigGeneratorConfig, 64 distortion.PoissonNoiseConfig, 65 ] 66): # yapf: disable 67 68 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 69 return distortion.PoissonNoiseConfig()
Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows::
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default
79class ImpulseNoiseConfigGeneratorConfig: 80 prob_presv_min: float = 0.95 81 prob_presv_max: float = 1.0
2def __init__(self, prob_presv_min=attr_dict['prob_presv_min'].default, prob_presv_max=attr_dict['prob_presv_max'].default): 3 self.prob_presv_min = prob_presv_min 4 self.prob_presv_max = prob_presv_max
Method generated by attrs for class ImpulseNoiseConfigGeneratorConfig.
84class ImpulseNoiseConfigGenerator( 85 DistortionConfigGenerator[ 86 ImpulseNoiseConfigGeneratorConfig, 87 distortion.ImpulseNoiseConfig, 88 ] 89): # yapf: disable 90 91 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 92 prob_presv = sample_float( 93 level=self.level, 94 value_min=self.config.prob_presv_min, 95 value_max=self.config.prob_presv_max, 96 prob_reciprocal=None, 97 rng=rng, 98 inverse_level=True, 99 ) 100 prob_not_presv = 1 - prob_presv 101 102 salt_ratio = rng.uniform() 103 prob_salt = prob_not_presv * salt_ratio 104 prob_pepper = prob_not_presv - prob_salt 105 106 return distortion.ImpulseNoiseConfig( 107 prob_salt=prob_salt, 108 prob_pepper=prob_pepper, 109 )
Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows::
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default
124class SpeckleNoiseConfigGenerator( 125 DistortionConfigGenerator[ 126 SpeckleNoiseConfigGeneratorConfig, 127 distortion.SpeckleNoiseConfig, 128 ] 129): # yapf: disable 130 131 def __call__(self, shape: Tuple[int, int], rng: RandomGenerator): 132 std = sample_float( 133 level=self.level, 134 value_min=self.config.std_min, 135 value_max=self.config.std_max, 136 prob_reciprocal=None, 137 rng=rng, 138 ) 139 140 return distortion.SpeckleNoiseConfig(std=std)
Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows::
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default