Continuing an interrupted batch

Question: I'm using a batch job in Drupal 8 to scrape pages from another site to import.
I'm using httpClient, and I have it wrapped in a try/catch.
try {
  $response = \Drupal::httpClient()->get($parts['scheme'] . '://' . $parts['host'] . $parts['path'], [
    'query' => $parts['query'],
    'headers' => array('Accept'=>'text/xml'),
    'verify' => FALSE,
    'connect_timeout' => 6,
    'CURLOPT_SSL_VERIFYPEER' => false,
    'CURLOPT_CERTINFO' => false,
  ]);
  return (string) $response->getBody();
}
catch (Exception $e) {
  watchdog_exception('scrape', $e);
  return FALSE;
}

However, from time to time the remote site times out, and the guzzle exception is not caught, which causes the batch job to end.
An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /batch?id=44&op=do_nojs&op=do
StatusText: Internal Server Error
ResponseText: The website encountered an unexpected error. Please try again later.GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 30001 milliseconds with 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in GuzzleHttp\Handler\CurlFactory::createRejection() (line 186 of vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php). GuzzleHttp\Handler\CurlFactory::finishError(Object, Object, Object) (Line: 103)

This job imports a few thousand pages, so when the error happens in the middle of the batch, it's a major pain.
I am getting ready to write a queue for the batch processor, where the batch pops a set of items off the queue and then marks them as done, so that I can continue the job after an exception.
But then I wondered, is there already a way to tell Drupal to continue a batch from where it left off or was interrupted? I did some googling and didn't find anything indicating that this feature already existed. I don't want to re-invent the wheel if I don't have to.
Can I re-start an incomplete, interrupted batch job?
Comment: were you able to figure this out, I am interested in the answer as well.
Comment: @awm I created a module that does batch queuing, I will try to release it shortly.
Answer: So I think there is no way to tell batch to restart when it's gotten an error response. What you could do is to not return a possible error but rather re-try the request using your own wrapper. So could do
try {

  $TestRetry = new TestRetry();
  return $TestRetry->test();
}
// And I think you won't need this but handle the exception in your wrapper class.
catch (Exception $e) {
  watchdog_exception('scrape', $e);
  return FALSE;
}

and in your wrapper, you could attempt to retry the request to avoid an error. So your wrapper may look like this:
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

class TestRetry {

  public function test() {
    $handlerStack = HandlerStack::create(new CurlHandler());
    $handlerStack->push(Middleware::retry($this->retryDecider(), $this->retryDelay()));
    $client = new Client(array('handler' => $handlerStack));

    $response = $client->request(
      'GET',
// @todo replace to a real url!!!
      'https://500-error-code-url'
    )->getBody()->getContents();

    return \GuzzleHttp\json_decode($response, TRUE);
  }

  public function retryDecider() {
    return function (
      $retries,
      Request $request,
      Response $response = NULL,
      RequestException $exception = NULL
    ) {
// Limit the number of retries to 5
      if ($retries >= 5) {
        return FALSE;
      }

// Retry connection exceptions
      if ($exception instanceof ConnectException) {
        return TRUE;
      }

      if ($response) {
// Retry on server errors
        if ($response->getStatusCode() >= 500) {
          return TRUE;
        }
      }

      return FALSE;
    };
  }

  /**
   * delay 1s 2s 3s 4s 5s
   *
   * @return Closure
   */
  public function retryDelay() {
    return function ($numberOfRetries) {
      return 1000 * $numberOfRetries;
    };
  }
}

Continuing an interrupted batch


Evaluating $\zeta\left(\frac{1}{2}\right)$ as an integral $ \zeta\left(\frac{1}{2}\right) = \frac{1}{2} \int_0^\infty \frac{[x]-x}{x^{3/2}} \, dx$

Question: I am reading the second chapter of Titchmarsh's book on the Riemann Zeta Function.  I would have written:
$$ \zeta\left(\frac{1}{2}\right) = 1 + \frac{1}{\sqrt{2}} + \frac{1}{\sqrt{3}} +  \dots = \infty $$
If you think about it for a moment.  This doesn't decay nearly fast enough, and so the sequence diverges.  Then I had to look up the actual definition of $\zeta(s)$ in the region $s = \sigma + it$ and $0 < \sigma < 1$.  We have:
$$ \zeta(s) = \left\{ \begin{array}{cl}
\sum \frac{1}{n^s} & \mathrm{Re}(s) > 1 \\ \\
s \int_0^\infty \frac{[x]-x}{x^{s+1}} dx  & 1 > \text{Re}(s) > 0
\end{array} \right. $$
Then if I evaluate at $s = \frac{3}{2} = \frac{3}{2} + 0i$ we use the second formula:
$$ \zeta\left(\frac{1}{2}\right) = \frac{1}{2} \int_0^\infty \frac{[x]-x}{x^{3/2}} \, dx = \sum_{n=0}^\infty \left[ 2 \sqrt{n} - 2 \sqrt{n+1} - \frac{1}{\sqrt{n+1}} \right]  \stackrel{?}{<} 0$$
Is this thing negative? Is $\zeta(\frac{1}{2}) < 0$.  The book overs several "analytic continuations" and I'm only looking at this first one, to make sure I understand.
Could someone help me evaluate the integral? I didn't use any fancy changes of variables.  The main step is:
$$  \int_0^\infty f(x) \,dx = \sum \int_{n}^{n+1} f(x) \,dx  = \int_0^1 f(x) \,dx + \int_1^2 f(x) \,dx + \dots $$
Comment: $\zeta(1/2)$ is negative, indeed (approximately $-1.4603545088095868128894991525152980125$).
Comment: @cactus314, what do you mean by "evaluate the integral"? $\zeta(1/2)$ is probably transcendental, so there's probably no nice closed form for it.
Comment: In any case, note that for $\Re(s) > 1$, partial summation allows us to write $\zeta(s) = s \int_{1}^{\infty} \lfloor x \rfloor x^{-s} \, \frac{dx}{x}$, which is $s \int_{1}^{\infty} x^{-s + 1} \, \frac{dx}{x} - s \int_{1}^{\infty} \{x\} x^{-s} \, \frac{dx}{x}$. The first term is $\frac{s}{s - 1}$ by evaluating the integral, while the second converges absolutely for all $\Re(s) > 0$. So this gives the analytic continuation $\zeta(s) = \frac{s}{s - 1} - s \int_{1}^{\infty} \{x\} x^{-s} \, \frac{dx}{x}$ valid for $\Re(s) > 0$ with $s \neq 1$.
Comment: Note that this is equal to $s \int_{0}^{\infty} \{x\} x^{-s} \, \frac{dx}{x}$ for $0 < \Re(s) < 1$. Anyway, taking $s = 1/2$ shows that $\zeta(\frac{1}{2}) = -1 - \frac{1}{2} \int_{1}^{\infty} \{x\} x^{-1/2} \, \frac{dx}{x}$; both terms are negative, so clearly $\zeta(1/2) < 0$. (In fact, it is easy to see that $\zeta(\sigma) > 1$ for $\sigma > 1$ and $\zeta(\sigma) < 0$ for $0 < \sigma < 1$ via this expression.)
Answer: Consider that for any $s$ such that $\text{Re}(s)>0$ we have:
$$ \eta(s)=\sum_{n\geq 1}\frac{(-1)^s}{n^s} = \left(1-\frac{2}{2^s}\right)\sum_{n\geq 1}\frac{1}{n^s} = \left(1-\frac{2}{2^s}\right)\zeta(s) $$
but the LHS is conditionally convergent for any $s$ such that $\text{Re}(s)>0$. This provides an analytic continuation:
$$\forall s:\text{Re}(s)>0,\qquad \zeta(s)=\left(1-\frac{2}{2^s}\right)^{-1}\sum_{n\geq 1}\frac{(-1)^{n+1}}{\Gamma(s)}\int_{0}^{+\infty}t^{s-1} e^{-nt}\,dt $$
and by the dominated convergence theorem:
$$\forall s:\text{Re}(s)>0,\qquad \zeta(s)=\frac{1}{\Gamma(s)}\left(1-\frac{2}{2^s}\right)^{-1}\int_{0}^{+\infty}\frac{t^{s-1}}{e^t+1}\,dt$$
such that:
$$ \zeta\left(\tfrac{1}{2}\right)=-\frac{2(1+\sqrt{2})}{\sqrt{\pi}}\int_{0}^{+\infty}\frac{du}{1+\exp(u^2)}\approx -1.46. $$
Comment: This is a different analytic continuation than the one I'm asking about. Certainly it gives the same answer. Here I am emphasizing resummation.
Answer: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

You can take advantage of the
  following identity:

\begin{align}
\zeta\pars{1 \over 2} & =
\sum_{k = 1}^{N}{1 \over \root{k}} - 2\root{N} - {1 \over 2}\int_{N}^{\infty}{x - \left\lfloor x\right\rfloor \over x^{3/2}}\,\dd x\,,\qquad N = 1,2,3,\ldots
\end{align}

Note that
  $\ds{0 < \verts{{1 \over 2}\int_{N}^{\infty}{x - \left\lfloor x\right\rfloor \over x^{3/2}}\,\dd x} < {1 \over \root{N}}}$ such that you don't need to evaluate the integral. Namely,

$$
\bbx{\zeta\pars{1 \over 2}  =
\lim_{N \to \infty}\pars{\sum_{k = 1}^{N}{1 \over \root{k}} - 2\root{N}}}
$$


Find maximum from combination of two tables (for-loop too slow)

Question: I have a data table "the.data", where the first column indicate a measurement instrument, and the rest different measured data.
instrument <- c(1,2,3,4,5,1,2,3,4,5)
hour <- c(1,1,1,1,1,2,2,2,2,2)
da <- c(12,14,11,14,10,19,15,16,13,11)
db <- c(21,23,22,29,28,26,24,27,26,22)
the.data <- data.frame(instrument,hour,da,db)

I also have defined groups of instruments, where for example group 1 (g1) refers to instruments 1 and 2.
g1 <- c(1,2)
g2 <- c(4,3,1)
g3 <- c(1,5,2)
g4 <- c(2,4)
g5 <- c(5,3,1,2,6)
groups <- c("g1","g2","g3","g4","g5")

I need to find out at which hour the sum of each group has maximum per data type, and its sum.
g1 hour 1: sum(da)=12+14=26
g1 hour 2: sum(da)=19+15=34
So, for g1 and da the answer is hour 2 and value 34.
I did this with a for-loop within a for-loop, but it takes too long time (I interrupted after a few hours). The issue is that the.data is about 100.000 rows long and that there are about 5.000 groups with 2-50 instruments each.
What can be a good method to do this?
Sincere thanks to all contributors to Stack-overflow.
Update: Now only five groups in examples.
/Chris
Answer: The group loop will have to stay, or at best be replaced by something like lapply(). The hour loop, however, can be totally replaced by reformatting to an instrument x hour matrix and then just doing vectorized algebra. For example:
library(reshape2)

groups = list(g1, g3)

the.data.a = dcast(the.data[,1:3], instrument ~ hour)

> sapply(groups, function(x) data.frame(max = max(colSums(the.data.a[x, -1])),
                                        ind = which.max(colSums(the.data.a[x, -1]))))
    [,1] [,2]
max 34   45
ind 2    2

Comment: It runs with two groups but throws an error for me with five groups.
Comment: @DWin That's cause only instruments 1-5 are in the example data. The other groups reference instruments that aren't present.
Comment: Thanks for quick and very good answer. From reading here, I realized that I had missed some instruments in "the.data" and corrected, but it turned out that my real data had missing measurements too (in Hour, not in Instrument).
Comment: @Chris Great, glad it helped. Good luck with your project!
Answer: Here's a slightly modified version of John Colby's answer, with some sample data.
set.seed(21)
instrument <- sample(100, 1e5, TRUE)
hour <- sample(24, 1e5, TRUE)
da <- trunc(runif(1e5)*10)
db <- trunc(runif(1e5)*10)
the.data <- data.frame(instrument,hour,da,db)
groups <- replicate(5000, sample(100, sample(50,1)))
names(groups) <- paste("g",1:length(groups),sep="")

library(reshape2)
system.time({
the.data.a <- dcast(the.data[,1:3], instrument ~ hour, sum)
out <- t(sapply(groups, function(i) {
  byHour <- colSums(the.data.a[i,-1])
  c(max(byHour), which.max(byHour))
}))
colnames(out) <- c("max.hour","max.sum")
})
# Using da as value column: use value.var to override.
#    user  system elapsed
#    3.80    0.00    3.81

Comment: Nice example, Josh! I'm always curious how fast we can get these things.
Answer: Here's one approach using plyr and reshape2 from Hadley. First, we'll add some boolean values to the.data depending on whether or not the instrument is in that group. Then we'll melt it into long format, subset out the rows we don't need, and then do a group by operation with ddply or data.table.
#add boolean columns
the.data <- transform(the.data,
                      g1 = instrument %in% g1,
                      g2 = instrument %in% g2,
                      g3 = instrument %in% g3,
                      g4 = instrument %in% g4,
                      g5 = instrument %in% g5
                      )

#load library
library(reshape2)
#melt into long format
the.data.m <- melt(the.data, id.vars = 1:4)
#subset out data that that has FALSE for the groupings
the.data.m <- subset(the.data.m, value == TRUE)

#load plyr and data.table
library(plyr)
library(data.table)

#plyr way
ddply(the.data.m, c("variable", "hour"), summarize, out = sum(da))
#data.table way
dt <- data.table(the.data.m)
dt[, list(out = sum(da)), by = "variable, hour"]

Do some benchmarking to see which is faster:
library(rbenchmark)
f1 <- function() ddply(the.data.m, c("variable", "hour"), summarize, out = sum(da))
f2 <- function() dt[, list(out = sum(da)), by = "variable, hour"]

> benchmark(f1(), f2(), replications=1000, order="elapsed", columns = c("test", "elapsed", "relative"))
  test elapsed relative
2 f2()    3.44 1.000000
1 f1()    6.82 1.982558

So, data.table is about 2x faster for this example. Your miles may vary.
And just to show that it's giving right values:
> dt[, list(out = sum(da)), by = "variable, hour"]
      variable hour out
 [1,]       g1    1  26
 [2,]       g1    2  34
 [3,]       g2    1  25
 [4,]       g2    2  29

...

Comment: I don't think your code is handling the choices of max and which.max yet.
Comment: @Dwin - doh, you're right! I misread / glossed over that earlier, will update in a bit. Thanks, -chase
Answer: You didn't provide your code (or a programmatic way to generate the groups, which would seem to be needed with a group count of 5000)  but this may be a more effective use of R:
groups <- list(g1,g2,g3,g4,g5)
gmax <- list()
# The "da" results
for( gitem in seq_along(groups) ) {
       gmax[[gitem]] <- with( subset(the.data , instrument %in% groups[[gitem]]),
                               tapply(da , hour, sum) ) }
damat <- matrix(c(sapply(gmax, which.max),
                  sapply(gmax, max)) , ncol=2)

# The "db" results
for( gitem in seq_along(groups) ) {
       gmax[[gitem]] <- with( subset(the.data , instrument %in% groups[[gitem]]),
                               tapply(db , hour, sum) ) }
dbmat <- matrix(c(sapply(gmax, which.max),
                  sapply(gmax, max)) , ncol=2)

#--------
> damat
     [,1] [,2]
[1,]    2   34
[2,]    2   29
[3,]    2   45
[4,]    1   14
[5,]    2   42
> dbmat
     [,1] [,2]
[1,]    2   50
[2,]    2   53
[3,]    1   72
[4,]    1   29
[5,]    1   73


How do I ouput a node reference field using a node reference template in Drupal 7?

Question: In drupal 6 you can create templates to control how node reference fields are output on a content type basis using the suggestion below:
node-nodereference-exampletype.tpl.php

and then output a node reference field that references 'exampletype' nodes in a node template like so:
$node->examplenodereference[0]['view'];

The file naming conventions and output syntax have changed in drupal 7.
Working off the information at this link http://drupal.org/node/998848 I have tried template suggestions like these with no luck:
node--nodereference--exampletype.tpl.php
node-nodereference--exampletype.tpl.php
node--nodereference-exampletype.tpl.php
node-node-reference-exampletype.tpt.php
and so on...

I have tried adjusting the display mode for the field from teaser to full content etc.
I have tried outputting the field with:
print render($content['field_example']);

and
print $field_screening_movie_title;

and variations on the above.
What is the correct way to name a node reference template file in drupal 7? Do I need to use the render function to output a node reference field using the template?
Thanks
Comment: how do you create node reference? with entity reference module? or something else?
Comment: The node references have been created with the 'References' module http://drupal.org/project/references
Answer: This module has one display formatter named Rendered node, in its setting you can select view mode which referenced node will render in that view mode.
so you can define new view mode and configure referenced node in that view mode
have a look at this post: http://mearra.com/blogs/juha-niemi/drupal-7-custom-node-view-modes


Adding a class to code blocks generated by knitr

Question: When I knit my R markdown file to HTML via the RWordPress package, the formatting of the
 and  tags is disrupted because of other styling/plugins (I think Crayon syntax highlighter is the biggest culprit, but I'm not willing to part with it). One simple solution might be to add a class to every
 and  tag generated by Knitr, so that they can be styled separately with some CSS, but I can't determine an easy way to do this. Do any Knitr experts know how this could be done automagically? Other solutions are welcome if they are similarly simple.
EDIT: To possibly clarify, I think that what I need to be doing is overriding the default 'source' hook generated by render_html() and add a new tag that way, but I'm struggling to figure out how by reading the documentation or the examples.
Comment: I think i got your issue.. but still little unclear. cant you provide a sample code or fiddle.. ?
Answer: You could use jQuery inside your Rmd document (which will be rendered to HTML) to add a class to every source code chunk:

This snippet adds the CSS class .yourClass to every
 element that already carries the class .r.
If you want to modify the  child of those elements use

From here on it is up to you how you style your yourClass code chunks with CSS.
Comment: Not what you wanted?


How to update the changes to this active record into the associated database table Yii2

Question: I need to update data in afterSave() method.
SummaryField is ActiveRecord class
I'm trying to use this code:
public function afterSave($insert, $changedAttributes)
{
    $sumField = new SummaryField();
    if ($insert==true)
    {
        $sumField->field_id = $_POST['Scientist']['field_name'];
        $sumField->scientist_id = $this->scientist_id;
        $sumField->save();
    }
    else
    {
        $sumField = SummaryField::findOne($this->scientist_id);
        $sumField->field_id = $_POST['Scientist']['field_name'];
        $sumField->scientist_id = $this->scientist_id;
        $sumField->update();
    }
}

So, save() method are working, but when we call ELSE statement:
Creating default object from empty value

on this  $sumField->field_id = $_POST['Scientist']['field_name'];
I can't use new stdClass();
Comment: seems findOne don't get the related model .. try var_dump($this->scientist_id) ..
Comment: Are you sure you have a id = 47 in your SummaryField table ?? check it plaese .. otherwise  .. check the content fo $_POST..
Answer: could be you must define the correct query  for find ..
try indicating the column_name  .. this way
$sumField = SummaryField::find()->where(['scientist' => $this->scientist_id])->one();


Setting default setting for all jQuery-UI datepicker instances

Question: I am using jQuery-UI Datepicker (via ZendX view helper) to select dates. Now I would like to set default settings (setDefaults) which sets default setting for all Datepicker instances.
This is how my form looks like:
<code><form method="post" action="/hello/world">
  <?php
    echo $this->datePicker("dp1",'',array(
                                        'defaultDate' =>date('Y m d',time()),
                                        'showWeek' => 'true',
                                        'firstDay' => 1,
                                        'dateFormat' => 'dd.mm.yy',
                                        'changeYear' => 'true',
                                        'changeMonth' => 'true'
                                        ));

    echo $this->datePicker("dp2",'',array());
  ?>
    <input type="submit" value="Submit" />
</form>
</code>
Does someone know how to set this when using Zend Framework?
Thanks, Udo
Answer: You could extend the DataPicker View Helper class (or what ever it is called) and create your own constructor. Then use that custom class...


JavaFX: Binding a property to a label does not update

Question: I'm trying to bind the value of an IntegerProperty (that I cast into a StringProperty in my getter) to a javafx label. However, the value does not change.
In the Head of my class:
private IntegerProperty totalMessaged;

In the constructor of the class:
this.totalMessaged = new SimpleIntegerProperty(0);

The getter from the class:
public StringBinding getTotalMessaged() {
    return this.totalMessaged.asString();
}

In the method that I use to increade the property:
this.totalMessaged.add(1);

In the controller class:
@FXML
private void initialize() {
    this.sentLabel.textProperty().bind(ClientHandler.getInstance().getTotalMessaged());
}

When I start the program the label will be set to 0, so the binding first seems to work. However, when it calls the method to increase the property, the property does not increase by 1, it stays 0 - the label doesn't change and if I print out the value to the console after using this.totalMessaged.add(1); it'll still say that this property is 0.
What did I do wrong?
Answer: Try this way. Use converter to convert form String to Integer.
In controller:
sentLabel.textProperty().bindBidirectional(ClientHandler.getInstance().totalMessagedProperty(), new NumberStringConverter());

In model:
private final IntegerProperty totalMessagedProp = new SimpleIntegerProperty(0);

public int getTotalMessaged() {
    return totalMessagedProp.get();
}

public IntegerProperty totalMessagedProperty() {
    return totalMessagedProp;
}

public void setTotalMessaged(int totalMessaged) {
    this.totalMessagedProp.set(totalMessaged);
}

Comment: Thanks for your answer, the NumberStringConverter came in handy later, but my problem was another that you couldn't see from the source i offered. I didn't user Platform.runlater in the task in which I increased the value, that's why it didn't work somehow. Totally my bad, but thank you anyway.


multiple markers shows in google map in php codeigniter

Question: How to execute the php foreach loop in java script? i need to loop latitude_code & longitude_code in java script
this line should be in loop
var centerLatLng = new google.maps.LatLng(latitudes,longitudes);
please suggest me..
thanks for advance..
Answer: in your view;

Comment: Hi Craig, Thanks for quick rply.. here i cant see the value by alert See take down the code as below
      var myLatlng = new google.maps.LatLng(latitude; ?>,longitude; ?>);

 alert(myLatlng);
Answer: Here's [demo](http://jsfiddle.net/2crQ7/),I think it will help you, in this example java script array is used and you are using php array.Just use your array for looping, don't pass location in setMarkers(map,locations) function .
Try with this example if you don't get answer I will provide my code.


DropDownMenu not showing when click in compose

Question: I have an image and I want to show a dropdownMenuItem when user click in the image. I was debugging the app and I can see that the code go through the DropdownDemo method but is not showing anything.
Am I doing something wrong?
Click code:
@Composable
fun Header(currentItem: CartListItems) {
    var showDialog by remember { mutableStateOf(false) }

    Box(Modifier.fillMaxWidth()) {
        Text(modifier = Modifier.align(Alignment.TopStart),
            text = currentItem.type,
            color = colorResource(id = R.color.app_grey_dark),
            fontSize = 12.sp)
        Image(painter = painterResource(R.drawable.three_dots),
            contentDescription = "more options button",
            Modifier
                .align(Alignment.CenterEnd)
                .width(24.dp)
                .height(6.75.dp)
                .clickable(indication = null,
                    interactionSource = remember { MutableInteractionSource() },
                    onClick = {
                        showDialog = true
                    }))

        if(showDialog) {
            DropdownDemo()
            showDialog = false
        }
    }
}

Dropmenu:
@Composable
fun DropdownDemo() {
    var expanded by remember { mutableStateOf(false) }
    val items = listOf("A", "B", "C", "D", "E", "F")
    val disabledValue = "B"
    var selectedIndex by remember { mutableStateOf(0) }
    Box(modifier = Modifier
        .fillMaxSize()
        .wrapContentSize(Alignment.TopStart)) {
        Text(items[selectedIndex],modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = { expanded = true })
            .background(
                Color.Gray
            ))
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.Red
                )
        ) {
            items.forEachIndexed { index, s ->
                DropdownMenuItem(onClick = {
                    selectedIndex = index
                    expanded = false
                }) {
                    val disabledText = if (s == disabledValue) {
                        " (Disabled)"
                    } else {
                        ""
                    }
                    Text(text = s + disabledText)
                }
            }
        }
    }
}

Comment: is `showDialog` a `mutableStateOf` object?
Comment: yes i put it to can open call the compose method.
Answer: showDialog appears to be a MutableState object. Hence, when the image is clicked, it becomes true, and a recomposition is triggered, after which the conditional block is executed, displaying the DropDownMenu. However, in the very next line. You equate showDialog to false, re-trigerring a recomposition, and rendering the DropDownMenu collapsed.


How do brass instrument tune to non-fundamental notes?

Question: Orchestras tune to A.  But a trombone in 1st position can only play Bb or F.  A trombone would need to be in 2nd position to play that A.  How does the trombonist know whether to adjust the tuning slide or adjust his/her usage of 2nd position?
Likewise for a C or F tuba: In a wind ensemble or brass band tuning to Bb, the C or F tuba will need to hold the 1st valve.  How does the tuba player know whether to adjust the main tuning slide or the 1st valve slide?
Comment: My experience with school concert band is that the conductor whips out a tuner, then gets everyone in tune with the Bb (but the percussionists not playing timpani), starting with the oboist(s). This takes a few minutes but may require multiple pass-throughs. I believe this even happened on stage in school recitals (and maybe even out-of-town festivals and contests). I don't know how brass band tuning processes work, sorry.
Comment: @Dekkadeci - usually, in the big bands I play in, the conductor will ask me (on keys) to play Bb. Sometimes he goes round individually with an electronic tuner. And it's always Bb, not A.
Comment: I have a feeling that both the trombonist and the tuba player would rather be tuning to (the concert band choice of) Bb than A, and any attempts to tune to A result in a high risk of the easier-to-play Bb being out of tune.
Comment: I've never heard an Oboe play a Bb before the conductor arrives at an orchestra. Is it likely that the brass section tunes the Bb before arriving in the concert hall?
Comment: Bb makes complete sense for that type of band (any band without bowed strings would use Bb), but it still poses the problem for the **C** or **F** tuba player.
Comment: "a trombone in 1st position can only play Bb or F": or (high) D.
Answer: In Brass bands the instruments are tuned by a given Bb => playing C or G (Eb instruments).
In the orchestra  I actually don't know:

The oboist can give them a Bb or F  to tune their zero position or they will be able to take the Bb by ear a semitone higher than the given A.

They can tune with the trumpets giving them an concert Bb
Comment: Oh, I never thought of the trumpets relaying a note to the trombones!  The 2nd valve is super short and doesn't have much room to move so I can see how there would be not much error introduced here.  It's very plausible.  I wonder if this really happens.
Comment: For brass bands, neither a **C** nor an **F** tuba can play a concert-**Bb** without pressing a valve (hence the second part of the question).
Comment: Yes, that's what I mean, trumpets play 2nd valve to tune with A and then play zero to tune with trombones the Bb.
Comment: I've never played in an orchestra, but that's the way I would tune them.
Answer: Orchestral trombonists happily tune to A.   We can refer it to the 1st position D, in much the same way that a violinist doesn't need to be given different notes for his other three strings!  And we know where 2nd position A is on our slides.
Also, our tuning slides aren't reset to zero when the instrument goes in the case.  If it was in the right place for A=440 yesterday, it still will be today.
Don't argue unless you've BEEN an orchestral trombonist.  I have .
Comment: Ah, tuning to the fourth!  I also didn't think about tuning that high.  The F-tubist could natively tune to the 5th of a Bb even easier than the fourth and has access to the A higher up.  The C-tubist might still have a tough time tuning to Bb.  Would only having access to the 2nd (C), 6th (G), and tritone (E) be too dissident for tuning?
Comment: Tuning a violin to a, say, C♯ reference would definitely be pretty problematic. Tuning to the fingered note would be very unreliable (sure the position is well-trained, but on-the-fly adjustments to intonation are anyway common. For tuning the _string_, you would be messed up. And tuning to the major third down would open a can of just-vs-12edo-vs-Pythagorean worms, as well as cause major distraction in the string section.The reason for "a violinist doesn't need to be given different notes for his other three strings" is that they're in _fiftths_ (though even that is a meantone minefield).
Comment: Yes.  That's why A to D works so well on trombone.  Though, to be honest, we could learn to use any other interval from any other note.
Comment: I think the 4th and 5th are particularly easy to tune on because of their resonance.  The easiest thing to tune is obviously the octave (2:1), but the fifth is (3:2) and the fourth is (4:3).  Other notes get quite dissident (2nd = 9:8), so it takes longer for the wavelengths to align making it harder to hear the "wah wah"s  and that makes them tough to compare to the tonic.  I think asking someone to tune the 2nd would be asking too much.
Answer: In a symphony orchestra almost every every player is able to fine-tune the pitch of notes while playing. The absolute exceptions are the piano (if used), harp, open notes on stringed instruments, and tuned percussion. The oboeist gets to maintain a stable note (A) for everyone else to tune to. String players tune open notes. Wind players including brass players know what concert A 'feels' like when it is centred.
On brass instruments the valve tuning slides generally don't need much adjustment, especially if players are always playing at the same pitch in an air-conditioned concert hall. If it is not quite right the player would be able to 'lip' it into tune. A good trombonist knows where the positions of the instrument are and will be able to get A reliably. If the tuning slide is not quite right a good player will use lip and slide to adjust.
When I played in junior orchestras many years ago, some conductors used the oboe to give a B-flat for the brass, and some expected all players to tune to A. Some would check by asking all brass players to play (concert) B-flat. I never came across a conductor who would do anything special to help the player of the G bass trombone.
Comment: Blimey!  In what sort of orchestra (and how long ago) did you encounter a G bass trombone?
Comment: This was a student orchestra in Melbourne (AU) in the early 1970s. The bass trombone may have belonged to the player's school.
Comment: @LaurencePayne - off topic, but I'm curious: heve you ever encountered a BBb bass trombone, an octave lower than a normal tenor, with a doubled slide?  I saw one once at a Renaissance Faire many years ago.


Prob. 14, Sec. 30, in Munkres' TOPOLOGY, 2nd ed: The product of a Lindelof space and a compact space is a Lindelof space

Question: Here is Prob. $14$, Sec. $30$, in the book Topology by James R. Munkres, 2nd edition:

Show that if $X$ is Lindelof and $Y$ is compact, then $X \times Y$ is Lindelof.

Here is my Math Stack Exchange post on this very problem.
In the present post, I seek to give an alternative proof.
My Attempt:

Let $\pi_1 \colon X \times Y \longrightarrow X$ and $\pi_2 \colon X \times Y \longrightarrow X$ be the projection mappings defined by
$$
\pi_1 ( x \times y) := x \qquad \mbox{ and } \qquad \pi_2 ( x \times y) := y.
$$
These two mappings are continuous open maps.

Let $\mathscr{A}$ be any open covering of $X \times Y$ by  the basis elements for the product topology of the form $U \times V$, where $U$ is open in $X$, and $V$ is open in $Y$.

This open covering $\mathscr{A}$ yields the open coverings $\mathscr{A}_X$ and $\mathscr{A}_Y$ of $X$ and $Y$ respectively, given by
$$
\mathscr{A}_X := \left\{ \pi_1 (A) \colon A \in \mathscr{A} \right\} \qquad \mbox{ and } \qquad \mathscr{A}_Y := \left\{ \pi_2 (A) \colon A \in \mathscr{A} \right\}.
$$
Since $X$ is Lindelof and $Y$ is compact, some countable subcollection
$$
\left\{ \pi_1 \left( A_1 \right), \pi_1 \left( A_2 \right), \pi_1 \left( A_3 \right), \ldots \right\}
$$
of $\mathscr{A}_X$ covers $X$, and some finite subcollection
$$
\left\{ \pi_2 \left( A_1^\prime \right), \ldots, \pi_2 \left( A_N^\prime \right) \right\}
$$
of $\mathscr{A}_Y$ covers $Y$.

Therefore, the collection
$$
\left\{ A_1, A_2, A_3, \ldots \right\} \cup \left\{ A_1^\prime, \ldots, A_N^\prime \right\}
$$
is a countable subcollection of $\mathscr{A}$ that also covers $X \times Y$. Thus, it follows that $X \times Y$ is Lindelof.

Is this proof correct? If so, is my write-up of the proof clear enough too? Or, is there something wrong?
PS:
I think I can figure out where I have gone wrong.

As
$$
X = \bigcup_{n=1}^\infty \pi_1 \left( A_n \right),
$$
so we have
\begin{align}
X \times Y &= \pi_1^{-1} (X) \\
&= \pi_1^{-1} \left( \bigcup_{n=1}^\infty \pi_1 \left( A_n \right) \right) \\
&= \bigcup_{n=1}^\infty  \pi_1^{-1} \left( \pi_1 \left( A_n \right) \right) \\
&\supset \bigcup_{n=1}^\infty  A_n, \qquad \tag{1}
\end{align}
and since the mapping $\pi_1 \colon X \times Y \longrightarrow X$ need not be injective, therefore the inclusion in (1) above need not be an equality, thus showing that the sets $A_1, A_2, A_3, \ldots$ need not cover $X \times Y$.

A similar argument shows that the sets $A_1^\prime, \ldots, A_N^\prime$ need not cover $X \times Y$.

Am I right?
Comment: If this proof is valid then it seems to me that along the same lines it can be proved that the product of two Lindelof-spaces is again Lindelof. This however is not the case (see the answer of Henno on the linked question).
Comment: @user264745 See my answer on the question.
Comment: @Vera but where exactly OP is doing wrong. I'm just curious. I think in construction of $\mathscr{A}_X$ and $\mathscr{A}_Y$.
Answer: Your proof does not work.

Therefore, the collection
$$
\left\{ A_1, A_2, A_3, \ldots \right\} \cup \left\{ A_1^\prime, \ldots, A_N^\prime \right\}
$$
is a countable subcollection of $\mathscr{A}$ that also covers $X \times Y$.

The collection is indeed countable but there is no guarantee that it covers $X\times Y$.
For each point $(x,y)\in X\times Y$ we can find sets $A_i=U_i\times V_i\in\mathcal A$ and $A_j^{\prime}=U_j^\prime\times V_j^\prime\in\mathcal A$ such that $x\in\pi_1(A_i)=U_i$ and $y\in\pi_2(A_j^{\prime})=V_j^\prime$.
So $(x,y)\in U_i\times V_j^\prime$, but there is no guarantee that $U_i\times V_j^\prime\in\mathcal A$.
Comment: @user264745 You might need covered eyes to see that there is no covering :-).
Comment: This tiny mistake are not visible by naked eyes.
Comment: @Vera thank you so much for taking time answering my question so diligently! Can you please also take a look at my PS?
Comment: What you are saying in your PS is correct. In general we have $B\subseteq f^{-1}(f(B))$ with equality if $f$ is injective.


Angular JS Express JS File Upload throwing error, undefined is not a function

Question: This is the error I'm getting on the console
TypeError: undefined is not a function
    at C:\nodefiles\new\server.js:101:16
    at Layer.handle [as handle_request] (C:\nodefiles\new\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\nodefiles\new\node_modules\express\lib\router\route.js:131:13)
    at done (C:\nodefiles\new\node_modules\multer\lib\make-middleware.js:36:7)
    at indicateDone (C:\nodefiles\new\node_modules\multer\lib\make-middleware.js:40:51)
    at C:\nodefiles\new\node_modules\multer\lib\make-middleware.js:142:11
    at WriteStream.<anonymous> (C:\nodefiles\new\node_modules\multer\storage\disk.js:43:9)
    at WriteStream.emit (events.js:129:20)
    at finishMaybe (_stream_writable.js:484:14)
    at afterWrite (_stream_writable.js:362:3)
    at onwrite (_stream_writable.js:352:7)
    at WritableState.onwrite (_stream_writable.js:105:5)
    at fs.js:1799:5
    at FSReqWrap.strWrapper (fs.js:568:5)

This is my HTML page
<!doctype html>

    <meta charset="utf-8">
    <title>Untitled Document</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">

Upload on file select

<button ngf-select="uploadFiles($files)" multiple
        accept="image/*" ngf-max-height="1000" ngf-max-size="1MB">
    Select Files</button>

Files:

{{f.name}} {{f.$error}} {{f.$errorParam}}
      = 0">

{{errorMsg}}

This is my controller
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.uploadFiles = function(files) {
        $scope.files = files;
        angular.forEach(files, function(file) {
            if (file && !file.$error) {
                file.upload = Upload.upload({
                    url: '/api/data',
                    file: file
                });

                file.upload.then(function (response) {
                    $timeout(function () {
                        file.result = response.data;
                    });
                }, function (response) {
                    if (response.status > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                });

                file.upload.progress(function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            }
        });
    }
}]);

This is my code on the server side
var multer = require('multer');
var upload = multer({
    dest: __dirname + '/public',
});

app.post('/api/data', upload.single('file'), function (req, res, next) {
    console.log("We are here fellas");
    return res.ok();
});

The problem I think is because of that single thing mentioned in the end where I have written the code at the backend. I am following several tutorials because I am new to this subject.
I am following the github of Danial Farid, he made that nf-fileupload thing.
Please help me how to resolve this error.

Sorry if I sound stupid, I am new to this.

I don't know if this is necessary but this is the error I get on my front end after selecting the files (In Google Chrome Developer Console)
angular.js:10661 POST http://localhost:1339/api/data 500 (Internal Server Error)

Comment: Whats on this line at C:\nodefiles\new\server.js:101:16?
Comment: return res.ok(); this was there now I have commented it and the code is working properly. Thanks you everyone.
Answer: The following line is wrong:
return res.ok();

Thats not an expressjs or nodejs function of the response object.
Should be something like:
res.status(200).send('OK');

Comment: No problem. glad to help.


Beta-Binomial regression or Poisson-Gamma model to account for uncertainty in (empricial Bayesian) prior? Explained in simple terms?

Question: I have a dataset of $m$ individuals. For each individual $m$ I have $n_m$ (binomial ) observations with $s_m$ corresponding to the number of 'successes'.
I use this data to fit a beta-binomial Bayesian model (with a conjugate prior) and update it likewise. This way I can estimate $p_m$ (probability of success) for a (possible new) individual given the observations (if existing) on that (new) individual.
However, on many individuals in my dataset I have only very few observations. These are uncertain records in my dataset. However, there is a relation between the number of observations and $p_m$. So, I should somehow also take into account these 'uncertain individuals' in my dataset. In other words: individuals with a low number of observations tend to have lower $p_m$'s (which I have proven to be true for my specific dataset). If I cut those individuals out (in order to get a more certain dataset) I am getting biased outcomes.
I am thinking about using either Beta-Binomial regression, or fitting in a Poisson-Gamma model as well. However, $n_m$ (number of observations on individual $m$) can grow towards infinity, and, well, I don't know if this is the best way to go? (Overdispersion?)
I don't have much experience with these kinds of models, so I would like to ask what could be a good way forward (Beta-Binomial regression?), and if someone could explain this using my example in simple terms? Thank you!
Comment: I would go with the beta binomial
Comment: Here is a tutorial I wrote for the beta binomial model in pymc3. https://docs.pymc.io/notebooks/GLM-hierarchical-binominal-model.html
Comment: Ok.  Let me code something up.
Comment: @jkm I mean the lifetime value. My data is always finite (to give an idea, there are about 60,000 individuals in my set, of which ~10 people with > 100,000 observations, ~700 people with > 10,000 observations, ~4000 people with > 1000 observations. About ~40,000 people have less than 100 observations, and about ~10,000 have less than 10)
Comment: Thank you Demetri! However, I am still a bit in the dark about how to actually implement the beta binomial regression for my problem (trying to account for the uncertainty in my dataset). Would you mind writing an answer for my question in which you could explain how to implement this model, beta-binomial with beta-binomial regression, for my problem? I know how to implement a simple Bayesian conjugate prior for a beta-binomial distribution (beta prior, binomial likelihood)
Comment: What software are you using to perform the beta binomial regression?
Comment: I will write it in Python. Either by using libraries, or writing all code myself.
Comment: Maybe I am not looking right - but did you also model the number of observations? What I mean is: there is a relationship between the number of observations and the number of successes corresponding to individuals in my dataset. Individuals with a low number of observations tend to have a smaller probability of performing a certain action. But they are uncertain (since there are only a few observations on those individuals), which is the problem.
Comment: No, but that seems like a very natural extension.  You can model the log odds of the probability of a success as a function of the number of observations.
Comment: Okay, but exactly that is what I need a little noob-proof help with. I already implemented the Beta-Binomial myself. This extension, for example modelling the log odds of the probability of a success as a function of the number of observations, like you said, I can not seem to get my hands on. This is why I started this question here. If you could possibly answer my question here with a noob-proof explanation of exactly what you just told me, I would be extremely happy to mark it as an answer! Sorry for asking.
Comment: @DamiaanReijnaers when you say the number of observations for a person can grow towards infinity, do you mean that for the data you're fitting on, or what you want to estimate (i.e. lifetime value with no stopping conditions)?
Answer: OK, so this isn't the greatest implementation, but I think it serves our purpose.
The beta binomial regression makes the assumption that the data are generated from a binomial distribution
$$ y_i\vert p_i, \kappa,  a, b \sim \operatorname{Binomial}(p_i, n) $$
Each person's probability of success is assumed to be iid Beta
$$ p_i \vert \kappa, a, b \sim \operatorname{Beta}(\mu\kappa, (1- \mu)\kappa)$$
Here, $\mu= \operatorname{inv-logit}(a + bn)$, so we are modelling the log odds of the mean of our Beta as a function of the number of times we observe each person.  Now, we are free to put priors on $a$ an $b$.
Here is an implementation of this model in python.
<code>import numpy as np
import pandas as pd
import pymc3 as pm
import theano.tensor as tt
from scipy.special import expit
import arviz as az
import matplotlib.pyplot as plt

#make data

N = 100
ids = np.arange(N)
n = np.random.randint(low=1, high=100, size = N)

true_betas = np.array([-2,0.05])
X = np.c_[np.ones_like(n),n]
true_mean = expit(X@true_betas)
true_kappa = 50

true_p = np.random.beta(true_mean*true_kappa, (1-true_mean)*true_kappa)
y = np.random.binomial(n=n, p = true_p)

with pm.Model() as model:

    intercept = pm.Normal('intercept',mu=0, sd=1)

    slope = pm.Normal('slope',mu=0, sd=1)

    mean = pm.math.invlogit(intercept + n*slope)

    ps = pm.Beta('ps', mean*50, (1-mean)*50, shape=N)

    Y = pm.Binomial('Y',n = n, p=ps, observed = y)

    trace = pm.sample(target_accept=0.9)

    data = az.from_pymc3(trace)
</code>
In the model, I haven't put a prior on $\kappa$ but you should.  You also should use better priors, I just chose these for exposition.
Model recovers the predicted probabilities quite well (see below).

This is far from a complete bayesian analysis, but I think it should help you get started.  Remember to choose priors justifiably and to check your model over and over.
EDIT:

What I do not understand: what do the second and third parameters in your pm.Beta() call mean?

This is just a different parameterization of the Beta distribution.  $\mu$ is the mean of the beta and $\kappa$ (which I have set to 50 for illustrative purposes) is a measure of dispersion.  When $\kappa$ is large, then the Beta is less variable.
Comment: @DamiaanReijnaers See my edit
Comment: @DamiaanReijnaers That's am Empirical Bayes technique.  I'm familiar with David's work but not familiar enough with the technique to be of mite use beyond this point.
Comment: @DamiaanReijnaers Kappa is a postitive parameter, so you would have to model kappa like $\log(\kappa) = \beta_0 + \beta_1x_1 + \beta_2x_2$
Comment: Thank you for this thorough explanation. I am starting to understand it. What I do not understand: what do the second and third parameters in your pm.Beta() call mean? And why are they multiplied with 50? What exactly do the variables (especially kappa?) represent in your model? Sorry for the many questions!
Comment: I'm starting to understand, but doing something wrong. What I do is fitting a Beta dist to the probabilities of individuals with n>=100 (by minimizing the neg-log-likelihood of the Beta's pdf), not taking n into account (my alpha=3.76;beta=10.31). I use my estimated value for Beta as the value for Kappa. I estimate the slope and intercept and find a new Beta distribution for each possible value of n. Is that correct? I think I should use my alpha as well? The results seem to be very wrong. Should I do something with 'ps'?Am I now correctly 'putting a prior on kappa' by using Beta from my fit?
Comment: I don't think the process you describe is Bayesian.
Comment: This method is inspired by a book by David Robinson who -tackling a similar problem- gives a similar answer like yours, and writes: "it's ok to momentarily forget we're Bayesians - we picked our alpha and beta using maximum likelihood, so it's OK to fit these using a maximum likelihood approach as well". Your answers differ however in that he divides mu and (1-mu) by kappa, and you multiply them. He obtains a, b and kappa using maximum likelihood.
Comment: Hi again! I want to thank you again for your help. After these months I came up with a new question: how can I account for more than one additional parameter, or, in other words; how can I write kappa not only in terms of mu, but in terms of more parameters as well? Should I just 'repeat' the process?


Eigenvalues, stability, Granger causality for a VAR(1) model

Question:
I know I have to check the eigenvalues for the stability condition in general but I do not know how to process this exercise. How can I bring it in the right form?
How do you check c) ?
Can anyone help with showing the single steps?

For c) I would say that c does not G-cause y becaue of the 0 in the matrix. But y G-causes c.
Comment: Granger causality in the variable of some equation is present if any of the lags of the other variable(s - but here we only have one) are nonzero.
Comment: Thank you. How would you attempt a); I would check for unit roots.
Comment: @Christoph Hanck how can I brind the equation in world representation from?
Comment: for a) you indeed may look at the appropriate eigenvalues
Comment: How can I bring the given equation in the right format?
Comment: @Christoph Hanck Do you check the eigenvalue for ct and yt separately?
Comment: Isnt it just necessary to impose restrictions on alpha and gamma?
Comment: not sure how to tackel d)
Comment: Please read up on impulse responses.
Comment: I did! I got it now. But not sure how to tackle e) Sorry. I meant e) before
Comment: Me neither. There is no e) in your screenshot.
Answer: You need to check the roots of
$$
det(I-Az)=0,
$$
where $A$ is your coefficient matrix on the lags.
Thus,
$$
det\left(I-\begin{pmatrix}\alpha&\beta\\0&\gamma\end{pmatrix}z\right)=det\begin{pmatrix}1-\alpha z&-\beta\\-0&1-\gamma z\end{pmatrix}=0
$$
or
$$
(1-\alpha z)(1-\gamma z)=0
$$
which has solutions
$$z_1=1/\alpha\quad z_2=1/\gamma,$$
which need to be outside the unit circle. You can take it from here.
Comment: Yeah, I have the same result. But then I also defined the parameters and said that $\alpha$ has to be between 0 and 1  (and between -1 and 0) as I need z bigger than 1 in absolute values to define a stable system.
Comment: Right, that is what I'm saying.
Comment: For a) I would suggest that alpha and gamma need to be between 0 and 1 to have a stable process because then the roots are bigger than 1 in absolute value.


Manipular um JSON com a biblioteca GSON

Question: Este é o json:
{
"professor": [
{
  "latitude": -23.1843473,
  "longitude": -45.8840718,
  "title": "Microcamp",
  "endereco": "rua vilaça 2010"
},
{
  "latitude": -23.1843473,
  "longitude": -45.8840718,
  "title": "Microcamp",
  "endereco": "rua vilaça 2010"
}
],
"medico": [
{
  "latitude": -23.1843473,
  "longitude": -45.8840718,
  "title": "Microcamp",
  "endereco": "rua vilaça 2010"
},
{
  "latitude": -23.1843473,
  "longitude": -45.8840718,
  "title": "Microcamp",
  "endereco": "rua vilaça 2010"
}
]
}

Gostaria de manipulá-lo, que seria ler o json, e gravar mais dados.
Por exemplo uma nova profissão e um novo local, podendo cadastrar vários locais em uma profissão. Ou seja gostaria de transformar esse json em objeto com gson!
Comment: Você possui uma classe `Profissoes`, ou várias como: `Professor`, `Medico`, etc? Veja se [essa](https://pt.stackoverflow.com/questions/37249/como-desserializar-um-pol%C3%ADgono-geom%C3%A9trico-com-gson?rq=1) resposta te ajuda.
Comment: Nao possuo essas classes, iria ser dinâmico!
Comment: existe a possibilidade de ser criado?
Comment: eu tinha visto essa que voce mandou eu dar uma olhada, vou tentar fazer um exemplo para ver
Comment: Você tem que criar a classe necessária para desserializar seu JSON, vou tentar te explicar em uma resposta.
Comment: Muito obrigado @Zulian, estou no aguardo
Answer: Como você possui profissões e locais em seu JSON, precisa ter essas classes com esses mesmos atributos para poder transformar esse JSON em um objeto manipulável.
A estrutura básica nesse seu caso é: um array de Profissões que possui um array de Locais. Este JSON aí precisa de mudanças, pois possui um array de Locais mas não um de profissões, pois falta uma chave ( [ ) no começo e no final de tudo. Além disso você precisa inserir um atributo para a descrição da profissão. Primeiro você precisa tratar isso para não ter uma classe de cada profissão. Ex correto:
[
  {
    "descricao": "Professor",
    "local": [
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      },
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      }
    ]
  },
  {
    "descricao": "Médico",
    "local": [
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      },
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      }
    ]
  }
]

Depois é só criar as classes:
Profissao:
public class Profissao {
  String descricao;
  ArrayList<Local> local;

  public Profissao() {  //construtor
    this.descricao = "";
    this.local = new ArrayList();
  }
  // setters e getters aqui
}

Local:
public class Local {
  Double latitute;
  Double longitude;
  String title;
  String endereco;

  public Local() {  //construtor
    this.latitute = 0.00;
    this.longitude = 0.00;
    this.title = "";
    this.endereco = "";
  }
  // setters e getters aqui
}

Depois de ter essas classes, para desserializar seu JSON é fácil, utilize o método gson.fromJson passando seu json e a classe a ser utilizada. Ex:
Profissao[] profissoes = gson.fromJson(json, Profissao[].class);

Se seu JSON estiver correto, você terá um array de profissoes com seus respectivos locais.
Note que o nome dos atributos devem ser iguais aos do JSON para o Gson pegar automaticamente, caso queira utilizar outros nomes, utilize anotações próprias do Gson.
PS.: Há um site bem bacana que gera as classes baseado em seu JSON!

Utilizando List
Para trabalhar com arrays dinâmicos, como List por exemplo, você precisa utilizar o TypeToken do Gson para converter seu json em um List. Depois fica fácil adicionar mais objetos e transformar para json novamente. Ex.:
Type type = new TypeToken<List<Profissao>>(){}.getType();
List<Profissao> profissoes = gson.fromJson(json, type); //converte o json para uma lista

Profissao profissaoNova = new Profissao(); //cria uma nova profissao
profissaoNova.setDescricao("Programador");

profissoes.add(profissaoNova); //adiciona a profissao no array

String jsonString = gson.toJson(profissoes); //transforma para json novamente

Comment: Cara você é um DEUS! muito obrigado salvou minha pele na faculdade ! <3
Comment: O codigo esta super funcional estou conseguindo ler perfeitamente! e seu eu quiser cadastrar mais dados dentro desse json como consigo fazer? voce poderia me ajudar?
Comment: Você precisa utilizar listas para facilitar. Atualizei a resposta.
Comment: beleza, quando coloquei o codigo ele disse para eu importar o type, porem apareceu varias opções de type tem alguma que seria a correta?
Comment: Consegui aqui, outra e ultima duvida se eu for cadastrar um local eu faria de que forma ? <3 valeu nois
Comment: Crie um local assim: `Local local = new Local();`. Sete os atributos necessários. Ex.: `local.setTitle("Teste");`. Depois adicione no array de locais da profissão, fazendo algo do tipo: `profissaoNova.getLocal().add(local);`.

Manipular um JSON com a biblioteca GSON


Javascript get key of multidimensional array

Question: I've the following array:
$test[$iterator][1][2]['name']

I have a function that needs to get $iterator of that array when ['name'] == 'value';
So how can I use Javascript to get the given $iterator?
Also, I did look at other questions, but I'm not sure if their answers apply to my question.
Comment: PHP or JavaScript? The `$` seems a little strange.
Answer: Are you looking for something like this?
var $iterator;
for (var i = 0; i < $test.length; i++) {
    if ($test[i][1][2]['name'] == 'value')
    {
        $iterator = i;
        break;
    }
}

Comment: Yes, this is what I was looking for.  I was trying some jQuery.methods, and they weren't working.
Thanks alot!

Also, yeah, I have a bad habit about sometimes using php variable names in my javascript code.


I want to save the data 'self.billdata' into a file?? But with this code no data is being printed in to the file saved

Question: I want to save the data 'self.billdata' into a file?? But with this code no data is being printed in to the file saved
<code>def save_bill(self):
        self.billdata = self.txtarea.get('1.0',END)
        files = [('All Files', '*.*'),
                 ('Python Files', '*.py'),
                 ('Text Document', '*.txt')]
        file = asksaveasfile(filetypes = files, defaultextension = ".txt")
        f1 = open(file,'w')
        f1.write(self.billdata)
        f1.close()
</code>
Answer: try this for me working
<code>    print(You Data", file=open(""+str("FILE NAME")+".txt", "a", encoding='utf-8'))
</code>


Electric piano synth patch

Question: I'm trying to reproduce, on my own, an electric piano sound. I'm open to using any type of synthesis(I actually use the Pyo DSP library ), but I'd particularly like to know how to do it with substractive synthesis, à la Yamaha CP-20/CP-30.
What types of waveforms are the best? How many oscillators? What kind of filters, effects, etc are necessary or interesting?
Most importantly, any advice and details about the envelopes? Is ADSR sufficient? What did the Yamaha CP line used?
I've been playing around a bit, using a few triangle oscillators and distortion, and all, but I'm still finding the result lacking.
Any resources on that would be welcomed.
Thanks!
Comment: Because of the bell-like character of electric pianos, you're going to find it very hard to make a convincing patch with subtractive synthesis. The best non-sampled EPs are usually FM synthesis - that's why the Yamaha DX7 was so popular. It used digitally controlled analog FM synthesis to create a great EP sound in a portable, lightweight format.
Comment: The Yamaha EP-20/EP-30 used substractive synthesis to achieve a sound similar to electric pianos. I know it's not exactly piano-like, but it's still the kind of sound I'd like to reproduce.
Comment: Sorry, Yamaha CP-20/CP-30.
Answer: I haven't been able to find anything authoritative about how the CP-20/30/25/35 generate sounds. They might not even be subtractive synths in the typical sense.
You can do a kind of FM on a purely subtractive synth if you can set the filter to self-oscillate and then modulate the cutoff frequency using one of the audio rate oscillators. That doesn't work so well on real polyphonic subtractive synths like the Prophet 5/6, but can work on virtual instrument re-creations of those kinds of synths.
There's also oscillator hard sync that might give you that EP sound. It's similar to FM and does give a kind of ringing sound like EP tines if you play with the base frequency ratio between the oscillators. This is what I would try first. Getting the envelopes right will be important, for both the amp and filter envelopes. Also adding just a little bit of volume modulation can help since a lot of EPs had tremolo units.
Comment: Thanks for the tips on hard sync, though that seems to be one of the few things the pyo library lacks, and that I'm I can implement myself. To be clear, I could and did try using FM, so I'm not interested in "emulating" FM using sub, rather finding good purely subtractive methods(with some additional effects, like disto or phaser or whatever) that can generate interesting sounds. I am currently playing around with filter and distortion envelopes, and the like, but have yet to reach my goal. And I'm pretty sure The CPs used some sort of subtractive method(I heard they used Pulse waves as well).
Comment: *I'm not sure I can implement myself...
Comment: @CharlesLanglois You might edit your question to remove the part where you wrote that you were open to any type of synthesis, since you wrote you want to use purely subtractive methods. Not all sounds can be well emulated using just waveforms and filters.
Comment: Well, let me backtrack a bit. I *am* open to specific suggestions for fm-based methods as well, but as I said, I am particularly interested in subtractive synthesis. And I have access, using the Pyo library, to many types of synthesis(including sub, fm, additive, and more), and I can combine them as I wish. However, simplicity has value.
Comment: And again, since the CP-30 seems to be using subtractive synthesis, at least that kind of sound should be feasible, especially if they could do it in the 1970s using purely analogue circuits with the methods available then.

Electric piano synth patch


Como fazer quando a linha da lista está imprimindo em mais de uma linha?

Question: Estou tentando imprimir minha lista composta pela junção de duas listas, mas quando rodo o programa o código está quebrando a linha e a imprimindo seu final em uma outra linha.
O código está dando esse resultado:
        VALUE      CLASS    TYPE      PUBLIC SYMBOL NAME              SIZE
    ======================================================================
    40000000H      DATA     DWORD     TimeWD
4
    40000004H      DATA     ---       GPIO_MASK
4
    40000008H      DATA     ---       GPIO1_MASK
4

E é assim que ele deveria imprimir:
        VALUE      CLASS    TYPE      PUBLIC SYMBOL NAME              SIZE
    ======================================================================
    40000000H      DATA     DWORD     TimeWD                          4
    40000004H      DATA     ---       GPIO_MASK                       4
    40000008H      DATA     ---       GPIO1_MASK                      4

Eu criei um arquivo a parte só para testar essa parte que está dando erro, mas quando mando rodar o erro não está acontecendo.
Aqui está o código:
lista1 = ['        8100498AH  DATA     BYTE      TEC2FUN', '        8100498BH  DATA     BYTE      EMREDE', '        8100498CH  DATA     BYTE      CAPSLOCK', '        8100498DH  DATA     BYTE      BALKEY', '        8100498EH  DATA     BYTE      PLUKEY', '        8100498FH  DATA     BYTE      ACTCOD']
lista2 = ['123', '234', '345', '456', '567', '678']

lista_arquivo = []
with open('ArquivoOrganizado.txt', 'w+') as arquivo_novo:

    title = '        VALUE      CLASS    TYPE      PUBLIC SYMBOL NAME              SIZE\n'
    div = '        ======================================================================\n'
    arquivo_novo.write(title)
    arquivo_novo.write(div)

    for (coluna1, coluna2) in zip(lista1, lista2):
        coluna_final = coluna1, '                          ', coluna2
        lista_final = ''.join(map(str, coluna_final))
        print(lista_final)
        lista_arquivo.append(str(lista_final)+'\n')
        '''print(lista_final)'''

    for items in lista_arquivo:
        arquivo_novo.write(str(items))
    print("File written successfully")

arquivo_novo.close()

Comment: Talvez esteja grande demais para.o prompt, não? E se você reduzir alguns espaços dessa linha?
Comment: redimensione o tamanho da janela do seu terminal para poder exibir os textos sem quebra de linha
Answer: acho que voce nao precisa colocar o + '\n' na linha lista_arquivo.append(str(lista_final)+'\n'), um novo print que vai ocorrer no proximo index do for já vai pular uma linha.


Install mysql on EC2 Lenny AMI

Question: I'm wondering how I can change the default location of the mysql db when I install Mysql using apt on debian.  EC2 instances set up /mnt to have the majority of disk space, leaving only 10gb for /.
So I'd like for the DB to be stored in /mnt somehow.  The configs etc can stay where they install by default, I just want the actual disk storage to be on /mnt.
Any tips?
Right, so I found that the datadir directive is where mysql stores it's DB's, so in /etc/mysql/my.cnf i changed datadir to /mnt/mysql.  But starting mysql now fails and I can't see anything in the mysql.err log, or any mysql log for that matter.  Any thoughts?
Answer: Ok, so I figured it out.  Had to copy over the files from the original mysql data dir (except the ib_xx files) and needed to chown mysql:mysql all those files.  Works like a charm now!
Comment: Don't forget that you loose the data stored on the local disks in EC2.  If you want to keep that data around, you should look into using an EBS (Elastic Block Store) for your data.
Comment: what do you mean you lose the data?  when you shut down the instance? or delete the instance?  This is my first EC2 so I don't really know all the ins and outs
Answer: You probably already figured it out by now, but he meant that you lose your data when you terminate the instance.

Install mysql on EC2 Lenny AMI


Drush Open_BaseDir Error

Question: I keep getting the following error, although I've turned off SafeMode in my .ini file, and confirmed it's off with a PHP status page.

The following restricted PHP modes have non-empty values:
      open_basedir. This configuration is incompatible with drush.  Please
      check your configuration settings in
      /usr/local/php-5.2.17/etc/php.ini or in your drush.ini file;

safe_mode            Off        Off
memory_limit        168M       168M
open_basedir    no value   no value

UPDATE:
I think it's worth noting that this is on MediaTemple's Grid Service (gs). It looks like the PHP CLI is actually using a different .ini file with open_basedir set. So I'm still trying to figure that part out.
Comment: I actually asked this previously: http://drupal.stackexchange.com/questions/10372/drush-open-basedir-error
Answer: My solution was to add this line to my .bash_profile
alias drush='/usr/bin/php5 -c /home/000000/etc/drush.ini ~/bin/drush/drush.php'

It turns out the CLI on MediaTemples Grid Service uses a different PHP.ini file. So that line basically tells it to find your drush.ini file to override some settings. Make sure to replace the 000000 in with your server number. Thanks to kiamlaluno and MediaTemples support folks for the help.
Comment: Making an alias to drush is discouraged in Drush-5.  Drush-4 has this advice:
Comment: Making an alias to drush.php is not covered in the Drush-5 README.txt because it is hard to get right; calling the `drush` script and using drush.ini is easier.  The README.txt in Drush-4 has some advice on how to correctly make an alias to drush.php; please consult that before following the advice above.  You must include a --php= option, etc.; if you do not call Drush correctly, then certain commands will fail (e.g. pm-update).
Answer: The error you are seeing is reported by Drush, which verifies the "open_basedir" directive contains an empty value, and returns an error if the value is not empty.
Drush can use the php.ini file used by PHP or its own INI file ("drush.ini"), which is looked for in the following directories:

Drush installation folder
User's .drush folder (i.e. "~/.drush/drush.ini")
System wide configuration folder (i.e. "/etc/drush/drush.ini")

The code I am referring to is contained in the _drush_environment_check_php_ini() function, and it is the following one:
  $ini_checks = array('safe_mode' => '', 'open_basedir' => '', 'disable_functions' => array('exec', 'system'), 'disable_classes' => '');

  // Test to insure that certain php ini restrictions have not been enabled
  $prohibited_list = array();
  foreach ($ini_checks as $prohibited_mode => $disallowed_value) {
    $ini_value = ini_get($prohibited_mode);
    $invalid_value = FALSE;
    if (empty($disallowed_value)) {
      $invalid_value = !empty($ini_value);
    }
    else {
      foreach ($disallowed_value as $test_value) {
        if (strstr($ini_value, $test_value) !== FALSE) {
          $invalid_value = TRUE;
        }
      }
    }
    if ($invalid_value) {
      $prohibited_list[] = $prohibited_mode;
    }
  }
  if (!empty($prohibited_list)) {
    drush_log(dt('The following restricted PHP modes have non-empty values: !prohibited_list. This configuration is incompatible with drush.  !php_ini_msg', array('!prohibited_list' => implode(' and ', $prohibited_list), '!php_ini_msg' => _drush_php_ini_loaded_file_message())), 'error');
  }

As you can see, the function returns an error if the "safe_mode" directive, or the "open_basedir" directive contains a non-empty value.
Comment: I guess I understand that. It's more confusion because my PHP.ini clearly has no value for open_basedir and safe_mode is off and I've verified those settings through the browser. Sorry if this is basic server setup but is Drush looking at a different PHP.ini than the browser or something? If so what steps would I take to find the .ini file that Drush is using?
Comment: I am sorry: I forgot replying about that. What I mean in my answer is that Drush doesn't check if the safe mode is enabled, and then check if the "open_basedir" directive contains any value.
Comment: That makes sense. I checked open_basedir by running php -i in the console and it is indeed using a different php.ini for the CLI than the website. So it has open_basedir => /nfs:/tmp:/usr/local:/etc/apache2/gs-bin => /nfs:/tmp:/usr/local:/etc/apache2/gs-bin
Answer: @Eric I.E. You're correct. That way solves the issue. But we do need to make a copy of drush.ini from http://drupalcode.org/project/drush.git/blob_plain/refs/heads/master:/examples/example.drush.ini
Also make sure "open_basedir = " on an uncommented line.
Answer: For those who couldn't fill in the missing blanks here, your drush.ini file will need the line, in addition to being sourced from the alias in .bash_profile:
open_basedir =

Answer: install drush on CentOS with Plesk
I tried to install drush 7.x-5.9 on on a CentOS system with the Plesk panel.
I was still getting this error:

drush
sh: /tput: No such file or directory The following restricted PHP
  modes have non-empty values: safe_mode.     [error] This configuration
  is incompatible with drush.  Please check your configuration settings
  in /etc/php.ini or in your drush.ini file; see
  examples/example.drush.ini for details. sh: /wget: No such file or
  directory Unable to create a temporary directory.
  [error] tempnam(): Unable to access  filesystem.inc:461
  [warning] sh: /curl: No such file or directory

This is how I fixed it
sudo -s
wget http://ftp.drupal.org/files/projects/drush-7.x-5.9.tar.gz
tar -zxf drush-7.x-5.9.tar.gz  -C /usr/local/share
ln -s /usr/local/share/drush/drush /usr/bin/drush
cd /usr/local/share/drush
cp ./examples/example.drush.ini drush.ini
vi drush.ini

Uncomment the following lines (at the bottom) by removing the ";" on the beginning of the line.
safe_mode =
open_basedir =

save the file
Tell dush to use this ini file

export DRUSH_INI='/usr/local/share/drush/drush.ini'

Add above line also to your ~/.bashrc file
Answer: Because (as @greg_1_anderson said) making an alias for drush.php is not covered in drush-5.x Readme file. A better solution is to copy the server's default configuration php.ini file to $HOME/.drush or to /etc/drush.
Then, edit it to set open_basedir to no value and add any other custom configuration for drush.
Also, check this answer for php.ini code sample.

Drush Open_BaseDir Error


How to notate two identical notes ringing together?

Question: In a melody I am composing, there are 4 E notes in a row, like this:
<code>-----------------
  ♪   ♪   ♪   ♪
-----------------

-----------------

-----------------

-----------------
</code>
(the length of the note is 1/8)
I think it sounds best when I play them 3 times on the first string, and the last time on the first and second strings, like in this tablature:
<code>--0---0---0---0--
--------------5--
-----------------
-----------------
-----------------
-----------------
</code>
How can I express it in the standard musical notation?
In addition: If I want the additional note to be performed with a hammer-on (and let the open string ring):
<code>--0---0---0------
-------------h5--
-----------------
-----------------
-----------------
-----------------
</code>
How can I write this in a standard notation?
Answer: To express the fact that 2 notes are sounding, you should use beam direction. It's as if one instrument is playing two parts simultaneously. See the picture, and note how each part gets its own "swimming lane" on the staff. Please also note that each bar on the staff that uses multiple parts should, in principle, make sure all timing for each part is accounted for. Hence the 1 1/8 rest beneath the 1/8th notes in the upper voice (beams up) - that rest needs to be there to indicate that the lower voice (beam down) is silent at that point.
To remove any doubt as to how to play it you can use circled numerals to indicate the strings that are to be used. (see picture). I think in this case, a single 0 (not circled) to indicate an open string at the upper voice would also work.

Letting the other string ring is easy with this notation, since you're essentially writing two parts on the same staff. The hammer is denoted with a + if google serves me right. Probably can't hurt to include a legend to express exactly what you denote by the +
Answer: 1st question - you can't on a single melody line. I am reticent to admit it, but in this situation, tab comes up trumps ! Rather like slurs : on the stave, it's a curved line; on tab it could be hammer/pull, or slide, bend up/let down, or R.H.tap.
2nd question - it could be written out as two parts, top - 2 crotchets and a minim (to ring on), bottom - three one beat rests and a slurred crotchet, which a guitarist would have to guess was played on another string to the 2nd part. Also depends where you're hammering from. I'm guessing nowhere, just hitting 5th fret ? N.B. you wrote quavers, I've used crotchets, but the concept remains the same.
I always feel that it's useful to use 16 dashes per bar, to show more accurately how long a note lasts. As in your sample :0---0---0---0---   because, unless 1) you know the tune, and 2) it's written with 'proper' music above, it's difficult to read. And - if you know the tune, why bother with tab...


Image tag not getting src attribute using JS

Question: I am trying to get files from user using <input type="file"/>. Multiple files can be uploaded. After upload I have used this code I found on stack which allows me to show the uploaded images. Initially the code was to upload one image and show it.
For multiple display, I have tried to clone the main div element and assign a new id to it and using the FileReader object I have tried to assign src to image tag. This is my code.
HTML code :

      <button type="button" onclick="removeUpload()" class="remove-image">Remove  Image</button>

JS Code :
function previewImage(element){
    if(element.files[0]){
        console.log(element)
        for(i=0;i<element.files.length;i++){
        var main=document.getElementById("showPic");
            var cloned=main.cloneNode(true)
            const reader = new FileReader();
            console.log(reader)
            reader.readAsDataURL(element.files[i])
            cloned.id="showPic"+i;
            cloned.children[0].id=i;
            reader.onload=()=>{
                cloned.children[0].src=reader.result;
            }
            cloned.style.display="block";
            document.getElementById("picsContainer").appendChild(cloned);
        }
}
}

The function gets called at the time of image upload using onchange. Problem is that in this code only the last added clone element is showing the image preview and all the other elements before that are not even getting anything in their src attribute. It just shows a "#" in src attribute.
What am I doing wrong here?
FileList {0: File, 1: File, 2: File, length: 3}
0: File {name: 'Screenshot (4).png', lastModified: 1633166516919, lastModifiedDate: Sat Oct 02 2021 14:51:56 GMT+0530 (India Standard Time), webkitRelativePath: '', size: 716548, …}
1: File {name: 'Screenshot (5).png', lastModified: 1633166527063, lastModifiedDate: Sat Oct 02 2021 14:52:07 GMT+0530 (India Standard Time), webkitRelativePath: '', size: 511571, …}
2: File {name: 'Screenshot (6).png', lastModified: 1633166534410, lastModifiedDate: Sat Oct 02 2021 14:52:14 GMT+0530 (India Standard Time), webkitRelativePath: '', size: 649065, …}
length: 3

This is what I am seeing if I log the element.files in console
Comment: Try to **set** the src **attribute** with [setAttribute](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute)
Answer: It seems to me that you might be overwriting your images array, but I can't say for sure without taking a look at the rest of your code.
My suggestion is instead of cloning the div, map your images array and create the components as needed.
Maybe something like this (ES6):
const previewImage = (element) => {
 const container = document.GetElementById('picsContainer')
 const reader = new FileReader();
 return element.files.map((image, index) => {
  // Create a new div
  const newElement = document.createElement('div')
  newElement.key = index
  newElement.id = `showPic${index}`
  newElement.style.display = 'block'
  // Create the image component
  const img = document.createElement('img')
  newElement.appendChiled(img)
  reader.readAsDataURL(image)
  reader.onload = () => {
   img.src = reader.result;
  }
  container.appendChild(newElement)
 }
}

This should work, but if you get any errors let me know and I'll try to help you again. Also, instead of using file reader, you could create a blob for each new image and get a custom url for them.
UPDATE
Since your FileList is an object with each file key being a number, and a length at the end, you can adapt my solution like this:
const previewImage = (element) => {
 const container = document.getElementById('picsContainer')
 for (let i=0; i<element.files.FileList.length; i++){
 const reader = new FileReader();
  // Create a new div
  const newElement= document.createElement('div')
  newElement.key = i
  newElement.id = `showPic${i}`
  newElement.style.display = 'block'
  // Create the image component
  const img = document.createElement('img')
  newElement.appendChild(img)
  reader.readAsDataURL(element.files.FileList[i])
  reader.onload = () => {
   img.src = reader.result;
  }
  container.appendChild(newElement)
 }
}

I'll leave the original solution since it should work for an array of files.
Comment: Your answer seems pretty good and straight forward. can you please guide me to this blob creation? How to do that and advantages and all that since I have no idea about that.
Comment: @BhaumikBhatt Sure, you can read the docs to get a better understanding (https://developer.mozilla.org/en-US/docs/Web/API/Blob). Essentially you need to create a blob from the uploaded images and then create a url
( URL.createObjectURL(blob) ) to that blob, which you can use as the preview src.
Comment: Implementing the solution, I am getting an error saying the map function doesn't exists at element.file
Comment: @BhaumikBhatt the 'element.files' should be an array from what I could see of your code, check if you're receiving it correctly
Comment: I have updated the question with what and how I am receiving in element.files
Comment: Updated the answer, since you're not receiving and array, you can map through your object with a simple for loop like you originally intended.


SPF "include:" does MX stay as included domain's MX, or get translated to the includer's MX?

Question: SPF records allow you to include another's domains SPF record within your own. For example, example.com can have include:_spf.google.com in its SPF record to include all of the SPF rules for _spf.google.com.
What happens to the mx and plain a (with no :hostname) parts of the included rules for example.com?
Would mx be parsed as the MX records for _spf.google.com or example.com?
(and yes I'm aware that there are no MX records for _spf.google.com, but I'm just trying to use an obvious include: example, but for a situation where there was one)
Answer: Firstly, just for clarity on include, s5.2 of RFC 4408 says:

Only the evaluated result of the referenced SPF record is used, rather
than acting as if the referenced SPF record was literally included in
the first.

As to whether the MX RR in an included record refers to your MX, or the MX of the included domain, I cheerfully concede that OpenSPF's protocol page is incredibly unhelpful on the subject, because their example uses the same domain for both source and included record.  But the RFC is (very slightly) clearer, when it writes (again in s5.2):

The "include" mechanism triggers a recursive evaluation of check_host().  The domain-spec is expanded as per Section 8.  Then
check_host() is evaluated with the resulting string as the <domain>.
The <ip> and <sender> arguments remain the same as in the current
evaluation of check_host().

I have highlighted the bit that I interpret as meaning that mx and similar records are evaluated in the context of the included domain.  But since nothing is as good as Real Data, I defined the following subdomains in one of my family's domains (waide.me.uk, and thanks for the temporary loan!):
foo     IN      TXT     "v=spf1 include:bar.waide.me.uk -all"
foo     IN      A       10.5.5.5

bar     IN      TXT     "v=spf1 mx -all"
bar     IN      MX      5       baz.bar
baz.bar IN      A       18.104.22.168

When I use the Beveridge tester (that openspf.org recommends), it says that mail from email@example.com arriving from 220.127.116.11 is an SPF pass.  Note that no reference is made to that address anywhere in foo.waide.me.uk.  There is an MX reference in bar.waide.me.uk's included record, but foo.waide.me.uk has no MX.  If the MX in the included record were not evaluated in the context of the included domain (bar.waide.me.uk) it could not resolve at all.
And just for completeness, when I remove the include from foo.waide.me.uk's SPF record, then as we would now expect, Beveridge reports a fail.
Comment: Based on my experience with gmail, they interpret included MX differently (as the MX of the including domain). Might be a bug, but to be safe it might be prudent to not rely on this feature at all.


Need help on a select to gather data from Access database

Question: inside a PowerShell script I need to gather data from an Access database. This is mainly a log of my terminal server: each time someone logs on or off, a script adds a line to my database with the below information (in fact many more but i keep it easy :) ).
Here are the reccord of the database :
table log_table
date_ac              Dept                          User_ac         Typeaccess
1/1/2014             dept1                         user1           logon
1/1/2014             dept3                         user2           logon
1/1/2014             dept3                         user2           logoff
1/1/2014             dept1                         user1           logoff
1/1/2014             dept1                         user1           logon
1/1/2014             dept1                         user1           logoff
2/1/2014             dept1                         user1           logon

I want to group this by departement, depending on the date requested, and showing the number of DIFFERENT people that logged on (so typeaccess logon only, not logoff) during that time line BASED on a day (so if a user log day one, it counts for one, if he logs day two, that counts for one too but if you log 20 times day one that will still counts just for one).
So in my exemple the result must be for a request 1/1/2014 -> 2/1/2014:

dept1: 2 (user1 logged otwice the 1/1 but it counts only for one and once the 2/1 so the total is 2 as it is day based)
dept3: 1

My problem is that I can't get rid of the "uniqueness" of the user name, as if a user logon twice or more per day that must count in my total as one.
Currently I tried this:
SELECT log_table.dept, Count(log_table.user_ac)
AS CountOfuser_ac
FROM log_table
WHERE (((log_table.[typeaccess])='LOGON') AND
  ((log_table.[date_ac]) Between #1/1/2014# And #2/1/2014#))
GROUP BY log_table.dept

That work BUT it does count the user each time he logs on.
As request here is the output i would like :
Dept              Total nbr user
dept1             2
dept3             1

Comment: Please [edit] your question to show the desired output you'd like to obtain from the sample data you provided.
Answer: Gather the unique users per department and day in a subquery where you group by dept, date_ac and user_ac, then group that subquery by department to get the numbers you want:
SELECT t1.[dept], Count(t1.[user_ac]) AS [Unique Logins]
FROM (
  SELECT [date_ac], [dept], [user_ac]
  FROM [log_table]
  WHERE [typeaccess] = 'LOGON' AND
    [date_ac] BETWEEN #1/1/2014# AND #2/1/2014#
  GROUP BY [dept], [date_ac], [user_ac]
) t1
GROUP BY t1.[dept]

Comment: Hello and thanks for taking time to answer ! I tried that request but it seems to not work :( i assume at the end it is a AS t1 but still he doesn't give me any output (i tried directly the query in access not from my script). It ask me enter paramater value typeaccess: ) then when i clik ok no output. Thanks !
Comment: @StephaneRiviere Put the identifiers in square brackets (see updated answer). Using the keyword AS for defining the subquery alias name is optional. Both `SELECT ... FROM (...) t1 ...` and `SELECT ... FROM (...) AS t1 ...` should work. I tested with Access 2013.
Comment: Added what i type in my question still same message
Comment: Never mind was a typo that just WORKS !!! Very impressed thank you very very much ! Have a nice day !


Sequence of solutions of an ODE

Question: I'm trying to solve the following problem, which says
Let $f : E \to E$ be continuos; suppose $f(x) \leq M$. For each $n=1,2,...,$ let $x_n: [0,1] \to E$ be a solution to $x'=f(x)$. If $x_n(0)$ converges, show that a subsequence of $\{x_n\}$ converges uniformly to a solution.
Using the fact that $f(x) \leq M$ i can fulfill the hypothesis of Arzelà–Ascoli theorem and prove that exist a subsequence of $\{x_n\}$ that converges uniformly, but i believe that i only have convergence and i need to prove that such subsequence converges to a solution, in order to do that i get stuck since i really don't know how to approach it and don't know how to use the fact that $x_n(0)$ converges.
If someone could helping me with that, thank you in advance
Comment: Why do u say that? $x_n$ satisfies the equality $x^{'}_n(t) = f(x_n(t))$
Comment: How's $ x_n $ a solution? Is it a function of $x$?
Answer: Hint:
-First try to prove that $x^{'}_n$ has a uniformly convergent sub-sequence.
-Use the following theorem:

Let ${f_n}$ be a sequence of differentiable functions defined on the
  closed interval [a, b], and assume ${f^{'}_n}$ converges uniformly to a
  function g on [a, b]. If there exists a point $x_0 ∈ [a, b]$ for which
  $f_n(x_0)$ is convergent, then $f_n$ converges uniformly. Moreover,
  the limit function $f = \lim  f_n$ is differentiable and satisfies
  $f^{'} = g$

-Finish the exercise
Comment: in the first part of the hint u say, prove that $x^{'}_n$ has a uniformly convergent sub-sequence, did u mean $x_n$ instead of $x^{'}_n$?
Comment: No, I mean $x^{'}_n$
Comment: so, in the part of the theorem that says "and assume $f_n$ converges uniformly to a function $g$" it should be $f^{'}_n$ instead of $f_n$? because i believe u want me to set $f_n = x_n$, and also in the first part of the hint should i use Arzelà–Ascoli ? thank you for the help
Comment: Yes, you're right, I edited it!
Comment: thank you, i already solved the problem, i knew that i was missing something, in this case i didn't remember that theorem.
Answer: You need to know that $x_n(0)$ converges in order to prove that $x_n$ is uniformly bounded (and you need to know that, if you want to apply Ascoli-Arzelà!).
This is clear if you apply the fundamental theorem of calculus to your sequence of solution:
$$
x_n(t) = x_n(0) + \int_0^t f(x_n(s)) ds.
$$

Sequence of solutions of an ODE


Categorize datetime value to shifts where shift ends on the next day

Question: I have a MySQL query for example:
select sum(qty) as qty
from myitem
where filedatetime >='2019-08-01 06:30:00'
  and filedatetime < '2019-09-01 18:30:00'

My table only have qty and filedatetime columns.
Based on this query is it possible to query out the following conditions:

if filedatetime is filedatetime >= '2019-08-01 06:30:00' and filedatetime < '2019-08-01 18:30:00' then

assign value 'Day' to column shift
assign value '2019-08-01' to column shiftDate

if filedatetime >= '2019-08-01 18:30:00' and filedatetime < '2019-08-02 06:30:00' then

assign value 'Night' to column shift
assign value '2019-08-01' to column shiftDate

So this how the result will be look like:
shiftDate   |   Shift   |   QTY     |
2019-08-01  |   Day     |   10      |
2019-08-01  |   Night   |   12      |
2019-08-02  |   Day     |   11      |
2019-08-02  |   Night   |   15      |
2019-08-11  |   Day     |   11      |
2019-08-11  |   Night   |   18      |
2019-08-12  |   Day     |   13      |
2019-08-12  |   Night   |   19      |

Comment: Make it possible to assist you: [mcve]
Comment: Provide some code you've written for this already and stuff you've already tried, and try and clean up the explanation a bit. I've read through it 3 times already and still don't know what you want to know.
Comment: Is it guaranteed that the two shifts are exactly 12 hours long?
Answer: You can use Time() function to extract time (hh:mm:ss) portion out of a datetime value. Once we have determined that, we can use CASE .. WHEN conditional expression, to check whether "Day" or "Night" shift based on your defined conditions. Also, we can use Date() function to extract the date (YYYY-mm-dd) portion and add/subtract 1 day accordingly.
We can then GROUP BY on these calculated expressions, and calculate sum of the qty; no need of using a subquery for further aggregation:
SELECT

  -- Determining day of the shift
  CASE
     -- When below 06:30:00 then previous day's Night shift
     WHEN TIME(filedatetime) < '06:30:00'
     THEN DATE(filedatetime) - INTERVAL 1 DAY
     -- Else When >= 06:30:00 then current day's shift
     ELSE DATE(filedatetime)
  END AS shiftDate,

  -- Determining Night / Day
  CASE
      -- When between 06:30:00 (inclusive) and 18:30:00 then Day
      WHEN TIME(filedatetime) >= '06:30:00' AND TIME(filedatetime) < '18:30:00'
      THEN 'Day'
      ELSE 'Night'
  END AS shift,

  -- Do aggregation
  SUM(qty) AS qty
FROM myitem
WHERE filedatetime >='2019-08-01 06:30:00'
  AND filedatetime < '2019-09-01 18:30:00'
GROUP BY shiftDate, shift

Comment: Nice great it working. Thanks. Such query is absolutely out of my mind.
Comment: My mistake. This query is runnable. but i not yet check the result it correct or not
Answer: You should be able to use time functionality directly:
select mi.*,
       (case when cast(filedatetime as time) >= '06:30:00' and
                  cast(filedatetime as time) < '18:30:00'
             then 'day' else 'night'
        end) as shift
from myitem mi;

In MySQL, you can also use time(filedatetime) function instead of cast().


How to verify the origin of a post in php using https

Question: How can I ensure that a https post is from a given domain? Consider the following setup:
A webpage on the frontend server, for example https://www.frontend.com/index.php sends a https post redirect to a webpage on a backend server, for example https://www.backend.com/index.php
Now the backend wants to verify that the post is really from  https://www.frontend.com/index.php .
(If the post is not from the frontend server, it should be rejected showing an error message.)
How can this be done in php? (as seen from the URLs above I am using SSL)
Comment: `$_SERVER['HTTP_REFERER']` but I would pass a token to make sure.
Comment: @Popnoodles But cannot another site (not frontend) just set this variable `$_SERVER['HTTP_REFERER']` to the frontend URL, pretending it is the frontend server? See: http://security.stackexchange.com/questions/32299/is-server-a-safe-source-of-data-in-php
Comment: Yes which is why I'd pass a token that only those two sites know how to generate.
Comment: Ok I see :) Thanks. I wondered if there were some other mechanisms that could be used, since I am using SSL.
Comment: That said there is probably a proper way to do it using CURL and credentials. I don't know about this though, so hopefully someone will help you out. Possibly SOAP does this.
Answer: A common technique here would be to use request signing. In short it uses a shared secret (a "password") to hash certain parts of the request to create an authentication token. For example, see here.
Specifically for SSL though, you may also use client-side certificates, in which the client sending the request proves his identity. SSL enables mutual authentication, it doesn't have to be one-sided as it typically is.


make an android app in which the launch icon is working as a link

Question: I was wondering if it is possible to make an android app and when the user hits the launch icon, the browser opens with a specific URL. I know that you can make shortcuts on your phone home screen with the URL you want but I was wondering if it is possible as an android app. So the app should don almost nothing just when i tap on it the browser should open..
is this possible? and is yes how should I think it?
Comment: yes possible. Open the browser.
Comment: haha...i would want for some users who are lazy to write an url or to bookmark an url just to tap an icon..please don't make useless coments..
Comment: Then please don't ask off-topic questions :D  http://stackoverflow.com/help/on-topic
Comment: What makes you think that users who are too lazy to create a bookmark are not too lazy to download an app?
Comment: this is just a demand..who am i to judge?
Answer: Do this in the main activity's onCreate:
<code>  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com"));
        startActivity(intent);
        finish();

    }
</code>
in the manifest add noDisplay theme:
<code>  <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme = "@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</code>