Skip to content

Commit

Permalink
Ecoinvent import: Add method map builder from file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedesroches committed Aug 2, 2023
1 parent 85fadab commit 2dfd3e0
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 37 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ch.kleis.lcaplugin.imports.ecospold.lci

import ch.kleis.lcaplugin.core.lang.expression.SubstanceType
import ch.kleis.lcaplugin.imports.ecospold.model.ElementaryExchange
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord
import java.io.Reader
import kotlin.streams.asSequence

typealias ID = String

object EcospoldMapper {
private val csvFormat: CSVFormat = CSVFormat.Builder.create().setHeader().build()

fun buildMapping(mapData: Reader): Map<ID, ElementaryExchange> =
CSVParser.parse(mapData, csvFormat).use { parser ->
if (validateHeaders(parser.headerMap)) {
parser.stream().asSequence().mapNotNull { record ->
when (record["flow_status"]) {
"mapped" -> mappedElement(record)
else -> null
}
}.toMap()
} else {
throw IllegalArgumentException("Header validation failed.")
}
}

private fun validateHeaders(headers: Map<String, Int>): Boolean =
headers.containsKey("flow_status")
&& headers.containsKey("id")
&& headers.containsKey("conversion_factor")
&& headers.containsKey("method_name")
&& headers.containsKey("method_unit")
&& headers.containsKey("method_compartment")
&& headers.containsKey("method_subcompartment")

private fun mappedElement(record: CSVRecord): Pair<ID, ElementaryExchange>? =
try {
val id = record["id"]
id to ElementaryExchange(
id,
record["conversion_factor"].toDouble(),
record["method_name"],
record["method_unit"],
record["method_compartment"],
record["method_subcompartment"],
SubstanceType.EMISSION,
"Mapped exchange"
)
} catch (_: IllegalArgumentException) {
null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ch.kleis.lcaplugin.imports.ecospold.lci

import ch.kleis.lcaplugin.core.lang.expression.SubstanceType
import ch.kleis.lcaplugin.imports.ecospold.model.ElementaryExchange
import com.jetbrains.rd.util.first
import java.io.InputStreamReader
import java.io.StringReader
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull

class EcospoldMapperTest {
@Test
fun test_buildMapping_whenNoData_thenError() {
// given
val emptyCSVReader = StringReader("")

// when then
assertFailsWith(IllegalArgumentException::class) {
EcospoldMapper.buildMapping(emptyCSVReader)
}
}

@Test
fun test_buildMapping_whenMapped_thenMap() {
// given when
val mapping = this::class.java.getResourceAsStream("one-record-map.csv")?.use {
EcospoldMapper.buildMapping(InputStreamReader(it))
}
// then
assertNotNull(mapping)
assertEquals("584ffb1c-036d-417b-a9d1-1ec694dc2cdc", mapping.first().key)
assertEquals(
ElementaryExchange(
"584ffb1c-036d-417b-a9d1-1ec694dc2cdc",
1.0,
"1,2-dichlorobenzene",
"kg",
"Emissions to air",
"Emissions to air, unspecified (long-term)",
SubstanceType.EMISSION,
"Mapped exchange"
),
mapping.first().value
)
}

@Test
fun test_buildMapping_whenOrphan_thenNoMap() {
// given when
val mapping = this::class.java.getResourceAsStream("orphan.csv")?.use {
EcospoldMapper.buildMapping(InputStreamReader(it))
}

// then
assertNotNull(mapping)
assert(mapping.isEmpty())
}

@Test
fun test_buildMapping_whenMixed_thenOnlyMapped() {
// given when
val mapping = this::class.java.getResourceAsStream("one-valid-one-invalid.csv")?.use {
EcospoldMapper.buildMapping(InputStreamReader(it))
}
// then
assertNotNull(mapping)
assertEquals(1, mapping.size)
assertEquals("584ffb1c-036d-417b-a9d1-1ec694dc2cdc", mapping.first().key)
assertEquals(
ElementaryExchange(
"584ffb1c-036d-417b-a9d1-1ec694dc2cdc",
1.0,
"1,2-dichlorobenzene",
"kg",
"Emissions to air",
"Emissions to air, unspecified (long-term)",
SubstanceType.EMISSION,
"Mapped exchange"
),
mapping.first().value
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,casNumber,formula,synonym,compartment,subcompartment,unitName,flow_status,flowUsed,conversion_factor,method_id,method_name,method_compartment,method_subcompartment,method_unit,compartment_status,acidification//accumulated exceedance (AE),climate change//global warming potential (GWP100),climate change: biogenic//global warming potential (GWP100),climate change: fossil//global warming potential (GWP100),climate change: land use and land use change//global warming potential (GWP100),particulate matter formation//impact on human health,ecotoxicity: freshwater//comparative toxic unit for ecosystems (CTUe),"ecotoxicity: freshwater, inorganics//comparative toxic unit for ecosystems (CTUe)","ecotoxicity: freshwater, organics//comparative toxic unit for ecosystems (CTUe)",eutrophication: marine//fraction of nutrients reaching marine end compartment (N),eutrophication: freshwater//fraction of nutrients reaching freshwater end compartment (P),eutrophication: terrestrial//accumulated exceedance (AE),human toxicity: carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: carcinogenic, organics//comparative toxic unit for human (CTUh)",human toxicity: non-carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: non-carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: non-carcinogenic, organics//comparative toxic unit for human (CTUh)",ionising radiation: human health//human exposure efficiency relative to u235,land use//soil quality index,ozone depletion//ozone depletion potential (ODP),photochemical oxidant formation: human health//tropospheric ozone concentration increase,energy resources: non-renewable//abiotic depletion potential (ADP): fossil fuels,material resources: metals/minerals//abiotic depletion potential (ADP): elements (ultimate reserves),water use//user deprivation potential (deprivation-weighted water consumption)
584ffb1c-036d-417b-a9d1-1ec694dc2cdc,"1,2-Dichlorobenzene",000095-50-1,C6H4Cl2,"benzene, 1,2-dichloro-; o-Dichlorobenzene",air,"low population density, long-term",kg,mapped,FALSE,1,4d9a8790-3ddd-11dd-9bc8-0050c2490048,"1,2-dichlorobenzene",Emissions to air,"Emissions to air, unspecified (long-term)",kg,mapped,,,,,,,0,,0,,,,0,,0,0,,0,,,,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,casNumber,formula,synonym,compartment,subcompartment,unitName,flow_status,flowUsed,conversion_factor,method_id,method_name,method_compartment,method_subcompartment,method_unit,compartment_status,acidification//accumulated exceedance (AE),climate change//global warming potential (GWP100),climate change: biogenic//global warming potential (GWP100),climate change: fossil//global warming potential (GWP100),climate change: land use and land use change//global warming potential (GWP100),particulate matter formation//impact on human health,ecotoxicity: freshwater//comparative toxic unit for ecosystems (CTUe),"ecotoxicity: freshwater, inorganics//comparative toxic unit for ecosystems (CTUe)","ecotoxicity: freshwater, organics//comparative toxic unit for ecosystems (CTUe)",eutrophication: marine//fraction of nutrients reaching marine end compartment (N),eutrophication: freshwater//fraction of nutrients reaching freshwater end compartment (P),eutrophication: terrestrial//accumulated exceedance (AE),human toxicity: carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: carcinogenic, organics//comparative toxic unit for human (CTUh)",human toxicity: non-carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: non-carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: non-carcinogenic, organics//comparative toxic unit for human (CTUh)",ionising radiation: human health//human exposure efficiency relative to u235,land use//soil quality index,ozone depletion//ozone depletion potential (ODP),photochemical oxidant formation: human health//tropospheric ozone concentration increase,energy resources: non-renewable//abiotic depletion potential (ADP): fossil fuels,material resources: metals/minerals//abiotic depletion potential (ADP): elements (ultimate reserves),water use//user deprivation potential (deprivation-weighted water consumption)
584ffb1c-036d-417b-a9d1-1ec694dc2cdc,"1,2-Dichlorobenzene",000095-50-1,C6H4Cl2,"benzene, 1,2-dichloro-; o-Dichlorobenzene",air,"low population density, long-term",kg,mapped,FALSE,1,4d9a8790-3ddd-11dd-9bc8-0050c2490048,"1,2-dichlorobenzene",Emissions to air,"Emissions to air, unspecified (long-term)",kg,mapped,,,,,,,0,,0,,,,0,,0,0,,0,,,,,,,
d21da01e-f96f-4db5-9746-7b70db8a1f2c,"1,4-Butanediol",000110-63-4,C4H10O2,"Butylene glycol; butane-1,4-diol",air,"low population density, long-term",kg,ecoinvent orphan,FALSE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,casNumber,formula,synonym,compartment,subcompartment,unitName,flow_status,flowUsed,conversion_factor,method_id,method_name,method_compartment,method_subcompartment,method_unit,compartment_status,acidification//accumulated exceedance (AE),climate change//global warming potential (GWP100),climate change: biogenic//global warming potential (GWP100),climate change: fossil//global warming potential (GWP100),climate change: land use and land use change//global warming potential (GWP100),particulate matter formation//impact on human health,ecotoxicity: freshwater//comparative toxic unit for ecosystems (CTUe),"ecotoxicity: freshwater, inorganics//comparative toxic unit for ecosystems (CTUe)","ecotoxicity: freshwater, organics//comparative toxic unit for ecosystems (CTUe)",eutrophication: marine//fraction of nutrients reaching marine end compartment (N),eutrophication: freshwater//fraction of nutrients reaching freshwater end compartment (P),eutrophication: terrestrial//accumulated exceedance (AE),human toxicity: carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: carcinogenic, organics//comparative toxic unit for human (CTUh)",human toxicity: non-carcinogenic//comparative toxic unit for human (CTUh),"human toxicity: non-carcinogenic, inorganics//comparative toxic unit for human (CTUh)","human toxicity: non-carcinogenic, organics//comparative toxic unit for human (CTUh)",ionising radiation: human health//human exposure efficiency relative to u235,land use//soil quality index,ozone depletion//ozone depletion potential (ODP),photochemical oxidant formation: human health//tropospheric ozone concentration increase,energy resources: non-renewable//abiotic depletion potential (ADP): fossil fuels,material resources: metals/minerals//abiotic depletion potential (ADP): elements (ultimate reserves),water use//user deprivation potential (deprivation-weighted water consumption)
d21da01e-f96f-4db5-9746-7b70db8a1f2c,"1,4-Butanediol",000110-63-4,C4H10O2,"Butylene glycol; butane-1,4-diol",air,"low population density, long-term",kg,ecoinvent orphan,FALSE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

0 comments on commit 2dfd3e0

Please sign in to comment.