Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.surf.gauss: Added seed option and flag #2931

Merged
merged 18 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions raster/r.surf.gauss/gaussurf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ int gaussurf(char *out, /* Name of raster maps to be opened. */

int row_count, col_count;

/****** INITIALISE RANDOM NUMBER GENERATOR ******/

/* You can set GRASS_RANDOM_SEED for repeatability */
G_math_srand_auto();

/****** OPEN CELL FILES AND GET CELL DETAILS ******/

fd_out = Rast_open_new(out, DCELL_TYPE);
Expand Down
23 changes: 23 additions & 0 deletions raster/r.surf.gauss/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ int main(int argc, char *argv[])

/****** INITIALISE ******/
double gauss_mean, gauss_sigma;
long seed_value;

struct GModule *module;
struct Option *out;
struct Option *mean;
struct Option *sigma;
struct Option *seed;

G_gisinit(argv[0]);

Expand All @@ -55,9 +57,30 @@ int main(int argc, char *argv[])
sigma->type = TYPE_DOUBLE;
sigma->answer = "1.0";

seed = G_define_option();
seed->key = "seed";
seed->type = TYPE_INTEGER;
seed->required = NO;
seed->label = _("Seed for random number generator");
seed->description = _("The same seed can be used to obtain same results"
" or random seed can be generated by other means.");

petrasovaa marked this conversation as resolved.
Show resolved Hide resolved
if (G_parser(argc, argv))
exit(EXIT_FAILURE);

/****** INITIALISE RANDOM NUMBER GENERATOR ******/
if (seed->answer) {
seed_value = atol(seed->answer);
G_srand48(seed_value);
G_verbose_message(_("Read random seed from %s option: %ld"), seed->key,
seed_value);
Fixed Show fixed Hide fixed
}
else {
/* default as it used to be */
seed_value = G_math_srand_auto();
G_verbose_message(_("Autogenerated random seed set to: %ld"), seed_value);
echoix marked this conversation as resolved.
Show resolved Hide resolved
}

sscanf(mean->answer, "%lf", &gauss_mean);
sscanf(sigma->answer, "%lf", &gauss_sigma);

Expand Down
100 changes: 100 additions & 0 deletions raster/r.surf.gauss/testsuite/test_surf_gauss.py
echoix marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python3

"""
MODULE: Test of r.surf.gauss

AUTHOR(S): Corey White <ctwhite48 gmail com>

PURPOSE: Tests random gauss surface generation

COPYRIGHT: (C) 2023 - 2024 by Corey White and the GRASS Development Team

This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
"""

import os
from grass.gunittest.case import TestCase
from grass.gunittest.main import test


class MeanSigmaTestCase(TestCase):
"""Test min and max of r.surf.random module"""

# Raster map name be used as output
output = "random_result"

@classmethod
def setUpClass(cls):
"""Ensures expected computational region"""
os.environ["GRASS_RANDOM_SEED"] = "42"
# modifying region just for this script
cls.use_temp_region()
# Only 100,000,000 seem to resonably (not 100%) ensure that all values
# are generated, so exceeding of ranges actually shows up.
cls.runModule("g.region", rows=10000, cols=10000)
petrasovaa marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def tearDownClass(cls):
"""Remove the temporary region"""
cls.del_temp_region()

def tearDown(self):
"""Remove the output created from the module"""
self.runModule("g.remove", flags="f", type="raster", name=[self.output])

def test_defaut_settings(self):
"""Check to see if double output has the expected range"""
default_mean = 0.0
default_sigma = 1.0
self.assertModule("r.surf.gauss", output=self.output)
self.assertRasterFitsUnivar(
self.output,
reference=dict(mean=default_mean, stddev=default_sigma),
msg="The mean and sigma values are not within the expected range.",
precision=0.01,
)

def test_mean_sigma_params(self):
"""Check if mean and sigma params are accepted"""
mean_value = 3.0
sigma_value = 5.8
self.assertModule(
"r.surf.gauss",
mean=mean_value,
sigma=sigma_value,
output=self.output,
)

self.assertRasterExists(self.output, msg="Output was not created")
self.assertRasterFitsUnivar(
self.output,
reference=dict(mean=mean_value, stddev=sigma_value),
msg="The mean and sigma values are not within the expected range.",
precision=0.01,
)

def test_random_seed_option(self):
"""Checks if random seed option sets random number"""
mean_value = 3.0
sigma_value = 5.8
self.assertModule(
"r.surf.gauss",
mean=mean_value,
sigma=sigma_value,
output=self.output,
seed=22,
)

self.assertRasterExists(self.output, msg="Output was not created")
self.assertRasterFitsUnivar(
self.output,
reference=dict(mean=mean_value, stddev=sigma_value),
msg="The mean and sigma values are not within the expected range.",
precision=0.01,
)


if __name__ == "__main__":
test()
Loading