PrototypeNormalizer.scala 4.08 KB
Newer Older
Daria M. Tomecka's avatar
Daria M. Tomecka committed
1
2
3
4
5
6
7
8
9
package eu.nomad_lab.normalizers

import eu.{ nomad_lab => lab }
import eu.nomad_lab.DefaultPythonInterpreter
import org.{ json4s => jn }
import scala.collection.breakOut
import eu.nomad_lab.normalize.ExternalNormalizerGenerator
import eu.nomad_lab.meta
import eu.nomad_lab.query
Daria M. Tomecka's avatar
Daria M. Tomecka committed
10
11
12
13
14
15
16
import eu.nomad_lab.resolve._
import eu.nomad_lab.h5.EmitJsonVisitor
import eu.nomad_lab.h5.H5EagerScanner
import eu.nomad_lab.h5.SectionH5
import eu.nomad_lab.parsers.ExternalParserWrapper
import eu.nomad_lab.JsonUtils
import scala.collection.mutable.StringBuilder
Daria M. Tomecka's avatar
Daria M. Tomecka committed
17

Daria M. Tomecka's avatar
Daria M. Tomecka committed
18
19
object PrototypesNormalizer extends ExternalNormalizerGenerator(
  name = "PrototypesNormalizer",
Daria M. Tomecka's avatar
Daria M. Tomecka committed
20
  info = jn.JObject(
Daria M. Tomecka's avatar
Daria M. Tomecka committed
21
    ("name" -> jn.JString("PrototypesNormalizer")) ::
Daria M. Tomecka's avatar
Daria M. Tomecka committed
22
      ("parserId" -> jn.JString("PrototypesNormalizer" + lab.PrototypesVersionInfo.version)) ::
Daria M. Tomecka's avatar
Daria M. Tomecka committed
23
24
25
26
      ("versionInfo" -> jn.JObject(
        ("nomadCoreVersion" -> jn.JObject(lab.NomadCoreVersionInfo.toMap.map {
          case (k, v) => k -> jn.JString(v.toString)
        }(breakOut): List[(String, jn.JString)])) ::
Daria M. Tomecka's avatar
Daria M. Tomecka committed
27
          (lab.PrototypesVersionInfo.toMap.map {
Daria M. Tomecka's avatar
Daria M. Tomecka committed
28
29
30
31
32
            case (key, value) =>
              (key -> jn.JString(value.toString))
          }(breakOut): List[(String, jn.JString)])
      )) :: Nil
  ),
33
  context = "calculation_context",
Daria M. Tomecka's avatar
Daria M. Tomecka committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  filter = query.CompiledQuery(query.QueryExpression("section_system"), meta.KnownMetaInfoEnvs.publicMeta),
  cmd = Seq(DefaultPythonInterpreter.pythonExe(), "${envDir}/normalizers/prototypes/normalizer/normalizer-prototypes/classify4me_prototypes.py",
    "${contextUri}", "${archivePath}"),
  resList = Seq(
    "normalizer-prototypes/setup_paths.py",
    "normalizer-prototypes/classify4me_prototypes.py",
    "nomad_meta_info/public.nomadmetainfo.json",
    "nomad_meta_info/common.nomadmetainfo.json",
    "nomad_meta_info/meta_types.nomadmetainfo.json",
    "nomad_meta_info/stats.nomadmetainfo.json"
  ) ++ DefaultPythonInterpreter.commonFiles(),
  dirMap = Map(
    "normalizer-prototypes" -> "normalizers/prototypes/normalizer/normalizer-prototypes",
    "nomad_meta_info" -> "nomad-meta-info/meta_info/nomad_meta_info",
    "python" -> "python-common/common/python/nomadcore"
  ) ++ DefaultPythonInterpreter.commonDirMapping(),
  metaInfoEnv = lab.meta.KnownMetaInfoEnvs.all
) {
Daria M. Tomecka's avatar
Daria M. Tomecka committed
52
  val trace: Boolean = false
Daria M. Tomecka's avatar
Daria M. Tomecka committed
53
54

  override def stdInHandler(context: ResolvedRef)(wrapper: ExternalParserWrapper)(pIn: java.io.OutputStream): Unit = {
Daria M. Tomecka's avatar
Daria M. Tomecka committed
55
56
57
    val out: java.io.Writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(pIn));
    val stringBuilder = if (trace)
      new StringBuilder
Daria M. Tomecka's avatar
Daria M. Tomecka committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    else
      null
    def writeOut(s: String): Unit = {
      out.write(s)
      if (trace) stringBuilder ++= s
    }
    def flush(): Unit = {
      out.flush()
      if (trace) {
        logger.info(stringBuilder.result())
        stringBuilder.clear()
      }
    }
    writeOut("[")
    var isFirst = true
    try {
      context match {
        case Calculation(archiveSet, c) =>
Daria M. Tomecka's avatar
Daria M. Tomecka committed
76
77
78
          val sysTable = c.sectionTable(Seq("section_run", "section_system"))

          def outputSysSection(sysSection: SectionH5): Unit = {
Daria M. Tomecka's avatar
Daria M. Tomecka committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
            if (!isFirst)
              writeOut(",")
            else
              isFirst = false
            //writeOut(s"""{
            //              |  "context": ${JsonUtils.escapeString(m.toRef.toUriStr(archiveSet.objectKind))},
            //              |  "section_system": """.stripMargin)
            val visitor = new EmitJsonVisitor(
              writeOut = writeOut
            )
            val scanner = new H5EagerScanner
            scanner.scanResolvedRef(Section(archiveSet, sysSection), visitor)
            //writeOut("}")
            flush()
          }

Daria M. Tomecka's avatar
Daria M. Tomecka committed
95
96
          val nSys = sysTable.lengthL
          if (nSys > 0)
97
98
            //  outputSysSection(sysTable(0))
            //if (nSys > 1)
Daria M. Tomecka's avatar
Daria M. Tomecka committed
99
            outputSysSection(sysTable(nSys - 1))
Daria M. Tomecka's avatar
Daria M. Tomecka committed
100
101
102
103
104
105
106
107
108
109
110
111
112

          writeOut("]")
          flush()
        case r =>
          throw new Exception(s"FhiAimsBasisNormalizer expected a calculation as context, but got $r")
      }
    } finally {
      out.close()
      pIn.close()
      wrapper.sendStatus = ExternalParserWrapper.SendStatus.Finished
    }
  }
}