generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ecoinvent import: Add method map builder from file.
- Loading branch information
1 parent
85fadab
commit 2dfd3e0
Showing
6 changed files
with
148 additions
and
37 deletions.
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lci/EF31ToEI391SubstanceMapper.kt
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lci/EcospoldMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lci/EcospoldMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/test/resources/ch/kleis/lcaplugin/imports/ecospold/lci/one-record-map.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,,,,,,, |
3 changes: 3 additions & 0 deletions
3
src/test/resources/ch/kleis/lcaplugin/imports/ecospold/lci/one-valid-one-invalid.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, |
2 changes: 2 additions & 0 deletions
2
src/test/resources/ch/kleis/lcaplugin/imports/ecospold/lci/orphan.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, |