Skip to content

Commit

Permalink
Merge branch 'main' into ms/#67-reduce-memory-usage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfeismann authored Nov 28, 2024
2 parents 3ef6261 + f24c56c commit b8ba717
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [1.0.0] - 2021-08-03
### Added
- Basic functionality to convert SimBench data sets to [PowerSystemDataModel](https://github.com/ie3-institute/powersystemdatamodel)
- Added an Extractor to preprocess and handle the new download link from Uni Kassel, enabling seamless processing of
the simbench_datalinks.csv file [#267](https://github.com/ie3-institute/simBench2psdm/issues/267)

[Unreleased]: https://github.com/ie3-institute/simbench2psdm/compare/v1.0...HEAD
[1.0.0]: https://github.com/ie3-institute/simbench2psdm/releases/tag/1.0
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id 'com.diffplug.spotless' version '6.25.0'// code format
id "de.undercouch.download" version "5.6.0" // downloads plugin
id 'jacoco' // java code coverage plugin
id "org.sonarqube" version "5.1.0.4882" // sonarqube
id "org.sonarqube" version "6.0.1.5171" // sonarqube
id 'org.scoverage' version '8.1' // Code coverage plugin for scala
id "com.github.maiflai.scalatest" version "0.32" // run scalatest without specific task
}
Expand All @@ -17,9 +17,9 @@ ext {
javaVersion = JavaVersion.VERSION_17

scalaVersion = '2.13'
scalaBinaryVersion = '2.13.14'
tscfgVersion = '0.9.986'
slf4jVersion = '2.0.13'
scalaBinaryVersion = '2.13.15'
tscfgVersion = '1.1.3'
slf4jVersion = '2.0.16'

scriptsLocation = 'gradle' + File.separator + 'scripts' + File.separator //location of script plugins
}
Expand Down Expand Up @@ -61,11 +61,11 @@ dependencies {
exclude group: 'com.github.ie3-institute'
}

implementation 'org.codehaus.groovy:groovy:3.0.22'
implementation 'org.codehaus.groovy:groovy:3.0.23'

implementation 'tech.units:indriya:2.2'
implementation 'tech.units:indriya:2.2.1'
implementation 'org.locationtech.jts:jts-core:1.19.0'
implementation 'commons-io:commons-io:2.16.1'
implementation 'commons-io:commons-io:2.18.0'

// logging
implementation 'org.apache.logging.log4j:log4j-api:+' // log4j
Expand All @@ -84,7 +84,7 @@ dependencies {
testImplementation "org.scalatest:scalatest_${scalaVersion}:3.2.19"
testImplementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
testImplementation "org.pegdown:pegdown:1.6.0" // HTML report for scalatest
implementation 'org.mockito:mockito-core:5.12.0' // mocking framework
implementation 'org.mockito:mockito-core:5.14.2' // mocking framework

// config //
implementation 'com.typesafe:config:+'
Expand Down
2 changes: 1 addition & 1 deletion gradle/scripts/scoverage.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/scoverage/gradle-scoverage/issues/109 for details
*/
scoverage {
scoverageVersion = "2.1.1"
scoverageVersion = "2.2.1"
scoverageScalaVersion = scalaBinaryVersion
coverageOutputHTML = false
coverageOutputXML = true
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config-template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CsvConfig {
io {
simbenchCodes = ["String"]
input {
download.baseUrl = "String" | "http://141.51.193.167/simbench/gui/usecase/download"
download.baseUrl = "String" | "https://daks.uni-kassel.de/bitstreams"
download.folder = "String" | "inputData/download/"
download.failOnExistingFiles = "Boolean" | false
csv = CsvConfig
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/edu/ie3/simbench/config/SimbenchConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object SimbenchConfig {
final case class Download(
baseUrl: java.lang.String,
failOnExistingFiles: scala.Boolean,
folder: java.lang.String
directory: java.lang.String
)
object Download {
def apply(
Expand All @@ -79,12 +79,12 @@ object SimbenchConfig {
SimbenchConfig.Io.Input.Download(
baseUrl =
if (c.hasPathOrNull("baseUrl")) c.getString("baseUrl")
else "http://141.51.193.167/simbench/gui/usecase/download",
else "https://daks.uni-kassel.de/bitstreams",
failOnExistingFiles =
c.hasPathOrNull("failOnExistingFiles") && c.getBoolean(
"failOnExistingFiles"
),
folder =
directory =
if (c.hasPathOrNull("folder")) c.getString("folder")
else "inputData/download/"
)
Expand Down Expand Up @@ -116,7 +116,7 @@ object SimbenchConfig {
final case class Output(
compress: scala.Boolean,
csv: SimbenchConfig.CsvConfig,
targetFolder: java.lang.String
targetDir: java.lang.String
)
object Output {
def apply(
Expand All @@ -132,7 +132,7 @@ object SimbenchConfig {
parentPath + "csv.",
$tsCfgValidator
),
targetFolder =
targetDir =
if (c.hasPathOrNull("targetFolder")) c.getString("targetFolder")
else "convertedData"
)
Expand Down
19 changes: 13 additions & 6 deletions src/main/scala/edu/ie3/simbench/io/Downloader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import edu.ie3.simbench.model.SimbenchCode

import scala.language.postfixOps
import scala.sys.process._
import scala.util.{Failure, Success, Try}

final case class Downloader(
downloadFolder: String,
downloadDir: String,
baseUrl: String,
uuidMap: Map[String, String],
failOnExistingFiles: Boolean = true
) extends LazyLogging {

Expand All @@ -25,13 +25,13 @@ final case class Downloader(
* A valid SimBench code
*/
def download(simbenchCode: SimbenchCode): Path = {
val downloadFolderPath = new File(s"$downloadFolder/")
val downloadDirPath = new File(s"$downloadDir/")
val downloadPath =
Paths.get(
s"${downloadFolderPath.getAbsolutePath}/${simbenchCode.code}.zip"
s"${downloadDirPath.getAbsolutePath}/${simbenchCode.code}.zip"
)
val downloadFile = downloadPath.toFile
if (downloadFolderPath.mkdirs()) {
if (downloadDirPath.mkdirs()) {
logger.debug("Created all non existing folders")
}

Expand All @@ -45,8 +45,15 @@ final case class Downloader(
logger.debug(s"Overwrite existing file ${downloadFile.getName}")
}

val uuid = uuidMap.getOrElse(
simbenchCode.code,
throw DownloaderException(
s"UUID not found for SimBench code: ${simbenchCode.code}"
)
)

val url = new URL(
s"$baseUrl/?Simbench_Code=${simbenchCode.code}"
s"$baseUrl/$uuid/download"
)
url #> downloadFile !!

Expand Down
67 changes: 67 additions & 0 deletions src/main/scala/edu/ie3/simbench/io/Extractor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package edu.ie3.simbench.io

import java.net.URL
import edu.ie3.simbench.config.SimbenchConfig
import java.nio.file.{Files, Paths, StandardCopyOption}
import scala.io.Source
import scala.util.matching.Regex
import com.typesafe.scalalogging.LazyLogging

class Extractor(simbenchConfig: SimbenchConfig) extends LazyLogging {

def download(): Unit = {
val url = new URL(
"https://daks.uni-kassel.de/bitstreams/b1fb0ccf-94a1-4d5e-921c-5f9fa44e5371/download"
)
val inputStream = url.openStream()

try {
val downloadFolder = simbenchConfig.io.input.download.directory
val outputPath = s"$downloadFolder/simbench_datalinks.csv"
val path = Paths.get(outputPath)
Files.createDirectories(path.getParent)
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING)
logger.debug(s"File downloaded successfully to $outputPath")
} finally {
inputStream.close()
}
}

/** Extracts a map of simbench-code to UUID from the downloaded file.
*/
def extractUUIDMap(): Map[String, String] = {
val downloadDir = simbenchConfig.io.input.download.directory
val outputPath = s"$downloadDir/simbench_datalinks.csv"
val uuidPattern: Regex =
"""[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}""".r

val source = Source.fromFile(outputPath)
val lines = source.getLines().toList

val header = lines.head.split(",").map(_.trim)
val data = lines.tail

val codeIndex = header.indexOf("code")
val csvIndex = header.indexOf("csv")

if (codeIndex == -1 || csvIndex == -1) {
throw new IllegalArgumentException(
"The required columns ('code', 'csv') are missing."
)
}

// Extract the map
val dataMap: Map[String, String] = data.flatMap { line =>
val columns = line.split(",").map(_.trim)
if (columns.length > csvIndex) {
val code = columns(codeIndex)
val csv = columns(csvIndex)
val uuidOpt = uuidPattern.findFirstIn(csv)
uuidOpt.map(uuid => code -> uuid)
} else None
}.toMap

source.close()
dataMap
}
}
19 changes: 15 additions & 4 deletions src/main/scala/edu/ie3/simbench/main/RunSimbench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import edu.ie3.datamodel.io.sink.CsvFileSink
import edu.ie3.simbench.config.{ConfigValidator, SimbenchConfig}
import edu.ie3.simbench.convert.GridConverter
import edu.ie3.simbench.exception.CodeValidationException
import edu.ie3.simbench.io.{Downloader, IoUtils, SimbenchReader, Zipper}
import edu.ie3.simbench.io.{
Downloader,
Extractor,
IoUtils,
SimbenchReader,
Zipper
}
import edu.ie3.simbench.model.SimbenchCode
import edu.ie3.util.io.FileIOUtils
import org.apache.commons.io.FilenameUtils
Expand All @@ -36,12 +42,17 @@ object RunSimbench extends SimbenchHelper {
/* Validate the config */
ConfigValidator.checkValidity(simbenchConfig)

val extractor = new Extractor(simbenchConfig)
extractor.download()
val uuidMap = extractor.extractUUIDMap()

simbenchConfig.io.simbenchCodes.foreach { simbenchCode =>
logger.info(s"$simbenchCode - Downloading data set from SimBench website")
val downloader =
Downloader(
simbenchConfig.io.input.download.folder,
simbenchConfig.io.input.download.directory,
simbenchConfig.io.input.download.baseUrl,
uuidMap,
simbenchConfig.io.input.download.failOnExistingFiles
)
val downloadedFile =
Expand All @@ -55,7 +66,7 @@ object RunSimbench extends SimbenchHelper {
val dataFolder =
Zipper.unzip(
downloadedFile,
downloader.downloadFolder,
downloader.downloadDir,
simbenchConfig.io.input.download.failOnExistingFiles,
flattenDirectories = true
)
Expand Down Expand Up @@ -87,7 +98,7 @@ object RunSimbench extends SimbenchHelper {
/* Check, if a directory hierarchy is needed or not */
val baseTargetDirectory =
IoUtils.ensureHarmonizedAndTerminatingFileSeparator(
simbenchConfig.io.output.targetFolder
simbenchConfig.io.output.targetDir
)
val csvSink = if (simbenchConfig.io.output.csv.directoryHierarchy) {
new CsvFileSink(
Expand Down
9 changes: 7 additions & 2 deletions src/test/scala/edu/ie3/simbench/io/DownloaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import scala.util.{Failure, Success, Try}

@Ignore
class DownloaderSpec extends UnitSpec with IoUtils {
val uuidMap: Map[String, String] = Map(
"1-LV-urban6--0-sw" -> "a5a1d286-99a8-431c-9b2b-943f86467f22"
)
val downloader: Downloader = Downloader(
"testData/download/",
"http://141.51.193.167/simbench/gui/usecase/download",
"https://daks.uni-kassel.de/bitstreams",
uuidMap,
failOnExistingFiles = false
)

Expand All @@ -41,7 +45,8 @@ class DownloaderSpec extends UnitSpec with IoUtils {
"fail downloading an already existing archive, when asked to do so" in {
val downloader: Downloader = Downloader(
"testData/download/",
"http://141.51.193.167/simbench/gui/usecase/download",
"https://daks.uni-kassel.de/bitstreams",
uuidMap,
failOnExistingFiles = true
)

Expand Down
Loading

0 comments on commit b8ba717

Please sign in to comment.