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 2 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
37 changes: 37 additions & 0 deletions raster/r.surf.gauss/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ int main(int argc, char *argv[])

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

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

G_gisinit(argv[0]);

Expand All @@ -55,9 +58,43 @@ 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
s_flag = G_define_flag();
s_flag->key = 's';
s_flag->label = _("Generate random seed");
cwhite911 marked this conversation as resolved.
Show resolved Hide resolved
s_flag->description =
_("Automatically generates random seed for random number"
" generator (use when you don't want to provide the seed option)");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

/****** INITIALISE RANDOM NUMBER GENERATOR ******/
if (s_flag->answer) {
seed_value = G_math_srand_auto();
G_verbose_message(_("Generated random seed (-s): %ld"), seed_value);
Fixed Show fixed Hide fixed
}
else if (seed->answer) {
seed_value = atol(seed->answer);
G_math_srand(seed_value);
echoix marked this conversation as resolved.
Show resolved Hide resolved
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(_("Warning set flag s or option seed. Generated "
"random seed (-s): %ld"),
seed_value);
Fixed Show fixed Hide fixed
petrasovaa marked this conversation as resolved.
Show resolved Hide resolved
}

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

Expand Down
97 changes: 97 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,97 @@
#!/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 by Corey White and the GRASS Development Team
echoix marked this conversation as resolved.
Show resolved Hide resolved

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

import os

import grass.script as gs

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


def get_raster_min_max(raster_map):
"""Get minimum and maximum value from raster metadata as a tuple"""
info = gs.raster_info(raster_map)
return info["min"], info["max"]


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"""
self.assertModule("r.surf.gauss", output=self.output)

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,
)

def test_random_flag(self):
"""Checks if random flag 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,
flags="s",
)

def test_random_seed_option(self):
"""Checks if random flag 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,
)


if __name__ == "__main__":
test()