How do I create a rule to display a system message to users that an event is now over?

Question: I have an event content type with a date field. Is there a rule I can create that will display a system message to the user that "This event is now over." based on comparing the current date and the date in the date field?
EDIT:
Answer: Rules can be used for this.  Your message will be displayed using drupal_set_message()
Starting at the Admin screen;

Click through Configuration > Rules > Add New Rule
Give it a name and select Content is viewed in React on event. Click Save.
Under Conditions click Add condition

Select Data > Data comparison
Select node:type and click Save
Fill in for your content type and save

Under Conditions click Add condition (again)

Select Entities > Entity has field
Data Selector should have node
Field should have the field you're wanting to check. This should be a date field

Under Conditions click Add condition (again)

Select Data > Data comparison
In Data Selector chose your field. Mine was node:field-end-date
Click Continue.
Under Operator select is lower than
Under Data value select site:current-date
Click Save

Under Actions click Add action

Select the System > Show a message on the site entry.
Enter your Message, select the Message type (this is the drupal_set_message() status), untick Repeat message and click Save

Browse to your expired content to see your message.
Comment: This worked well Thanks. Any thought on how to accommodate for a repeating event. One that starts and ends on one day and is repeated every week? Any way to compare the last date?
Comment: Are you meaning that the one node is a repeating event?  i.e. there's not multiple nodes?  I'm not sure. I was fortunate enough to have this sort of situation on the project I'm currently working on.  But each node only has one date.
Comment: correct. one node using date repeats.
Comment: That's getting messy. :/ I imagine you'll need to dig around in some code to get that working. I would start by exploring the options in the date comparison though.
Comment: When I add my date field to the data comparison, I see 4 options even though I only have one date field for the entire site. THis might have something to do with it? I have updated my question above with a screenshot to further clarify what I see.
Comment: I suspect that these 4 values relate to the data collected for the repeating dates.  Looking at the Conditions section again I see that there are "Add or" and "Add and" options.  You'll want to set nested condition based on what those extra fields hold/represent. Sorry I can't offer more advice.
Answer: Your question is vague. What is a "message" to the user, a pop-up window ? a fake field you display above the other fields if the date is passed ?
The easiest thing for you to do without coding a custom module is probably using Display Suite to create a custom_field in your content type. Use PHP to set the string value of the message if the date has passed as the field body. Or if the date has passed then call drupal_set_message('Event has already occurred'); Or else return an empty string.
If you want a popup-window you can probably find a suitable module that shows a popup window from this popup module comparison matrix.
Comment: Sorry for the confusion. It would be a system message. I have edited my question accordingly.


Find coordinates of a regular polygon in a plane.

Question: It is easy to calculate the coordinates of a regular polygon in the $X$-$Y$ plane.
For a $N$ sided polygon with radius $r$. You get the $x$,$y$ and $z$ coordinates with:
$x = r*\cos(2 \pi \frac{n}{N})$ and
$y = r*\sin(2 \pi \frac{n}{N})$
$z = 0$
Let's assume I have a plane $P$ defined by its normal vector $\vec n$ and a point $p$. Therefore $P$ defined by all x that solve $(\vec x - \vec p)\vec n = 0$
If I want to have the $N$ sided polygon with center at the point "p" I need to transform the $X$-$Y$ plane to the plane defined by its normal vector.
It is propably the best to transform the center of the original polygon $(0,0,0)$ to the new center $p$.
An affine transformation looks like $f(\vec x) = A*\vec x + \vec t$.
In this case $\vec t = \vec p$. But how do I get the transformation matrix $A$?
Answer: The transformation is not fully defined by what you've given. You have an $N$-gon in the plane centered at $p$, but you don't have anything that specifies how the $N$-gon is oriented in that plane. So, there is a continuum of transformations that will do the job.
This is why you are having trouble figuring it out. There isn't enough information to solve the problem without making some arbitrary choices. So, let's make an arbitrary choice, and then move on with that choice.
Pick an arbitrary point $\vec q \ne \vec p$ on $P$ and set $$\hat x = \frac {\vec q - \vec p}{\|\vec q - \vec p\|}$$ This unit vector will be our choice for the direction of "positive x-axis" in $P$. Define $\hat y = \vec n \times \hat x$. Then since $\hat y$ is also perpendicular $\vec n$, it points along $P$ as well, and is perpendicular to $\hat x$, and can serve as the positive y-axis direction in $P$.
Now we just define $A$ to carry $\hat i$ to $\hat x$, $\hat j$ to $\hat y$ and $\hat k$ to $\vec n$. $$A = \begin{bmatrix} \hat x & \hat y & \hat n\end{bmatrix}$$
(That is, the first column of $A$ is $\hat x$, etc.)


How to save a free text views query entered into an exposed filter by the user?

Question: I would like to save the entered exposed filter text by the user possibly into its own content type. Would I have to look at the url path for this via $_GET vars?
Comment: I must admit - I have no idea what you are trying to ask. Downvote is not from me. Close->Unclear is.
Comment: I think I understand what the question is, but it's kinda broad, and sounds like a basic PHP/HTTP question, and at this stage, unrelated to Drupal. So I vote off-topic. But to try to be helpful, yes, you'd start by examining the contents of $_GET to find the exposed filter settings.
Comment: Lets try to make is simpler: User goes to Views, user enters a filter query. I would like to save the query that is entered. :)
Answer: Use hook_form_alter and add an extra submit handler:
<code>$form['#submit'][] = 'my_callback';
</code>
In your callback function use node_save to save the values you extracted from <code>$form_state['values'];</code>
Comment: Awesome! Thx! I appreciate it.
Comment: Glad I could help :)


DTO inheritance in Nest js ony includes properties from parent class

Question: I'm tryng to create a DTO Class responsible for parsing paginated query filters.
So I created this class:

    import { Transform } from 'class-transformer';
    import { IsNumber, IsOptional, IsEnum, Min } from 'class-validator';
    import { SortOrder } from '../enums/SortOrder';
    import { toNumber } from '../helpers/toNumber';

    export class GenericFilter {
      @Transform(({ value }) => toNumber(value, { default: 1 }))
      @IsNumber({}, { message: ' "page" atrribute should be a number' })
      @Min(1)
      public page?: number = 1;

      @Transform(({ value }) => toNumber(value, { default: 50 }))
      @Min(1)
      @IsNumber({}, { message: ' "pageSize" attribute should be a number ' })
      public pageSize?: number = 50;

      @IsOptional()
      public orderBy?: string;

      @IsEnum(SortOrder)
      @IsOptional()
      public sortOrder?: SortOrder = SortOrder.DESC;
    }

Then I try to extend this class from other DTO for specific cases of filtering, like this:
    import { IsDateString, IsNumber, IsOptional } from 'class-validator';
    import { GenericFilter } from 'src/common/interfaces/GenericFilter';

    export class OperationLogFilterDto extends GenericFilter {
      @IsDateString()
      @IsOptional()
      public dateFrom?: Date;

      @IsDateString()
      @IsOptional()
      public dateTo?: Date;

      @IsNumber()
      @IsOptional()
      public userId?: number;
    }

And use it like this:
    import { Controller, Get, Query } from '@nestjs/common';
    import { OperationLogFilterDto } from './dto/OperationLogFilter.dto';
    import { AuditService } from './audit.service';

    @Controller('audit')
    export class AuditController {
      constructor(private service: AuditService) {}

      @Get('/operation-logs')
      getAllOperationLogs(@Query() filters: OperationLogFilterDto) {
        console.log(filters);
        return this.service.getOperationLogs(filters);
      }
    }

The problem is that the console log shows only the properties from the Parent class, and ignores any other filters I send that are included in OperationLogFilterDto
IE:

How can I solve the problem?
Answer: I just had a typo on the request I was using to test..


Can I aggregate, proxy, or imitate @SuppressWarnings in Java?

Question: I use Spring to build a ModelMap, which is rendered and then parsed by Mustache. For this reason, I have some classes that only exist to wrap data for Mustache's convenience.
These classes have fields and methods that are never used in the code, but are in fact used by Spring (and, in turn, Mustache). I want to explicitly state that this is intentional by annotating the class with a custom annotation @ForMustache that looks like this:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface ForMustache {
}

My goal is basically a fancy way of saying @SuppressWarnings("unused") // For Mustache.
I attempted to annotate ForMustache with @SuppressWarnings("unused"), but its effects apply inside my annotation instead of being propagated to the type annotated with @ForMustache.
Can I proxy or imitate @SuppressWarnings("unused") with another annotation?
Comment: No, these annotations are meant for the compiler and it will only apply them verbatim to the element they are on. But maybe there is a way not to trigger that warning.
Comment: [This question](http://stackoverflow.com/questions/33345605/java-custom-annotation-aggregate-multiple-annotations) is very closely related to what you're trying to do (you're just trying to create a meta-annotation that only contains one annotation rather than multiple). This may even be a duplicate of that question.
Comment: @zapl Thanks for the hint, I'll try to investigate!
Comment: @JonK No, I don't think it is, because of the nature of SuppressWarnings. Besides, The annotations are processed by Spring, so "meta-annotations" are available to me.
Answer: The @SuppressWarnings annotation is meant for the compiler and other compile time tools that work directly on the source files.
It is also specified as
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings { ...

meaning it's not supposed to be available anymore at runtime. This means that even springs meta-annotation aware processing can't see it anymore.
Given enough time, you could build an annotation processor that, during compile-time, transforms a custom @ForMustache annotation into a @SuppressWarnings so the compiler can pick it up. In similar fashion Lombok can turn
@Data
class Foo {
    private final String value;
}

into a) code that compiles and doesn't complain about no assignment to a final field and b) it wouldn't generate a warning because it generates a public getter for the field. Those can only be assumed to be used / they are usable from places outside the compiler's reach.
Besides putting @SuppressWarnings everywhere, your only chance is to remove the condition that causes the warning.

Disable the check with a compiler option
add public getters or other code so the fields are used (with lombok for example)
hook into the compile process.
Comment: Great answer! I did consider the option of writing an annotation processor, but it seems like way too much effort for this goal. Thanks a lot for your suggestions!


Exibir o nome do usuario logado pelo email e senha cadastrados no firebase no aplicativo

Question: Olá, preciso da ajuda de vocês, pois já busquei de varias maneiras e não consigo resolver.
Estou querendo exibir o nome do usuário logado no aplicativo pelo Firebase.
NO Edititext o retorno é essa linha com link para o firebase (.firebaseio.com/usuario/9FrkXtxNuDW97HI0JtrGufquIxQ2/email).
Estou usando o seguinte código na activity:
<code>final String user_id = mAuth.getCurrentUser().getUid();

mDatabase = FirebaseDatabase.getInstance().getReference().child("usuario").child(user_id).child("email");

mEditText.setText(mDatabase.toString());
</code>
Funciona, mas exibe somente aquela url. O que é que está faltando aí. O Usuário entra na aplicação com o login email/senha do Firebase, e vai para MainActivity, onde executo este comando para exibir o nome. Mas, não parece o nome que está cadastrado no Firebase:

Alguém pode me dar uma ajuda?
Answer: Everlanio para exibir o nome do usuario logado você não precisa buscar os dados no Banco. Você terá que recuperar utilizando o Auth de Login.
[Firebase Auth]: https://firebase.google.com/docs/auth/android/manage-users?hl=pt-br "
Comment: Eu fiz da seguinte forma, não sei se a mas correta porem a que resolveu para mim.
Eu so salvo os dados no firebase apos o usuario logar.
Usuario loga em uma activity , ele é direcionado para a activity principal do projeto onde faco a verificação se esta logado msm.
Caso esteja salvo no Realtime Database.
if (mAuth.getCurrentUser() != null) {
            keyUsuarioLogado = mAuth.getCurrentUser().getUid();
            userLogado.setIdUsuario(mAuth.getCurrentUser().getUid());                      dataUser.child(keyUsuarioLogado).setValue(userLogado);
Comment: Bom dia Allef Sousa. Fiz conforme me indicou, mas percebi que ele está exibindo um novo usuário, e não o usuário que foi logado na entrada do app, com a tela de login. a id do usuario  é: "**ZXZlcmxhbmlvQGhvdG1haWwuY29t**". Mas quando entra na main activity a id do usuario que é apresentado é esta: "**9FrkXtxNuDW97HI0JtrGufquIxQ2**". Por isso ele retorna **null**. Como posso fazer para que o usuário logado seja o mesmo do login.
Comment: Ok, meu amigo! Obrigado por sua ajuda. consegui resolver.


How to create the interaction term when one of the variables is transformed?

Question: In my study, I am hypothesizing an interaction effect of variables $x_1$ and $x_2$ on $y$.
Because the univariate distribution of $x_2$ is not normal, I have log-transformed the $x_2$ variable.
I was wondering how to create the interaction term. Should the fitted model include a multiplicative term of $x_1$ and untransformed $x_2$, or $x_1$ and transformed $x_2$?
Similarly, for plotting the interaction, should I include the transformed or untransformed $x_2$?
Comment: Why do you need the marginal distribution of $x_2$ to be normal?
Comment: @MatthewDrury, In my case, when I included the untransformed x2, the residual diagnostics indicated violations. This got rectified when I used the log-transformed x2. That is the reason I was wanting to keep the transformed x2.
Comment: Ok, but that is very different from needing the marginal distribution of $x_2$ to be normal.
Comment: @MatthewDrury, I am not sure if I technically understand what you noted. Would you please help me get an intuitive sense of it? Thanks so much.
Answer:
My suggestion is that it could be either, but it depends on what you want to do with the interaction term. Said another way, the interaction term is going to change the slope for one of the variables in the interaction. Let me start with a much simpler scenario.
If you have the model
$$y=b_0 + b_1 x + b_2 a + b_3 xa$$
and if we assume $x$ is a dummy variable (say 0 for male and 1 for female), and $a$ is a scalar variable (say age), then we can think of the interaction term as a change in slope parameter for one of the "groups." The most straightforward example with this scenario is to compare the male group ($x=0$) with the female group ($x=1$). Respectively, we have would have
$$\begin{align}\text{Males:  } & y = b_0 + b_2·a \\ \text{Females: } & y = (b_0 + b_1) + (b_2 + b_3)·a\end{align}$$
The interaction term changed the slope of $a$ from males to females.
Now, it is a little stranger to conceptualize this when working with an actual grouping variable (like $x$ for gender) vs. a scalar variable (like $a$ for age)....but, let's say we want to ask what the difference is for groups #1 and #2, where the 2nd group is one year older. The equations for these groups are
$$\begin{align}\text{Younger: } & y = (b_0 + b_2a_0) + (b_1 +b_3a_0)· x \\ \text{Older: } & y = (b_0 + b_2a_0 + b_2) + (b_1 + b_3a_0 + b_3)·x\end{align}$$
where group #1 is age $a=a_0$, and group #2 is age $a=a_0+1$. Now, $x$ is a dummy variable, so its "slope" would be interpreted as the difference in the groups, and in this case, the one year difference in age between the groups causes a difference in the gender.
So, ¿how does this apply to your query? Well, if you want to interpret the interaction term from the perspective of how it changes the slope for your transformed variable, then you would need to have the transformed variable in the interaction:
$$y = b_0 + b_1 t + b_2 a + b_3 ta$$
where $t$ is the transformed variable and $a$ is the other variable.  When $a$ is some fixed value, $a_0$, then this equation can be written as
$$y = (b_0 + b_2 a_0) + (b_1 + b_3 a_0)· t$$
Again, the interaction term is just changing the slope for the transformed variable. If you have the untransformed variable, say $u$, in the interaction term, this "change of slope" picture becomes much more complicated (if even possible to calculate in terms of change of $t$).
If, on the other hand, you care more about the change in slope for the other variable, $a$, then it really doesn't matter if the interaction is transformed or not:
$$\begin{align}y = (b_0 + b_1 t_0) + (b_2 + b_3 t_0)·a \\ y = (b_0 + b_1 t_0) + (b_2 + b_3 u_0)·a\end{align}$$
whether you use the transformed value ($t_0$) or the untransformed value ($u_0$), the change in slope for $a$ is quantifiable.
Comment: Thank you so much. This very clearly explains when to use transformed variable in interaction.


Group of order 24 has subgroup of index 4

Question: I have a question .
A group of order 24 will have a subgroup of index 4 .
Is this always true ?
I tried proving but this can't find any way  .
I searched for classification of group of order 24  on Wikipedia.
It is given  there are 15 non- isomorphic groups of order 24 and each has a subgroup of order 6 .
So here definitely I can conclude that there is subgroup of index 4 in every group of order 24.
Now , I want to show this explicitly.
I tried to show it using class equation by considering possibilities of cardinality of conjugacy class , but i couldn't show that class equation necessarily has a conjugacy class of Cardinality 4 .
Can someone please help me with this ? What are possible approach to tackle such problem?
Comment: Let $G$ be a group of order $24$, let $T$ be a Sylow $3$-subgroup, and let $n_{3}$ be the number of Sylow $3$-subgroups. Sylow's theorems imply that $n_{3}$ is $1$ or $4$. If $n_{3} = 4$, then the normaliser of a $T$ has index $4$. If $n_{3} =1$, then $G/T$ is a group of order $8$, so it has a subgroup of index $4$, and by the correspondence theorem so has $G$.
Answer: I have slightly expanded my comment above into an answer.
Let $G$ be a group of order $24$, let $T$ be a Sylow $3$-subgroup, and let $n_{3}$ be the number of Sylow $3$-subgroups.
Sylow's theorems imply that $n_{3}$ is $1$ or $4$.

If $n_{3} = 4$, then the normaliser of $T$ has index $4$.
If $n_{3} =1$, then $T$ is normal in $G$, and $G/T$ is a group of order $8$, so it has a subgroup of index $4$, and by the correspondence theorem so has $G$. Or, in a more elementary fashion, if $D$ is a subgroup of $G$ of order $2$ (which exists by Cauchy's theorem), then $T D$ is a subgroup of index $4$.
Comment: How can we say normaliser of $T$ is normal in $G$ and the corresponding index is $4$?
Comment: @MessiLio, the normaliser of a Sylow subgroup is self-normalising, so if it is normal in $G$, it coincides with $G$. If $n_{3} = 4$, then by Sylow's theorems the nornaliser has index $4$, but I am not claiming it is normal in $G$.
Answer: Andrea's answer is best possible. However, in case you are familiar with supersolvable groups you can check your list you have - for any finite supersolvable group, there are subgroups of every possible order, i.e., for $|G|=24$ there are proper nontrivial subgroups of orders $2,3,4,6,8,12$, hence of index $12,8,6,4,3,2$. This is "Langrange Converse", see for example the post
Complete classification of the groups for which converse of Lagrange's Theorem holds
Now in your list, from the $15$ groups, all but three are supersolvable, namely $S_4$, $A_4\times C_2$ and $SL(2,3)$ are not supersolvable. For them, it is true by direct inspection.


How do I delete all the user's posts, using drush?

Question: I frequently need to delete all comments/posts of a particular abusive user. It takes a hell of time to accomplish this with a browser. It would be great if someone could tell me how to do it using drush.
Answer: You could run something like this via drush php-script:
<?php
global $user;
$user = user_load(1);
$result = db_query("SELECT nid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE u.name = '%s'", 'alice');
while ($row = db_fetch_object($result)) {
  node_delete($row->nid);
}
?>

There's also http://drupal.org/project/user_delete.
Comment: I probably don't need to try the script, as the 'User Delete' module seems to be exactly what I was looking for. Thanks Dylan.


Theorem 11.2.i; Elementary Analysis, Ross 2nd Ed

Question: I am having trouble understanding what this theorem means:
11.2 Theorem
Let $(s_n)$ be a sequence.
i. If t is in $\mathbb{R}$, then there is a subsequence of $(s_n)$ converging to t if and only if the set $\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$ is infinite for all $\epsilon > 0.$
Does this mean:
If the $lims_n =t$ for $n\geq N$ and the set of all n's, for which this is true, is infinite,  then $\exists (s_{{n}_{k}})$ such that $(s_{{n}_{k}}) \rightarrow t$ (i.e. I can pick some n's from my infinite set to create indices of a subsequence that will converge to t)?
&
Suppose I have some $(s_{{n}_{k}}) \rightarrow t$, then there exists an infinite set of n's such that $\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$
(i.e. If I can find a subsequence, from the given parent sequence, that converges to t, then there must exist an infinite set of indices that create a sequence convergent to t)?
Particularly, I am confused about the usage of infinite and finite sets in both cases. I am interpreting that there are infinite subsequences that converge...
Answer:
Suppose I have some $(s_{{n}_{k}}) \rightarrow t$, then there exists an infinite set of n's such that $\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$
(i.e. If I can find a subsequence, from the given parent sequence, that converges to t, then there must exist an infinite set of indices that create a sequence convergent to t)?

This is correct. Note how this is just the forward implication of the if-and-only-if statement.

Let $(s_n)$ be a sequence.
i. If t is in $\mathbb{R}$, then there is a subsequence of $(s_n)$ converging to t if and only if the set $\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$ is infinite for all $\epsilon > 0.$

You also say

If the $lim\ s_n =t$ for $n\geq N$ and the set of all n's, for which this is true, is infinite,  then $\exists (s_{{n}_{k}})$ such that $(s_{{n}_{k}}) \rightarrow t$ (i.e. I can pick some n's from my infinite set to create indices of a subsequence that will converge to t)?

This is where I think you're confusing things. You say that $\lim_{n \to \infty} s_n = t $, but the theorem is general, it does not require that $s_n$ should converge. It holds for any $s_n$.
I think where you are confused are is that you are thinking that the statement

$\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$ is infinite for

is saying that after a $N$, all elements of the sequence must follow this. But it is actually saying that $|s_{n}-t|<\epsilon$ must be true for infinite values of $n$, irrespective of where these elements are in the sequence.
For example, consider the sequence $<s_n> = <t_1, u_1, t_2, u_2, ....>$ where $t_n \to t$ and $u_n \to u, u \neq t$. Then there is no $n \geq N$ for which $\{|s_{n}-t|<\epsilon \}$ or $\{|s_{n}-u|<\epsilon \}$ is true, yet there exist two subsequences of $s_n$ which converge to limits $t,u$ respectively.
In the context of this example, what the backward implication of the theorem is saying is that it is possible that there is a subsequence of $s_n$ which converges to $t$ only because there are infinite terms of $s_n$ which satisfy $\{|s_{n}-t|<\epsilon \}$ and similarly for $u$.
That is, in general, if the set $\{n \in \mathbb{N}: |s_{n}-t|<\epsilon \}$ is infinite for all $\epsilon > 0$ then there must be a subsequence of $s_n$ which converges to $t$. If it is finite then there cannot exist a subsequence of $s_n$ which would converge to $t$.


How to manually choose where the data is displayed on a heatmap in Python with Matplotlib or Seaborn?

Question: I have the following table in a pandas DataFrame, with the stats for a quarterback:
||    pass_type   |    WPA    ||
||----------------|---------- ||
|| deep - left    | -0.202351 ||
|| short - left   | 0.456855  ||
|| deep - middle  | -1.698436 ||
|| short - middle | 0.879427  ||
|| deep - right   | 0.378192  ||
|| short - right  | 1.448731  ||

I would like to display those stats in a heatmap, but I'd like to have the possibility to choose where each value is displayed. My goal is to "recreate" the field, with the first row being the short passes, the 2nd row being the long passes, the left column being the left passes, etc.
To be precise, what I'd like to get is:
deep  || -0.20 | -1.69  | 0.37  ||
short || 0.45  | 0.87   | 1.44  ||
---------------------------------
      ||  left | middle | right ||

I have been able to do it using plotly, by creating two lists, one with deep passes and the other with the short ones. But I would like to be able to do it using Seaborn or Matplotib, as I like their format options + it's easier to publish in a Jupyter Notebook on github.
Comment: *I have the following table* what do you use to store that table? list? numpy array? pandas dataframe?...
Comment: It's a pandas Dataframe. Sorry, forgot to mention it. I've edited my initial question.
Answer: You can separate the two parts with str.split:
plot_data = (df.join(df['pass_type'].str.split(' - ', expand=True))
               .pivot(index=0, columns=1, values='WPA')
            )

Then you would get plot_data as:
1          left    middle     right
0
deep  -0.202351 -1.698436  0.378192
short  0.456855  0.879427  1.448731

which you can pass to sns.heatmap:
sns.heatmap(plot_data)

Output:


Django TCP socket communication

Question: I am new to TCP socket programming. I have a django based server communicating with a microcontroller. Now, I want to implement TCP based socket on the server side in order to communicate with the TCP socket on the microcontroller. Can anyone give me an idea on how to do this ? What libraries should I use on my django server  The microprocessor basically opens the socket every 5 seconds and sends a notification to the server. I on the server side should be able to read this and pump data back to the microprocessor using this socket which was opened by the microprocessor.
Comment: Do you have experience with Django?
Answer: If you are already familiar with django, and your microcontroller supports sending HTTP (REST) requests and can parse Json , you can just add a regular django based view that returns json:
# views.py
from django.http import JsonResponse
def my_view(request):
    # handle input here, probably using request.GET / request.POST
    return JsonResponse({'status': 'OK'})

However, if your microcontroller is very simple and cannot do HTTP/json, you can use a simple SocketServer from python's standard library instead:
import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

Keep in mind you can still import django's libraries and use django's ORM (DB) inside a SocketServer.
Another popular option is to use Tornado's tcpserver:
from tornado.ioloop import IOLoop
from tornado.tcpserver import TCPServer

class MyTCPServer(TCPServer):
    def handle_stream(self, stream, address):
        def got_data(data):
            print "Input: {}".format(repr(data))
            stream.write("OK", stream.close)

        stream.read_until("\n", got_data)

if __name__ == '__main__':
    server = MyTCPServer()
    server.listen(9876)
    IOLoop.instance().start()

Django TCP socket communication


Why is linux not listing the IP I want?

Question: I have a linux based cluster (using openMosix). The machine intended to be used as a server has 2 network cards. Cluster is working, and all machines ping to each other inside the cluster (internal network).
However, I set the server's public IP as static, to be XXX.XXX.198.247. If I do a ifconfig in the console, it shows me that the machine is indeed xxx.xxx.198.247. Pinging "myself" does work. However, If I use an external site to check my IP (i.e. ping.eu) it shows me as xxx.xxx.198.231. Trying to ping  both xxx.xxx.198.247 and xxx.xxx.198.231 from the syte or from other machines conected to the internet results in no response (total package loss).
Here's my etc/network/interfaces file:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
auto eth0
iface eth0 inet static
address 192.168.1.105
netmask 255.255.255.0
gateway 192.168.1.1

# The secondary network interface
allow-hotplug eth1
iface eth1 inet static
address xxx.xxx.198.247/24
netmask 255.255.255.224
network xxx.xxx.198.0
broadcast xxx.xxx.198.255
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers xxx.xxx.195.14
dns-search laborator[/code]

I'd apreciate any help.
Comment: This is a routing problem.
Answer: What's at 198.231?  Presumably it's a NAT address for traffic being sent to the internet via your 192.168.1 network?
Your default gateway's on the inside interface, so that's the path that will be taken to get the internet.  You'll need to determine what you want this server's networking configuration to look like from a routing perspective; perhaps default gateway on eth1 and a static route for 192.168.0.0/16 on eth0?

Why is linux not listing the IP I want?


C++ template function from callable class instance to std::function

Question: I do need std::function for stateful operation.
So I would like to make an instance of a class implementing operator() into std::function. I did test std::bind works, but can't wrap it up as a function.
struct StatefulOperation
{
    StatefulOperation(int num) : num_(num) {}
    int operator()(int i)
    {
        num_ += i;
        return num_;
    }

    int num_;
};

// This works

const StatefulOperation obj(40);

using std::placeholders::_1;
std::function<int(int)> op = std::bind(&StatefulOperation::operator(), obj, _1);

// Want something like this

template<typename A, typename B, typename C>
std::function op(C& obj);

// This doesn't work

template<typename A, typename B, typename C>
std::function op(C& obj)
{
    using std::placeholders::_1;
    return std::bind(&C::operator(), obj, _1);
}

The last implementation attempt fails with error like below;
main.cpp:52:21: note:   template argument deduction/substitution failed:
main.cpp:75:35: note:   couldn't deduce template parameter 'A'
   75 |     std::function<int(int)> f = op(a);
      |                                 ~~^~~

How can I get this done?
Answer: The compiler cannot deduce A and B just from the return type. You need to specify them:
std::function<int(int)> f = op<int, int>(a);

However, you don't really need these additional template parameters. Just use auto to deduce the return type:
template<typename C>
auto op(C& obj)
{
    using std::placeholders::_1;
    return std::bind(&C::operator(), std::ref(obj), _1);
}

The return type will not be exactly the same, but convertible to what you want (std::function). If you want to specify the exact return type, you can do so with a combination of this and std::invoke_result_t.
Comment: perhaps worth mentioning that `auto` will also not deduce the right types for the `std::function` but the type returned can be converted to `std::function`
Answer: Instead of using bind you should use lambda expressions. Also you StatefulOperation should NOT be const (otherwise it can't update its internal state).
#include <functional>
#include <iostream>

struct StatefulOperation
{
    StatefulOperation(int num) : num_(num) {}
    int operator()(int i)
    {
        num_ += i;
        return num_;
    }

    int num_;
};

int main()
{
    /*const*/ StatefulOperation obj(40); // your state cannot be const since you are changing its internal state

    // instead of bind rely on lambda expressions (bind is still there because of well... history)
    std::function<int(int)> op{ [&](int num) { return obj(num); } };

    for (int i = 0; i < 10; ++i)
    {
        std::cout << op(i) << " ";
    }

    return 0;
}

Answer: Since your class is a callable that implements a function call operator with the expected types, you can initialize a std::function in the most simple way:
std::function<int(int)> op{StatefulOperation(40)};

Do not forget that the state is copied when op is copied.


Ajuda com formulário HTML

Question: Desculpem o título do post, é que não estou pensando em algo pra especificar o que estou querendo (fiquem à vontade pra editar). É o seguinte:
Tenho um formulário em html e um botão:
<form id="formflor" method="post">

  <input type="text" class="form-control" id="codigo" name="codigo" placeholder="código de barras">
  <input type="text" class="form-control" id="nome" name="nome" placeholder="Rosa">
  <textarea class="form-control" id="informacoes" name="informacoes" rows="3" placeholder="descreva aqui..."></textarea>
  <input type="file" id="imagem" name="imagem">
  <button type="button" class="btn btn-primary" onclick="editaFlor(1, 'jasmim');">Enviar dados</button>
</form>

que ao ser clicado chama uma função jquery:
function editaFlor(id, descricao){
  if (confirm("Confirma a alteração de " + descricao + "?"))
  {
    var myForm = document.getElementById('formflor');
    var form = new FormData(myForm);

    $.ajax({
      type: "POST",
      url: "functions/editarFlor.php",
      data: form,
      cache: false,
      contentType: false,
      processData: false,
      success: function(data) {
        if (data == 'ok'){
          alert(descricao + ' editado com sucesso!');
          listaFlor();
        }
        else{
          alert(data);
        }
      }
    });
  }
}

Esse form funciona normalmente, sendo que eu preciso do seguinte: incluir o parâmetro id no formulário form e enviá-lo junto com os dados do formulário.
Existe alguma forma de fazer isso?
Comment: Já tentou `form.append('id', document.getElementById('inputID').value);` ?
Answer: Você pode adicionar o seu id ao FormData
function editaFlor(id, descricao){
  if (confirm("Confirma a alteração de " + descricao + "?"))
  {
    var myForm = document.getElementById('formflor');
    var form = new FormData(myForm);
    form.append('id', id);
    //...resto do código

Answer: Você pode colocar um outro input no seu formulário com o ID e esconder esse campo. algo como
<input type="hidden" name="id" value="2">

No value você joga o ID que vc precisa.
Comment: Onde essa informação de ID está? Você renderiza a página com ela ou recupera com javascript?
Comment: Você pode, então, passar o id na querystring `url: "functions/editarFlor.php?id="`.
Comment: O @LeandroAngelo deu uma possível solução, passe o id pela url e recupere esse valor. Só tome cuidado para validar esse valor antes de usar.
Comment: @ItaloRodrigo esquece, é uma péssima ideia, só em última alternativa
Comment: infelizmente, não posso alterar o formulário, tem que ser no botão, pois terei diversos botões para cada item
Comment: A página é criada com um script PHP que já gera os buttons com is respectivos id

Ajuda com formulário HTML


Notating hitting cymbal with shoulder vs tip of stick

Question: I am writing a pep band piece (although the type of piece probably makes no difference) and  the snare part is going to have syncopation between hitting the marching cymbals (as if they are ride cymbals) with the shoulders of their sticks (as seen in the first image) and the tips of their sticks (as seen in the second image). Is there an "proper" or "typical" way to notate this or should I just make a key at the beginning of the music?

Comment: I'd have said the 1st pic (shoulder) is more of a crash, 2nd pic (tip) more of a ride.
Comment: I suspect the best answer is to notate for what you want to hear and let the drummer decide how to best produce that sound in context. Micromanaging stick technique will probably not get you what you really want - a great performance that fits the character if the piece. A popular way to notate any syncopation is with accent marks.
Comment: Watching with interest, though I note that they're different in which part of the cymbal is being struck, not just which part of the stick is doing it!
Comment: @AndyBonner - that's basically the difference between crashing and riding.
Answer: Hitting with the tip would be more like a ride strike, and the oft-used symbol is x on the top line (equivalent to F on treble clef), and the other is going to produce a crash, written as x on the leger line above (A line, treble clef), But as drum music can vary quite a bit, and is only sometimes intuitive, it's worth fortifying this with a note at the top of the music. No confusion then!
Answer: In Behind Bars, Elaine Gould includes a section on "Beating spots (striking points)". Her first recommendation is to just write words above the notes.

Different locations on an instrument's surface can be indicated in words: e.g., at the edge, on the rim, dome, center, etc. (p. 295)

Her alternative, for a single percussion part (i.e., not multiple instruments on the same staff), is to label each staff line as a position on the instrument. She gives the example of a suspended cymbal with three staff lines representing, from bottom to top, "rim", "midpoint", and "dome". (p. 295)


The significance of the one in the derivatives of inverse hyperbolic functions

Question: I have noted  the value of one in the derivatives and wondered if it's fundamental then in my study I encountered several examples of this sort:
$$ \int \frac {dx}{a^{2} + x^{2} } = \frac {1}{a} \tan ^{-1}{\left(\frac{x}{a}\right)},
$$
which I had no problem proving.
The intuition I developed was that we could derive a  relation between the integrals of  the divisions of an angle and the number of divisions.

Questions that arose:

Is it not possible to simply divide the angle and get the integral for the result like it's an angle on its own or is there a certain significance?
Does $a $ have to be an integer? Could it be a function or is that nonsense (and why is it nonsense)?
Answer: This is just a change of variable. Consider $$I=\int \frac {dx}{a^{2} + x^{2} } $$ Let $x=a y$, $dx=a \,dy$ to make $$I=\int \frac {a\,dy}{a^{2} +a^2 y^{2} }=\frac 1a \int \frac {dy}{1 + y^{2} }$$ $a$ being anything you want (integer, rational, irrational, complex, ...) as long as it is not $0$.


Call a server Ip simulating domain name

Question: in order to migrate my domain name to my new server, I would like to test it with real domain name.
I think we can call Ip server giving as argument domain name ?
How can I do that ?
Something like that :
IP ADRESS + Domain name as argument ------> Server getting domain name in HTTP_HOST $_SERVER
Thank you !
Edit : I would like it with web browser maybe with addon
Answer: Use telnet:
telnet <ip> 80
GET / HTTP/1.1
HOST: example.com
<return>

It should return a 200 return code and your / document for the site example.com.
Comment: Thank you, but can't we do that with browser ? (I forgot to specify that)
Answer: Just add the IP address and the host name (separated by a space or tab) in the hosts file (/etc/hosts on Linux, %windir%\system32\drivers\etc\hosts on Windows). Any application of the system (including your browser) will then use that IP address when you use the specified host name.

Call a server Ip simulating domain name


What is cosX-cosY when X and Y are uniform random variables

Question: Let $X$ and $Y$ be iid uniform  $$X,Y \sim U[-\pi,\pi]$$
Consider the following $$ U = cos (X)$$ $$V=cos(Y)$$ What is the distribution of $$W=U-V$$
I know that $$f_{U}(u) = \frac{1}{\pi\sqrt{1-u^2}}\hspace{1cm} -1 \leq u\leq 1$$ $$f_{V}(v) = \frac{1}{\pi\sqrt{1-v^2}}\hspace{1cm} -1 \leq v\leq 1$$
So then should I be taking the convolution $$\int_{-1}^{1} f_{U}(w+v) f_{V}(v)dv $$ Is this the right way to go? Any help is appreciated.
Thanks
Answer: You will probably have a better luck going to the Fourier transforms of the densities (use contour integration), multiplying them, then using the inverse Fourier transform to get the density of the difference (see Characteristic function).
Comment: I don't think there is a nice closed form of the Fourier of this type of densities.
Comment: Try to use contour integration.


while True dentro de while True

Question: como faço pra finalizar a sessão totalmente no while True dentro de outro? Ex: Eu fiz com break, porém está finalizando somente ele dentro de outro (while True).
olha um exemplo abaixo.
while True:
    test = input('Digite pra ir pro proximo while True: ')
    while True:
        test1 = input('Digite (S) pra sair totalmente: ').upper()
        if test1 == 'S':
            break

Comment: Isso não parece fazer muito sentido, certamente deve escrever esse código de outra forma e aí esse problema não existiria.
Comment: sim, o break só sai da execução do último while... que tal ao invés de usar um `while true`, colocar uma condição que possa sair por sí própria?, por exemplo algo como `while test1 != "S"`?
Comment: while True:
    test = input('Digite pra ir no proximo while True: ')
    test1 = input('Digite (S) pra sair totalmente: ').upper()
    while test1 != "S":
        test1 = input('Digite (S) pra sair totalmente: ').upper()
    else:
        break
Comment: ficaria desse jeito?
Comment: Maniero, e qual seria outra forma?
Answer: Se é uma situação criada como um passa tempo ou quebra cabeças, pode-se levantar uma exceção tratada:
while True:
  try:
    test = input('Digite pra ir pro proximo while True: ')
    while True:
        test1 = input('Digite (S) pra sair totalmente: ').upper()
        if test1 == 'S':
            raise RuntimeError()
  except RuntimeError:
    break

Mas o ideal é a refatoração do código:
test = None
while True:
  if test is None:
    test = input('Digite pra ir pro proximo while True: ')
  else:
    test1 = input('Digite (S) pra sair totalmente: ').upper()
    if test1 == 'S':
       break

Answer: Uma outra maneira de sair seria criar uma variável de apoio, ex:
exit = False
while True:
    test = input('Digite pra ir pro proximo while True: ')
    while True:
        test1 = input('Digite (S) pra sair totalmente: ').upper()
        if test1 == 'S':
            exit = True
            break
    if exit:
        break

Ou:
exit = False
while not exit:
    test = input('Digite pra ir pro proximo while True: ')
    while True:
        test1 = input('Digite (S) pra sair totalmente: ').upper()
        if test1 == 'S':
            exit = True
            break

Answer: de acordo com esse link https://stackoverflow.com/a/9038199/2258921 o recomendado é usar um return, já que em python não é possível usar um break 2.
Comment: e se precisar processar algo dentro do primeiro while, ou ainda depois dele? não me parece uma boa solução
Comment: Nesse caso, você não deveria usar while true para o seu segundo while e sim uma condição que você possa controlar, assim como você comentou: while test1 != "S". Mas como a pergunta começa com "como faço pra finalizar a sessão totalmente no while True dentro de outro", creio que o desejo não é executar algo no primeiro while e sim apenas terminar a execução.
Comment: é esse mesmo o ponto, talvez colocar isso na sua resposta fique melhor... é como "olha, o return faria isso, mas não é a melhor solução, bla bla bla", é legal sempre dar uma melhor solução. é como aquela brincadeira "meu código está dando erro! comenta ele que para de dar erro" :)
Comment: Não tem lógica usar dois While True, utilize apenas um e controle o que vc quer com if

while True dentro de while True


Managing page turns with a PDF file

Question: There's a once lively discussion about page turning here which deals alot with page turns of the wood pulp variety. However, all of my music is in PDF format and I read it from a computer.
I wasn't able to find much in a google search for page turning software for Mac.
Does anybody have any suggestions or know of a good way to get the PDF to turn? I suppose mapping a key combination might be one way to speed it up, but I'm wondering if that's the best option. This is especially tricky because I like to magnify the PDF so that a full screen is about one half-page.
While we're at it, let's bring up page sizing. Most PDFs are 8.5 x 11 because in ancient times that was a typical dimension one would use when creating slices of wood pulp to put into the printing press. These days we have extra horizontal real-state on our screens. Does anybody know of any ways to take advantage of that extra screen space with standard 8.5x11 PDFs?
Comment: There are plenty of USB foot-pedals designed for exactly this purpose.  (with driver apps to do the page turning, etc). Do a little online shopping to find them.
Comment: Your paper size assumptions seem US-biased looking at [wikipedia, paper size](https://en.wikipedia.org/wiki/Paper_size). Anyway, printed sheet music does not use standard formats, and one can wonder, whether this intentionally to hamper copying and scanning.
Comment: There are reliable apps e.g. https://michaelng126.github.io/ai-page-turner/ which use your face gestures (e.g. winking) to turn pages. It's worked well for me :)
Answer: Here's an example of what you want -- a set of foot pedals designed precisely for this purpose. It works via Bluetooth, wirelessly.
AirTurn

I know several gigging jazz musicians who have their fake books and charts as PDF files on iPads, and use the AirTurn to flip pages with a tap of the foot while they are playing their instruments.
There are numerous apps for PC, Mac, iPad, iPhone, and Android that can be used to organize and display PDF files of sheet music, and can work with the AirTurn to flip pages. You can find a list of compatible apps at the AirTurn website at the link above.
Comment: This is really cool!  I've thought that this is a no-brainer to put together.  It would be a great thing for accompanists in chamber music as well.
Answer: As Carl Witthoft said, you can find some USB foot-pedals, I've never tried them, but it exists. But usually, what I do is putting all my sheets as images, not PDFs, and putting them on a PowerPoint presentation. Then, you just have to define how much time it takes to play one sheet and launch the presentation.
Positive point : Free and automatic.
Negative point : You have to be (or to use) a metronome.
Comment: Good idea! The foot pedal is not the best solution for me but I think it's a great idea
Answer: You can use auto scroll. Adobe Reader can do this.
And if you want to scroll accurately, PlayMusic.space have some web tools.
You can play music without turning page.
Answer: You would be best served with a custom application - designed especially for music.
I use and can recommend MuseScore on an iPad. It supports page-turner pedals (wired and wireless), such as this one, which would also work with a laptop or desktop PC.
MuseScore has a feature for mapping half a page at a time in landscape mode. Another function allows you to 'tear the page in half' so you can turn the top half of the page before you reach the bottom so you can see the relevant parts of both pages at once.
I'm sure most music will be in A4/letter format for many years to come, so features like those I described above are going to be far more use to you than anything that aims to replace this legacy format with something that better fits a computer screen.
If you are thinking of getting a tablet then I would strongly suggest trying to get something with a 4:3 screen aspect ratio because this fits a standard page of A4/letter with far less wasted space than widescreen 16:9.

Managing page turns with a PDF file


highcharts yaxis hours as actual hours not days

Question: I have a JSfiddle here: http://jsfiddle.net/mW9XD/
I'm struggling to get the amount of hours on the yaxis to actually show as the amount of hours rather than slip into days.  The example shows that after 24 hours it slips into being the 2nd of Jan rather than 25 or 26 hours.
my yaxis code looks somewhat like:
yAxis: {
            type: 'datetime',
            dateTimeLabelFormats: {
                 hour: '%H:%M:%S'
            },
            title: {
                 text: 'Hours'
            },
            min: 1000,
            startOnTick: false,
            showFirstLabel: false
        },

How can i get it to display the amount of hours rather than hours/days?
Answer: I changed my yAxis to look like this:
yAxis: {
           type: 'linear',
           labels:{
               formatter: function(){
                    var seconds = (this.value/ 1000) | 0;
                    this.value -= seconds * 1000;

                    var minutes = (seconds / 60) | 0;
                    seconds -= minutes * 60;

                    var hours = (minutes / 60) | 0;
                    minutes -= hours * 60;
                    return hours;
                }
           },
           title: {
               text: 'Hours'
           },
           min: 1000,
           startOnTick: false,
           showFirstLabel: false
      },

Works how i want it to, wish it were a bit more native though.


Correct setup for leave-one-subject-out cross-validation

Question: I've got a question concerning leave-one-subject-out cross-validation of a classifier and correct outlier handling in this case.
Let's suppose I've got 5 subjects. Within each subject the features are standardised (mean subtracted and divided by standard deviation) missing values or outliers are represented by NaNs.
Now I'm doing an outer leave-one-subject-out cross-validation. In every outer fold a classifier is trained on the data of 4 subjects and tested on the fifth held out subject. NaNs are filled with non-informational values i. e. the mean of the training or test set respectively.
First question: Setting NaNs to the mean of the training or test set is ok?
Second question: Is it ok not to standardise the features within the training set? So I have then standardised for every subject but then do not standardise them over subjects in the training set?
Now within each outer fold I plan to tune a classifier's parameter with help of another cross-validation.
Third question: Does the inner cross-validation necessarily need to be leave-one-subject-out as well? I suppose, but can't really see the differences and implications of using another folding for the inner cross-validation.
Fourth question: For the inner fold the NaNs again need to be set to non-informational values (mean) of the corresponding training "subset"?
Answer:
Setting NaNs to the mean of the training or test set is ok?

You need to define a procedure that you always follow. I see two valid options here:

either use some value (e.g. mean) calculated for that subject (see also below)
or some value calculated for the training set, basically a hyperparameter "value to be used for replacing NAs". This should not be calculated from the whole test set (independent testing also means that no parameters calculated from other test subjects should be used: the processing should not depend on the composition of the test set).

Edit:
Which method to follow (replace by value computed within subject or by value computed within training set) should IMHO be decided from the knowledge about the application and the data, we cannot tell you more than very general guidelines here.
Why not by value computed within test set?: That would mean that the value used to replace NAs in test subject A depends on whether subject A is tested together with subject B or subject C – which doesn't seem to be a desirable or sensible behaviour to me.
You may also want to look up "Imputation" which is the general term for techniques that fill in missing values.

Centering and scaling (standardization): if you have "external" (scientific) knowledge that suggests that a standardization within the subjects should take place, then go ahead with that. Whether this is sensible depends on your application and data, so we cannot answer this question.
For a more general discussion of centering and standardization, see e.g. Variables are often adjusted (e.g. standardised) before making a model - when is this a good idea, and when is it a bad one? and When conducting multiple regression, when should you center your predictor variables & when should you standardize them?

Now within each outer fold I plan to tune a classifier's parameter with help of another cross-validation.

With 4 subjects you probably won't be able to compare classifiers anyways: the uncertainty due to having only 4 test cases is far too high. Can't you fix this parameter by experience with similar data?
To illustrate the problem: assume you observe 4 correct classifications out of 4 test subjects. That gives you a point estimate of 100% correct predictions. If you look at confidence interval calculations for this, you get e.g. for the Agresti-Coull method a 95% ci of 45 - 105% (obviously not very precise with the small sample size), Bayes method with uniform prior makes it 55 - 100%. In any case it means that even if you observe perfect test results, it is not quite clear whether you can claim that the model is actually better than guessing. As long as you do not need to fear that fixing the parameter beforehand will produce a model that is clearly worse than guessing, you anyways cannot measure improvements in the practically important range.
The situation may be less drastic if you optimize e.g. Brier score but with 4 subjects I'd suspect that you still do not reach the precision you need for the expected improvement during the optimization.

Edit: Unfortunately, while 20 subjects are far more than 4, from a classifier validation statistics point of view, 20 is still very few.
We recently concluded that if you need to stick with the frequently used proportions for characterizing your classifier, at least in our field you cannot expect to have a useful precision in the test results with less than 75 - 100 test subjects (in the denominator of the proportion!). Again, you may be better off if you can switch to e.g. Brier's score, and with a paired design
for classifier comparison, but I'd call it lucky if that gains you a factor of 5 in sample size.
You can find our thoughts here: Beleites, C. and Neugebauer, U. and Bocklitz, T. and Krafft, C. and Popp, J.: Sample size planning for classification models. Anal Chim Acta, 2013, 760, 25-33.
DOI: 10.1016/j.aca.2012.11.007
accepted manuscript on arXiv: 1211.1323
AFAIK, dealing with the random uncertainty on test results during classifier optimization is an unsolved problem. (If not, I'd be extremely interested to papers about the solution!)
So my recommendation would be to do a preliminary experiment/analysis at the end of which you try to estimate the random uncertainty on the comparison results. If these do not allow to optimize (which I'd unfortunately expect to be the outcome), report this result and argue that in consequence you do not have any choice at the moment but fixing the hyper-parameters to some sensible (though not optimized) value.

Does the inner cross-validation necessarily need to be leave-one-subject-out as well?

If you do inner cross validation it would be better to do it subject-wise as well: without this, you'll get overly optimistic inner results. Which would not a problem iff the bias were constant. However, it usually isn't and you have the additional problem that due to the random uncertainty together with the optimistic bias you may observe many models that seem to be perfect. Among these you cannot distinguish (after all, they all seem to be perfect) which can completely mess up the optimization.
Again, with so few subjects I'd avoid this inner optimization and fix the parameter.
Comment: A. I just mentioned 5 to have a reference number when talking about "training the remaining 4 subjects" etc. In this particular
case, there is data from 20 subjects. Sorry for the confusion this may have caused.

B. About the two valid options for setting NaNs: I don't know if understand you correctly. I thought it might be a valid option to set in each fold the NaNs in the left out subject for this fold to the mean of this set, and the NaNs of the other data which is used to train the classifier in this fold to the mean of that data.So keeping training and test set in every fold independent.
Comment: C. Thanks for your pointers! I'll check the linked questions about standardisation and maybe come back with that again later. The question was less about if I should standardise data, but rather how - just within each subject or again for the whole cross-subject data.
Comment: @statikop: I'm not sure, but we may have been talking at cross-purposes about the replacement of the `NA`s. (I think you mean `NA` = not available as opposed to `NaN` = not a number [things like $\pm \infty$ or other results of calculations that do not yield a number]). Anyways, I tried to clarify my answer and also expanded on the 20 subjects a bit more in detail.
Comment: Thanks for your edit, remarks and pointers to the paper. I'll look through them next week. At first sight: I think in my case there is no problem with having 20 subjects since the number of subjects is very different from the number of samples -- there are more than 1000 samples per subject. Let me know if I'm getting it wrong on the fly, or otherwise I will notice so next week while looking at it in detail.
Comment: @statikop: the effective sample size of a data set with 1000 samples $\times$ 20 subjects can be *anywhere* between 20 and 20000 - I frequently have data of similar layout where the effective sample size is far closer to 20 than to 20000. Look e.g. at the example data in the paper I linked. Whether or not this is the case with your data cannot be said from outside, you'll have to check this. In the end the question boils down to how much more similar are samples of the same subject compared to samples of another subject.


is the quadratic equation appropriate for this?

Question: I work in a paper mill as a tech. There is a formula for percent solvents in a liquor solution. It is s=(A*P^2) + (B*P)
$S$ is the percent solvent, $A$ and $B$ are constants, $3.21953$ and $8.117$ respectively. I would like to solve for $p$, as instrumentation can tell me percent solvents, but I would like to verify the percentage for calibration purposes. I started moving stuff around and got confused, as college math classes were quite some time ago. I get that there is a squared variable there, so I have to square root something, and that percent solvents will never be negative, so that's probably an absolute value. This looks like a quadratic equation to me, if I replace the coefficients to $P^2$ and $p$ with $a$,$b$, and call percent solvents ($s$) $c$.
So then I subtracted $s$, and get $0= AP^2+Bp -C$. cool. So then I assumed I could use the quadratic equation,
$P= \frac{-b \pm \sqrt{b^2+4ac}}{2a}$.
Is this a safe assertion? Or have I misused this formula?
(also if someone who knows how to format math on this website were to come tidy this up for me it would be much appreciated.
Answer: Yes, this is how it would be done.
You would then have $\displaystyle P = \frac{-B \pm \sqrt{B^2-4As}}{2A}$.
When you evaluate both the $+$ and $-$, make sure you pick the $P$ for which the percent solvent makes sense physically.


Microsoft WPF Map Control - InvalidOperationException - "Cannot perform this operation while dispatcher processing is suspended."

Question: I have a WPF application built on .NET 4.5.  The latest version of the map control (version 18.104.22.168) is causing an issue if it is used within a datatemplate.  This does not happen with the previous version I was using from NuGet (version 22.214.171.124).    It is easy to reproduce the issue:
MainWindow.xaml:
<Window x:Class="MicrosoftMapError.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Content="{Binding ViewModel,
                          RelativeSource={RelativeSource Self}}">
   <Window.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Views/MapView.xaml" />
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Window.Resources>
</Window>

MainWindow.cs:
using MicrosoftMapError.ViewModels;
using System.Windows;

namespace MicrosoftMapError
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.ViewModel = new MapViewModel();
        }

        public object ViewModel
        {
            get { return (object)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }
        public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
            "ViewModel",
            typeof(object),
            typeof(MainWindow));
    }
}

MapView.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:wpf="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
                    xmlns:viewModels="clr-namespace:MicrosoftMapError.ViewModels">
   <DataTemplate DataType="{x:Type viewModels:MapViewModel}">
      <Grid>
         <wpf:Map />
      </Grid>
   </DataTemplate>
</ResourceDictionary>

The MapViewModel class is just an empty class; there is nothing interesting to see there.
And this is the stack trace for the exceptions.
Outer exception:
XamlParseException - The invocation of the constructor on type 'Microsoft.Maps.MapControl.WPF.Map' that matches the specified binding constraints threw an exception.
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
   at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
   at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
   at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
   at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
   at System.Windows.FrameworkElement.ApplyTemplate()
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.Border.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Window.MeasureOverrideHelper(Size constraint)
   at System.Windows.Window.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Interop.HwndSource.SetLayoutSize()
   at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
   at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
   at System.Windows.Window.SetRootVisual()
   at System.Windows.Window.SetRootVisualAndUpdateSTC()
   at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at MicrosoftMapError.App.Main() in d:\SoftwareDevelopment\Working\MicrosoftMapError\MicrosoftMapError\obj\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Inner exception:
InvalidOperationException - Cannot perform this operation while dispatcher processing is suspended.
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at System.Windows.Threading.Dispatcher.Invoke(Delegate method, Object[] args)
   at Microsoft.Maps.MapControl.WPF.Core.MapConfigurationFromWeb.ConfigLoaded(String requestKey, Dictionary`2 sections)
   at Microsoft.Maps.MapControl.WPF.Core.MapConfigurationFromWeb.GetConfigurationSection(String version, String sectionName, String culture, MapConfigurationCallback callback, Boolean reExecuteCallback, Object userState)
   at Microsoft.Maps.MapControl.WPF.Core.MapConfiguration.GetSection(String version, String sectionName, String culture, String key, MapConfigurationCallback callback, Boolean reExecuteCallback, Object userState)
   at Microsoft.Maps.MapControl.WPF.Core.MapConfiguration.GetSection(String version, String sectionName, String culture, String key, MapConfigurationCallback callback, Boolean reExecuteCallback)
   at Microsoft.Maps.MapControl.WPF.Map..ctor()

Comment: Why, on earth, are you creating a ViewModel property on your window?  The VM should be the DataContext of the window.
Comment: Also, you just might not be able to create that control in a resource....
Answer: Are dev team has looked into this and I believe they have found the issue. They are working on putting together a new release. Also, Microsoft never officially released a Nuget package, someone just took some old buggy libraries and posted them on Nuget without asking Microsoft. We are planning to make an official Nuget package.
Comment: Thanks for the update.  Is there somewhere I can follow this issue?  A ticketing system or something for Bing WPF Maps?
Comment: If you have an Enterprise Bing Maps license you can contact the Bing Maps Enterprise support team and they will create a ticket for this and be able to provide you with updates. That said, I believe the updated WPF control will be released in the next couple of weeks.
Comment: It looks like our team already pushed out the updated WPF control as a Nuget package here: http://www.nuget.org/packages/Microsoft.Maps.MapControl.WPF/ The original download page will likely be updated shortly.


Counter example for Cantor's intersection theorem

Question: Could anyone point out which line of the following reasoning is incorrect?
Let $\mathbb R$ be equipped with the indiscrete topology, $\{\emptyset,  \mathbb R\}$.

Then, every subset of $\mathbb R$ is compact.
The sequence of sets $\{(n, \infty)\mid n \in \mathbb N\}$ is decreasing and all compact.
According to Cantor's intersection, the intersection of the sequence of sets should be non-empty.

However, 3. is obviously false. Is Cantor's intersection theorem only applied to metric space? Is there any generalized version to only get the non-empty intersection result?
Comment: Possible duplicate: http://math.stackexchange.com/questions/247645/intersection-of-nested-compact-subspaces-in-non-hausdorff-space
Answer: The proof uses the fact that the complement of a compact set is open, which as we see is not true in every topological space. It's true in any Hausdorff space.


sqlalchemy CompileError Unconsumed column names when deleting row from m2m table

Question: There is a m2m table which connects instances of one model making parent-child relations.
companies_connections = db.Table(
    'companies_connections',
    db.Column('parent_id', db.BigInteger(), db.ForeignKey('company.id'), primary_key=True),
    db.Column('child_id', db.BigInteger(), db.ForeignKey('company.id'), primary_key=True),
)

Try to delete row from table in after_insert event listener I have only Connection object because Session is dealing with other flush events.
But using
q = companies_connections.delete(
    and_(
        companies_connections.c.parent_id == 10,
        companies_connections.c.child_id == 23
    )
)
connection.execute(q)

I get
CompileError: Unconsumed column names: parent_id_1, child_id_1

Why?
Answer: You should specify conditions inside where method:
q = companies_connections.delete().where(
   and_(
       companies_connections.c.parent_id == 10,
       companies_connections.c.child_id == 23
   )
)
connection.execute(q)

http://docs.sqlalchemy.org/en/latest/core/tutorial.html#deletes
Also, tables should be defined with metadata: http://docs.sqlalchemy.org/en/latest/core/tutorial.html#define-and-create-tables
Comment: OP is using Flask-SQLAlchemy's [Table](https://flask-sqlalchemy.palletsprojects.com/en/3.1.x/api/#flask_sqlalchemy.SQLAlchemy.Table) method, which does not require a metadata object.


How to search a specific SD card and read from\write to it using Qt c++ on Windows?

Question: I'm writing a desktop application in Qt IDE in C++ language, on Windows 7 OS.
I need my program to search in the computer devices list a SD card, named :"MY_CARD".
Only if this specific SD card is plugged - my program has to read the files in it, and to write to it new files.
I googled a lot, but didn't find any way how to do it.
Any idea?
thanks!
Comment: You googled for a ready-made solution to your **specific** problem. Instead, think of what *concepts* underlie your problem and are essential to it, *then* search for those concepts individually. For example, you want to detect device insertion and removal, and want to check the volume label of a drive.
Comment: ok, you were right. I googled again, now for the concepts underlie my issue, and found great solutions. thanks!
Answer: ok, guys, I found the solution. you should see the comment above... thanks!


Form processing

Question: what I'm trying to do is run a select statement for each answer to select the answer in the database where the questionID = $i and the userID = $userID so I have the query like this set up so far but not sure what I'm missing or am I right and not missing anything? Also no matter what i do both fields have values but I'm still getting the error message that I need to fill out both form fields.
<?php
                        $i = 1;
                        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                        ?>

<label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?>

<input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" size="54" />

                        <?php
                        ++$i;
                        }
                        ?>

if (empty($_POST['answer1'][$i]) || trim($_POST['answer1'][$i])=="") {$errors = "yes";}
if (empty($_POST['answer2'][$i]) || trim($_POST['answer2'][$i])=="") {$errors = "yes";}

// Error checking, make sure all form fields have input
if ($errors == "yes") {

    // Not all fields were entered error
    $message = "You must enter values to all of the form fields!";

    $output = array('errorsExist' => true, 'message' => $message);

} else {

    $userID = mysqli_real_escape_string($dbc,$_POST['userID']);
    $answer1 = mysqli_real_escape_string($dbc,$_POST['answer1'][$i]);
    $answer2 = mysqli_real_escape_string($dbc,$_POST['answer2'][$i]);

    $query = "SELECT * FROM manager_users_secretAnswers WHERE questionID = '".$questionID."' AND userID = '".$userID."'";
    $result = mysqli_query($dbc,$query);
    echo $query;

Comment: dude.. you keep repeating the same mistake even though we're telling you to do it otherwise. empty(trim($_POST['answer1'][$i])) is enough, you don't have to do the extra || thing.
Answer: You can have a problem with php type autoquessing. Let suppose thatyou have questions with ids: 3,5,7,8 then you are using:
 empty($_POST['answer1'][$i])

$_POST['answer1'][3] so you are fetching third element of array.
So I suggest to use not array notation, but:
 For input name: answer|${id} or answer_${id} instead of answer[$id]

Form processing


Filtrar lista de objetos python

Question: Olá, sou novo em Python e estou com dúvidas para filtrar um objeto de uma lista.
Eu tenho a seguinte lista:
[{ ID: 1, Name: 'Teste 1' }, { ID: 2, Name: 'Teste 2' }]

E eu estou querendo realizar um filtro cujo ID é igua; a 1.
Eu encontrei na internet uma forma de filtrar que não está funcionando que é essa:
filter(None, lista)

Como posso realizar esse filtro?
Desde já agradeço pela atenção.
Comment: Isto responde à sua pergunta? [Filtrar elementos de uma lista em Python](https://pt.stackoverflow.com/questions/190078/filtrar-elementos-de-uma-lista-em-python)
Answer: primeiro crie um loop que observe todos os objetos dentro da lista
for i in lista:

depois teste se a propriedade ID é igual ao valor desejado (1 por ex.)
if i.ID == 1:

depois edite o nome deste objeto para "teste" por ex.
i.Name = "teste"

código completo:
 for i in lista:
    if i.ID == 1:
        i.Name = "teste"

Comment: Bem vindo à comunidade! Apesar de sua resposta estar correta, é desejável que haja a explicação do `porquê` da sua solução, para que a resposta fique mais completa e sirva de referência para outras pessoas.
Comment: ok, perdao vou reescrever a resposta
Answer: A função embutida filter(function, iterable) está definida assim no manual da linguagem:

filter(function, iterable)
Constrói um iterador a partir dos elementos de iterável para os quais
function retorna verdadeiro. O iterável pode ser uma sequência, um contêiner
que com suporte a iteração, ou um iterador. Se function for None, a função
identidade será usada, isto é, todos os elementos do iterável que são falsos
são removidos.
Note que filter(function, iteravel) é equivalente a expressão geradora:

(item for item in iteravel if function(item)) se function não for None.
(item for item in iteravel if item) se function for None.

Então quando fez filter(None, lista) você informou ao interpretador que gostaria de obter o gerador (item for item in lista if item) que não realidade não filtra a lista apenas produz o gerador que reitera sobre seus itens.
Para realizara filtragem é preciso passa uma função de comparativa, como argumento no primeiro parâmetro. Essa deve retornar um valor Booleano onde:

True informa que o item será aceito pelo filtro.
False informa que o item será rejeitado pelo filtro.

Como filtro pode ser utilizada uma função anônima.
Exemplo: Numa lista de 100 dicionários {"ID":inteiro , "Name": string} cujo os IDs variam de 1 até 100, imprima apenas os dicionários cujo o ID <= 10 e que ID seja par.
#Cria uma lista com dicionários cujo os ids vão de 1 até 100.
entrada =[{"ID": i + 1, "Name": f"Teste{i + 1}"} for i in range(100)]

#Cria o filtro que só retorna True se o ID de x for menor que 10 e a mesmo tempo o ID seja par.
filtro = lambda x: x["ID"] <= 10 and x["ID"] % 2 == 0

#Filtra a lista entrada.
filtrados = list(filter(filtro , entrada))

#Imprime o resultado.
print(filtrados)

Esse exemplo resultará em:
[{'ID': 2, 'Name': 'Teste2'},
 {'ID': 4, 'Name': 'Teste4'},
 {'ID': 6, 'Name': 'Teste6'},
 {'ID': 8, 'Name': 'Teste8'},
 {'ID': 10, 'Name': 'Teste10'}]

Voltando ao exemplo da pergunta onde você quer realizar uma filtragem do elemento cujo ID é igual a 1:
entrada =[{"ID": i + 1, "Name": f"Teste{i + 1}"} for i in range(100)]

#Filtro para aceitar apenas o x cujo o ID é um.
filtro = lambda x: x["ID"] == 1

filtrados = list(filter(filtro , entrada))

print(filtrados)

O que retorna:
[{'ID': 1, 'Name': 'Teste1'}]

Answer: A função filter() aplica uma função a um objeto iterável.
No seu caso, teríamos algo mais ou menos assim:
def filtro(x):
    if x['ID'] == 1:
        return True
    else:
        return False

lista = [{ ID: 1, Name: 'Teste 1' }, { ID: 2, Name: 'Teste 2' }]

resultado = filter(filtro, lista)

for _ in resultado:
    print(_)

{'ID': 1, 'Name': 'Teste 1'}

Answer: Você pode utilizar a expressão lambda e testar se o ID é 1
lista = [{'ID': 1, 'Name': 'Teste 1' }, {'ID': 2, 'Name': 'Teste 2' }]

filtrado = filter(lambda x: x['ID'] == 1, lista)

Saída
[f for f in filtrado]
[{'ID': 1, 'Name': 'Teste 1'}]

Filtrar lista de objetos python


RANDOM learning rate in gradient descent

Question: I read this paper on the "Cyclical Learning Rate" method, which cyclically decreases and then INCREASES the learning rate in gradient descent:
http://arxiv.org/abs/1506.01186
Can anyone point me to, or does anyone know of, cases where someone tried randomly changing the learning rate instead of using some prescribed learning rate update schedule?
For example, randomly choosing a learning rate uniformly within some bounds every N iterations, or drawing the rate from a normal distribution?
Comment: This should be a comment. I haven't tried the author approach, however, what I often do with streaming algorithms is training them multiple times with the same parameters on shuffled versions of the original data. This improves performance and the why is vastly documented. I guess that multiple epochs with random learning rates is equivalent to multiple shuffling and training. Note that it is specially interesting in the case of large files as you do not need to go through costly shufflings any more.
Answer: I just want to add, after three years the question was raised, that there is a paper submitted to ICLR 2019:
LEARNING WITH RANDOM LEARNING RATES
https://openreview.net/pdf?id=S1fcnoR9K7
The main idea of the paper is:

We present the All Learning Rates At Once (Alrao) optimization method
  for neural networks: each unit or feature in the network gets its own learning rate
  sampled from a random distribution spanning several orders of magnitude. This
  comes at practically no computational cost. Perhaps surprisingly, stochastic gradient
  descent (SGD) with Alrao performs close to SGD with an optimally tuned
  learning rate, for various architectures and problems.


Posicionamento de label e inputs em um formulário html

Question: Olá, estou estudando HTML5 e CSS3 e estou tentando estilizar realizando o posicionamento dos inputs/checkbox/combobox e suas respectivas labels.
Suponhamos que tenho o seguinte código:

<form>
    Input 1
<input id="Input1" type="text">

Input 2
<input id="Input2" type="text">

Input 3
<input id="Input3" type="text">

Combobox 1
<select id="combobox1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

Combobox 2
<select id="combobox2">
    <option value="1">1</option>
    <option value="2">2</option>
</select>

Input 4
<input id="Input4" type="text">

<input type="checkbox" id="checkbox" value="selecionado"> Checkbox

Input 5
<input id="Input5" type="text">
</form>

Como faço para que a pagina fique posiciona nesse estilo:
Answer: Existem várias soluções para esse layout. Uma solução passa por visualizar o espaço em três colunas e assim criar três
, um para cada coluna e por os
 lado a lado com display flex. Depois é necessário colocar cada um dos elementos que estavam no <form> na coluna apropriada.
Exemplo:

form {
  display:flex; /*O form tem os três divs logo é o que fica com o display flex*/
}

.coluna {
  padding:10px; /*cria um espaçamento para dentro nas colunas para não ficarem "coladas"*/
}

.coluna input, .coluna select {
  margin-bottom:20px; /*distanciar as caixas verticalmente dentro dos divs*/
}
<form>

    Input 1
    <input id="Input1" type="text">

    Combobox 1
    <select id="combobox1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>

    Input 4
    <input id="Input4" type="text">

    Input 5
    <input id="Input5" type="text">

    Input 2
    <input id="Input2" type="text">
    Combobox 2
    <select id="combobox2">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>

    <input type="checkbox" id="checkbox" value="selecionado"> Checkbox

    Input 3
    <input id="Input3" type="text">

</form>
Comment: Gostei :)  Apenas realizei algumas alterações. Na classe coluna troquei o padding de 10px para 1.5rem, e acrescentei o width: 10% e para finalizar, na coluna última classe troquei o margin-bottom de 20px para .5rem


regarding the validity of a trend line and R2

Question: Let's imagine that I want to know if my tree gives more and more apples as time goes by. I have two variables: X (unit of time, say week), and Y (number of apples). I have 100 weeks on record.
When I create a scatter plot and add the straight trend line to it, I get a growing line, but I've heard that the $R^2$ value is important to determine if the line is valid or not, and my $R^2$ value is 0.1 which seems very low.
My question is: Does this mean that my trend line is misleading? Or can I say without fear that the number of apples per week is growing and even say how much?
EDIT: Thank you so much for all the answers, I will try to clarify because now I see the example wasn't really good. I will take the example of the light bulbs in a factory that was hinted by one of the answers. The factory produces light bulbs every week and there's a randomness to the number of light bulbs produced. After reviewing the data collected from 2 years, one can construct a dispersion graphic and add a linear trend line to it. This trend line (through the trend line equation) can tell us how much (in %) the light bulb production has increased, but: is this % reliable if the trend line has a $R^2$ value of 0.1?
Maybe I can ignore $R^2$ altogether? I don't want to predict or explain anything, only to show that the number of light bulbs/year has grown and tell how much has grown in %...
Comment: First we're imagining data and then you seem to be implying that you have $R^2$ to hand from real data. Which is it? How literally are we expected to take the example? In particular, 100 weeks is almost 2 years and I don't expect apple trees to ignore the seasons and show a linear trend over 2 years.
Comment: It's unclear what precisely you mean by "valid" and "misleading". You can be certain that the number of applies is growing in the observed data (in the particular sense that is measured by the line); whether it even makes sense to talk about it in some broader sense would require us to identify a population about which we're trying to perform inference and to establish the sense in which we can be said to be randomly sampling from it, along with a number of other issues.
Comment: It sounds like you are relatively new here, and you are asking a question in human-speak and not statistician-speak.  They are, sadly enough, not mutually inclusive languages.  It sounds like there is a good stats question behind your human-speak question.  How wrong is this interpretation: "How do I understand what $R^2$ means in context of this grandma-friendly example"?  What can and can't $R^2$ tell me?
Comment: Hi EngrStudent! Your reformulation of my question sounds excellent. Can you answer to those?
Answer: It would be necessary to analyze the residuals of the model and the significance of the parameters. If the parameters are significant and all the assumptions of the model are met, you may not have a good model to predict but you can accept the existence of a trend.
In the Anscombe's quartet you can see some examples where the adjustment would be misleading (a single outlier in the data can change the trend line (slope) of the whole series). To better understand this problem I recommend the book Exploratory Data Analysis by John Tukey.
Answer: First, $R^2$ is valid regardless. The question is whether it tells you what you want to know. $R^2$ here is a measure of the linear relationship between week and number of apples.
But that's not what you want to know. Trees don't give fruit in a way that grows steadily over time. They give fruit in certain seasons. Apple trees in the northern hemisphere usually give fruit in the late summer or early fall. So, while you have 100 weeks of data, you really only have 2 repeats - year 1 and year 2.  That's not enough data to tell you anything about change in harvest from year to year.
Now, let's suppose that was a fake example and you really have something that works week-by-week - let's say it was number of light bulbs produced in a factory. Then you have a more reasonable amount of data, but you still may have problems. This is time series data, which has its own set of procedures. There may be seasonality (even if it won't be as extreme as with apples).


Como parar a execução da função $.map?

Question: Como faço para parar a execução da função $.map quando uma condição do if for verdadeiro?
Com for eu usuaria um break assim:
for (int i = 0; i < count; i++){
    if (true)
        break;
}

E na função $.map do Jquery, como seria?
Comment: Qual é o objetivo? Talvez `map` não seja a função adequada, porque ela é feita para transformar *todos* os valores da array.
Answer: Você não pode "quebrar" a execução da função map.
Recomendo dar uma olhada na função $.each.
Comment: o $each eu consigo para a execução?
Comment: Sim, retornando `false` da função passada a ele. Mas seria legal esclarecer melhor o seu objetivo com a pergunta, talvez exista alguma solução melhor. E `for` comum você também pode usar e dar `break`, exatamente como em C#.
Answer: Você pode criar sua própria função de mapeamento se a do jQuery não lhe resolve.
var stoppableMap = function (inputArray, mainFunction, haltFunction) {
    var result = [];
    for (var i = 0; i < inputArray.length; i++) {
        if (haltFunction(result, inputArray[i], i) === false) {
            break;
        }
        result.push(mainFunction(inputArray[i], i));
    }
    return result;
}

O que acontece aqui é o seguinte: você chama a função acima passando um vetor como entrada e uma função que recebe índice e elemento, de forma semelhante ao que faria com a função $.map..
Para cada elemento do vetor, a função passada como o parâmetro mainFunction será executada uma vez. o primeiro parâmetro que ela recebe é elemento atual, e o segundo parâmetro é o índice do elemento atual.
A diferença está na função passada como o terceiro parâmetro, haltFunction. Ela também é executada para cada elemento do vetor original, mas antes da mainFunction. Ela recebe três parâmetros: o vetor resultante do mapeamento até o momento, o elemento atual e o índice do elemento atual. Se ela retornar false (explícito - "", 0, null e undefined não servem), o laço da função de mapeamento é interrompido. CQD.
Você pode modificar a função a vontade: fazer a função de parada executar depois e não antes da função principal, passar um parâmetro a mais para ter mais contexto ou o que mais sua lógica precisar.
Note que essa função é bem mais simples do que a $.map. Ela não itera sobre objetos e nem achata vetores a cada rodada da função principal que você passa. Você pode implementar esses detalhes se necessário.
Exemplo de uso, que para quando encontra um elemento de valor 3:
stoppableMap([1, 2, 3, 4, "a Maria é um barato"],
        function (elem) {
            return "elemento de valor " + elem;
        },
        function (accumulate, elem) {
            if (elem == 3) return false;
        });

Boa sorte ;)
Answer: Não é possível interromper o fluxo de jQuery.map() em si pois seu propósito é ser aplicado em todos os elementos do array dado. Mas é possível sim fazer seu callback ignore determinados valores da iteração, não se aplicando neles de acordo com alguma condição.
Rotinas de mapping na grande maioria das vezes visam, como o próprio nome sugere, mapear os elementos de um array de alguma coisa para alguma outra coisa. Caso algum elemento não deva ser mapeado, basta retornar o próprio elemento:

var arr = [ "a", "b", "c", "d", "e" ];

arr = jQuery.map( arr, function( n, i ) {
  if ( i == 2 ) { return n; }
  return ( n.toUpperCase() );
});

console.log( arr );

Observando o console veremos que todos os elementos do array passado foram mapeados para letras maiúsculas, exceto o terceiro índice que foi mantido inalterado.
É possível também, apesar de errado, usar jQuery.map() como alternativa a jQuery.each(), executando alguma coisa para cada elemento do array de entrada sem, de fato, transformá-lo em nada.
Para esses casos, ao invés de retornar o valor corrente, um return false permite "pular" a iteração corrente quando alguma condição se satisfizer.

$.map( [ 'one', 'two', 'three', 'four' ], function( val, i ) {

    if( i > 2 ) return false;

    alert( val );
});

Nesse exemplo eu verifico o índice corrente da iteração. Se for maior que 2 eu encerro a iteração e, consequentemente, a entrada four não é mostrada no alert().
Comment: Obrigado pelos dois pés no peito sem um comentário sequer. Até parece que se enganar é proibido...
Comment: Passando por aqui e vi seu comentario, agradeço pela resposta. Desculpe ;)

Como parar a execução da função $.map?


Custom exposed filter with single on/off checkbox doesn't respect the default value and the $_GET parameter

Question: I've created a custom filter with a checkbox exposed field in my module.
class my_module_helper_handler_filter_timetracking_children extends views_handler_filter {

  /*...*/

  function value_form(&$form, &$form_state) {
    $form['value'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show subtasks'),
      '#default_value' => isset($this->value) ? $this->value : NULL,
    );
    return $form;
  }

  /*...*/

}

When by default in Views UI settings checkbox is disabled, everything works fine; when it's enabled, it doesn't work. It doesn't respect the default value and the $_GET parameter.
I can't understand this strange behavior of single on/off checkboxes in exposed filters in a view.
I also tried using '#attributes' => array('checked'=>'checked'), but the filter didn't work correctly.
Might somebody can show his code how to create custom exposed filter with single checkbox? Or give usefull link
Comment: There is no need to return the form here since its passed by reference and I'm able to understand your question fully. could you please elaborate.
Comment: Yes you are right about return. I created exposed filter with "checkbox". Than I add my filter in view, expose it and set it as checked by default. And the view doesn't filter any thing with my checkbox. When I set is as disabled by default everything works fine. Or when I use "select" view forks fine too.
Answer: Views doesn't handle the default checkbox state because when you load your view page for the first time (or if the user clicks the Reset button), the URL doesn't have any parameter in it, so that the $_GET parameter is useless. Here is a complete form definition that will handle the checkbox, whether the URL has parameters or not:
class my_module_helper_handler_filter_timetracking_children extends views_handler_filter {

  /*...*/

  function value_form(&$form, &$form_state) {

    $params = my_module_get_params();

    // First time the view is loaded (or user click the Reset button), URL doesn't have parameters
    if(!$params) {
      $value = 1; // checkbox checked by default
    }
    // If URL has parameters
    else {
      // If our filter is present in the URL as parameter and its value is '1'
      if (isset($params['timetracking_children']) && $params['timetracking_children'] == 1) {
        $value = 1;
      }
      else {
        $value = 0;
      }
    }

    $form['value'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show subtasks'),
      '#default_value' => isset($this->value) ? $this->value : NULL,
      '#value' => $value,
    );
    return $form;
  }

  /*...*/

}

function my_module_get_params() {
  global $base_url;
  $query = array();

  $url = $base_url.request_uri();
  $url = parse_url($url);

  if(isset($url['query'])) {
    parse_str($url['query'],$query);
  }

  return $query;
}