问:
PySpark CrossValidator Fails - org.apache.spark.SparkException: Failed to execute user defined function
I have created an ML Pipeline with PySpark, and I can fit and transform the pipeline with LogisticRegression successfully. I get a trained model.

The issue is when I try to use CrossValidate. It runs part way, and then always fails with the following error:

```<code>Py4JJavaError: An error occurred while calling o1883.evaluate.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 135.0 failed 1 times, most recent failure: Lost task 0.0 in stage 135.0 (TID 110) (06496cbd55cf executor driver): org.apache.spark.SparkException: Failed to execute user defined function (HasSimpleAnnotate$$Lambda$3820/0x0000000841652840: (array<array<struct<annotatorType:string,begin:int,end:int,result:string,metadata:map<string,string>,embeddings:array<float>>>>) => array<struct<annotatorType:string,begin:int,end:int,result:string,metadata:map<string,string>,embeddings:array<float>>>)
    at org.apache.spark.sql.errors.QueryExecutionErrors$.failedExecuteUserDefinedFunctionError(QueryExecutionErrors.scala:136)
    at org.apache.spark.sql.errors.QueryExecutionErrors.failedExecuteUserDefinedFunctionError(QueryExecutionErrors.scala)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anon$1.hasNext(WholeStageCodegenExec.scala:759)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at org.apache.spark.util.collection.ExternalSorter.insertAll(ExternalSorter.scala:197)
    at org.apache.spark.shuffle.sort.SortShuffleWriter.write(SortShuffleWriter.scala:63)
    at org.apache.spark.shuffle.ShuffleWriteProcessor.write(ShuffleWriteProcessor.scala:59)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:52)
    at org.apache.spark.scheduler.Task.run(Task.scala:131)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.Exception: feature rules is not set
    at com.johnsnowlabs.nlp.serialization.Feature.$anonfun$getOrDefault$1(Feature.scala:116)
    at scala.Option.getOrElse(Option.scala:189)
    at com.johnsnowlabs.nlp.serialization.Feature.getOrDefault(Feature.scala:116)
    at com.johnsnowlabs.nlp.HasFeatures.$$(HasFeatures.scala:73)
    at com.johnsnowlabs.nlp.HasFeatures.$$$(HasFeatures.scala:73)
    at com.johnsnowlabs.nlp.AnnotatorModel.$$(AnnotatorModel.scala:28)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.$anonfun$tag$5(TokenizerModel.scala:340)
    at scala.collection.Iterator$$anon$11.nextCur(Iterator.scala:486)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:492)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:513)
    at scala.collection.Iterator.foreach(Iterator.scala:943)
    at scala.collection.Iterator.foreach$(Iterator.scala:943)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1431)
    at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
    at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
    at scala.collection.TraversableOnce.to(TraversableOnce.scala:366)
    at scala.collection.TraversableOnce.to$(TraversableOnce.scala:364)
    at scala.collection.AbstractIterator.to(Iterator.scala:1431)
    at scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:358)
    at scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:358)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1431)
    at scala.collection.TraversableOnce.toArray(TraversableOnce.scala:345)
    at scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:339)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1431)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.$anonfun$tag$2(TokenizerModel.scala:392)
    at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:286)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at scala.collection.TraversableLike.map(TraversableLike.scala:286)
    at scala.collection.TraversableLike.map$(TraversableLike.scala:279)
    at scala.collection.AbstractTraversable.map(Traversable.scala:108)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.tag(TokenizerModel.scala:288)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.annotate(TokenizerModel.scala:400)
    at com.johnsnowlabs.nlp.HasSimpleAnnotate.$anonfun$dfAnnotate$1(HasSimpleAnnotate.scala:46)
    ... 19 more

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2454)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2(DAGScheduler.scala:2403)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2$adapted(DAGScheduler.scala:2402)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2402)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1(DAGScheduler.scala:1160)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1$adapted(DAGScheduler.scala:1160)
    at scala.Option.foreach(Option.scala:407)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:1160)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2642)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2584)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2573)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:938)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2214)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2235)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2254)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2279)
    at org.apache.spark.rdd.RDD.$anonfun$collect$1(RDD.scala:1030)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:414)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:1029)
    at org.apache.spark.rdd.PairRDDFunctions.$anonfun$collectAsMap$1(PairRDDFunctions.scala:737)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:414)
    at org.apache.spark.rdd.PairRDDFunctions.collectAsMap(PairRDDFunctions.scala:736)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.confusions$lzycompute(MulticlassMetrics.scala:61)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.confusions(MulticlassMetrics.scala:52)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.labelCountByClass$lzycompute(MulticlassMetrics.scala:66)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.labelCountByClass(MulticlassMetrics.scala:64)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure(MulticlassMetrics.scala:227)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure$lzycompute(MulticlassMetrics.scala:235)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure(MulticlassMetrics.scala:235)
    at org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator.evaluate(MulticlassClassificationEvaluator.scala:153)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.ClientServerConnection.waitForCommands(ClientServerConnection.java:182)
    at py4j.ClientServerConnection.run(ClientServerConnection.java:106)
    at java.base/java.lang.Thread.run(Thread.java:829)
</code>```

I have tried to debug this, and I am stuck. If I use just my OneHotEncoder column as the 'features' vector I have no issues. It is when I add in the TFIDF vectors too that it starts to fail.

Here is my code to build the pipeline:

```<code>import sparknlp
spark = sparknlp.start()

from pyspark.ml.feature import HashingTF, IDF
from pyspark.ml.feature import CountVectorizer
from pyspark.ml.feature import StringIndexer
from pyspark.ml.feature import OneHotEncoder
from pyspark.ml.feature import VectorAssembler
from pyspark.ml import Pipeline

documentAssemblerTitle = DocumentAssembler().setInputCol('title').setOutputCol('titles')
documentAssemblerBody = DocumentAssembler().setInputCol('body').setOutputCol('bodies')
tokenizer_titles = Tokenizer().setInputCols(['titles']).setOutputCol('tokenized_titles')
tokenizer_bodies = Tokenizer().setInputCols(['bodies']).setOutputCol('tokenized_bodies')
stemmer_titles = Stemmer().setInputCols(['tokenized_titles']).setOutputCol('stemmed_titles')
stemmer_bodies = Stemmer().setInputCols(['tokenized_bodies']).setOutputCol('stemmed_bodies')
ngrammer_titles = NGramGenerator().setInputCols(['stemmed_titles']).setOutputCol('ngrams_titles').setN(1).setEnableCumulative(True)
ngrammer_bodies = NGramGenerator().setInputCols(['stemmed_bodies']).setOutputCol('ngrams_bodies').setN(1).setEnableCumulative(True)
finisher = Finisher().setInputCols(['ngrams_titles','ngrams_bodies'])
# tf_titles = HashingTF(inputCol='finished_stemmed_titles',outputCol='tf_features_titles')
# tf_bodies = HashingTF(inputCol='finished_stemmed_bodies',outputCol='tf_features_bodies')
tf_titles = CountVectorizer(inputCol='finished_ngrams_titles',outputCol='tf_features_titles')
tf_bodies = CountVectorizer(inputCol='finished_ngrams_bodies',outputCol='tf_features_bodies')
idf_titles = IDF(inputCol='tf_features_titles', outputCol='idf_titles',minDocFreq=5)
idf_bodies = IDF(inputCol='tf_features_bodies', outputCol='idf_bodies',minDocFreq=5)
author_indexer = StringIndexer(inputCol="author_association", outputCol="author_index")
ohe = OneHotEncoder(inputCol="author_index", outputCol="aa")
assembler = VectorAssembler(inputCols=['aa', 'idf_titles', 'idf_bodies'],outputCol='features')

pipeline_stages = [documentAssemblerTitle,
                    documentAssemblerBody,
                    tokenizer_titles,
                    tokenizer_bodies,
                    stemmer_titles,
                    stemmer_bodies,
                    ngrammer_titles,
                    ngrammer_bodies,
                    finisher,
                    tf_titles,
                    tf_bodies,
                    idf_titles,
                    idf_bodies,
                    author_indexer,
                    ohe,
                    assembler]

from pyspark.ml.classification import LogisticRegression

label_stringIdx = StringIndexer(inputCol = "Target", outputCol = "label")
pipeline_stages.append(label_stringIdx)

lr = LogisticRegression(maxIter=20)  # regParam=0.3, elasticNetParam=0.8
pipeline_stages.append(lr)

pipeline = Pipeline().setStages(pipeline_stages)
pipeline_stages
</code>```

Here is the code for the cross-validation:

```<code>from pyspark.ml.evaluation import MulticlassClassificationEvaluator, BinaryClassificationEvaluator
from pyspark.ml.tuning import TrainValidationSplit, CrossValidator, ParamGridBuilder
import numpy as np

# pipeline = Pipeline().setStages(pipeline_stages)
paramGrid_lr = ParamGridBuilder().addGrid(lr.maxIter, [1,2]).build()
# paramGrid_lr = ParamGridBuilder() \
#     .addGrid(lr.regParam, [0.01, 0.5, 1]) \
#     .addGrid(lr.elasticNetParam, [0.0, 0.5, 1]) \
#     .addGrid(model.maxIter, [10, 20]) \
#     .addGrid(idf.numFeatures, [10, 100, 1000]) \
#     .build()
# evaluator = BinaryClassificationEvaluator()
evaluator = MulticlassClassificationEvaluator()
cv = TrainValidationSplit(estimator=pipeline,
                    estimatorParamMaps=paramGrid_lr,
                    evaluator=evaluator)

cvModel = cv.fit(train)
</code>```

And my preprocessing code:

```<code>import re
from pyspark.sql.functions import udf

@udf("String")
def strip_text(text):
  if text is not None:
    stripped = text.lower()

    # remove all headings, bold text, and HTML comments from the Markdown text.
    # These items have all been used by the React team in their issue templates on GitHub
    headings_pattern = r'(<=\s|^)#{1,6}(.*?)$'
    bold_pattern = r'\*\*(.+?)\*\*(?!\*)'
    comments_pattern = r'<!--((.|\n)*?)-->'
    combined_pattern = r'|'.join((headings_pattern, bold_pattern, comments_pattern))

    stripped = re.sub(combined_pattern, '', stripped)

    # find all URLs in the string, and then remove the final directory from each to leave the general URL form
    # there may be useful patterns based on what URLs issues are commonly linking to
    url_pattern = re.compile(r'(https?://[^\s]+)')
    for url in re.findall(url_pattern, stripped):
        new_url = url.rsplit("/", 1)[0]
        stripped = stripped.replace(url, new_url)

    non_alpha_pattern = r'[^A-Za-z ]+'
    stripped = re.sub(non_alpha_pattern, '', stripped)

    return ' '.join(stripped.split())
  else:
    return " "

from pyspark.sql.functions import col
train = train.withColumn("body", strip_text(col("body"))).withColumn("title", strip_text(col("title")))
validation = validation.withColumn("body", strip_text(col("body"))).withColumn("title", strip_text(col("title")))
</code>```

Thanks for any help you may be able to provide. I have been working on this with no progress for a long time now.

I have tried removing my pre-processing UDF, but there was no difference.

I have tried to impute null values as I saw this could potentially be an issue, but it doesn't fix it

```<code>train = train.fillna(0)
</code>```

I have tried removing the IDF text features altogether just leaving the OneHotEncoded column, and this did work. So I believe it is an issue with the text column, but I am not sure what to do next.

Body:
I have created an ML Pipeline with PySpark, and I can fit and transform the pipeline with LogisticRegression successfully. I get a trained model.

The issue is when I try to use CrossValidate. It runs part way, and then always fails with the following error:

Py4JJavaError: An error occurred while calling o1883.evaluate.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 135.0 failed 1 times, most recent failure: Lost task 0.0 in stage 135.0 (TID 110) (06496cbd55cf executor driver): org.apache.spark.SparkException: Failed to execute user defined function (HasSimpleAnnotate$$Lambda$3820/0x0000000841652840: (array<array<struct<annotatorType:string,begin:int,end:int,result:string,metadata:map<string,string>,embeddings:array<float>>>>) => array<struct<annotatorType:string,begin:int,end:int,result:string,metadata:map<string,string>,embeddings:array<float>>>)
    at org.apache.spark.sql.errors.QueryExecutionErrors$.failedExecuteUserDefinedFunctionError(QueryExecutionErrors.scala:136)
    at org.apache.spark.sql.errors.QueryExecutionErrors.failedExecuteUserDefinedFunctionError(QueryExecutionErrors.scala)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anon$1.hasNext(WholeStageCodegenExec.scala:759)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
    at org.apache.spark.util.collection.ExternalSorter.insertAll(ExternalSorter.scala:197)
    at org.apache.spark.shuffle.sort.SortShuffleWriter.write(SortShuffleWriter.scala:63)
    at org.apache.spark.shuffle.ShuffleWriteProcessor.write(ShuffleWriteProcessor.scala:59)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:52)
    at org.apache.spark.scheduler.Task.run(Task.scala:131)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.Exception: feature rules is not set
    at com.johnsnowlabs.nlp.serialization.Feature.$anonfun$getOrDefault$1(Feature.scala:116)
    at scala.Option.getOrElse(Option.scala:189)
    at com.johnsnowlabs.nlp.serialization.Feature.getOrDefault(Feature.scala:116)
    at com.johnsnowlabs.nlp.HasFeatures.$$(HasFeatures.scala:73)
    at com.johnsnowlabs.nlp.HasFeatures.$$$(HasFeatures.scala:73)
    at com.johnsnowlabs.nlp.AnnotatorModel.$$(AnnotatorModel.scala:28)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.$anonfun$tag$5(TokenizerModel.scala:340)
    at scala.collection.Iterator$$anon$11.nextCur(Iterator.scala:486)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:492)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:513)
    at scala.collection.Iterator.foreach(Iterator.scala:943)
    at scala.collection.Iterator.foreach$(Iterator.scala:943)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1431)
    at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
    at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
    at scala.collection.TraversableOnce.to(TraversableOnce.scala:366)
    at scala.collection.TraversableOnce.to$(TraversableOnce.scala:364)
    at scala.collection.AbstractIterator.to(Iterator.scala:1431)
    at scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:358)
    at scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:358)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1431)
    at scala.collection.TraversableOnce.toArray(TraversableOnce.scala:345)
    at scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:339)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1431)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.$anonfun$tag$2(TokenizerModel.scala:392)
    at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:286)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at scala.collection.TraversableLike.map(TraversableLike.scala:286)
    at scala.collection.TraversableLike.map$(TraversableLike.scala:279)
    at scala.collection.AbstractTraversable.map(Traversable.scala:108)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.tag(TokenizerModel.scala:288)
    at com.johnsnowlabs.nlp.annotators.TokenizerModel.annotate(TokenizerModel.scala:400)
    at com.johnsnowlabs.nlp.HasSimpleAnnotate.$anonfun$dfAnnotate$1(HasSimpleAnnotate.scala:46)
    ... 19 more

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2454)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2(DAGScheduler.scala:2403)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2$adapted(DAGScheduler.scala:2402)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2402)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1(DAGScheduler.scala:1160)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1$adapted(DAGScheduler.scala:1160)
    at scala.Option.foreach(Option.scala:407)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:1160)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2642)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2584)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2573)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:938)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2214)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2235)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2254)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2279)
    at org.apache.spark.rdd.RDD.$anonfun$collect$1(RDD.scala:1030)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:414)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:1029)
    at org.apache.spark.rdd.PairRDDFunctions.$anonfun$collectAsMap$1(PairRDDFunctions.scala:737)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:414)
    at org.apache.spark.rdd.PairRDDFunctions.collectAsMap(PairRDDFunctions.scala:736)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.confusions$lzycompute(MulticlassMetrics.scala:61)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.confusions(MulticlassMetrics.scala:52)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.labelCountByClass$lzycompute(MulticlassMetrics.scala:66)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.labelCountByClass(MulticlassMetrics.scala:64)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure(MulticlassMetrics.scala:227)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure$lzycompute(MulticlassMetrics.scala:235)
    at org.apache.spark.mllib.evaluation.MulticlassMetrics.weightedFMeasure(MulticlassMetrics.scala:235)
    at org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator.evaluate(MulticlassClassificationEvaluator.scala:153)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.ClientServerConnection.waitForCommands(ClientServerConnection.java:182)
    at py4j.ClientServerConnection.run(ClientServerConnection.java:106)
    at java.base/java.lang.Thread.run(Thread.java:829)

I have tried to debug this, and I am stuck. If I use just my OneHotEncoder column as the 'features' vector I have no issues. It is when I add in the TFIDF vectors too that it starts to fail.

Here is my code to build the pipeline:

import sparknlp
spark = sparknlp.start()

from pyspark.ml.feature import HashingTF, IDF
from pyspark.ml.feature import CountVectorizer
from pyspark.ml.feature import StringIndexer
from pyspark.ml.feature import OneHotEncoder
from pyspark.ml.feature import VectorAssembler
from pyspark.ml import Pipeline

documentAssemblerTitle = DocumentAssembler().setInputCol('title').setOutputCol('titles')
documentAssemblerBody = DocumentAssembler().setInputCol('body').setOutputCol('bodies')
tokenizer_titles = Tokenizer().setInputCols(['titles']).setOutputCol('tokenized_titles')
tokenizer_bodies = Tokenizer().setInputCols(['bodies']).setOutputCol('tokenized_bodies')
stemmer_titles = Stemmer().setInputCols(['tokenized_titles']).setOutputCol('stemmed_titles')
stemmer_bodies = Stemmer().setInputCols(['tokenized_bodies']).setOutputCol('stemmed_bodies')
ngrammer_titles = NGramGenerator().setInputCols(['stemmed_titles']).setOutputCol('ngrams_titles').setN(1).setEnableCumulative(True)
ngrammer_bodies = NGramGenerator().setInputCols(['stemmed_bodies']).setOutputCol('ngrams_bodies').setN(1).setEnableCumulative(True)
finisher = Finisher().setInputCols(['ngrams_titles','ngrams_bodies'])
# tf_titles = HashingTF(inputCol='finished_stemmed_titles',outputCol='tf_features_titles')
# tf_bodies = HashingTF(inputCol='finished_stemmed_bodies',outputCol='tf_features_bodies')
tf_titles = CountVectorizer(inputCol='finished_ngrams_titles',outputCol='tf_features_titles')
tf_bodies = CountVectorizer(inputCol='finished_ngrams_bodies',outputCol='tf_features_bodies')
idf_titles = IDF(inputCol='tf_features_titles', outputCol='idf_titles',minDocFreq=5)
idf_bodies = IDF(inputCol='tf_features_bodies', outputCol='idf_bodies',minDocFreq=5)
author_indexer = StringIndexer(inputCol="author_association", outputCol="author_index")
ohe = OneHotEncoder(inputCol="author_index", outputCol="aa")
assembler = VectorAssembler(inputCols=['aa', 'idf_titles', 'idf_bodies'],outputCol='features')

pipeline_stages = [documentAssemblerTitle,
                    documentAssemblerBody,
                    tokenizer_titles,
                    tokenizer_bodies,
                    stemmer_titles,
                    stemmer_bodies,
                    ngrammer_titles,
                    ngrammer_bodies,
                    finisher,
                    tf_titles,
                    tf_bodies,
                    idf_titles,
                    idf_bodies,
                    author_indexer,
                    ohe,
                    assembler]

from pyspark.ml.classification import LogisticRegression

label_stringIdx = StringIndexer(inputCol = "Target", outputCol = "label")
pipeline_stages.append(label_stringIdx)

lr = LogisticRegression(maxIter=20)  # regParam=0.3, elasticNetParam=0.8
pipeline_stages.append(lr)

pipeline = Pipeline().setStages(pipeline_stages)
pipeline_stages

Here is the code for the cross-validation:

from pyspark.ml.evaluation import MulticlassClassificationEvaluator, BinaryClassificationEvaluator
from pyspark.ml.tuning import TrainValidationSplit, CrossValidator, ParamGridBuilder
import numpy as np

# pipeline = Pipeline().setStages(pipeline_stages)
paramGrid_lr = ParamGridBuilder().addGrid(lr.maxIter, [1,2]).build()
# paramGrid_lr = ParamGridBuilder() \
#     .addGrid(lr.regParam, [0.01, 0.5, 1]) \
#     .addGrid(lr.elasticNetParam, [0.0, 0.5, 1]) \
#     .addGrid(model.maxIter, [10, 20]) \
#     .addGrid(idf.numFeatures, [10, 100, 1000]) \
#     .build()
# evaluator = BinaryClassificationEvaluator()
evaluator = MulticlassClassificationEvaluator()
cv = TrainValidationSplit(estimator=pipeline,
                    estimatorParamMaps=paramGrid_lr,
                    evaluator=evaluator)

cvModel = cv.fit(train)

And my preprocessing code:

import re
from pyspark.sql.functions import udf

@udf("String")
def strip_text(text):
  if text is not None:
    stripped = text.lower()

    # remove all headings, bold text, and HTML comments from the Markdown text.
    # These items have all been used by the React team in their issue templates on GitHub
    headings_pattern = r'(<=\s|^)#{1,6}(.*?)$'
    bold_pattern = r'\*\*(.+?)\*\*(?!\*)'
    comments_pattern = r''
    combined_pattern = r'|'.join((headings_pattern, bold_pattern, comments_pattern))

    stripped = re.sub(combined_pattern, '', stripped)

    # find all URLs in the string, and then remove the final directory from each to leave the general URL form
    # there may be useful patterns based on what URLs issues are commonly linking to
    url_pattern = re.compile(r'(https?://[^\s]+)')
    for url in re.findall(url_pattern, stripped):
        new_url = url.rsplit("/", 1)[0]
        stripped = stripped.replace(url, new_url)

    non_alpha_pattern = r'[^A-Za-z ]+'
    stripped = re.sub(non_alpha_pattern, '', stripped)

    return ' '.join(stripped.split())
  else:
    return " "

from pyspark.sql.functions import col
train = train.withColumn("body", strip_text(col("body"))).withColumn("title", strip_text(col("title")))
validation = validation.withColumn("body", strip_text(col("body"))).withColumn("title", strip_text(col("title")))

Thanks for any help you may be able to provide. I have been working on this with no progress for a long time now.

I have tried removing my pre-processing UDF, but there was no difference.

I have tried to impute null values as I saw this could potentially be an issue, but it doesn't fix it

train = train.fillna(0)

I have tried removing the IDF text features altogether just leaving the OneHotEncoded column, and this did work. So I believe it is an issue with the text column, but I am not sure what to do next.


来源: stackexchange

问:
Deployment issue in Oracle Service Bus
I am not able to deploy jars into oracle service bus console , the session activation is not getting successful showing error like 'A connection to the server has failed(status=unknown)'.</p>

ContentLicense: CC BY-SA 4.0

Title: Deployment issue in Oracle Service Bus


问:
No CPU spike in Java's ArrayBlockingQueue 'take' method
Java's ArrayBlockingQueue's take() method blocks, and sits and waits until something is put into the queue.

Another method would be to have the thread call poll() in a loop until it doesn't return null.

Why does the take() method not cause a CPU spike to happen but the poll() method does? I understand why the poll version spikes the CPU, but what is happening under the hood with the take() method that doesn't cause the CPU to spike?


问:
Need to send blob file response from laravel 9 to react js
I'm trying to send a pdf file which is available in the server, now I want to send this file as blob file to the frontend which is a react.js application. Currently I'm using response->download() and response->file() method and both seems not working. I'm getting an error

```<code>Error: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in file E:\api.example.com\app\Http\Middleware\Cors.php on line 19
</code>```

My Code:

```<code>return response()->download(public_path('storage/abc.pdf'));
</code>```

Cors Middleware:

```<code><?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        return $next($request)->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

</code>```

I'm expecting a response with binary so I can convert this binary file to a valid file on frontend

Body:
I'm trying to send a pdf file which is available in the server, now I want to send this file as blob file to the frontend which is a react.js application. Currently I'm using response->download() and response->file() method and both seems not working. I'm getting an error

Error: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in file E:\api.example.com\app\Http\Middleware\Cors.php on line 19

My Code:

return response()->download(public_path('storage/abc.pdf'));

Cors Middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        return $next($request)->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

I'm expecting a response with binary so I can convert this binary file to a valid file on frontend


问:
How I can navigate to multible sites using C# and selenium?
```<code>private const string Url1 = "https://www.w3schools.com/"
</code>```
```<code>private const string Url2 = "https://www.@example.com/"
</code>```

.
.
.

```<code>[Test]
public void CC1()
{
    IWebDriver driver = new ChromeDriver();
    WebDriverWait _wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
    InitialLogin(driver, _wait);
    driver.Navigate().GoToUrl(Url1);//here i want to change the link with Url2
}
</code>```

I want to run the test case for multile pages which take the link
and do the same opreasions on all pages instead of using

```<code>driver.Navigate().GoToUrl("url string");
</code>```

again and again

Body:
private const string Url1 = "https://www.w3schools.com/"

private const string Url2 = "https://www.@example.com/"

.
.
.

[Test]
public void CC1()
{
    IWebDriver driver = new ChromeDriver();
    WebDriverWait _wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
    InitialLogin(driver, _wait);
    driver.Navigate().GoToUrl(Url1);//here i want to change the link with Url2
}

I want to run the test case for multile pages which take the link
and do the same opreasions on all pages instead of using

driver.Navigate().GoToUrl("url string");

again and again


问:
How to create multiple csv file based on result values(query result) and naming csv file with dynamic result values in python fastapi
To generate in csv file this i am using below  piece of codes

```<code>def csv(data: List[dict]):
        file=io.StringIO()
        name = [
            "id",
            "name",
            "amount",
        ]
        today = str(date.today())
        with open('report_'+today+'.csv', 'w') as report:
            writer = csv.DictWriter(report, fieldnames=name)
            writer.writeheader()
            writer.writerows(data)
            return file
</code>```
```<code>def get_data():
    results=[]
    response =report()
        for o in response:
            results.append({
                "id":o.id,
                "name":o.name,
                "amount":o.amount,
                })
        return results
</code>```

my main function

```<code>@app.get("/report")
def gen_csv(
):

 data= csv(
  get_data()
        )
  return data
</code>```

using this code i am able to generate report_8_12_2022.csv and data inside is

`id             name               amount

4              surya             10000.0

4              surya             10000.0

5              vikas             50000.0

5              vikas             300.0

6              praveen           790.0

6              praveen           900.0

Now  i need to generate 3 csv files(multiple i have mentioned only 3 sets of data ) and the name of that file should be report_8_12_2022_{name}.csv(ex: report_8_12_2022_surya.csv
with single function or loop  how can i achieve that(other ideas also invitable).

Thanks for all the inputs. i stuck here since week.

Body:
To generate in csv file this i am using below  piece of codes

def csv(data: List[dict]):
        file=io.StringIO()
        name = [
            "id",
            "name",
            "amount",
        ]
        today = str(date.today())
        with open('report_'+today+'.csv', 'w') as report:
            writer = csv.DictWriter(report, fieldnames=name)
            writer.writeheader()
            writer.writerows(data)
            return file

def get_data():
    results=[]
    response =report()
        for o in response:
            results.append({
                "id":o.id,
                "name":o.name,
                "amount":o.amount,
                })
        return results

my main function

@app.get("/report")
def gen_csv(
):

 data= csv(
  get_data()
        )
  return data

using this code i am able to generate report_8_12_2022.csv and data inside is

`id             name               amount

4              surya             10000.0

4              surya             10000.0

5              vikas             50000.0

5              vikas             300.0

6              praveen           790.0

6              praveen           900.0

Now  i need to generate 3 csv files(multiple i have mentioned only 3 sets of data ) and the name of that file should be report_8_12_2022_{name}.csv(ex: report_8_12_2022_surya.csv
with single function or loop  how can i achieve that(other ideas also invitable).

Thanks for all the inputs. i stuck here since week.


问:
Query of select a datetime type column but getting back a UTC format data
I store my data into SQL table as a local time format, like below

But then when I do select query of this data and display it on a dashboard, JavaScript returns it as UTC format like below

Is there a way for me when I select query the data is not converted?

Thanks

I did some readings how to convert it back to local time, but seems it didnt give a result as expected.


问:
How to Mock r2dbcEntityTemplate.getDatabaseClient().sql method in DaoImpl for Unit Test
I want to Mock the r2dbcEntityTemplate and Unit Test the DaoImpl class.

I have the DaoImpl class with definition:

```<code>public class StudentDaoImpl implements StudentDao{

private final R2dbcEntityTemplate r2dbcEntityTemplate;

@Override
  public Mono<Student> upsert(Student student) {

    return r2dbcEntityTemplate
        .getDatabaseClient()
        .sql(DaoUtil.UPSERT_QUERY)
        .bind("student_id", student.getStudentId())
        .bind("name", student.getName())
        .bind("roll", student.getRoll())
        .map(DaoUtil.STUDENT_MAPPING_FUNCTION)
        .one();
  }

}
</code>```

Dao Class:

```<code>public interface StudentDao {
  Mono<Student> upsert(Student student);
}
</code>```

and DaoUtil class:

```<code>public class DaoUtil{
public static final String UPSERT_QUERY = "INSERT INTO \n "
          + "student \n"
          + "  (student_id, \n "
          + "  name, \n"
          + "  roll) \n"
          + "  VALUES \n"
          + "  (:student_id, \n"
          + "  :name, \n"
          + "  :roll) \n"
          + "   ON CONFLICT \n"
          + "   (student_id, \n"
          + "   name) \n"
          + "   DO UPDATE SET \n"
          + "   roll = \n"
          + "   excluded.roll"
          + "   RETURNING *;";

public static final Function<Row, Student>
      STUDENT_MAPPING_FUNCTION =
          row ->
             Student.builder()
                  .id(row.get("id", Long.class))
                  .studentId(row.get("student_id", String.class))
                  .name(row.get("name", String.class))
                  .roll(row.get("roll", String.class))
                  .build();

}
</code>```

I am not finding correct Constructors to mock the r2dbcEntityTemplate

Please guide me to a suitable example for this usecause.

Thanks in advance.

Body:
I want to Mock the r2dbcEntityTemplate and Unit Test the DaoImpl class.

I have the DaoImpl class with definition:

public class StudentDaoImpl implements StudentDao{

private final R2dbcEntityTemplate r2dbcEntityTemplate;

@Override
  public Mono<Student> upsert(Student student) {

    return r2dbcEntityTemplate
        .getDatabaseClient()
        .sql(DaoUtil.UPSERT_QUERY)
        .bind("student_id", student.getStudentId())
        .bind("name", student.getName())
        .bind("roll", student.getRoll())
        .map(DaoUtil.STUDENT_MAPPING_FUNCTION)
        .one();
  }

}

Dao Class:

public interface StudentDao {
  Mono<Student> upsert(Student student);
}

and DaoUtil class:

public class DaoUtil{
public static final String UPSERT_QUERY = "INSERT INTO \n "
          + "student \n"
          + "  (student_id, \n "
          + "  name, \n"
          + "  roll) \n"
          + "  VALUES \n"
          + "  (:student_id, \n"
          + "  :name, \n"
          + "  :roll) \n"
          + "   ON CONFLICT \n"
          + "   (student_id, \n"
          + "   name) \n"
          + "   DO UPDATE SET \n"
          + "   roll = \n"
          + "   excluded.roll"
          + "   RETURNING *;";

public static final Function<Row, Student>
      STUDENT_MAPPING_FUNCTION =
          row ->
             Student.builder()
                  .id(row.get("id", Long.class))
                  .studentId(row.get("student_id", String.class))
                  .name(row.get("name", String.class))
                  .roll(row.get("roll", String.class))
                  .build();

}

I am not finding correct Constructors to mock the r2dbcEntityTemplate

Please guide me to a suitable example for this usecause.

Thanks in advance.


问:
Can I get a piece of information from a JSON file this way?
I recently started to program in Python and haven't encountered too many errors where I couldn't figure it out, but this one stumped me. Long story short, I am creating a Banking system and now in the process of adding a feature where the user can have the ability to change their name. Through research, I found that the code below should have worked, but I am trying to understand why this can't work or won't work. Compiler states "list indices must be integers or slices, not str". I am completely lost, I've tried with multiple different variants and still no luck.

`

```<code>        print("What would you like to change?: ")
        print("\n")
        print("(1) Name")
        choice = input("Enter number: ")
        print("\n")

        if choice == "1":
            newname = input("Enter your new name: ")
            try:
                with open (filename, 'r') as f:
                    data = json.load(f)

                data["Name"] = newname //compiler tells me this is my issue

                with open(filename, "w") as f:
                    json.dump(data, f, indent=4)
            except FileNotFoundError:
                print("Error: the userInfo.json file does not exist.")
            except json.JSONDecodeError:
                print("Error: the userInfo.json file is not formatted correctly.")
        break
</code>```

`

As stated previously, I've tried multiple variants. With try, without try, trying to declare a dictionary etc.

Body:
I recently started to program in Python and haven't encountered too many errors where I couldn't figure it out, but this one stumped me. Long story short, I am creating a Banking system and now in the process of adding a feature where the user can have the ability to change their name. Through research, I found that the code below should have worked, but I am trying to understand why this can't work or won't work. Compiler states "list indices must be integers or slices, not str". I am completely lost, I've tried with multiple different variants and still no luck.

`

        print("What would you like to change?: ")
        print("\n")
        print("(1) Name")
        choice = input("Enter number: ")
        print("\n")

        if choice == "1":
            newname = input("Enter your new name: ")
            try:
                with open (filename, 'r') as f:
                    data = json.load(f)

                data["Name"] = newname //compiler tells me this is my issue

                with open(filename, "w") as f:
                    json.dump(data, f, indent=4)
            except FileNotFoundError:
                print("Error: the userInfo.json file does not exist.")
            except json.JSONDecodeError:
                print("Error: the userInfo.json file is not formatted correctly.")
        break

`

As stated previously, I've tried multiple variants. With try, without try, trying to declare a dictionary etc.


问:
Validate a input text filed on Gravity Form?
How to validate a input text filed only allow letters not allow Number and symbol on Gravity Form?</p>
```<code>add_filter( 'gform_field_validation_13_6', function( $result, $value, $form, $field ) {

   if (preg_match('#^[A-Za-z\s]{1,}$#', $a) == 1) {
      echo "ok\n";
      $result['is_valid'] = false;
      $result['message'] = 'Please enter only letters';

   }

   return $result;

}, 10, 4 );

</code>```

Body:
How to validate a input text filed only allow letters not allow Number and symbol on Gravity Form?

add_filter( 'gform_field_validation_13_6', function( $result, $value, $form, $field ) {

   if (preg_match('#^[A-Za-z\s]{1,}$#', $a) == 1) {
      echo "ok\n";
      $result['is_valid'] = false;
      $result['message'] = 'Please enter only letters';

   }

   return $result;

}, 10, 4 );


问:
Voice angle detection using 4 mems microphones on STM32 using Acoustic SL library
Hope you are doing well. I am an Embedded Engineer working on STM32, I have recently shifted to STM32 and I use Stm32CubeIde for programming and nucleo-64(F411RE) board. I have a project with 4 mems microphones and have to perform angle detection using it and turn an led on that specific angle. I have 12 LEDs for each angle.

I know that Acoustic library can be used to do this job but I don't know how to implement it. I just want someone to help me with an example or a piece of code. I don't want the whole code or program, just a hint.

https://documentation.help/STM32-AcousticSL/struct_acoustic_s_l___handler__t.html

https://www.st.com/resource/en/user_manual/um2212-getting-started-with-acousticsl-realtime-sound-source-localization-middleware-stmicroelectronics.pdf

Well I tried to interface mems microphone Acoustic SL library but couldn't find any proper documentation for it.


问:
How to fix alignment in ScrollView in SwiftUI with ([.horizontal, .vertical]) configuration. Alignment breaks when fast scrolling done
I have a SwiftUI view that contains ScrollView with ([.horizontal, .vertical]) attribute. I want to show pinned headers in the horizontal and vertical stack views, inside ScrollView. So I am using LazyVStack and LazyHStack. But when I am scrolling the view, its layout is breaking. To reproduce this issue I have created a sample code below(Actual project code is very complex). I am also attaching screenshots of the broken layout showing after a fast scroll. Does anyone has faced a similar problem, and knows the solution to it? Please share.

`

```<code>import SwiftUI

struct ContentView: View {
  var body: some View {
    VStack {
      ScrollViewReader { scrollView in
        ScrollView([.horizontal, .vertical])
        {
          LazyVStack(alignment: .leading, spacing: 0)
          {
            ForEach(Array(stride(from: 1, to: 1000, by: 10)), id: \.self) { rowindex in
              LazyHStack(alignment: .top, spacing: 0)
              {
                ForEach(0..<10, id: \.self) { columnindex in
                  ZStack() {
                    Rectangle().stroke(.blue)
                    Text("\(rowindex+columnindex)")
                      .padding(10)
                      .frame(width: 80, height: 40, alignment: .center)
                  }
                  .id(rowindex+columnindex)
                }
              }
            }
          }
        }
      }
    }
  }
}
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
</code>```

`

If I make any one of stack view non lazy issue got fix. But the problem is I have to add pinned headers so cant do that. Also the data is based on API response and can have diffrent layouts.So calculation of height and width will not be accurate or rally complex.

Body:
I have a SwiftUI view that contains ScrollView with ([.horizontal, .vertical]) attribute. I want to show pinned headers in the horizontal and vertical stack views, inside ScrollView. So I am using LazyVStack and LazyHStack. But when I am scrolling the view, its layout is breaking. To reproduce this issue I have created a sample code below(Actual project code is very complex). I am also attaching screenshots of the broken layout showing after a fast scroll. Does anyone has faced a similar problem, and knows the solution to it? Please share.

`

import SwiftUI

struct ContentView: View {
  var body: some View {
    VStack {
      ScrollViewReader { scrollView in
        ScrollView([.horizontal, .vertical])
        {
          LazyVStack(alignment: .leading, spacing: 0)
          {
            ForEach(Array(stride(from: 1, to: 1000, by: 10)), id: \.self) { rowindex in
              LazyHStack(alignment: .top, spacing: 0)
              {
                ForEach(0..<10, id: \.self) { columnindex in
                  ZStack() {
                    Rectangle().stroke(.blue)
                    Text("\(rowindex+columnindex)")
                      .padding(10)
                      .frame(width: 80, height: 40, alignment: .center)
                  }
                  .id(rowindex+columnindex)
                }
              }
            }
          }
        }
      }
    }
  }
}
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

`

If I make any one of stack view non lazy issue got fix. But the problem is I have to add pinned headers so cant do that. Also the data is based on API response and can have diffrent layouts.So calculation of height and width will not be accurate or rally complex.


问:
Using Nesterov's accelerated gradient on the Ackley function
I am working on a project where I want to use Nesterov's accelerated gradient method on the Ackley function below

to go from the initial point of (25, 20) to within the distance of the global minimizer and minimum of (2e-4, 5e-4).

```<code>def nag(func, x, lr, num_iters, jac, tol, callback, gamma=0.9, *args, **kwargs):
    vals = [func(x)]
    opt_res = OptimizeResult()
    update = np.zeros(x.size)
    for i in range(1, num_iters+1):
        grad = jac(x - gamma * update)
        prev_x = x
        update = gamma * update + lr(i) * grad
        x = x - update
        vals.append(func(x))
        callback(x)
        if np.linalg.norm(x-prev_x) <= tol:
            break
    opt_res.x = x
    opt_res.nit = i
    return opt_res, np.array(vals, dtype=object)
</code>```

Using gamma = 0.2, num_iters = 5000, and a customized learning rate function

```<code>def lr_(t):
    if t < 5:
        return 1e-4
    elif t < 10:
        return 1e-2
    else:
        return 0.1
</code>```

I was able to get around (0.0008 and 0.0022), but couldn't get closer to the desired global minimizer and minimum as specified despite playing around with different values for a long time. Does anyone know what I could try so that I can get closer to the desired result?

Or are there other optimization methods that would work better than NAG? I heard Adam's or Adagrad should work, but haven't had much success with them.

Body:
I am working on a project where I want to use Nesterov's accelerated gradient method on the Ackley function below

to go from the initial point of (25, 20) to within the distance of the global minimizer and minimum of (2e-4, 5e-4).

def nag(func, x, lr, num_iters, jac, tol, callback, gamma=0.9, *args, **kwargs):
    vals = [func(x)]
    opt_res = OptimizeResult()
    update = np.zeros(x.size)
    for i in range(1, num_iters+1):
        grad = jac(x - gamma * update)
        prev_x = x
        update = gamma * update + lr(i) * grad
        x = x - update
        vals.append(func(x))
        callback(x)
        if np.linalg.norm(x-prev_x) <= tol:
            break
    opt_res.x = x
    opt_res.nit = i
    return opt_res, np.array(vals, dtype=object)

Using gamma = 0.2, num_iters = 5000, and a customized learning rate function

def lr_(t):
    if t < 5:
        return 1e-4
    elif t < 10:
        return 1e-2
    else:
        return 0.1

I was able to get around (0.0008 and 0.0022), but couldn't get closer to the desired global minimizer and minimum as specified despite playing around with different values for a long time. Does anyone know what I could try so that I can get closer to the desired result?

Or are there other optimization methods that would work better than NAG? I heard Adam's or Adagrad should work, but haven't had much success with them.


问:
replace string in json file run time using python
In my code have block where data to be written in json file, I am able to dump data but it is not dumping in the way which I want.
sample json:

```<code>{val2: data2}
{val3: data3}
</code>```

where I want the data to be stored like below:

```<code>{ val1: data1, val2: data2, val3: data3}
</code>```

logic in my code:

```<code>with open("cache_metadata.json", "a+") as f:
   json.dump(data, f, indent=4)
</code>```

here it is opening file when there is data to be updated, so is there a way that i can put some login under this block to replace {} with ,?
Also have tried that from google but somehow in last extra } still there.

Body:
In my code have block where data to be written in json file, I am able to dump data but it is not dumping in the way which I want.
sample json:

{val2: data2}
{val3: data3}

where I want the data to be stored like below:

{ val1: data1, val2: data2, val3: data3}

logic in my code:

with open("cache_metadata.json", "a+") as f:
   json.dump(data, f, indent=4)

here it is opening file when there is data to be updated, so is there a way that i can put some login under this block to replace {} with ,?
Also have tried that from google but somehow in last extra } still there.


问:
MySQL - get average of temperature for every 6 hours
I have created_at column in my table. Let's say data as follows-

I want to get the average of temperature of each 6 hour segment. Let's say time starts from first hour of the day. Thus, my rows will be like as follows-

Note: I am able to get average of temperature of each hour using HOUR FROM of MySQL.

Any help is appreciated.
Thanks!


问:
How to bring back import records from favorites to create menu in odoo15
import records button  is moved to favorites menu in odoo15. I want to create import button at create button. How to do it.

```<code>from odoo import models

class BaseImport(models.Model):
    _name = "base.import"
    _inherit = "base_import.import"

</code>```

I am not able to write views for creating this.

Body:
import records button  is moved to favorites menu in odoo15. I want to create import button at create button. How to do it.

from odoo import models

class BaseImport(models.Model):
    _name = "base.import"
    _inherit = "base_import.import"

I am not able to write views for creating this.


问:
Can one write an assertion on `self` with `patch.object`?
Say I patch a member function of a class with unittest.mock.patch.object

Can one write an assertion on another member variable of the object via self at the time of the mock's call?

Consider the following example

```<code>from unittest.mock import patch

class ToBeTestedClass:
    def __init__(self, a: int):
        self.a = a

    def to_be_tested_function(val: int) -> None:
        # self.a is important here
        return

def intermediary_function_expected() -> None:
    t = ToBeTestedClass(3)
    t.to_be_tested_function(10)

def test_main():

    with patch.object(
        ToBeTestedClass, "to_be_tested_function"
    ) as mock_to_be_tested_function:
        intermediary_function_expected()

        # TODO
        # Check that when to_be_tested_function was called the t.a was 3.

</code>```

Inside the test, I would like to check whether the object's a member variable had the value of 3 at the time of the call to the mocked function.

There are other ways to achieve the same thing. I could mock the object itself, even use monkeypatch…. I am specifically wondering about this patch.object approach.

Looking at the call args of the mock after intermediary_function_expected function call looks like this

```<code>(Pdb) p mock_to_be_tested_function.call_args
call(10)
</code>```

So the self is not available at the call args, even though t.to_be_tested_function(10) is equivalent to ToBeTestedClass.to_be_tested_function(t, 10) . At least this is in python 3.10.8

Body:
Say I patch a member function of a class with unittest.mock.patch.object

Can one write an assertion on another member variable of the object via self at the time of the mock's call?

Consider the following example

from unittest.mock import patch

class ToBeTestedClass:
    def __init__(self, a: int):
        self.a = a

    def to_be_tested_function(val: int) -> None:
        # self.a is important here
        return

def intermediary_function_expected() -> None:
    t = ToBeTestedClass(3)
    t.to_be_tested_function(10)

def test_main():

    with patch.object(
        ToBeTestedClass, "to_be_tested_function"
    ) as mock_to_be_tested_function:
        intermediary_function_expected()

        # TODO
        # Check that when to_be_tested_function was called the t.a was 3.

Inside the test, I would like to check whether the object's a member variable had the value of 3 at the time of the call to the mocked function.

There are other ways to achieve the same thing. I could mock the object itself, even use monkeypatch…. I am specifically wondering about this patch.object approach.

Looking at the call args of the mock after intermediary_function_expected function call looks like this

(Pdb) p mock_to_be_tested_function.call_args
call(10)

So the self is not available at the call args, even though t.to_be_tested_function(10) is equivalent to ToBeTestedClass.to_be_tested_function(t, 10) . At least this is in python 3.10.8


问:
Apache POI - Conditional formatting rule using java
I am trying to set below conditional formatting rule in java using apache poi

```<code>            String kkf = "IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(FILTERXML(\"<t><s>\"&SUBSTITUTE(J4,\",\",\"</s><s>\")&\"</s></t>\",\"//s\"),FILTERXML(\"<t><s>\"&SUBSTITUTE(PossibleValues!$D$2,\",\",\"</s><s>\")&\"</s></t>\",\"//s\"),0))),\"\"),TRUE())";
</code>```

The expectation is the formula to highlight invalid cells in column 'J' but the entire 'J' column values gets highlighted even for valid cells.  When I extracted the sheet 1.xml file from Excel, the formula was in below manner

Sheet1.xml via Java

```<code><cfRule type="expression" dxfId="1" priority="2">
    <formula>
        <![CDATA[IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(J4,",","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&SUBSTITUTE(PossibleValues!$D$2,",","</s><s>")&"</s></t>","//s"),0))),""),TRUE())]]>
    </formula>
</cfRule>
</code>```

Excel values :

Later I Clicked on conditional formatting rule ,Clicked on edit and Clicked OK ( Without changing anything ). Post this action the column values are getting highlighted correctly. So I extracted sheet1.xml again post making these changes. Found this

Sheet1.xml post Excel Edit

```<code><x14:cfRule type="expression" priority="2" id="{00000000-000E-0000-0000-000002000000}">
    <xm:f>IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(_xlfn.FILTERXML("<t><s>"&SUBSTITUTE(J4,",","</s><s>")&"</s></t>","//s"),_xlfn.FILTERXML("<t><s>"&SUBSTITUTE(PossibleValues!$D$2,",","</s><s>")&"</s></t>","//s"),0))),""),TRUE())</xm:f>
    <x14:dxf>
        <font>
            <b/>
            <color indexed="10"/>
        </font>
    </x14:dxf>
</x14:cfRule>
</code>```

Excel Later making changes :

Is CDATA causing some issues when creating the formula using java or Did I missed something.

Body:
I am trying to set below conditional formatting rule in java using apache poi

            String kkf = "IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(FILTERXML(\"<t><s>\"&SUBSTITUTE(J4,\",\",\"</s><s>\")&\"</s></t>\",\"//s\"),FILTERXML(\"<t><s>\"&SUBSTITUTE(PossibleValues!$D$2,\",\",\"</s><s>\")&\"</s></t>\",\"//s\"),0))),\"\"),TRUE())";

The expectation is the formula to highlight invalid cells in column 'J' but the entire 'J' column values gets highlighted even for valid cells.  When I extracted the sheet 1.xml file from Excel, the formula was in below manner

Sheet1.xml via Java

<cfRule type="expression" dxfId="1" priority="2">
    <formula>
        <![CDATA[IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(J4,",","</s><s>")&"</s></t>","//s"),FILTERXML("<t><s>"&SUBSTITUTE(PossibleValues!$D$2,",","</s><s>")&"</s></t>","//s"),0))),""),TRUE())]]>
    </formula>
</cfRule>

Excel values :

Later I Clicked on conditional formatting rule ,Clicked on edit and Clicked OK ( Without changing anything ). Post this action the column values are getting highlighted correctly. So I extracted sheet1.xml again post making these changes. Found this

Sheet1.xml post Excel Edit

<x14:cfRule type="expression" priority="2" id="{00000000-000E-0000-0000-000002000000}">
    <xm:f>IF(AND(NOT(ISBLANK(J4)),ISERROR(SUM(MATCH(_xlfn.FILTERXML("<t><s>"&SUBSTITUTE(J4,",","</s><s>")&"</s></t>","//s"),_xlfn.FILTERXML("<t><s>"&SUBSTITUTE(PossibleValues!$D$2,",","</s><s>")&"</s></t>","//s"),0))),""),TRUE())</xm:f>
    <x14:dxf>

            <color indexed="10"/>

    </x14:dxf>
</x14:cfRule>

Excel Later making changes :

Is CDATA causing some issues when creating the formula using java or Did I missed something.


问:
Native-Image binary not supporting a server effeciently
In my java application i have created a JettyServer.
In GraalVM i have implemented it and did a load test with it , i didn't find any issues.
I then converted the java application to a static native-image binary .
And if I do load test with this, I get many Connection refused errors
For example : in 10k requests atleast 3k gave me connection refused errors, where as in graalvm-java it worked fine.
The following args i have given while building the image
native-image -J-Xmx359M -J-Xms256M -J-Xmn256M --static -H:+MultiThreaded


问:
How to perform a Kruskal-Wallis test with interaction terms in R?
I am doing some analysis on R. I am trying to test capture rates against season, habitat, and species.
The data is non-parametric so I am able to use kruskal.test(`Capture rate` ~ Season, data = data) for example to check for the significance of season, and similarly for the other factors individually. I would like to check for interactions between these factors, namely season:species, season:habitat, and species:habitat

How can I do this?

Follow-up question: I would also like to run the Dunn test for the pairwise differences of the interactions. I would like to know how I can do this as well.

Thanks in advance


问:
How to remove headers and footer from the PPTX file using python?
I am not found any code related this.

not found packages also if anything is there let me know


问:
Destroying and re-initializing the jQuery-datatable
```<code>Uncaught TypeError: Cannot read properties of undefined (reading 'node')
    at m.destroy (datatables.min.js?v=2.95:269:483)
    at HTMLTableElement.<anonymous> (datatables.min.js?v=2.95:272:201)
    at HTMLTableElement.dispatch (jquery-1.12.4.js:5226:27)
    at elemData.handle (jquery-1.12.4.js:4878:28)
    at Object.trigger (jquery-1.12.4.js:5130:12)
    at HTMLTableElement.<anonymous> (jquery-1.12.4.js:5860:17)
    at Function.each (jquery-1.12.4.js:370:19)
    at jQuery.fn.init.each (jquery-1.12.4.js:137:17)
    at jQuery.fn.init.trigger (jquery-1.12.4.js:5859:15)
    at u (datatables.min.js?v=2.95:127:431)
</code>```

I am facing this error when destroying the jQuery DataTable

**Here is my code when I am initializing the Data Table **

```<code>$(document).ready(function () {
    boothtable =  $('#boothstaffTable').DataTable({
        processing: true,
        serverSide: true,
        scrollX: true,
        ajax: {
            url: urlnew,
        },
        language: {
            processing: '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading..n.</span> '
        },
        select: {
            style: 'multi',
            selector: 'td:first-child',
        },
        columnDefs: dataTableHeads,
        dom: 'Blfrtip',}):
</code>```

**After this I want to destroy the data Table and re-initialize the table **

Check if the table exists and destroy it

```<code> if ( jQuery.fn.DataTable.isDataTable('#boothstaffTable') ) {
        boothtable.destroy(true);
    }
    jQuery('#boothstaffTable tbody').empty();

</code>```

On reinitialize

```<code> $('#boothstaffTable').dataTable({
        processing: true,
        serverSide: true,
        scrollX: true,
        ajax: {
            url: urlnew,
            data: function (d) {
                d.dataFilter = JSON.stringify(jsondataorder);
                d.selectedColumns = JSON.stringify(selectedcolumns);
                d.orderByColumnsName = orderbycolumnsname;
                d.sortingType = sortingtype;
            }
        },
        language: {
            processing: '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading..n.</span> '
        },
        select: {
            style: 'multi',
            selector: 'td:first-child',
        },
        columnDefs: dataTableHeads,
        dom: 'Blfrtip',
});
</code>```

Body:
Uncaught TypeError: Cannot read properties of undefined (reading 'node')
    at m.destroy (datatables.min.js?v=2.95:269:483)
    at HTMLTableElement.<anonymous> (datatables.min.js?v=2.95:272:201)
    at HTMLTableElement.dispatch (jquery-1.12.4.js:5226:27)
    at elemData.handle (jquery-1.12.4.js:4878:28)
    at Object.trigger (jquery-1.12.4.js:5130:12)
    at HTMLTableElement.<anonymous> (jquery-1.12.4.js:5860:17)
    at Function.each (jquery-1.12.4.js:370:19)
    at jQuery.fn.init.each (jquery-1.12.4.js:137:17)
    at jQuery.fn.init.trigger (jquery-1.12.4.js:5859:15)
    at u (datatables.min.js?v=2.95:127:431)

I am facing this error when destroying the jQuery DataTable

**Here is my code when I am initializing the Data Table **

$(document).ready(function () {
    boothtable =  $('#boothstaffTable').DataTable({
        processing: true,
        serverSide: true,
        scrollX: true,
        ajax: {
            url: urlnew,
        },
        language: {
            processing: 'Loading..n. '
        },
        select: {
            style: 'multi',
            selector: 'td:first-child',
        },
        columnDefs: dataTableHeads,
        dom: 'Blfrtip',}):

**After this I want to destroy the data Table and re-initialize the table **

Check if the table exists and destroy it

 if ( jQuery.fn.DataTable.isDataTable('#boothstaffTable') ) {
        boothtable.destroy(true);
    }
    jQuery('#boothstaffTable tbody').empty();

On reinitialize

 $('#boothstaffTable').dataTable({
        processing: true,
        serverSide: true,
        scrollX: true,
        ajax: {
            url: urlnew,
            data: function (d) {
                d.dataFilter = JSON.stringify(jsondataorder);
                d.selectedColumns = JSON.stringify(selectedcolumns);
                d.orderByColumnsName = orderbycolumnsname;
                d.sortingType = sortingtype;
            }
        },
        language: {
            processing: 'Loading..n. '
        },
        select: {
            style: 'multi',
            selector: 'td:first-child',
        },
        columnDefs: dataTableHeads,
        dom: 'Blfrtip',
});


问:
What does NoLanguage language mode in PowerShell really mean?
I'm new to PowerShell and I'm looking to understand what the NoLanguage language mode means, I checked the Microsoft Documentation below but it is quite vague:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes?source=recommendations&view=powershell-7.3

Can someone give some examples what cmdlets I wouldn't be able to run in the NoLanguage language mode?


问:
EEException: Image.sampleRectangle: Too many pixels in sample; must be <= 262144. Got 953050
```<code>def clip_images(image) :
    return image.clip(AOI)
def read_file(file):
    with rasterio.open(file) as src:
        return(src.read(1))
def year_month(year) :
    MONTHS = list(range(1, 13))
    return [f"{year}-{x}" for x in MONTHS]
dataset = (ee.ImageCollection("MODIS/006/MOD16A2")
                               .filterBounds(AOI)
                               .map(clip_images))
ET = dataset.select('ET')
PET = dataset.select('PET')
ET_d = dataset.first().select('ET').lt(-15).sampleRectangle(AOI)
dataset.size().getInfo()
img = np.array(ET_d.getInfo()['properties']['ET'])

</code>```

I am trying to get the array details of the 'ET' but its throwing error..
EEException: Image.sampleRectangle: Too many pixels in sample; must be <= 262144. Got 953050.

Body:
def clip_images(image) :
    return image.clip(AOI)
def read_file(file):
    with rasterio.open(file) as src:
        return(src.read(1))
def year_month(year) :
    MONTHS = list(range(1, 13))
    return [f"{year}-{x}" for x in MONTHS]
dataset = (ee.ImageCollection("MODIS/006/MOD16A2")
                               .filterBounds(AOI)
                               .map(clip_images))
ET = dataset.select('ET')
PET = dataset.select('PET')
ET_d = dataset.first().select('ET').lt(-15).sampleRectangle(AOI)
dataset.size().getInfo()
img = np.array(ET_d.getInfo()['properties']['ET'])

I am trying to get the array details of the 'ET' but its throwing error..
EEException: Image.sampleRectangle: Too many pixels in sample; must be <= 262144. Got 953050.


问:
How to select a dataset in ChartJS without removing the others?
I've looked everywhere online and I can't find a way to select a dataset where it won't remove or hide the dataset(because I want you to be able to select the others).
This is ChartJS in React so I'm using the react-chartjs wrapper.

Here is how the example looks:

And I want to be able to click on dataset 1 and then dataset 2 and dataset 3 get greyed out(not deleted), basically showing that dataset 1 was selected.

I've put a sandbox together: https://codesandbox.io/s/chartjs-select-dataset-r1kixh
I'm unable to update the color and I'm assuming with the way I have it that if I was able to update the color I probably wouldn't be able to get back the original color unless I find a way to re-render it? Not sure how I could also deselect all?

TL;DR:

How to select one dataset without removing the other?

How to be able to select a different dataset(hence using its original color)

How to "reset" the colors by deselecting(clicking in the space where there isn't a graph maybe)
The first two questions are this stack overflow question.

Here is the code:

```<code>
import React, { MouseEvent, useRef } from "react";
import {
  Chart as ChartJS,
  LinearScale,
  CategoryScale,
  BarElement,
  PointElement,
  LineElement,
  Legend,
  Tooltip
} from "chart.js";
import { Chart, getDatasetAtEvent } from "react-chartjs-2";
import faker from "faker";

ChartJS.register(
  LinearScale,
  CategoryScale,
  BarElement,
  PointElement,
  LineElement,
  Legend,
  Tooltip
);

export const options = {
  scales: {
    y: {
      beginAtZero: true
    }
  }
};

const labels = ["January", "February", "March", "April", "May", "June", "July"];

export const data = {
  labels,
  datasets: [
    {
      type: "line" as const,
      label: "Dataset 1",
      backgroundColor: "rgba(75, 192, 192, 1)",
      borderColor: "rgba(255, 99, 132,1)",
      borderWidth: 2,
      fill: false,
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
    },
    {
      type: "bar" as const,
      label: "Dataset 2",
      backgroundColor: "rgba(75, 192, 192, 1)",
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
      borderColor: "white",
      borderWidth: 2
    },
    {
      type: "bar" as const,
      label: "Dataset 3",
      backgroundColor: "rgba(53, 162, 235, 1)",
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
    }
  ]
};

export function App() {
  const chartRef = useRef<ChartJS>(null);

  const onClick = (event: MouseEvent<HTMLCanvasElement>) => {
    const { current: chart } = chartRef;
    const element = getDatasetAtEvent(chart, event);
    const datasetIndex = element[0].datasetIndex;

    const selectedDataset = data.datasets[datasetIndex].label;
    if (!chart) {
      return;
    }
    console.log("SELECTED: ", selectedDataset);

    for (let i = 0; i < data.datasets.length; i++) {
      if (data.datasets[i].label !== selectedDataset) {
        console.log("NOT SELECTED: ", data.datasets[i].label);
        data.datasets[i].backgroundColor = "rgba(255,255,255,0.2)";
      }
    }
    chartRef.current.update();
  };

  return (
    <Chart
      ref={chartRef}
      type="bar"
      onClick={onClick}
      options={options}
      data={data}
    />
  );
}

</code>```

Thanks for any help!

Body:
I've looked everywhere online and I can't find a way to select a dataset where it won't remove or hide the dataset(because I want you to be able to select the others).
This is ChartJS in React so I'm using the react-chartjs wrapper.

Here is how the example looks:

And I want to be able to click on dataset 1 and then dataset 2 and dataset 3 get greyed out(not deleted), basically showing that dataset 1 was selected.

I've put a sandbox together: https://codesandbox.io/s/chartjs-select-dataset-r1kixh
I'm unable to update the color and I'm assuming with the way I have it that if I was able to update the color I probably wouldn't be able to get back the original color unless I find a way to re-render it? Not sure how I could also deselect all?

TL;DR:

How to select one dataset without removing the other?

How to be able to select a different dataset(hence using its original color)

How to "reset" the colors by deselecting(clicking in the space where there isn't a graph maybe)
The first two questions are this stack overflow question.

Here is the code:

import React, { MouseEvent, useRef } from "react";
import {
  Chart as ChartJS,
  LinearScale,
  CategoryScale,
  BarElement,
  PointElement,
  LineElement,
  Legend,
  Tooltip
} from "chart.js";
import { Chart, getDatasetAtEvent } from "react-chartjs-2";
import faker from "faker";

ChartJS.register(
  LinearScale,
  CategoryScale,
  BarElement,
  PointElement,
  LineElement,
  Legend,
  Tooltip
);

export const options = {
  scales: {
    y: {
      beginAtZero: true
    }
  }
};

const labels = ["January", "February", "March", "April", "May", "June", "July"];

export const data = {
  labels,
  datasets: [
    {
      type: "line" as const,
      label: "Dataset 1",
      backgroundColor: "rgba(75, 192, 192, 1)",
      borderColor: "rgba(255, 99, 132,1)",
      borderWidth: 2,
      fill: false,
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
    },
    {
      type: "bar" as const,
      label: "Dataset 2",
      backgroundColor: "rgba(75, 192, 192, 1)",
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
      borderColor: "white",
      borderWidth: 2
    },
    {
      type: "bar" as const,
      label: "Dataset 3",
      backgroundColor: "rgba(53, 162, 235, 1)",
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
    }
  ]
};

export function App() {
  const chartRef = useRef<ChartJS>(null);

  const onClick = (event: MouseEvent<HTMLCanvasElement>) => {
    const { current: chart } = chartRef;
    const element = getDatasetAtEvent(chart, event);
    const datasetIndex = element[0].datasetIndex;

    const selectedDataset = data.datasets[datasetIndex].label;
    if (!chart) {
      return;
    }
    console.log("SELECTED: ", selectedDataset);

    for (let i = 0; i < data.datasets.length; i++) {
      if (data.datasets[i].label !== selectedDataset) {
        console.log("NOT SELECTED: ", data.datasets[i].label);
        data.datasets[i].backgroundColor = "rgba(255,255,255,0.2)";
      }
    }
    chartRef.current.update();
  };

  return (
    <Chart
      ref={chartRef}
      type="bar"
      onClick={onClick}
      options={options}
      data={data}
    />
  );
}

Thanks for any help!


问:
order SQL result by a specific field data
Using following PHP code I am getting the below given result.

```<code>$sql = "SELECT * FROM addresses WHERE user='$userid'";

id | address | default
1 | address1 |   no
2 | address2 |   no
3 | address3 |   no
4 | address4 |   yes
5 | address5 |   no
6 | address6 |   no
</code>```

See the default 'yes', I would like to show that first always. there will be only one yes in default field always.

Following is the desired result.

```<code>id | address | default
--------------------
 4 | address4 |   yes
 1 | address1 |   no
 2 | address2 |   no
 3 | address3 |   no
 5 | address5 |   no
 6 | address6 |   no
</code>```

I tried the following, but it shows error.

$sql = "SELECT * FROM addresses WHERE user='$userid' ORDER BY FIELD(default,'yes')";

error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''yes')' at line 1

Fix: Thanks to @Jens, as he suggeted ("default is a keyword. rename the column or enclose it with backticks") I renamed the default column into defaultaddress and it worked.

Body:
Using following PHP code I am getting the below given result.

$sql = "SELECT * FROM addresses WHERE user='$userid'";

id | address | default
1 | address1 |   no
2 | address2 |   no
3 | address3 |   no
4 | address4 |   yes
5 | address5 |   no
6 | address6 |   no

See the default 'yes', I would like to show that first always. there will be only one yes in default field always.

Following is the desired result.

id | address | default
--------------------
 4 | address4 |   yes
 1 | address1 |   no
 2 | address2 |   no
 3 | address3 |   no
 5 | address5 |   no
 6 | address6 |   no

I tried the following, but it shows error.

$sql = "SELECT * FROM addresses WHERE user='$userid' ORDER BY FIELD(default,'yes')";

error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''yes')' at line 1

Fix: Thanks to @Jens, as he suggeted ("default is a keyword. rename the column or enclose it with backticks") I renamed the default column into defaultaddress and it worked.


问:
How python rich library print?
I'm new in python and I use the rich python library to color and format my console.
I don't have level to understnad the library alone , but i want to understand how it work to print colored pixel and format text in a console ?
Do it draw pixel after pixel ?
If someone can explain me ?

Thanks

I tried to looking for it in internet but not success

Title: How python rich library print?


问:
Why does changing the new list cloned with list(), also make changes to the original in this case?
I thought cloning with new_list = list(old_list) makes a new copy.
But in the following code, the original "res" is changed.

```<code>def subsets(self, nums: List[int]) -> List[List[int]]:

        res = []
        res.append([])

        for curNum in range(len(nums)):
            resLen = len(res)
            newSubset = list(res)
            for i in range(len(newSubset)):
                newSubset[i].append(nums[curNum]) #this line changes res
                print(res)
                print(newSubset)

            res = res + newSubset

        return res
</code>```

Does this have to do with deep copy vs shallow copy?
Is list() a shallow or deep copy? If it is shallow for example, which shallow does it mean?  Some sites say shallow copy just copied the memory address of the original, like two variables pointing to the same object. Some sites are saying shallow copy copies the compound object and insert references into it.
I am so confused and lost.

Body:
I thought cloning with new_list = list(old_list) makes a new copy.
But in the following code, the original "res" is changed.

def subsets(self, nums: List[int]) -> List[List[int]]:

        res = []
        res.append([])

        for curNum in range(len(nums)):
            resLen = len(res)
            newSubset = list(res)
            for i in range(len(newSubset)):
                newSubset[i].append(nums[curNum]) #this line changes res
                print(res)
                print(newSubset)

            res = res + newSubset

        return res

Does this have to do with deep copy vs shallow copy?
Is list() a shallow or deep copy? If it is shallow for example, which shallow does it mean?  Some sites say shallow copy just copied the memory address of the original, like two variables pointing to the same object. Some sites are saying shallow copy copies the compound object and insert references into it.
I am so confused and lost.


问:
How to pass in props to snapshot test React native typescript?
i am very new to testing and im trying to create a simple snapshot test for a presentational component that accepts props. Am I suppose to 'mock' some props? Also  I am not using ts-jest as im not sure if i should.

NutritionValue.tsx

```<code>interface Item {
    title: string,
    valueConsumed: number,
    valueRequired: number,
}

export const NutritionValue = (props: Item): JSX.Element => {
    const { title, valueConsumed, valueRequired } = props
    let label:string = ''
    switch (title) {
        case 'Calories':
            label = 'cal'
            break
        default:
            label = 'g'
    }
    return (
        <View style={styles.macrocontainer}>
            <Text style={styles.title}> {title} </Text>
            <Progress.Bar progress={valueConsumed/valueRequired} width={75} height={2} color="#5F8D4E" />
            <Text style={styles.subtitle}> {valueConsumed} / {valueRequired} {label} </Text>
        </View>
    )

}
//const styles = StyleSheet.create ({...})
</code>```

NutritionValue.test.tsx

```<code>import renderer from 'react-test-renderer'
import { NutritionValue } from '../NutritionValue'

test('renders correctly', () => {
    const tree = renderer.create(<NutritionValue  />).toJSON();
    expect(tree).toMatchSnapshot();
  });
</code>```

Body:
i am very new to testing and im trying to create a simple snapshot test for a presentational component that accepts props. Am I suppose to 'mock' some props? Also  I am not using ts-jest as im not sure if i should.

NutritionValue.tsx

interface Item {
    title: string,
    valueConsumed: number,
    valueRequired: number,
}

export const NutritionValue = (props: Item): JSX.Element => {
    const { title, valueConsumed, valueRequired } = props
    let label:string = ''
    switch (title) {
        case 'Calories':
            label = 'cal'
            break
        default:
            label = 'g'
    }
    return (
        <View style={styles.macrocontainer}>
            <Text style={styles.title}> {title} </Text>
            <Progress.Bar progress={valueConsumed/valueRequired} width={75} height={2} color="#5F8D4E" />
            <Text style={styles.subtitle}> {valueConsumed} / {valueRequired} {label} </Text>
        </View>
    )

}
//const styles = StyleSheet.create ({...})

NutritionValue.test.tsx

import renderer from 'react-test-renderer'
import { NutritionValue } from '../NutritionValue'

test('renders correctly', () => {
    const tree = renderer.create(<NutritionValue  />).toJSON();
    expect(tree).toMatchSnapshot();
  });


问:
Sending email from ASP.NET MVC web app not working after deployment on GoDaddy
I have created an ASP.NET MVC application using C# in Visual Studio 2022. In it, I have implemented email functionality using SMTP. The email functionality works perfectly fine in local, but as we deploy to our Godaddy server, it does not work anymore - it neither sends any mail, nor does it show any errors.

Here is my code:

web.config:

```<code><add key="Port" value="25"/>
<add key="Host" value="relay-hosting.secureserver.net"/>
<add key="FromEmail" value="senderemail@gmail.com"/>
<add key="FromPassword" value="senderpassword"/>
<add key="ToEmail" value="reciveremail@gmail.com"/>
<add key="IsSSL" value="false" />
</code>```

C# code in controller method:

```<code>MailMessage _mailmsg = new MailMessage();
_mailmsg.IsBodyHtml = true;
_mailmsg.From = new MailAddress(emailSender);
_mailmsg.To.Add(new MailAddress(ToEmail));
_mailmsg.Subject = "Test Mail";
_mailmsg.Body = "Body";

SmtpClient _smtp = new SmtpClient();
_smtp.Host = emailSenderHost;
_smtp.EnableSsl = emailIsSSL;
_smtp.Port = emailSenderPort;
_smtp.UseDefaultCredentials = false;

NetworkCredential _network = new NetworkCredential(emailSender, emailSenderPassword); // credetials of google gmail account
_smtp.Credentials = _network;
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

try
{
    _smtp.Send(_mailmsg);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
    return false;
}
return true
</code>```

If anyone has faced this issue in the past, or has any suggestions to solve this issue, please provide answer as I have already spent a couple of days trying to resolve this issue.

Any help or suggestions would be highly appreciated.

Thanks

Body:
I have created an ASP.NET MVC application using C# in Visual Studio 2022. In it, I have implemented email functionality using SMTP. The email functionality works perfectly fine in local, but as we deploy to our Godaddy server, it does not work anymore - it neither sends any mail, nor does it show any errors.

Here is my code:

web.config:

<add key="Port" value="25"/>
<add key="Host" value="relay-hosting.secureserver.net"/>
<add key="FromEmail" value="senderemail@gmail.com"/>
<add key="FromPassword" value="senderpassword"/>
<add key="ToEmail" value="reciveremail@gmail.com"/>
<add key="IsSSL" value="false" />

C# code in controller method:

MailMessage _mailmsg = new MailMessage();
_mailmsg.IsBodyHtml = true;
_mailmsg.From = new MailAddress(emailSender);
_mailmsg.To.Add(new MailAddress(ToEmail));
_mailmsg.Subject = "Test Mail";
_mailmsg.Body = "Body";

SmtpClient _smtp = new SmtpClient();
_smtp.Host = emailSenderHost;
_smtp.EnableSsl = emailIsSSL;
_smtp.Port = emailSenderPort;
_smtp.UseDefaultCredentials = false;

NetworkCredential _network = new NetworkCredential(emailSender, emailSenderPassword); // credetials of google gmail account
_smtp.Credentials = _network;
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

try
{
    _smtp.Send(_mailmsg);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
    return false;
}
return true

If anyone has faced this issue in the past, or has any suggestions to solve this issue, please provide answer as I have already spent a couple of days trying to resolve this issue.

Any help or suggestions would be highly appreciated.

Thanks


问:
Upload an asset folder to GitHub release using travis
I use travis to automate build and deploy to GitHub release page. Now the structure is:

Assets:

file1

file2

But I want to it like this:

Assets:

folder1

file1

file2

The travis script is (Omit unrelated parts):

```<code>function uploadHasFolder {

  echo "upload folder"
  cd root_worksapce
  touch file2
  echo hello > file2

  mkdir folder1
  cd folder1
  touch file1
  echo hello > file1

}
</code>```

And the .travis.yaml is (Omit unrelated parts):

```<code>deploy:
  provider: releases
  file_glob: true
  file: root_worksapce-${TRAVIS_TAG}/**/*
  skip_cleanup: true
  on:
    tags: true

</code>```

I read the travis docs and no clues.
Doesn't travis support upload folder to GitHub release?

Body:
I use travis to automate build and deploy to GitHub release page. Now the structure is:

Assets:

file1

file2

But I want to it like this:

Assets:

folder1

file1

file2

The travis script is (Omit unrelated parts):

function uploadHasFolder {

  echo "upload folder"
  cd root_worksapce
  touch file2
  echo hello > file2

  mkdir folder1
  cd folder1
  touch file1
  echo hello > file1

}

And the .travis.yaml is (Omit unrelated parts):

deploy:
  provider: releases
  file_glob: true
  file: root_worksapce-${TRAVIS_TAG}/**/*
  skip_cleanup: true
  on:
    tags: true

I read the travis docs and no clues.
Doesn't travis support upload folder to GitHub release?


问:
Android MS-Office files opening issue
I want to open XLS,DOC, and PPT files inside android application using android studio without using any external app , this app should works in offline mode.

I searched a lot on internet. but did't get any solution for that.

Title: Android MS-Office files opening issue


问:
PnP PowerShell - Update AltText in WebPart
I want to translate and update the alt text of an image in my existing SharePoint Page. To do this, I have PowerShell code running which executes functions of the PnPPowerShell module.

The Start-Translation function works fine, but the Setter Set-PnPPageWebPart of the PnP module does not seem to work. Might this be a bug in the module? What do I miss?

```<code>$newPage = Get-PnPPage -Identity $identity

# Next, take all text controls and translate those
$textControls = $newPage.Controls
foreach ($textControl in $textControls) {
    $translatedControlText = ""
    # Is my Text Control type of Text
    if ($textControl.Type.FullName -eq "PnP.Core.Model.SharePoint.PageText") {
        # Logic
    } elseif ($textControl.Type.FullName -eq "PnP.Core.Model.SharePoint.PageWebPart") {
        # Check if text control is type of Image (CanvasControl)
        if ($textControl.Type.BaseType.FullName -eq "PnP.Core.Model.SharePoint.CanvasControl") {
            $webPartProps = ConvertFrom-Json $textControls.PropertiesJson
            if ($webPartProps.altText) { $webPartProps.altText = Start-Translation -text $webPartProps.altText -language $targetLanguage -tl $translator }
            if ($webPartProps.overlayText) { $webPartProps.overlayText = Start-Translation -text $webPartProps.overlayText -language $targetLanguage -tl $translator }
            $propertiesJson = ConvertTo-Json $webPartProps
            Set-PnPPageWebPart -Page $newPage -InstanceId $textControl.InstanceId -PropertiesJson $propertiesJson
            $a = "Set-PnPPageWebPart -Page $newPage -InstanceId $textControl.InstanceId -PropertiesJson $type"
        }
    }
    #Out-File -Encoding Ascii -FilePath $response -inputObject "{'message':'Page $a'}"
}
</code>```

Outcome of $a:
Set-PnPPageWebPart -Page PnP.Core.Model.SharePoint.Page -InstanceId PnP.Core.Model.SharePoint.PageWebPart.InstanceId -PropertiesJson {
"altText": "ein am Straßenrand geparktes Auto",
...
}

Body:
I want to translate and update the alt text of an image in my existing SharePoint Page. To do this, I have PowerShell code running which executes functions of the PnPPowerShell module.

The Start-Translation function works fine, but the Setter Set-PnPPageWebPart of the PnP module does not seem to work. Might this be a bug in the module? What do I miss?

$newPage = Get-PnPPage -Identity $identity

# Next, take all text controls and translate those
$textControls = $newPage.Controls
foreach ($textControl in $textControls) {
    $translatedControlText = ""
    # Is my Text Control type of Text
    if ($textControl.Type.FullName -eq "PnP.Core.Model.SharePoint.PageText") {
        # Logic
    } elseif ($textControl.Type.FullName -eq "PnP.Core.Model.SharePoint.PageWebPart") {
        # Check if text control is type of Image (CanvasControl)
        if ($textControl.Type.BaseType.FullName -eq "PnP.Core.Model.SharePoint.CanvasControl") {
            $webPartProps = ConvertFrom-Json $textControls.PropertiesJson
            if ($webPartProps.altText) { $webPartProps.altText = Start-Translation -text $webPartProps.altText -language $targetLanguage -tl $translator }
            if ($webPartProps.overlayText) { $webPartProps.overlayText = Start-Translation -text $webPartProps.overlayText -language $targetLanguage -tl $translator }
            $propertiesJson = ConvertTo-Json $webPartProps
            Set-PnPPageWebPart -Page $newPage -InstanceId $textControl.InstanceId -PropertiesJson $propertiesJson
            $a = "Set-PnPPageWebPart -Page $newPage -InstanceId $textControl.InstanceId -PropertiesJson $type"
        }
    }
    #Out-File -Encoding Ascii -FilePath $response -inputObject "{'message':'Page $a'}"
}

Outcome of $a:
Set-PnPPageWebPart -Page PnP.Core.Model.SharePoint.Page -InstanceId PnP.Core.Model.SharePoint.PageWebPart.InstanceId -PropertiesJson {
"altText": "ein am Straßenrand geparktes Auto",
...
}


问:
build error icedtea7-native using meta-java layer
I am getting a build error for icedtea7-native

make[6]: *** [../../../common/Library.gmk:225: /home/amodster/Yocto/poky-rpi3/build/tmp/work/x86_64-linux/icedtea7-native/2.1.3-r1.0/icedtea-2.1.3/build/openjdk.build-boot/lib/amd64/libj2pcsc.so] Error 1

make[6]: Leaving directory '/home/amodster/Yocto/poky-rpi3/build/tmp/work/x86_64-linux/icedtea7-native/2.1.3-r1.0/icedtea-2.1.3/build/openjdk-boot/jdk/make/sun/security/smartcardio'

More info when running make from command line

~/Yocto/poky-rpi3/build/tmp/work/x86_64-linux/icedtea7-native/2.1.3-r1.0/icedtea-2.1.3/build/openjdk-boot/jdk/make/sun/security/smartcardio$ make

Java sources to be compiled: (listed in file ../../../../build/linux-amd64/tmp/sun/sun.security.smartcardio/j2pcsc/.classes.list)

../../../../src/share/classes/sun/security/smartcardio/CardImpl.java

../../../../src/share/classes/sun/security/smartcardio/ChannelImpl.java

../../../../src/share/classes/sun/security/smartcardio/PCSCException.java

../../../../src/share/classes/sun/security/smartcardio/PCSC.java

../../../../src/share/classes/sun/security/smartcardio/PCSCTerminals.java

../../../../src/share/classes/sun/security/smartcardio/SunPCSC.java

../../../../src/share/classes/sun/security/smartcardio/TerminalImpl.java

../../../../src/solaris/classes/sun/security/smartcardio/PlatformPCSC.java

Running javac:

-sourcepath ../../../../build/linux-amd64/gensrc:../../../../src/solaris/classes:../../../../src/share/classes -bootclasspath ../../../../build/linux-amd64/classes: -d ../../../../build/linux-amd64/classes @../../../../build/linux-amd64/tmp/sun/sun.security.smartcardio/j2pcsc/.classes.list
/bin/sh: 7: -sourcepath: not found
make: *** [../../../../make/common/Rules.gmk:236: .compile.classlist] Error 127

I am checking if these exist
-sourcepath ../../../../build/linux-amd64/gensrc:../../../../src/solaris/classes:../../../../src/share/classes
ls ../../../../build/linux-amd64/gensrc ---this is not there---
ls ../../../../src/solaris/classes
ls ../../../../src/share/classes

Host: Ubuntu 22.04
Target: RPi3B+
Build System: Yocto / bitbake

First tried: bitbake icedtea7-native

Second tried: ~/Yocto/poky-rpi3/build/tmp/work/x86_64-linux/icedtea7-native/2.1.3-r1.0/icedtea-2.1.3/build/openjdk-boot/jdk/make/sun/security/smartcardio$ make