I want to get the advertisement milliseconds of the devices by subtracting the two timestamps of the same device. But both timestamps are the same value. Could someone help?
override fun onScanResult(callbackType: Int, result: ScanResult) {
val timeStampFirst = result.timestampNanos
val tag = deviceMap.computeIfAbsent(result.device.address) {
val newTag = BleTag(result.device.name ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, result.timestampNanos,"0 Sek.")
deviceList.add(newTag)
newTag
}
tag.name = result.device.name ?: "Unbekannt"
tag.rssi = result.rssi
tag.advertisementData = result.scanRecord?.bytes
tag.timeStampFirst = result.timestampNanos
deviceList.forEach {
if(it.mac == result.device.address){
val timeStampSecond = result.timestampNanos
val advertisementMillis = timeStampSecond - timeStampFirst
Log.e("Advertisement ms", advertisementMillis)
}
}
Log.e(resultTime.toString(),"")
deviceList.sortBy {
result.rssi
}
recyclerView.adapter?.notifyDataSetChanged()
//menu.findItem(R.id.count).title = "Geräte: " + deviceList.size
super.onScanResult(callbackType, result)
}
To show the problem and the solution I have commented out all source code that is not necessary for the calculation of the time interval between two consecutive advertisement.
override fun onScanResult(callbackType: Int, result: ScanResult) {
val timeStampFirst = result.timestampNanos
// val tag = deviceMap.computeIfAbsent(result.device.address) {
// val newTag = BleTag(result.device.name ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, result.timestampNanos,"0 Sek.")
// deviceList.add(newTag)
// newTag
// }
// tag.name = result.device.name ?: "Unbekannt"
// tag.rssi = result.rssi
// tag.advertisementData = result.scanRecord?.bytes
// tag.timeStampFirst = result.timestampNanos
// deviceList.forEach {
// if(it.mac == result.device.address){
val timeStampSecond = result.timestampNanos
// will always be ZERO
val advertisementMillis = timeStampSecond - timeStampFirst
Log.e("Advertisement ms", advertisementMillis)
// }
// }
// Log.e(resultTime.toString(),"")
// deviceList.sortBy {
// result.rssi
// }
// recyclerView.adapter?.notifyDataSetChanged()
//menu.findItem(R.id.count).title = "Geräte: " + deviceList.size
// super.onScanResult(callbackType, result)
}
As you use for both timestamps the value of the current advertisement the result will always be 0.
But the object from the deviceList does not help either, because it already contains the data from the current advertisement.
In pseudo code you would have to do the following to find the interval between two consecutive advertisement:
onNewAdvertismentReceived
If Broadcaster seen before
Retrieve timestamp from the advertisment beforehand
Calculate the interval from both timestamps(old,new)
Else
First time Broadcaster seen
Not possible to calculate interval
Fi
Running test and the ordering of table3 is always different so AssertEquals doesn't work.
val expectedDataSet = new CsvDataSet(new File(BatchJobIntegrationTest.getTestResource("folder/expected/")))
val actualDataSet = connection.createDataSet(Array(
"table1",
"table2",
"table3"
))
Assertion.assertEquals(expectedDataSet, actualDataSet)
Tried but didn't work:
Assertion.assertEquals(new SortedDataSet(expectedDataSet), new SortedDataSet(actualDataSet))
Turns out the primary key was the issue, this helped:
val expectedTable = expectedDataSet.getTable("table")
val actualTable = actualDataSet.getTable("table")
val actualFilteredTable = DefaultColumnFilter.excludedColumnsTable(actualTable, Array("table_id"))
val expectedFilteredTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, Array("table_id"))
//Assertion.assertEquals(expectedTable, actualFilteredTable)
val expectedColumns = expectedFilteredTable.getTableMetaData().getColumns()
val sortedExpected = new SortedTable(expectedFilteredTable, expectedColumns)
val sortedActual = new SortedTable(actualFilteredTable, expectedColumns)
Assertion.assertEquals(sortedExpected, sortedActual)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello
I am using Scala 2.11.8 and spark 1.6.1. whenever I call function inside map, it throws the following exception:
"Exception in thread "main" org.apache.spark.SparkException: Task not serializable"
You can find below my full code
package moviestream.recommender
import java.io
import java.io.Serializable
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.recommendation.ALS
import org.apache.spark.mllib.recommendation.Rating
import org.jblas.DoubleMatrix
class FeatureExtraction{
val conf = new SparkConf().setMaster("local[2]").setAppName("Recommendation")
val sc = new SparkContext(conf)
val rawData = sc.textFile("data/u.data")
val rawRatings = rawData.map(_.split("\t").take(3))
//create rating object from rawratings
val ratings = rawRatings.map{case Array(user,movie,rating) => Rating(user.toInt,movie.toInt,rating.toDouble)}
//user Spark ALS library to train our model
// Build the recommendation model using ALS
val model = ALS.train(ratings,50,10,0.01)
//val model = ALS.trainImplicit(ratings,50,10,0.01,0.1) //last parameter is alpha
val predictedRating = model.predict(789,123)
//top ten recommended movies for user id 789, where k= number of recommended(10) 789=userid
val topKRecs = model.recommendProducts(789,10)
val movies = sc.textFile("data/u.item")
val titles = movies.map(line=>line.split("\\|").take(2)).map(array=>(array(0).toInt,array(1))).collectAsMap()
//how many movies this user has rated
val moviesForUser = ratings.keyBy(_.user).lookup(789)
//we will take the 10 movies with the highest ratings ction using the field of the object.
//moviesForUser.sortBy(-_.rating).take(10).map(rating=>(titles(rating.product),rating.rating)).foreach(println)
//let’s take a look at the top 10 recommendations for this user and see what the titles
//topKRecs.map(rating=>(titles(rating.product),rating.rating)).foreach(println)
// we will then need to create a DoubleMatrix object
val itemId = 567
val itemFactor = model.productFeatures.lookup(itemId).head
val itemVector = new DoubleMatrix(itemFactor)
//we are ready to apply our similarity metric to each item
/*val sims = model.productFeatures.map{ case (id, factor) =>
val factorVector = new DoubleMatrix(factor)
val sim = cosineSimilarity(factorVector, itemVector)
(id, sim)
}*/
//we can compute the top 10 most similar items by sorting out the similarity score for each item
//val sortedSims = sims.top(10)(Ordering.by[(Int,Double),Double]{case(id,similarity)=>similarity})
//we can sense check our item-to-item similarity
//val sortedSims2 = sims.top(11)(Ordering.by[(Int,Double),Double]{case(id,similarity)=>simintellij idea debugilarity})
//sortedSims2.slice(1,11).map{case (id,sim)=>(titles(id),sim)}.foreach(println)
//Finally,we can print the 10 items with the highest computed similarity metric to our given item:
//println("Result = "+titles(123))
def cosineSimilarity(vect1:DoubleMatrix,vect2:DoubleMatrix): Double = {
vect1.dot(vect2)/(vect1.norm1()*vect2.norm2())
}
val actualRating = moviesForUser.take(1)(0)
val predictedRatings = model.predict(789,actualRating.product)
//println(predictedRatings)
val squaredError = math.pow(predictedRatings - actualRating.rating,2.0)
val usersProducts = ratings.map{case Rating(user,product,rating) => (user,product)}
val predictions = model.predict(usersProducts).map{case Rating(user,product,rating)
=>((user,product),rating)}
val ratingsAndPredictions = ratings.map{case Rating(user,product,rating)=>((user,product),rating)}
.join(predictions)
val MSE = ratingsAndPredictions.map{case ((user,product),(actual,predicted))
=> math.pow((actual-predicted),2)}.reduce(_ + _)/ratingsAndPredictions.count()
//println("Mean Squared Error = " + MSE)
val RMSE = math.sqrt(MSE)
println("Root Mean Squared Error = "+ RMSE)
def avgPrecisionK(actual:Seq[Int],predicted:Seq[Int],k:Int):Double = {
val predk = predicted.take(k)
var score = 0.0
var numHits = 0.0
for((p,i)<- predk.zipWithIndex){
if(actual.contains(p)){
numHits += 1.0
score += numHits/(i.toDouble+1.0)
}
}
if(actual.isEmpty) {
1.0
}
else{
score/scala.math.min(actual.size,k).toDouble
}
}
val actualMovies = moviesForUser.map(_.product)
val predictedMovies = topKRecs.map(_.product)
//predictedMovies.foreach(println)
val apk10 = avgPrecisionK(actualMovies,predictedMovies,10)
//println(apk10)
//Locality Sensitive Hashing
val itemFactors = model.productFeatures.map{case (id,factor)=>factor}.collect()
val itemMatrix = new DoubleMatrix(itemFactors)
//println(itemMatrix.rows,itemMatrix.columns)
val imBroadcast = sc.broadcast(itemMatrix)
//println(imBroadcast)
val allRecs = model.userFeatures.map{case (userId,array)=>
val userVector = new DoubleMatrix(array)
val scores = imBroadcast.value.mmul(userVector)
val sortedWithId = scores.data.zipWithIndex.sortBy(- _._1)
val recommendedIds = sortedWithId.map(_._2 +1).toSeq
(userId,recommendedIds)
}
println(allRecs)
}
As mentioned in comments above, the question is too broad. But one guess could help. You use inside map the broadcasted value imBroadcast. I guess it includes functions declared in the same scope as sparkContext, right? Move them to separate object then.
I am attempting to get the items and some of the related information from a Purchase Order with SuiteTalk. I am able to get the desired Purchase Orders with TransactionSearch using the following in Scala:
val transactionSearch = new TransactionSearch
val search = new TransactionSearchBasic
...
search.setLastModifiedDate(searchLastModified) //Gets POs modified in the last 10 minutes
transactionSearch.setBasic(search)
val result = port.search(transactionSearch)
I am able to cast each result to a record as an instance of the PurchaseOrder class.
if (result.getStatus().isIsSuccess()) {
println("Transactions: " + result.getTotalRecords)
for (i <- 0 until result.getTotalRecords) {
try {
val record = result.getRecordList.getRecord.get(i).asInstanceOf[PurchaseOrder]
record.get<...>
}
catch {...}
}
}
From here I am able to use the getters to access the individual fields, except for the ItemList.
I can see in the NetSuite web interface that there are items attached to the Purchase Orders. However using getItemList on the result record is always returning a null response.
Any thoughts?
I think you have not used search preferences and that is why you are not able to fetch purchase order line items. You will have to use following search preferences in your code -
SearchPreferences prefrence = new SearchPreferences();
prefrence.bodyFieldsOnly = false;
_service.searchPreferences = prefrence;
Following is working example using above preferences -
private void SearchPurchaseOrderByID(string strPurchaseOrderId)
{
TransactionSearch tranSearch = new TransactionSearch();
TransactionSearchBasic tranSearchBasic = new TransactionSearchBasic();
RecordRef poRef = new RecordRef();
poRef.internalId = strPurchaseOrderId;
poRef.type = RecordType.purchaseOrder;
poRef.typeSpecified = true;
RecordRef[] poRefs = new RecordRef[1];
poRefs[0] = poRef;
SearchMultiSelectField poID = new SearchMultiSelectField();
poID.searchValue = poRefs;
poID.#operator = SearchMultiSelectFieldOperator.anyOf;
poID.operatorSpecified = true;
tranSearchBasic.internalId = poID;
tranSearch.basic = tranSearchBasic;
InitService();
SearchResult results = _service.search(tranSearch);
if (results.status.isSuccess && results.status.isSuccessSpecified)
{
Record[] poRecords = results.recordList;
PurchaseOrder purchaseOrder = (PurchaseOrder)poRecords[0];
PurchaseOrderItemList poItemList = purchaseOrder.itemList;
PurchaseOrderItem[] poItems = poItemList.item;
if (poItems != null && poItems.Length > 0)
{
for (var i = 0; i < poItems.Length; i++)
{
Console.WriteLine("Item Line On PO = " + poItems[i].line);
Console.WriteLine("Item Quantity = " + poItems[i].quantity);
Console.WriteLine("Item Descrition = " + poItems[i].description);
}
}
}
}
I am adding a new column with the following script, however, the column is coming without a border on the right hand side.
How can I set a BLACK border on the right hand side?
var mytable = reportContext.getDesignHandle().findElement(tableName);
var colbinds = mytable.getColumnBindings( );
var cs1 = StructureFactory.createComputedColumn( );
cs1.setName("Q2");
cs1.setExpression( "dataSetRow[\"Q2\"]" );
colbinds.addItem( cs1 );
//second parameter is before(-1) or after(1)
mytable.insertColumn(2,1);
//get header and add label
var myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( 2 );
var mylabel = elementFactory.newLabel( null );
mylabel.setText( "Number of Responses (2010 Sites)" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
//tcell.getStyle().borderRightColor = "Black"?
//get first detail row
mydetail = mytable.getDetail( ).get( 0 );
//get first column and add detail data item
var tcell = mydetail.getCells( ).get( 2 );
var mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "Q2");
tcell.getContent( ).add( mydata );
//tcell.getStyle().borderRightColor = "Black"?
You should set style also:
tcell.getStyle().borderRightColor = "Black";
tcell.getStyle().borderRightStyle = "solid";
and optionally:
tcell.getStyle().borderRightWidth = "1pt";