Exemplo n.º 1
0
def test_opendss_to_json():
    """Test the JSON writer with OpenDSS models as input."""
    from ditto.readers.opendss.read import Reader
    from ditto.store import Store
    from ditto.writers.json.write import Writer

    opendss_models = [
        f for f in os.listdir(
            os.path.join(current_directory, "data/small_cases/opendss/"))
        if not f.startswith(".")
    ]
    for model in opendss_models:
        m = Store()
        r = Reader(
            master_file=os.path.join(
                current_directory,
                "data/small_cases/opendss/{model}/master.dss".format(
                    model=model),
            ),
            buscoordinates_file=os.path.join(
                current_directory,
                "data/small_cases/opendss/{model}/buscoord.dss".format(
                    model=model),
            ),
        )
        r.parse(m)
        m.set_names()
        output_path = tempfile.TemporaryDirectory()
        w = Writer(output_path=output_path.name)
        w.write(m)
Exemplo n.º 2
0
    def apply(cls, stack, model, *args, **kwargs):
        base_dir = None
        if 'base_dir' in kwargs:
            base_dir = kwargs['base_dir']

        if base_dir is None:
            return model
        if not os.path.exists(base_dir):
            os.makedirs(base_dir)

        writer = Writer(output_path=base_dir, filename="full_model.json")
        writer.write(model)

        return model
Exemplo n.º 3
0
def test_json_serialize_deserialize():
    """Write a model to JSON, read it back in, and test that both models match."""
    from ditto.readers.opendss.read import Reader
    from ditto.store import Store
    from ditto.writers.json.write import Writer
    from ditto.readers.json.read import Reader as json_reader

    opendss_models = [
        f for f in os.listdir(
            os.path.join(current_directory, "data/small_cases/opendss/"))
        if not f.startswith(".")
    ]
    opendss_models.remove("storage_test")
    for model in opendss_models:
        m = Store()
        r = Reader(
            master_file=os.path.join(
                current_directory,
                "data/small_cases/opendss/{model}/master.dss".format(
                    model=model),
            ),
            buscoordinates_file=os.path.join(
                current_directory,
                "data/small_cases/opendss/{model}/buscoord.dss".format(
                    model=model),
            ),
        )
        r.parse(m)
        m.set_names()
        w = Writer(output_path="./")
        w.write(m)
        jr = json_reader(input_file="./Model.json")
        jr.parse(m)
        jr.model.set_names()

        for obj in m.models:
            if hasattr(obj, "name"):
                json_obj = jr.model[obj.name]
                assert compare(obj, json_obj)

        for json_obj in jr.model.models:
            if hasattr(json_obj, "name"):
                obj = m[json_obj.name]
                assert compare(json_obj, obj)

        os.remove("./Model.json")
Exemplo n.º 4
0
def test_cyme_to_json():
    """Test the JSON writer with CYME models as input."""
    from ditto.readers.cyme.read import Reader
    from ditto.store import Store
    from ditto.writers.json.write import Writer

    cyme_models = [
        f for f in os.listdir(
            os.path.join(current_directory, "data/small_cases/cyme/"))
        if not f.startswith(".")
    ]
    for model in cyme_models:
        m = Store()
        r = Reader(data_folder_path=os.path.join(
            current_directory, "data/small_cases/cyme", model))
        r.parse(m)
        m.set_names()
        output_path = tempfile.TemporaryDirectory()
        w = Writer(output_path=output_path.name)
        w.write(m)
Exemplo n.º 5
0
def test_lines_write():
    m = Store()

    r = Reader(
        master_file=os.path.join(
            current_directory, "../../../readers/opendss/Lines/test_linegeometries.dss"
        )
    )
    r.parse(m)
    m.set_names()

    output_path = tempfile.gettempdir()
    jw = Json_Writer(output_path=output_path)
    jw.write(m)

    w = Writer(output_path=output_path)
    w.write(m)

    # Check that the OpenDSS writer created a Master file
    assert os.path.exists(os.path.join(output_path, "Master.dss"))

    r_w = Reader(master_file=os.path.join(output_path, "Master.dss"))
    r_w.parse(m)
    m.set_names()

    jw = Json_Writer(output_path="./")
    jw.write(m)

    with open(os.path.join(output_path, "Model.json"), "r") as f1:
        reader_json = json.load(f1)
    with open("./Model.json", "r") as f2:
        writer_json = json.load(f2)

    assert reader_json["model"] == writer_json["model"]
Exemplo n.º 6
0
logger = logging.getLogger(__name__)
test_list = os.walk('data')
for (dirpath, dirname, files) in test_list:
    if files != []:
        reader_type = dirpath.split('\\')[2]
        m = Store()
        if reader_type == 'opendss':
            reader = Reader_opendss(
                master_file=os.path.join('..', dirpath, 'master.dss'),
                buscoordinates_file=os.path.join('..', dirpath,
                                                 'buscoord.dss'))
        elif reader_type == 'cyme':
            reader = Reader_cyme(data_folder_path=os.path.join('..', dirpath))
        else:
            #Update with other tests if they get added to the persistence tests
            continue
        reader.parse(m)
        m.set_names()
        output_path = tempfile.TemporaryDirectory()
        w = Writer(output_path=output_path.name, log_path=output_path)
        w.write(m)
        original = json_tricks.load(open(os.path.join(dirpath, files[0]), 'r'))
        update = json_tricks.load(
            open(os.path.join(output_path.name, 'Model.json'), 'r'))
        try:
            assert update["model"] == original["model"]
        except AssertionError as e:
            logger.error("Model differs for usecase {loc}".format(loc=dirpath))
            e.args += ("Model differs for usecase {loc}".format(loc=dirpath), )
            raise