Exemple #1
0
def test_header():
    """
    Tests if the DataLine class correctly parses section headers.
    """
    header_line = DataLine("ROWS")  # empty section header.
    assert_equal(header_line.first_header_word(), "ROWS")

    header_line = DataLine("INDEP         DISCRETE")  # parameterised header.
    assert_equal(header_line.first_header_word(), "INDEP")
    assert_equal(header_line.second_header_word(), "DISCRETE")
Exemple #2
0
def test_discrete():
    """
    Tests if a discrete section is parsed correctly, and returns an appropriate
    discrete distribution (from ``scipy.stats``).
    """
    indep = Indep("DISCRETE")

    # First part of the LandS stoch file. Discrete distribution (p_i, x_i), with
    # [(0.3, 3), (0.4, 5), (0.3, 7)].
    lines = [
        "    RHS       DEMAND1   3.0            PERIOD2   0.3",
        "    RHS       DEMAND1   5.0            PERIOD2   0.4",
        "    RHS       DEMAND1   7.0            PERIOD2   0.3"
    ]

    for line in lines:
        data_line = DataLine(line)
        indep.add_entry(data_line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("RHS", "DEMAND1")

    assert_almost_equal(distr.pk, [0.3, 0.4, 0.3])
    assert_almost_equal(distr.xk, [3, 5, 7])

    assert_equal(distr.name, "discrete")
Exemple #3
0
def test_raw(line, expected):
    """
    Tests if the raw method returns the original string, cleaned for
    line-breaks and the like on the right.
    """
    data_line = DataLine(line)
    assert_equal(data_line.raw(), expected)
Exemple #4
0
def test_first_header_word(line, expected):
    """
    The first word field on a header line is the 1-14 column range (inclusive).
    """
    header_line = DataLine(line)

    assert_(header_line.is_header())
    assert_equal(header_line.first_header_word(), expected)
Exemple #5
0
def test_second_number_columns(line, expected):
    """
    The second numeric field is the 50-61 column range (inclusive).
    """
    padding = " " * 49  # starts at column 50, so 49 spaces.
    data_line = DataLine(padding + line)

    assert_almost_equal(data_line.second_number(), expected)
Exemple #6
0
def test_third_name_columns(line, expected):
    """
    The third name field is the 40-47 column range (inclusive).
    """
    padding = " " * 39  # starts at column 40, so 39 spaces.
    data_line = DataLine(padding + line)

    assert_equal(data_line.third_name(), expected)
Exemple #7
0
def test_first_number_column(line, expected):
    """
    The first numeric field is the 25-36 column range (inclusive).
    """
    padding = " " * 24  # starts at column 25, so 24 spaces.
    data_line = DataLine(padding + line)

    assert_almost_equal(data_line.first_number(), expected)
Exemple #8
0
def test_second_name_columns(line, expected):
    """
    The second name field is the 15-12 column range (inclusive).
    """
    padding = " " * 14  # starts at column 15, so 14 spaces.
    data_line = DataLine(padding + line)

    assert_equal(data_line.second_name(), expected)
Exemple #9
0
def test_first_name_columns(line, expected):
    """
    The first name field is the 5-12 column range (inclusive).
    """
    padding = " " * 4  # starts at column 5, so 4 spaces.
    data_line = DataLine(padding + line)

    assert_equal(data_line.first_name(), expected)
Exemple #10
0
def test_indicator_and_name():
    """
    Tests if the indicator and name are correctly parsed.
    """
    data_line = DataLine(" N  OBJ")  # from the LandS.cor file.

    assert_equal(data_line.indicator(), "N")
    assert_equal(data_line.first_name(), "OBJ")
Exemple #11
0
def test_second_header_word(line, expected):
    """
    The second word field on a header line is the 15-72 column range
    (inclusive).
    """
    padding = "NAME" + " " * 10  # second data word starts at column 15.
    header_line = DataLine(padding + line)

    assert_(header_line.is_header())
    assert_equal(header_line.second_header_word(), expected)
Exemple #12
0
def test_has_second_data_entry(line, exp_name, exp_number):
    """
    Tests if the DataLine class correctly tests if there is a second data
    entry, and parses the result. Also checks that this does not intervene with
    the first data entry.
    """
    padding = " " * 39  # starts at column 40, so 5 spaces.
    data_line = DataLine(padding + line)

    assert_equal(data_line.has_third_name(), exp_name)
    assert_equal(data_line.has_second_number(), exp_number)
Exemple #13
0
def test_first_data_entry():
    """
    Tests if the DataLine class correctly parses the first data entry.
    """
    # From the sslp_5_25_50.cor file.
    line = "    x_1       c2                 188"
    data_line = DataLine(line)

    assert_equal(data_line.first_name(), "x_1")
    assert_equal(data_line.second_name(), "c2")
    assert_almost_equal(data_line.first_number(), 188)
Exemple #14
0
    def _read_file(self) -> Generator[DataLine, None, None]:
        """
        Reads the file, one line at a time (generator).

        Yields
        ------
        DataLine
            A DataLine wrapping a single line in the input file.
        """
        with open(str(self.file_location())) as fh:
            for line in fh:
                yield DataLine(line)
Exemple #15
0
def test_gamma():
    """
    Tests if a Gamma distribution section is parsed correctly, and returns an
    appropriate Gamma distribution (from ``scipy.stats``).
    """
    indep = Indep("GAMMA")

    # A Gamma distribution: (VAR, CONSTR) ~ Gamma(shape = 2, scale = 5).
    line = DataLine("    VAR       CONSTR    5.0                      2.0")
    indep.add_entry(line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("VAR", "CONSTR")

    assert_almost_equal(distr.mean(), 2 * 5)  # = shape * scale.
    assert_almost_equal(distr.var(), 2 * 5**2)  # = shape * scale ** 2.

    assert_equal(distr.dist.name, "gamma")
Exemple #16
0
def test_uniform():
    """
    Tests if a uniform distribution section is parsed correctly, and returns an
    appropriately set uniform distribution (from ``scipy.stats``).
    """
    indep = Indep("UNIFORM")

    # A uniform distribution: (VAR, CONSTR) ~ U[a = 0, b = 5].
    line = DataLine("    VAR       CONSTR    0.0                      5.0")
    indep.add_entry(line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("VAR", "CONSTR")

    assert_almost_equal(distr.mean(), 5 / 2)  # = (b - a) / 2
    assert_almost_equal(distr.var(), 5**2 / 12)  # = (b - a) ** 2 / 12

    assert_equal(distr.dist.name, "uniform")
Exemple #17
0
def test_normal():
    """
    Tests if a normal distribution section is parsed correctly, and returns an
    appropriately set normal distribution (from ``scipy.stats``).
    """
    indep = Indep("NORMAL")

    # Inspired by the the LandS stoch file. Normal distribution, as
    # (RHS, DEMAND1) ~ N(mu = 7, sigma^2 = 2)
    line = DataLine("    RHS       DEMAND1   7.0            PERIOD2   2")
    indep.add_entry(line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("RHS", "DEMAND1")

    assert_almost_equal(distr.mean(), 7)
    assert_almost_equal(distr.var(), 2)

    assert_equal(distr.dist.name, "norm")
Exemple #18
0
def test_log_normal():
    """
    Tests if a log normal distribution section is parsed correctly, and returns
    an appropriate log normal distribution (from ``scipy.stats``).
    """
    indep = Indep("LOGNORM")

    # A log normal distribution: (VAR, CONSTR) ~ LogNorm(mu = 4, sigma^2 = 2).
    line = DataLine("    VAR       CONSTR    4.0                      2.0")
    indep.add_entry(line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("VAR", "CONSTR")

    # See https://en.wikipedia.org/wiki/Log-normal_distribution for a
    # description of mean and var.
    assert_almost_equal(distr.mean(), np.exp(4 + 2 / 2))
    assert_almost_equal(distr.var(), (np.exp(2) - 1) * np.exp(2 * 4 + 2))

    assert_equal(distr.dist.name, "lognorm")
Exemple #19
0
def test_beta():
    """
    Tests if a Beta distribution section is parsed correctly, and returns an
    appropriate Beta distribution (from ``scipy.stats``).
    """
    indep = Indep("BETA")

    # A Beta distribution: (VAR, CONSTR) ~ Beta(a = 5, b = 3).
    line = DataLine("    VAR       CONSTR    5.0                      3.0")
    indep.add_entry(line)

    assert_equal(len(indep), 1)

    distr = indep.get_for("VAR", "CONSTR")

    # See https://en.wikipedia.org/wiki/Beta_distribution for a description of
    # mean and var.
    assert_almost_equal(distr.mean(), 5 / (5 + 3))
    assert_almost_equal(distr.var(), 5 * 3 / ((5 + 3)**2 * (5 + 3 + 1)))

    assert_equal(distr.dist.name, "beta")
Exemple #20
0
def test_str():
    """
    Tests if the string representation is sensible.
    """
    data_line = DataLine(" N  OBJ")  # from the LandS.cor file.
    assert_equal(str(data_line), " N  OBJ")
Exemple #21
0
def test_indicator_columns(line, expected):
    """
    The indicator field is the 2-3 column range (inclusive).
    """
    data_line = DataLine(line)
    assert_equal(data_line.indicator(), expected)
Exemple #22
0
def test_len(length, string):
    """
    Tests if the __len__ function correctly looks at the raw string's length.
    """
    data_line = DataLine(string)
    assert_equal(len(data_line), length)
Exemple #23
0
def test_repr():
    """
    Tests if the specific repr implementation is sensible.
    """
    data_line = DataLine(" N  OBJ")  # from the LandS.cor file.
    assert_equal(repr(data_line), "DataLine(' N  OBJ')")
Exemple #24
0
def test_is_header(line, expected):
    """
    Tests if the DataLine class correctly detects section headers.
    """
    data_line = DataLine(line)
    assert_equal(data_line.is_header(), expected)
Exemple #25
0
def test_is_comment(line, expected):
    """
    Tests if the DataLine class correctly detects comment lines.
    """
    data_line = DataLine(line)
    assert_equal(data_line.is_comment(), expected)