java.io.FileReader Scala Examples

The following examples show how to use java.io.FileReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: DetectSeriesSpec.scala    From mimir   with Apache License 2.0 5 votes vote down vote up
package mimir.statistics

import java.io.{BufferedReader, File, FileReader, StringReader}
import java.sql.SQLException

import scala.collection.JavaConversions._
import org.specs2.mutable._
import org.specs2.matcher.FileMatchers
import mimir._
import mimir.sql._
import mimir.parser._
import mimir.algebra._
import mimir.optimizer._
import mimir.ctables._
import mimir.exec._
import mimir.util._
import mimir.test._
import mimir.statistics._

object DetectSeriesSpec 
	extends SQLTestSpecification("DetectSeriesTest"){
	
	sequential
	
	def testDetectSeriesof(oper: Operator) = {
	  val (schema, df) = SparkUtils.getDataFrameWithProvFromQuery(db, oper)
    DetectSeries.seriesOf(df, schema, 0.1).collect().toSeq
	}
	
	
	"The DetectSeriesSpec" should {

		"Be able to load DetectSeriesTest1" >> {
			db.loader.loadTable("test/data/DetectSeriesTest1.csv"); ok
		}
		
		"Be able to detect Date and Timestamp type" >> {
			val queryOper = select("SELECT * FROM DetectSeriesTest1")
			val colSeq: Seq[String] = testDetectSeriesof(queryOper).map{_.columnName.toString}
			
			colSeq must have size(4)
			colSeq must contain("TRAN_TS","EXP_DT", "JOIN_DT", "DOB")
		}

		"Be able to create a new schema and detect Date and Timestamp type" >> {
			db.catalog.materializedTableProvider().createStoredTableAs(
					HardTable(Seq(
						ID("JN_DT") -> TDate(), 
						ID("JN_TS") -> TTimestamp()
					), Seq()),
					ID("DetectSeriesTest3"),
					db
        )

			val queryOper = select("SELECT * FROM DetectSeriesTest3")
			val colSeq: Seq[String] = testDetectSeriesof(queryOper).map{_.columnName.toString}
			
			colSeq must have size(2)
			colSeq must contain("JN_DT", "JN_TS")
		}

		"Be able to load DetectSeriesTest2" >> {
			db.loader.loadTable("test/data/DetectSeriesTest2.csv"); ok
		}

		"Be able to detect Date, Timestamp and increasing-decreasing Numeric type" >> {
			val queryOper = select("SELECT * FROM DetectSeriesTest2")
			val colSeq: Seq[String] = testDetectSeriesof(queryOper).map{_.columnName.toString}
			
			colSeq must have size(6)
			colSeq must contain("TRAN_TS","EXP_DT", "JOIN_DT", "DOB", "ROW_ID", "QUALITY")
		}
		
	}	
} 
Example 2
Source File: datatable.scala    From scala-course   with GNU General Public License v3.0 5 votes vote down vote up
import java.io.{File,FileReader}
import com.github.tototoshi.csv._
import com.github.martincooper.datatable._
import scala.annotation.tailrec
import scala.util.Try

object StringCol

object DatatableTest {

  def readCsv(name: String, file: FileReader, colTypes: Map[String,Object]): DataTable = {
    val reader=CSVReader.open(file)
    val all=reader.allWithHeaders()
    reader.close()
    val ks=colTypes.keys
    val colSet=ks map {key => (key,all map {row => row(key)}) }
    val dataCols=colSet map {pair => colTypes(pair._1) match { 
      case StringCol => new DataColumn[String](pair._1,pair._2)
      case Int       => new DataColumn[Int](pair._1,pair._2 map {x=>
                                        Try(x.toInt).toOption.getOrElse(-99)})
      case Double    => new DataColumn[Double](pair._1,pair._2 map {x=>
                                        Try(x.toDouble).toOption.getOrElse(-99.0)})
      } 
    }
    DataTable(name,dataCols).get
  }

  def writeCsv(df: DataTable,out: File): Unit = {
    val writer = CSVWriter.open(out)
    writer.writeRow(df.columns.map{_.name})
    df.foreach{r=>writer.writeRow(r.values)}
    writer.close()
  }


  def main(args: Array[String]) = {

    val colTypes=Map("DriveTrain" -> StringCol, 
                     "Min.Price" -> Double, 
                     "Cylinders" -> Int, 
                     "Horsepower" -> Int, 
                     "Length" -> Int, 
                     "Make" -> StringCol, 
                     "Passengers" -> Int, 
                     "Width" -> Int, 
                     "Fuel.tank.capacity" -> Double, 
                     "Origin" -> StringCol, 
                     "Wheelbase" -> Int, 
                     "Price" -> Double, 
                     "Luggage.room" -> Double, 
                     "Weight" -> Int, 
                     "Model" -> StringCol, 
                     "Max.Price" -> Double, 
                     "Manufacturer" -> StringCol, 
                     "EngineSize" -> Double, 
                     "AirBags" -> StringCol, 
                     "Man.trans.avail" -> StringCol, 
                     "Rear.seat.room" -> Double, 
                     "RPM" -> Int, 
                     "Turn.circle" -> Double, 
                     "MPG.highway" -> Int, 
                     "MPG.city" -> Int, 
                     "Rev.per.mile" -> Int, 
                     "Type" -> StringCol)
    val df=readCsv("Cars93",new FileReader("../r/cars93.csv"),colTypes)
    println(df.length,df.columns.length)

    val df2=df.filter(row=>row.as[Double]("EngineSize")<=4.0).toDataTable
    println(df2.length,df2.columns.length)

    val oldCol=df2.columns("Weight").as[Int]
    val newCol=new DataColumn[Double]("WeightKG",oldCol.data.map{_.toDouble*0.453592})
    val df3=df2.columns.add(newCol).get
    println(df3.length,df3.columns.length)

    writeCsv(df3,new File("out.csv"))

    //println("Done")
  }



} 
Example 3
Source File: H2OLoader.scala    From ForestFlow   with Apache License 2.0 5 votes vote down vote up
package ai.forestflow.serving.impl

import java.io.{ByteArrayInputStream, FileReader}
import java.nio.file.Paths

import ai.forestflow.serving.MLFlow.H2OMLFlowSpec
import ai.forestflow.serving.interfaces.Loader
import cats.syntax.either._
import ai.forestflow.domain.{FQRV, FlavorShim, ServableSettings}
import ai.forestflow.serving.MLFlow.H2OMLFlowSpec
import ai.forestflow.serving.interfaces.Loader
import hex.genmodel.MojoReaderBackendFactory
import hex.genmodel.MojoReaderBackendFactory.CachingStrategy
import io.circe.{Error, yaml}

trait H2OLoader extends Loader {
  def version: Option[String]
  override def createServable(servableBinary: Array[Byte], fqrv: FQRV, settings: ServableSettings)(implicit eCTX: EnvironmentContext): H2OServable = {
    import hex.genmodel.MojoModel

    val mojoReader = MojoReaderBackendFactory.createReaderBackend(
      new ByteArrayInputStream(servableBinary),
      CachingStrategy.MEMORY)

    H2OServable(MojoModel.load(mojoReader), fqrv, settings)
  }
}

case class MLFlowH2OLoader(dataPath: String, version: Option[String]) extends H2OLoader {

  override def getRelativeServablePath(implicit eCTX: EnvironmentContext): String = {
    val json = yaml.parser.parse(new FileReader(Paths.get(eCTX.localDir.getAbsolutePath, dataPath, "h2o.yaml").toFile)) // TODO move "h2o.yaml" constant to configuration

    val h2oSpec = json
      .leftMap(err => err: Error)
      .flatMap(_.as[H2OMLFlowSpec])
      .valueOr(throw _)

    Paths.get(dataPath, h2oSpec.modelFile).toString
  }
}


trait BasicH2OMojoLoader extends H2OLoader  {
  this : FlavorShim with Loader =>
  val mojoPath: String
  val version: Option[String]

  override def getRelativeServablePath(implicit eCTX: EnvironmentContext): String = mojoPath
} 
Example 4
Source File: BenchmarkSubmitter.scala    From Argus-SAF   with Apache License 2.0 5 votes vote down vote up
package org.argus.jnsaf.submitter

import java.io.{BufferedReader, FileReader}

import org.argus.amandroid.plugin.TaintAnalysisApproach
import org.argus.jawa.core.util._

object BenchmarkSubmitter {
  private def readExpected(expectedPath: String): IMap[String, Int] = {
    val result: MMap[String, Int] = mmapEmpty
    val uri = FileUtil.toUri(expectedPath)
    val file = FileUtil.toFile(uri)
    val rdr: BufferedReader = new BufferedReader(new FileReader(file))
    var line = rdr.readLine()
    while(line != null){
      val entry = line.split("\\s+")
      result(entry(0)) = entry(1).toInt
      line = rdr.readLine()
    }
    rdr.close()
    result.toMap
  }
  def apply(sourcePath: String, address: String, port: Int, expectedFile: String, approach: TaintAnalysisApproach.Value): Unit = {
    val expected = readExpected(expectedFile)
    val analysisResult = ApkSubmitter(sourcePath, address, port, approach)
    case class Compare(var expected: Int = 0, var result: Int = 0)
    val compare: MMap[String, Compare] = mmapEmpty
    expected.foreach { case (name, num) =>
      compare(name) = Compare(expected = num)
    }
    analysisResult.foreach { case (name, res) =>
      var num = 0
      res match {
        case Some(r) =>
          num = r.paths.size
        case None =>
      }
      compare.getOrElseUpdate(name, Compare()).result = num
    }
    println("Taint analysis result:")
    println("Expected\tResult\t\tApk")
    compare.toList.sortBy(_._1).foreach { case (name, comp) =>
      println(s"${comp.expected}\t\t${comp.result}\t\t$name")
    }
  }
} 
Example 5
Source File: LibraryAPISummary.scala    From Argus-SAF   with Apache License 2.0 5 votes vote down vote up
package org.argus.jawa.core

import java.io.{BufferedReader, FileReader, StringReader}

import org.argus.jawa.core.elements.JawaType
import org.argus.jawa.core.util._

trait LibraryAPISummary {
  def isLibraryClass: JawaType => Boolean
}


class NoneLibraryAPISummary extends LibraryAPISummary {
  private val appPackages: MSet[String] = msetEmpty
  private val appPackagePrefixes: MSet[String] = msetEmpty
  private def doLoad(rdr: BufferedReader): Unit = {
    var line = Option(rdr.readLine())
    while(line.isDefined){
      line match {
        case Some(str) =>
          if(str.endsWith(".*"))
            appPackagePrefixes += str.substring(0, str.length - 2)
          else appPackages += str
        case None =>
      }
      line = Option(rdr.readLine())
    }
  }
  def load(filePath: String): Unit = {
    val rdr: BufferedReader = new BufferedReader(new FileReader(filePath))
    doLoad(rdr)
    rdr.close()
  }
  def loadFromString(str: String): Unit = {
    val rdr: BufferedReader = new BufferedReader(new StringReader(str))
    doLoad(rdr)
    rdr.close()
  }
  override def isLibraryClass: JawaType => Boolean = { typ =>
    !appPackages.contains(typ.getPackageName) && !appPackagePrefixes.exists(typ.getPackageName.startsWith)
  }
}

object NoLibraryAPISummary extends LibraryAPISummary {
  override def isLibraryClass: JawaType => Boolean = _ => false
} 
Example 6
Source File: MultiModuleMavenModelProviderTest.scala    From RTran   with Apache License 2.0 5 votes vote down vote up
package com.ebay.rtran.maven

import java.io.{File, FileReader}

import org.apache.commons.io.FileUtils
import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.codehaus.plexus.util.xml.Xpp3Dom
import org.scalatest.{FlatSpecLike, Matchers}

import collection.JavaConversions._


class MultiModuleMavenModelProviderTest extends FlatSpecLike with Matchers {

  val projectRoot = new File(getClass.getClassLoader.getResource("mvnproject").getFile)

  "MavenModelProvider" should "resolve all the pom files in the project" in {
    val projectCtx = new MavenProjectCtx(projectRoot)
    val provider = new MultiModuleMavenModelProvider
    val model = provider.create(projectCtx)

    model.modules foreach { m =>
      m.resolvedDependencies foreach {dep =>
        Option(dep.getVersion) should not be None
      }
    }
  }

  "MavenModelProvider" should "resolve all the pom files recursively in the project" in {
    val dir = new File(getClass.getClassLoader.getResource("recursive").getFile)
    val projectCtx = new MavenProjectCtx(dir)
    val provider = new MultiModuleMavenModelProvider
    val model = provider.create(projectCtx)
    model.modules.size should be (5)
  }

  "MavenModelProvider" should "not remove empty property nodes" in {
    val dir = new File(projectRoot.getParent, projectRoot.getName + "-bak")
    FileUtils.deleteQuietly(dir)
    FileUtils.copyDirectory(projectRoot, dir)
    val projectCtx = new MavenProjectCtx(dir)
    val provider = new MultiModuleMavenModelProvider
    val model = provider.create(projectCtx)

    provider save model

    val pom = new MavenXpp3Reader().read(new FileReader(new File(dir, "pom.xml")))
    pom.getProperties.getProperty("empty.property1") should be ("")
    pom.getProperties.getProperty("empty.property2") should be ("")
    pom.getProperties.getProperty("empty.property3") should be ("")
  }

  "MavenModelProvider" should "not break on xlint element" in {
    val dir = new File(projectRoot.getParent, projectRoot.getName + "-bak")
    FileUtils.deleteQuietly(dir)
    FileUtils.copyDirectory(projectRoot, dir)
    val projectCtx = new MavenProjectCtx(dir)
    val provider = new MultiModuleMavenModelProvider
    val model = provider.create(projectCtx)

    for {
      root <- model.parents.headOption
      build <- Option(root.pomModel.getBuild)
      sourcePlugin <- build.getPlugins.find(_.getArtifactId == "some-maven-plugin")
    } {
      build.removePlugin(sourcePlugin)
    }
    provider save model

    val pom = new MavenXpp3Reader().read(new FileReader(new File(dir, "pom.xml")))
    pom.getBuild.getPlugins.size() should be(1)
    val plugin = pom.getBuild.getPlugins.find(_.getArtifactId == "maven-source-plugin")
    plugin shouldNot be(None)
    plugin.map(_.getConfiguration.asInstanceOf[Xpp3Dom].getChild("compilerArguments").getChildCount) should be(Some(3))

  }

} 
Example 7
Source File: MavenUtilTest.scala    From RTran   with Apache License 2.0 5 votes vote down vote up
package com.ebay.rtran.maven.util

import java.io.FileReader

import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.apache.maven.{model => maven}
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.graph.Dependency
import org.scalatest.{FlatSpecLike, Matchers}

import scala.collection.JavaConversions._


class MavenUtilTest extends FlatSpecLike with Matchers {

  import MavenUtil._

  "MavenUtil" should "be able resolve an artifact" in {
    val artifact = MavenUtil.resolveArtifact(new DefaultArtifact("org.springframework:spring-parent:pom:3.1.4.RELEASE"))
    val model = new MavenXpp3Reader().read(new FileReader(artifact.getFile))
    model.getDependencyManagement.getDependencies.size should not be 0
  }

  "MavenUtil" should "be able to get all dependencies for an artifact" in {
    val dependencies = MavenUtil.allDependencies(new DefaultArtifact("com.typesafe.akka:akka-remote_2.11:jar:2.3.12"))
    dependencies.size should not be 0
    // com.typesafe:config:jar:1.2.1 is transitive dependency for com.typesafe.akka:akka-actor_2.11:jar:2.3.12
    dependencies exists (_.getArtifactId == "config") should be (true)
  }

  "MavenUtil" should "be able to get all dependencies for an maven dependency w/o cache" in {
    val dependency = new maven.Dependency()
    dependency.setGroupId("com.typesafe.akka")
    dependency.setArtifactId("akka-remote_2.11")
    dependency.setType("jar")
    dependency.setVersion("2.3.12")
    val dependencies = MavenUtil.getTransitiveDependencies(dependency, enableCache = true)
    dependencies.size should not be 0
    // com.typesafe:config:jar:1.2.1 is transitive dependency for com.typesafe.akka:akka-actor_2.11:jar:2.3.12
    dependencies exists (_.getArtifactId == "config") should be (true)

    val cachedDependencies = MavenUtil.getTransitiveDependencies(dependency, enableCache = true)
    cachedDependencies.toString should be (dependencies.toString)
  }

  "MavenUtil" should "be able to get all release versions" in {
    val versions = MavenUtil.findAvailableVersions("org.springframework", "spring-parent")
    versions.size should not be 0
    versions forall {version => !version.contains("SNAPSHOT")} should be (true)
  }

  "MavenUtil" should "be able to get all snapshot versions" in {
    val versions = MavenUtil.findAvailableVersions("org.springframework", "spring-parent", snapshot = true)
    versions.size should be (0)
  }

  "MavenUtil" should "be able to get all micro release versions" in {
    val versions = MavenUtil.findAvailableVersions("org.springframework", "spring-parent", "3.1")
    versions.size should not be 0
    versions foreach (_.startsWith("3.1") should be (true))
    versions forall {version => !version.contains("SNAPSHOT")} should be (true)
  }

  "MavenUtil" should "be able to get all micro snapshot versions" in {
    val versions = MavenUtil.findAvailableVersions("org.springframework", "spring-parent", "3.1", snapshot = true)
    versions.size should be (0)
  }

  "MavenUtil" should "be able to convert maven.model.Dependency to aether.graph.dependency" in {
    val mavenDependency = new maven.Dependency
    mavenDependency.setArtifactId("spring-parent")
    mavenDependency.setGroupId("org.springframework")
    mavenDependency.setVersion("3.1.4.RELEASE")
    val aetherDependency: Dependency = mavenDependency
    aetherDependency.getArtifact.getArtifactId should be (mavenDependency.getArtifactId)
    aetherDependency.getArtifact.getGroupId should be (mavenDependency.getGroupId)
    aetherDependency.getArtifact.getVersion should be (mavenDependency.getVersion)
  }

} 
Example 8
Source File: datatable.scala    From blog   with Apache License 2.0 5 votes vote down vote up
import java.io.{File,FileReader}
import com.github.tototoshi.csv._
import com.github.martincooper.datatable._
import scala.annotation.tailrec
import scala.util.Try

object StringCol

object DatatableTest {

  def readCsv(name: String, file: FileReader, colTypes: Map[String,Object]): DataTable = {
    val reader=CSVReader.open(file)
    val all=reader.allWithHeaders()
    reader.close()
    val ks=colTypes.keys
    val colSet=ks map {key => (key,all map {row => row(key)}) }
    val dataCols=colSet map {pair => colTypes(pair._1) match { 
      case StringCol => new DataColumn[String](pair._1,pair._2)
      case Int       => new DataColumn[Int](pair._1,pair._2 map {x=>
                                        Try(x.toInt).toOption.getOrElse(-99)})
      case Double    => new DataColumn[Double](pair._1,pair._2 map {x=>
                                        Try(x.toDouble).toOption.getOrElse(-99.0)})
      } 
    }
    DataTable(name,dataCols).get
  }

  def writeCsv(df: DataTable,out: File): Unit = {
    val writer = CSVWriter.open(out)
    writer.writeRow(df.columns.map{_.name})
    df.foreach{r=>writer.writeRow(r.values)}
    writer.close()
  }


  def main(args: Array[String]) = {

    val colTypes=Map("DriveTrain" -> StringCol, 
                     "Min.Price" -> Double, 
                     "Cylinders" -> Int, 
                     "Horsepower" -> Int, 
                     "Length" -> Int, 
                     "Make" -> StringCol, 
                     "Passengers" -> Int, 
                     "Width" -> Int, 
                     "Fuel.tank.capacity" -> Double, 
                     "Origin" -> StringCol, 
                     "Wheelbase" -> Int, 
                     "Price" -> Double, 
                     "Luggage.room" -> Double, 
                     "Weight" -> Int, 
                     "Model" -> StringCol, 
                     "Max.Price" -> Double, 
                     "Manufacturer" -> StringCol, 
                     "EngineSize" -> Double, 
                     "AirBags" -> StringCol, 
                     "Man.trans.avail" -> StringCol, 
                     "Rear.seat.room" -> Double, 
                     "RPM" -> Int, 
                     "Turn.circle" -> Double, 
                     "MPG.highway" -> Int, 
                     "MPG.city" -> Int, 
                     "Rev.per.mile" -> Int, 
                     "Type" -> StringCol)
    val df=readCsv("Cars93",new FileReader("../r/cars93.csv"),colTypes)
    println(df.length,df.columns.length)

    val df2=df.filter(row=>row.as[Double]("EngineSize")<=4.0).toDataTable
    println(df2.length,df2.columns.length)

    val oldCol=df2.columns("Weight").as[Int]
    val newCol=new DataColumn[Double]("WeightKG",oldCol.data.map{_.toDouble*0.453592})
    val df3=df2.columns.add(newCol).get
    println(df3.length,df3.columns.length)

    writeCsv(df3,new File("out.csv"))

    //println("Done")
  }



} 
Example 9
Source File: ExpressionOptimizerSpec.scala    From mimir   with Apache License 2.0 5 votes vote down vote up
package mimir.optimizer;

import java.io.{StringReader,FileReader}

import org.specs2.mutable._

import mimir._
import mimir.parser._
import mimir.algebra._
import mimir.sql._
import mimir.test.RASimplify
import mimir.optimizer.expression._
import mimir.optimizer.operator._

object ExpressionOptimizerSpec 
  extends Specification 
  with RASimplify
{
  
  def typechecker = new Typechecker  
  def expr = ExpressionParser.expr _

  def conditionals(x:String) = 
    simplify(PropagateConditions(expr(x)))

  def booleanOpts(x:String) =
    new FlattenBooleanConditionals(typechecker)(PullUpBranches(expr(x)))

  "Propagate Conditions" should {

    "Simplify Redundant Expressions" >> {
      conditionals("((A = 2) AND (A = 2))") must be equalTo expr("A = 2")
    }
    "Simplify Redundant Falsehoods" >> {
      conditionals("((A = 2) AND (A = 3))") must be equalTo expr("FALSE")
      conditionals("((A = 2) AND (A != 2))") must be equalTo expr("FALSE")
    }
    "Simplify If Statements" >> {
      conditionals("((A = 2) AND (CASE WHEN A = 2 THEN 5 ELSE 6 END))") must be equalTo expr("(A = 2) AND 5")
      conditionals("((A = 3) AND (CASE WHEN A = 2 THEN 5 ELSE 6 END))") must be equalTo expr("(A = 3) AND 6")
      conditionals("((A IS NULL) AND (CASE WHEN A IS NULL THEN 5 ELSE 6 END))") must be equalTo expr("(A IS NULL) AND 5")
    }    
    "Simplify Negation" >> {
      conditionals("((A IS NOT NULL) AND (A IS NULL))") must be equalTo expr("FALSE")
      conditionals("((A IS NOT NULL) AND (CASE WHEN A IS NULL THEN 5 ELSE 6 END))") must be equalTo expr("(A IS NOT NULL) AND 6")
    }
  }

  "PullUpBranches" should {
    "Simplify Arithmetic" >> {
      booleanOpts("(CASE WHEN A = 1 THEN B ELSE C END) + 2") must be equalTo expr("""
        CASE WHEN A = 1 THEN B + 2 ELSE C + 2 END
      """)
    }

    "Flatten Boolean Expressions" >> {
      booleanOpts("(CASE WHEN A = 1 THEN B ELSE C END) = 2") must be equalTo expr("""
        ((A = 1) AND (B = 2)) OR ((A != 1) AND (C = 2))
      """)
    }
  }
} 
Example 10
Source File: Adapter.scala    From attic-nlp4l   with Apache License 2.0 5 votes vote down vote up
package org.nlp4l.util

import java.io.{FileReader, BufferedReader, File}

import org.nlp4l.core.RawReader
import resource._

trait Adapter {
  def parseCommonOption(parsed: Map[Symbol, String], list: List[String]): Map[Symbol, String] = list match {
    case Nil => parsed
    case "-s" :: value :: tail => parseCommonOption(parsed + ('schema -> value), tail)
    case "-f" :: value :: tail => parseCommonOption(parsed + ('field -> value), tail)
    case "--type" :: value :: tail => parseCommonOption(parsed + ('type -> value), tail)
    case "--tfmode" :: value :: tail => parseCommonOption(parsed + ('tfmode -> value), tail)
    case "--smthterm" :: value :: tail => parseCommonOption(parsed + ('smthterm -> value), tail)
    case "--idfmode" :: value :: tail => parseCommonOption(parsed + ('idfmode -> value), tail)
    case "-o" :: value :: tail => parseCommonOption(parsed + ('outdir -> value), tail)
    case "--features" :: value :: tail => parseCommonOption(parsed + ('features -> value), tail)
    case "--outputSep" :: value :: tail => parseCommonOption(parsed + ('outputSep -> value), tail)
    case "--values" :: value :: tail => parseCommonOption(parsed + ('values -> value), tail)
    case "--valuesSep" :: value :: tail => parseCommonOption(parsed + ('valuesSep -> value), tail)
    case "--boosts" :: value :: tail => parseCommonOption(parsed + ('boosts -> value), tail)
    case value :: tail => parseCommonOption(parsed + ('index -> value), tail)
  }

  def fieldValues(reader: RawReader, docIds: List[Int], fields: Seq[String]): List[Map[String, Seq[String]]] = {
    docIds.map(id => reader.document(id) match {
      case Some(doc) => {
        fields.map(f => (f, doc.getValue(f).getOrElse(List.empty))).toMap
      }
      case _ => Map.empty[String, Seq[String]]
    })
  }

  def readFeatures(featureFile: String): Set[String] = {
    val file = new File(featureFile)
    // build word set from feature file
    val builder = Set.newBuilder[String]
    if (file.exists()) {
      for (input <- managed(new BufferedReader(new FileReader(file)))) {
        def read(): Unit = input.readLine() match {
          case null => ()
          case line => {
            builder += (line.trim)
            read()
          }
        }
        read()
      }
    }
    builder.result()
  }

  def readTermBoosts(boostFile: String): Map[String, Double] = {
    val file = new File(boostFile)
    // build term boosts map
    val builder = Map.newBuilder[String, Double]
    if (file.exists()) {
      for (input <- managed(new BufferedReader(new FileReader(file)))) {
        def read(): Unit = input.readLine() match {
          case null => ()
          case line => {
            val cols = line.split(",")
            builder += (cols(0).trim -> cols(1).trim.toDouble)
            read()
          }
        }
        read()
      }
    }
    builder.result()
  }
} 
Example 11
Source File: SparkILoop.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.repl

import java.io.{BufferedReader, FileReader}

import Predef.{println => _, _}
import scala.util.Properties.{jdkHome, javaVersion, versionString, javaVmName}

import scala.tools.nsc.interpreter.{JPrintWriter, ILoop}
import scala.tools.nsc.Settings
import scala.tools.nsc.util.stringFromStream


  def run(code: String, sets: Settings = new Settings): String = {
    import java.io.{ BufferedReader, StringReader, OutputStreamWriter }

    stringFromStream { ostream =>
      Console.withOut(ostream) {
        val input = new BufferedReader(new StringReader(code))
        val output = new JPrintWriter(new OutputStreamWriter(ostream), true)
        val repl = new SparkILoop(input, output)

        if (sets.classpath.isDefault)
          sets.classpath.value = sys.props("java.class.path")

        repl process sets
      }
    }
  }
  def run(lines: List[String]): String = run(lines.map(_ + "\n").mkString)
} 
Example 12
Source File: TryCatchFinally.scala    From Scala-and-Spark-for-Big-Data-Analytics   with MIT License 5 votes vote down vote up
package com.chapter3.ScalaFP
import java.io.IOException
import java.io.FileReader
import java.io.FileNotFoundException
object TryCatch {
  def main(args: Array[String]) {
    try {
      val f = new FileReader("data/data.txt")
    } catch {
      case ex: FileNotFoundException => println("File not found exception")
      case ex: IOException => println("IO Exception") 
    } finally {
      println("Finally block always executes");
    }
  }
} 
Example 13
package com.chapter3.ScalaFP

import java.io.BufferedReader
import java.io.IOException
import java.io.FileReader

object ScalaExceptionHandling {
  def errorHandler(e:IOException){
    println("stop doing somehting!")
  }
  val file:String = "C:/Exp/input.txt"
  val input = new BufferedReader(new FileReader(file))
try {
  try {
    for (line <- Iterator.continually(input.readLine()).takeWhile(_ != null)) {
      Console.println(line)
    }
  } finally {
    input.close()
  }
} catch {
  case e:IOException => errorHandler(e)
}

  
} 
Example 14
Source File: MqttSSLSocketFactory.scala    From stream-reactor   with Apache License 2.0 5 votes vote down vote up
package com.datamountaineer.streamreactor.connect.mqtt.source

import java.io.FileReader
import java.security.{KeyStore, Security}

import com.typesafe.scalalogging.StrictLogging
import javax.net.ssl.{KeyManagerFactory, SSLContext, SSLSocketFactory, TrustManagerFactory}
import org.bouncycastle.cert.X509CertificateHolder
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.openssl.jcajce.{JcaPEMKeyConverter, JcePEMDecryptorProviderBuilder}
import org.bouncycastle.openssl.{PEMEncryptedKeyPair, PEMKeyPair, PEMParser}


object MqttSSLSocketFactory extends StrictLogging {
  def apply(caCrtFile: String,
            crtFile: String,
            keyFile: String,
            password: String): SSLSocketFactory = {
    try {

      
      context.getSocketFactory
    }
    catch {
      case e: Exception =>
        logger.warn(e.getMessage, e)
        null
    }
  }
} 
Example 15
Source File: FileIO.scala    From korolev   with Apache License 2.0 5 votes vote down vote up
package korolev.effect.io

import java.io.{BufferedReader, FileInputStream, FileOutputStream, FileReader}
import java.nio.file.Path

import korolev.effect.syntax._
import korolev.effect.{Effect, Stream}

object FileIO {

  def readBytes[F[_]: Effect](path: Path): F[LazyBytes[F]] = {
    val inputStream = new FileInputStream(path.toFile)
    LazyBytes.fromInputStream(inputStream)
  }

  def readLines[F[_]: Effect](path: Path): F[Stream[F, String]] = {
    Stream.unfoldResource[F, BufferedReader, Unit, String](
      default = (),
      create = Effect[F].delay(new BufferedReader(new FileReader(path.toFile))),
      loop = (reader, _) => Effect[F].delay {
        ((), Option(reader.readLine()))
      }
    )
  }

  
  def write[F[_]: Effect](path: Path, append: Boolean = false): Stream[F, Array[Byte]] => F[Unit] = { stream =>
    val outputStream = new FileOutputStream(path.toFile, append)
    def aux(): F[Unit] = {
      stream.pull().flatMap {
        case Some(chunk) => Effect[F]
          .delay(outputStream.write(chunk))
          .after(aux())
          .recover {
            case error =>
              outputStream.close()
              throw error
          }
        case None =>
          Effect[F].delay(outputStream.close())
      }
    }
    aux()
  }
} 
Example 16
Source File: ObservableMain.scala    From advanced-scala-code   with Apache License 2.0 5 votes vote down vote up
object ObservableMain {

  def main(args: Array[String]): Unit = {

    import monix.reactive.Observable
    val linesO = Observable.defer {
      import java.io.{BufferedReader, FileReader}
      val br = new BufferedReader(new FileReader("license.txt"))
      Observable.fromLinesReaderUnsafe(br)
    }

    printStatistics(linesO)
    printStatistics(linesO)

    def printStatistics(linesO: Observable[String]): Unit = {
      val wordsO = linesO.flatMap { line =>
        val arr = line.split("\\W").map(_.toLowerCase)
          .map(_.trim).filter(!_.isEmpty)
        Observable.fromIterable(arr.toIterable)
      }

      val rawResultO = wordsO.foldLeft(Map.empty[String, Int]) { (acc, next) =>
        acc.get(next) match {
          case None => acc + (next -> 1)
          case Some(num) => acc + (next -> (1 + num))
        }
      }

      import monix.reactive.Consumer
      val finalResultT = rawResultO.map { map =>
        map.toList.sortWith( _._2 > _._2).take(5).map(_._1)
      }.consumeWith(Consumer.head)

      import monix.execution.Scheduler.Implicits.global
      val resultCF = finalResultT.runToFuture

      import scala.concurrent.Await
      import scala.concurrent.duration._
      val result = Await.result(resultCF, 30.seconds)
      println(result)
      // List(the, or, of, and, to)
    }

  }

  import cats.kernel.Monoid
  import monix.reactive.Observable
  def alternativeMonoid(wordsO: Observable[String]): Unit = {
    import cats.instances.int.catsKernelStdGroupForInt
    import cats.instances.map.catsKernelStdMonoidForMap

    val listT = wordsO.map(word => Map(word -> 1)).toListL
    val totals = listT.map { data =>
      Monoid[Map[String, Int]].combineAll(data)
    }
    // totalsT: Task[Map[String, Int]]

    val finalResultT = totals.map { data =>
      data.toList.sortWith( _._2 > _._2).take(5).map(_._1)
    }

    import monix.execution.Scheduler.Implicits.global
    import scala.concurrent.Await
    import scala.concurrent.duration._
    val result = Await.result(finalResultT.runToFuture, 30.seconds)
    println(result)
  }
} 
Example 17
Source File: AppProperties.scala    From spark-pip   with Apache License 2.0 5 votes vote down vote up
package com.esri

import java.io.{File, FileReader}
import java.util.Properties

import org.apache.spark.SparkConf

import scala.collection.JavaConverters._


object AppProperties {

  def loadProperties(filename: String, sparkConf: SparkConf) = {
    val file = new File(filename)
    if (file.exists()) {
      val reader = new FileReader(file)
      try {
        val properties = new Properties()
        properties.load(reader)
        properties.asScala.foreach { case (k, v) => sparkConf.set(k, v) }
      }
      finally {
        reader.close()
      }
    }
    sparkConf
  }
} 
Example 18
Source File: MarkdownPagesEndpoint.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash.web.guide.markdown

import java.io.{BufferedReader, File, FileReader}
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap

import com.avsystem.commons._
import com.vladsch.flexmark.ext.toc.TocExtension
import com.vladsch.flexmark.html.HtmlRenderer
import com.vladsch.flexmark.parser.Parser

import scala.concurrent.{ExecutionContext, Future}

final class MarkdownPagesEndpoint(guideResourceBase: String)(implicit ec: ExecutionContext) extends MarkdownPageRPC {

  private val tocExtension = TocExtension.create
  private val parser = Parser.builder.extensions(JList(tocExtension)).build
  private val renderer = HtmlRenderer.builder.extensions(JList(tocExtension)).build
  private val renderedPages = new ConcurrentHashMap[MarkdownPage, (Future[String], Instant)]

  private def render(file: File): Future[String] = Future {
    val reader = new BufferedReader(new FileReader(file))
    val document = parser.parseReader(reader)
    renderer.render(document)
  }

  override def loadContent(page: MarkdownPage): Future[String] = {
    val (result, _) = renderedPages.compute(page, { (_, cached) =>
      val pageFile = new File(guideResourceBase + page.file)
      cached.opt.filter {
        case (currentRender, renderedInstant) =>
          currentRender.value.exists(_.isSuccess) && renderedInstant.toEpochMilli >= pageFile.lastModified()
      }.getOrElse((render(pageFile), Instant.ofEpochMilli(pageFile.lastModified())))
    })
    result
  }
} 
Example 19
Source File: KafkaConfiguratorAppSpec.scala    From kafka-configurator   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.sky.kafka.configurator

import java.io.{File, FileReader}

import com.sky.kafka.configurator.error.{ConfiguratorFailure, TopicNotFound}
import common.BaseSpec
import io.circe.generic.AutoDerivation
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar

import scala.util.{Failure, Success}

class KafkaConfiguratorAppSpec extends BaseSpec with MockitoSugar with AutoDerivation {

  val topicConfigurator = mock[TopicConfigurator]
  val kafkaConfiguratorApp = KafkaConfiguratorApp(topicConfigurator)

  it should "provide logs and errors when file has been parsed successfully" in {
    val file = new File(getClass.getResource("/topic-configuration-with-error.yml").getPath)
    val topics = TopicConfigurationParser(new FileReader(file)).right.value

    val error = TopicNotFound(topics(1).name)

    when(topicConfigurator.configure(topics.head))
      .thenReturn(Success(()).withLog("foo"))
    when(topicConfigurator.configure(topics(1)))
      .thenReturn(Failure[Unit](error).asWriter)
    when(topicConfigurator.configure(topics(2)))
      .thenReturn(Success(()).withLog("bar"))

    kafkaConfiguratorApp.configureTopicsFrom(List(file)) shouldBe Success((
      List(ConfiguratorFailure(topics.tail.head.name, error)),
      List("foo", "bar")
    ))
  }

  it should "succeed when given empty configuration file" in {
    val invalidFile = File.createTempFile("empty", "yml")
    invalidFile.deleteOnExit()
    kafkaConfiguratorApp.configureTopicsFrom(List(invalidFile)) shouldBe a[Success[_]]
  }

  it should "fail-fast when the file does not exist" in {
    kafkaConfiguratorApp.configureTopicsFrom(List(new File("does-not-exist"))) shouldBe a[Failure[_]]
  }

} 
Example 20
Source File: Implicit_1_Class.scala    From HadoopLearning   with MIT License 5 votes vote down vote up
package com.c503.scala

import java.io.{BufferedReader, File, FileReader}

import scala.collection.mutable.ListBuffer


  implicit class Files(file: File) {
    def lines: List[String] = {
      val fileReader = new FileReader(file)
      val reader = new BufferedReader(fileReader)
      try {
        var lines = ListBuffer[String]()
        var line = reader.readLine()
        while (line != null) {
          lines = lines :+ line
          line = reader.readLine()
        }
        lines.toList
      } finally {
        if (fileReader != null) {
          fileReader.close()
        }
        if (reader != null) {
          reader.close()
        }
      }
    }
  }

  def main(args: Array[String]): Unit = {

    val file = new File("")
    file.lines.foreach(e => {
      println(e)
    })

  }

} 
Example 21
Source File: Clause_6_exception.scala    From HadoopLearning   with MIT License 5 votes vote down vote up
package com.c503.scala

import java.io.{FileNotFoundException, FileReader, IOException}


object Clause_6_exception {

  def main(args: Array[String]): Unit = {

    try {
      val f = new FileReader("input.txt")
      println(f.getClass.getName)
    } catch {
      case ex: FileNotFoundException => {
        println("Missing file exception")
      }
      case bx: IOException => {
        println("IO Exception")
      }
    } finally {
      println("Exiting finally...")
    }

  }

} 
Example 22
Source File: _06_Finally.scala    From LearningScala   with Apache License 2.0 5 votes vote down vote up
package _090_failure_handling

import java.io.FileReader
import java.net.{MalformedURLException, URL}

object _06_Finally {

  { // Example 1
    val file = new FileReader("input.txt")
    try {
      // Use the file
    } finally {
      file.close() // Be sure to close the file
    }
  }

  
  //noinspection RemoveRedundantReturn
  def f(): Int = try return 1 finally return 2

  def g(): Int = try 1 finally 2
} 
Example 23
Source File: SigProofTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen

import java.io.{BufferedReader, File, FileReader}
import java.util.Optional
import java.{lang, util}

import com.horizen.box.WithdrawalRequestBox
import com.horizen.box.data.WithdrawalRequestBoxData
import com.horizen.cryptolibprovider.{SchnorrFunctionsImplZendoo, ThresholdSignatureCircuitImplZendoo}
import com.horizen.proposition.MCPublicKeyHashProposition
import com.horizen.schnorrnative.SchnorrSecretKey
import com.horizen.utils.BytesUtils
import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.{Ignore, Test}

import scala.collection.JavaConverters._
import scala.util.Random

class SigProofTest {
  private val classLoader: ClassLoader = getClass.getClassLoader
  private val sigCircuit: ThresholdSignatureCircuitImplZendoo = new ThresholdSignatureCircuitImplZendoo()
  private val schnorrFunctions: SchnorrFunctionsImplZendoo = new SchnorrFunctionsImplZendoo()

  private def buildSchnorrPrivateKey(index: Int): SchnorrSecretKey = {
    var bytes: Array[Byte] = null
    try {
      val resourceName = "schnorr_sk0"+ index + "_hex"
      val file = new FileReader(classLoader.getResource(resourceName).getFile)
      bytes = BytesUtils.fromHexString(new BufferedReader(file).readLine())
    }
    catch {
      case e: Exception =>
        assertEquals(e.toString(), true, false)
    }

    SchnorrSecretKey.deserialize(bytes)
  }

  //Test will take around 2 minutes, enable for sanity checking of ThresholdSignatureCircuit
  @Ignore
  @Test
  def simpleCheck(): Unit = {
    val keyPairsLen = 7
    val threshold = 5 //hardcoded value

    val keyPairs = (0 until keyPairsLen).view.map(buildSchnorrPrivateKey).map(secret => (secret, secret.getPublicKey))
    val publicKeysBytes: util.List[Array[Byte]] = keyPairs.map(_._2.serializePublicKey()).toList.asJava
    val provingKeyPath = new File(classLoader.getResource("sample_proving_key_7_keys_with_threshold_5").getFile).getAbsolutePath;
    val verificationKeyPath = new File(classLoader.getResource("sample_vk_7_keys_with_threshold_5").getFile).getAbsolutePath;

    val sysConstant = sigCircuit.generateSysDataConstant(publicKeysBytes, threshold)

    val mcBlockHash = Array.fill(32)(Random.nextInt().toByte)
    val previousMcBlockHash = Array.fill(32)(Random.nextInt().toByte)

    val wb: util.List[WithdrawalRequestBox] = Seq(new WithdrawalRequestBox(new WithdrawalRequestBoxData(new MCPublicKeyHashProposition(Array.fill(20)(Random.nextInt().toByte)), 2345), 42)).asJava

    val messageToBeSigned = sigCircuit.generateMessageToBeSigned(wb, mcBlockHash, previousMcBlockHash)

    val emptySigs = List.fill[Optional[Array[Byte]]](keyPairsLen - threshold)(Optional.empty[Array[Byte]]())
    val signatures: util.List[Optional[Array[Byte]]] = (keyPairs
      .map{case (secret, public) => schnorrFunctions.sign(secret.serializeSecretKey(), public.serializePublicKey(), messageToBeSigned)}
      .map(b => Optional.of(b))
      .take(threshold)
      .toList ++ emptySigs)
      .asJava

    val proofAndQuality: utils.Pair[Array[Byte], lang.Long] = sigCircuit.createProof(wb, mcBlockHash, previousMcBlockHash, publicKeysBytes, signatures, threshold, provingKeyPath)

    val result = sigCircuit.verifyProof(wb, mcBlockHash, previousMcBlockHash, proofAndQuality.getValue, proofAndQuality.getKey, sysConstant, verificationKeyPath)

    assertTrue("Proof verification expected to be successfully", result)
  }

} 
Example 24
Source File: ForgerBoxMerklePathInfoTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.validation

import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter}
import java.lang.{Byte => JByte}
import java.util
import java.util.{ArrayList => JArrayList}

import com.horizen.box.ForgerBox
import com.horizen.fixtures.BoxFixture
import com.horizen.utils.{BytesUtils, ForgerBoxMerklePathInfo, ForgerBoxMerklePathInfoSerializer, MerklePath, Pair}
import com.horizen.vrf.VrfGeneratedDataProvider
import org.junit.Assert.{assertEquals, assertNotEquals, assertTrue}
import org.junit.Test
import org.scalatest.junit.JUnitSuite

class ForgerBoxMerklePathInfoTest extends JUnitSuite with BoxFixture {
  val vrfGenerationSeed = 907
  val vrfGenerationPrefix = "ForgerBoxMerklePathInfoTest"

  //uncomment if you want update vrf related data
  if (false) {
    VrfGeneratedDataProvider.updateVrfPublicKey(vrfGenerationPrefix, vrfGenerationSeed)
  }

  val forgerBox: ForgerBox = getForgerBox(
    getPrivateKey25519("123".getBytes()).publicImage(),
    1000L,
    100L,
    getPrivateKey25519("456".getBytes()).publicImage(),
    VrfGeneratedDataProvider.getVrfPublicKey(vrfGenerationPrefix, vrfGenerationSeed)
  )
  val emptyMerklePath: MerklePath = new MerklePath(new JArrayList())

  val nonEmptyMerklePath: MerklePath = new MerklePath(util.Arrays.asList(
    new Pair[JByte, Array[Byte]](0.toByte, BytesUtils.fromHexString("29d000eee85f08b6482026be2d92d081d6f9418346e6b2e9fe2e9b985f24ed1e")),
    new Pair[JByte, Array[Byte]](1.toByte, BytesUtils.fromHexString("61bfbdf7038dc7f21e2bcf193faef8e6caa8222af016a6ed86b9e9d860f046df"))
  ))

  @Test
  def comparison(): Unit = {
    assertNotEquals("Box merkle path info expected to be different.", emptyMerklePath, nonEmptyMerklePath)
  }

  @Test
  def serialization(): Unit = {
    // Test 1: empty merkle path (single element in merkle tree)
    val boxWithEmptyPath = ForgerBoxMerklePathInfo(forgerBox, emptyMerklePath)
    var boxBytes = boxWithEmptyPath.bytes
    var deserializedBox = ForgerBoxMerklePathInfoSerializer.parseBytes(boxBytes)
    assertEquals("Deserialized box merkle path info hashCode expected to be equal to the original one.", boxWithEmptyPath.hashCode(), deserializedBox.hashCode())
    assertEquals("Deserialized box merkle path info expected to be equal to the original one.", boxWithEmptyPath, deserializedBox)


    // Test 2: non empty merkle path
    val boxWithNonEmptyPath = ForgerBoxMerklePathInfo(forgerBox, nonEmptyMerklePath)
    boxBytes = boxWithNonEmptyPath.bytes
    deserializedBox = ForgerBoxMerklePathInfoSerializer.parseBytes(boxBytes)
    assertEquals("Deserialized box merkle path info hashCode expected to be equal to the original one.", boxWithNonEmptyPath.hashCode(), deserializedBox.hashCode())
    assertEquals("Deserialized box merkle path info expected to be equal to the original one.", boxWithNonEmptyPath, deserializedBox)

    // Set to true and run if you want to update regression data.
    if (false) {
      val out = new BufferedWriter(new FileWriter("src/test/resources/boxmerklepathinfo_hex"))
      out.write(BytesUtils.toHexString(boxBytes))
      out.close()
    }

    // Test 3: try to deserialize broken bytes.
    assertTrue("ForgerBoxMerklePathInfo expected to be not parsed due to broken data.", ForgerBoxMerklePathInfoSerializer.parseBytesTry("broken bytes".getBytes).isFailure)
  }

  @Test
  def serializationRegression(): Unit = {
    var bytes: Array[Byte] = null
    try {
      val classLoader = getClass.getClassLoader
      val file = new FileReader(classLoader.getResource("boxmerklepathinfo_hex").getFile)
      bytes = BytesUtils.fromHexString(new BufferedReader(file).readLine())
    }
    catch {
      case e: Exception =>
        fail(e.toString)
    }

    val boxMerklePathInfoTry = ForgerBoxMerklePathInfoSerializer.parseBytesTry(bytes)
    assertTrue("ForgerBoxMerklePathInfo expected to by parsed.", boxMerklePathInfoTry.isSuccess)

    val boxWithNonEmptyPath = ForgerBoxMerklePathInfo(forgerBox, nonEmptyMerklePath)
    assertEquals("Parsed info is different to original.", boxWithNonEmptyPath, boxMerklePathInfoTry.get)
  }
} 
Example 25
Source File: GoogleAuthentication.scala    From amadou   with Apache License 2.0 5 votes vote down vote up
package com.mediative.amadou.bigquery

import java.io.{File, FileReader}
import scala.collection.JavaConversions._
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
import com.google.api.client.googleapis.auth.oauth2.{
  GoogleAuthorizationCodeFlow,
  GoogleClientSecrets
}
import com.google.api.client.http.{HttpRequest, HttpRequestInitializer}
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.store.FileDataStoreFactory
import org.apache.spark.sql.SparkSession

sealed abstract class GoogleAuthentication(val scopes: String*)

object GoogleAuthentication {
  lazy val HTTP_TRANSPORT = new NetHttpTransport()
  lazy val JSON_FACTORY   = new JacksonFactory()

  case object Dbm
      extends GoogleAuthentication("https://www.googleapis.com/auth/doubleclickbidmanager")

  def apply(auth: GoogleAuthentication, spark: SparkSession): HttpRequestInitializer = auth match {
    case Dbm =>
      val clientFilePath = spark.conf.get("spark.google.cloud.auth.client.file")
      require(clientFilePath != null, "'google.cloud.auth.client.file' not configured")

      val clientFile = new File(clientFilePath)
      require(clientFile.exists, s"$clientFilePath does not exists")

      val clientSecrets    = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(clientFile))
      val dataStoreFactory = new FileDataStoreFactory(clientFile.getParentFile)

      val flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT,
        JSON_FACTORY,
        clientSecrets,
        auth.scopes)
        .setDataStoreFactory(dataStoreFactory)
        .build()

      val cred = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
        .authorize("user")
      new CustomHttpRequestInitializer(cred)
  }

  class CustomHttpRequestInitializer(wrapped: HttpRequestInitializer)
      extends HttpRequestInitializer {
    override def initialize(httpRequest: HttpRequest) = {
      wrapped.initialize(httpRequest)
      httpRequest.setConnectTimeout(10 * 60000) // 10 minutes connect timeout
      httpRequest.setReadTimeout(10 * 60000)    // 10 minutes read timeout
      ()
    }
  }
} 
Example 26
Source File: TestUtils.scala    From keystone   with Apache License 2.0 5 votes vote down vote up
package keystoneml.utils

import java.io.{FileReader, ByteArrayInputStream}
import breeze.linalg.DenseMatrix
import breeze.stats.distributions.{Gaussian, RandBasis, ThreadLocalRandomGenerator, Rand}
import edu.berkeley.cs.amplab.mlmatrix.RowPartitionedMatrix
import org.apache.commons.io.IOUtils
import org.apache.commons.math3.random.MersenneTwister
import org.apache.spark.SparkContext

import scala.io.Source
import scala.util.Random


  def genChannelMajorArrayVectorizedImage(x: Int, y: Int, z: Int): ChannelMajorArrayVectorizedImage = {
    ChannelMajorArrayVectorizedImage(genData(x, y, z), ImageMetadata(x,y,z))
  }

  def genRowColumnMajorByteArrayVectorizedImage(x: Int, y: Int, z: Int): RowColumnMajorByteArrayVectorizedImage = {
    RowColumnMajorByteArrayVectorizedImage(genData(x,y,z).map(_.toByte), ImageMetadata(x,y,z))
  }

  def createRandomMatrix(
      sc: SparkContext,
      numRows: Int,
      numCols: Int,
      numParts: Int,
      seed: Int = 42): RowPartitionedMatrix = {

    val rowsPerPart = numRows / numParts
    val matrixParts = sc.parallelize(1 to numParts, numParts).mapPartitionsWithIndex { (index, part) =>
      val randBasis: RandBasis = new RandBasis(new ThreadLocalRandomGenerator(new MersenneTwister(seed+index)))
      Iterator(DenseMatrix.rand(rowsPerPart, numCols, Gaussian(0.0, 1.0)(randBasis)))
    }
    RowPartitionedMatrix.fromMatrix(matrixParts.cache())
  }

  def createLocalRandomMatrix(numRows: Int, numCols: Int, seed: Int = 42): DenseMatrix[Double] = {
    val randBasis: RandBasis = new RandBasis(new ThreadLocalRandomGenerator(new MersenneTwister(seed)))
    DenseMatrix.rand(numRows, numCols, Gaussian(0.0, 1.0)(randBasis))
  }
} 
Example 27
Source File: KafkaConfiguratorApp.scala    From kafka-configurator   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.sky.kafka.configurator

import java.io.{File, FileReader}

import cats.data.Reader
import cats.implicits._
import com.sky.kafka.configurator.error.ConfiguratorFailure

import scala.util.{Failure, Success, Try}

case class KafkaConfiguratorApp(configurator: TopicConfigurator) {

  def configureTopicsFrom(files: List[File]): Try[(List[ConfiguratorFailure], List[String])] =
    files.traverse { file =>
      for {
        fileReader <- Try(new FileReader(file))
        topics <- TopicConfigurationParser(fileReader).toTry
      } yield configureAll(topics)
    }.map(_.separate.bimap(_.flatten, _.flatten))

  private def configureAll(topics: List[Topic]): (List[ConfiguratorFailure], List[String]) = {
    val (errors, allLogs) = topics.map { topic =>
      configurator.configure(topic).run match {
        case Success((logs, _)) => Right(logs)
        case Failure(t) => Left(ConfiguratorFailure(topic.name, t))
      }
    }.separate
    (errors, allLogs.flatten)
  }
}

object KafkaConfiguratorApp {
  def reader: Reader[AppConfig, KafkaConfiguratorApp] =
    TopicConfigurator.reader.map(KafkaConfiguratorApp.apply)
} 
Example 28
Source File: RDataSource.scala    From ScalaStan   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.cibo.scalastan.data

import java.io.FileReader
import scala.util.parsing.combinator.JavaTokenParsers

object RDataSource {

  private object RDataParser extends JavaTokenParsers {

    override protected val whiteSpace = """(\s|#.*)+""".r

    private def value: Parser[String] = floatingPointNumber | (stringLiteral ^^ { s => s.tail.dropRight(1) })

    private def valueList: Parser[Vector[String]] = repsep(value, ",") ^^ { _.toVector }

    private def label: Parser[String] = (stringLiteral ^^ { s => s.tail.dropRight(1) }) | ident

    private def vector: Parser[Vector[String]] = "c" ~> "(" ~ valueList ~ ")" ^^ { case _ ~ ns ~ _ => ns }

    private def structure: Parser[(Vector[Int], Vector[String])] =
      "structure" ~> "(" ~ vector ~ "," ~ ".Dim" ~ "=" ~ vector ~ ")" ^^ { case _ ~ vs ~ _ ~ _ ~ _ ~ ds ~ _ =>
        (ds.map(_.toInt), vs)
      }

    private def assignment: Parser[String] = "<-" | "="

    private def scalarValue: Parser[DataValue] = label ~ assignment ~ value ^^ { case name ~ _ ~ v =>
      DataValue(name, Vector.empty, Vector(v))
    }

    private def vectorValue: Parser[DataValue] = label ~ assignment ~ vector ^^ { case name ~ _ ~ vs =>
      DataValue(name, Vector(vs.length), vs)
    }

    private def structureValue: Parser[DataValue] = label ~ assignment ~ structure ^^ { case name ~ _ ~ v =>
      DataValue(name, v._1, v._2)
    }

    private def statement: Parser[DataValue] = (scalarValue | vectorValue | structureValue) <~ opt(";")

    private def statements: Parser[Seq[DataValue]] = statement.*

    def parse(s: String): ParseResult[Seq[DataValue]] = parseAll(statements, s)

    def parseFile(fileName: String): ParseResult[Seq[DataValue]] = parseAll(statements, new FileReader(fileName))
  }

  def fromString(content: String): DataSource = {
    val values = RDataParser.parse(content) match {
      case RDataParser.Success(vs, _)  => vs
      case RDataParser.Error(msg, _)   => throw new IllegalArgumentException(s"error parsing string: $msg")
      case RDataParser.Failure(msg, _) => throw new IllegalArgumentException(s"error parsing string: $msg")
    }
    DataSource(values)
  }

  def fromFile(fileName: String): DataSource = {
    val values = RDataParser.parseFile(fileName) match {
      case RDataParser.Success(vs, _)  => vs
      case RDataParser.Error(msg, _)   => throw new IllegalArgumentException(s"error parsing $fileName: $msg")
      case RDataParser.Failure(msg, _) => throw new IllegalArgumentException(s"error parsing $fileName: $msg")
    }
    DataSource(values)
  }
} 
Example 29
Source File: CSVReader.scala    From Scientific-Computing-with-Scala   with MIT License 5 votes vote down vote up
import scala.collection.mutable.{MutableList, Map}
import java.io.{FileReader, BufferedReader}

object CSVReader {
 def main(args: Array[String]) {
   val file = new FileReader("iris.csv")
   val reader = new BufferedReader(file)
   try {
     val alldata = new MutableList[Array[String]]
     var line:String = null
     while ({line = reader.readLine(); line} != null) {
       if (line.length != 0) {
         val delimiter: String = ","
         var splitline: Array[String] = line.split(delimiter).map(_.trim)
         alldata += splitline
       }
     }
     val labels = MutableList("sepal length", "sepal width",
       "petal length", "petal width", "class")
     val labelled = labels.zipWithIndex.map {
       case (label, index) => label -> alldata.map(x => x(index))
     }
     val csvdata: Map[String, MutableList[String]] = Map()
     for (pair <- labelled) {
       csvdata += pair                                           
     }
   }
   finally {
     reader.close()
   }
 }
} 
Example 30
Source File: plot.scala    From Scientific-Computing-with-Scala   with MIT License 5 votes vote down vote up
import org.jfree.chart._
import org.jfree.data.xy._
import scala.math._
import scala.collection.mutable.{MutableList, Map}
import java.io.{FileReader, BufferedReader}

object ParallelCoordinates {
  def readCSVFile(filename: String): Map[String, MutableList[String]] = {
    val file = new FileReader(filename)
    val reader = new BufferedReader(file)
    val csvdata: Map[String, MutableList[String]] = Map()
    try {
      val alldata = new MutableList[Array[String]]
      var line:String = null
      while ({line = reader.readLine(); line} != null) {
        if (line.length != 0) {
          val delimiter: String = ","
          var splitline: Array[String] = line.split(delimiter).map(_.trim)
          alldata += splitline
        }
      }
      val labels = MutableList("sepal length", "sepal width",
        "petal length", "petal width", "class")
      val labelled = labels.zipWithIndex.map {
        case (label, index) => label -> alldata.map(x => x(index))
      }
      for (pair <- labelled) {
        csvdata += pair
      }
    } finally {
      reader.close()
    }
    csvdata
  }

  def main(args: Array[String]) {
    val data = readCSVFile("iris.csv")
    val dataset = new DefaultXYDataset
    for (i <- 0 until data("sepal length").size) {
      val x = Array(0.0, 1.0, 2.0, 3.0)
      val y1 = data("sepal length")(i).toDouble
      val y2 = data("sepal width")(i).toDouble
      val y3 = data("petal length")(i).toDouble
      val y4 = data("petal width")(i).toDouble
      val y = Array(y1, y2, y3, y4)
      val cls = data("class")(i)
      dataset.addSeries(cls + i, Array(x, y))
    }
    val frame = new ChartFrame("Parallel Coordinates",
      ChartFactory.createXYLineChart("Parallel Coordinates", "x", "y",
      dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL,
      false, false, false))
    frame.pack()
    frame.setVisible(true)
  }
} 
Example 31
Source File: plot.scala    From Scientific-Computing-with-Scala   with MIT License 5 votes vote down vote up
import scala.collection.mutable.{MutableList, Map}
import scala.math._
import org.jfree.chart._
import org.jfree.data.xy._
import org.jfree.data.statistics._
import java.io.{FileReader, BufferedReader}
import java.awt.GridLayout
import javax.swing.JFrame
import javax.swing.JPanel

object ScatterPlotMatrix {
  def readCSVFile(filename: String): Map[String, MutableList[String]] = {
    val file = new FileReader(filename)
    val reader = new BufferedReader(file)
    val csvdata: Map[String, MutableList[String]] = Map()
    try {
      val alldata = new MutableList[Array[String]]
      var line:String = null
      while ({line = reader.readLine(); line} != null) {
        if (line.length != 0) {
          val delimiter: String = ","
          var splitline: Array[String] = line.split(delimiter).map(_.trim)
          alldata += splitline
        }
      }
      val labels = MutableList("sepal length", "sepal width",
        "petal length", "petal width", "class")
      val labelled = labels.zipWithIndex.map {
        case (label, index) => label -> alldata.map(x => x(index))
      }
      for (pair <- labelled) {
        csvdata += pair
      }
    } finally {
      reader.close()
    }
    csvdata
  }

  def main(args: Array[String]) {
    val data = readCSVFile("iris.csv")
    val frame = new JFrame("Scatter Plot Matrix")
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setLayout(new GridLayout(4, 4))
    val attributes = List("sepal length", "sepal width", 
      "petal length", "petal width")
    val classes = List("Iris-setosa", "Iris-versicolor", "Iris-virginica")
    for ((a1, i) <- attributes.zipWithIndex) {
      for ((a2, j) <- attributes.zipWithIndex) {
        if (a1 == a2) {
          val dataset = new HistogramDataset();
          dataset.setType(HistogramType.RELATIVE_FREQUENCY);
          val xs = (for (x <- data(a1)) yield { x.toDouble }).toArray
          dataset.addSeries(a1, xs, 11);
          val chart = ChartFactory.createHistogram(null, a1, "frequency",
            dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL,
            false, false, false)
          frame.add(new ChartPanel(chart, 200, 200, 200, 200, 200, 200,
            false, true, true, true, true, true))
        } else {
          val dataset = new DefaultXYDataset
          for (cls <- classes) {
            val xs = (for ((x, index) <- data(a1).zipWithIndex
              if data("class")(index) == cls)
            yield { x.toDouble }).toArray
            val ys = (for ((y, index) <- data(a2).zipWithIndex
              if data("class")(index) == cls)
            yield { y.toDouble }).toArray
            dataset.addSeries(cls, Array(xs, ys))
          }
          val chart = ChartFactory.createScatterPlot(null, 
            a1, a2, dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL, 
            false, false, false)
          frame.add(new ChartPanel(chart, 200, 200, 200, 200, 200, 200,
            false, true, true, true, true, true))
        }
      }
    }
    frame.pack()
    frame.setVisible(true)
  }
} 
Example 32
Source File: plot.scala    From Scientific-Computing-with-Scala   with MIT License 5 votes vote down vote up
import org.jfree.chart._
import org.jfree.data.xy._
import scala.math._
import scala.collection.mutable.{MutableList, Map}
import java.io.{FileReader, BufferedReader}

object AndrewsCurve {
  def readCSVFile(filename: String): Map[String, MutableList[String]] = {
    val file = new FileReader(filename)
    val reader = new BufferedReader(file)
    val csvdata: Map[String, MutableList[String]] = Map()
    try {
      val alldata = new MutableList[Array[String]]
      var line:String = null
      while ({line = reader.readLine(); line} != null) {
        if (line.length != 0) {
          val delimiter: String = ","
          var splitline: Array[String] = line.split(delimiter).map(_.trim)
          alldata += splitline
        }
      }
      val labels = MutableList("sepal length", "sepal width",
        "petal length", "petal width", "class")
      val labelled = labels.zipWithIndex.map {
        case (label, index) => label -> alldata.map(x => x(index))
      }
      for (pair <- labelled) {
        csvdata += pair
      }
    } finally {
      reader.close()
    }
    csvdata
  }

  def andrewsCurve(row: Array[Double]) = (t: Double) => {
    var result: Double = 0.0
    for ((attr, i) <- row.zipWithIndex) {
      if (i == 0) {
        result = result + row(i) / sqrt(2.0)
      } else if (i % 2 != 0) {
        result = result + row(i) * sin(((i + 1) / 2) * t)
      } else {
        result = result + row(i) * cos(((i + 1) / 2) * t)
      }
    }
    result
  }

  def main(args: Array[String]) {
    val data = readCSVFile("iris.csv")
    val x: Array[Double] = Array.tabulate(100) {
      (i: Int) => -Pi + 2.0 * Pi * (i / 100.0)
    }
    val dataset = new DefaultXYDataset
    for (i <- 0 until data("sepal length").size) {
      val x1 = data("sepal length")(i).toDouble
      val x2 = data("sepal width")(i).toDouble
      val x3 = data("petal length")(i).toDouble
      val x4 = data("petal width")(i).toDouble
      val cls = data("class")(i)
      val curve = x.map(andrewsCurve(Array(x1, x2, x3, x4)))
      dataset.addSeries(cls + i, Array(x, curve))
    }
    val frame = new ChartFrame("Andrews Curve",
      ChartFactory.createXYLineChart("Andrews Curve", "x", "y",
      dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL,
      false, false, false))
    frame.pack()
    frame.setVisible(true)
  }
} 
Example 33
Source File: Util.scala    From avrohugger   with Apache License 2.0 5 votes vote down vote up
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException

object Util {
  
  def readFile(fileName: String, maxTries: Int = 3): String = {
    def readFile0(count: Int): String = {
      try { // if file is empty, try again, it should be there
        val contents: String = scala.io.Source.fromFile(fileName).mkString
        if (contents.isEmpty && (count < maxTries)) readFile0(count + 1)
        else contents
      } catch { // if file is not found, try again, it should be there
        case e: Throwable =>
          if (count < maxTries) readFile0(count + 1)
          else sys.error("File not found: " + fileName)
      }
    }
    readFile0(0)
  }
  
  
} 
Example 34
Source File: OpenMLTask.scala    From DynaML   with Apache License 2.0 5 votes vote down vote up
package io.github.mandar2812.dynaml.openml

import java.io.{BufferedReader, FileReader}
import org.openml.apiconnector.xml.Task
import OpenML._
import io.github.mandar2812.dynaml.dataformat.{ARFF, ArffFile}


case class OpenMLTask(t: Task) {

  def inputs(): Array[Task#Input] = t.getInputs

  def getDataSplitsAsStream: Stream[String] = {
    val estimation_procedure_index = inputs().map(_.getName).indexOf("estimation_procedure")

    val splits = inputs()(estimation_procedure_index)
      .getEstimation_procedure.getDataSplits(t.getTask_id)

    val arff = new ArffFile()
    arff.parse(new BufferedReader(new FileReader(splits)))
    ARFF.asStream(arff)
  }

  def getDataAsStream: Stream[String] = {
    val data_index = inputs().map(_.getName).indexOf("source_data")
    val data_id = inputs()(data_index).getData_set.getData_set_id
    val data = dataset(data_id)
    data.getFormat match {
      case "ARFF" =>  ARFF.asStream(loadDataIntoARFF(data_id))
    }
  }

}