How do you visualize binary outcomes versus a continuous predictor?

Question: I have some data I need to visualize and am not sure how best to do so. I have some set of base items $Q = \{ q_1, \cdots, q_n \}$ with respective frequencies $F = \{f_1, \cdots, f_n \}$ and outcomes  $O \in \{0,1\}^n$. Now I need to plot how well my method "finds" (i.e., a 1-outcome) the low frequency items. I initially just had an x-axis of frequency and a y axis of 0-1 with point-plots, but it looked horrible (especially when comparing data from two methods). That is, each item $q \in Q$ is has an outcome (0/1) and is ordered by its frequency.

Here is an example with a single method's results:

My next idea was to divide the data into intervals and compute a local sensitivity over the intervals, but the problem with that idea is the frequency distribution is not necessarily uniform. So how should I best pick the intervals?

Does anyone know of a better/more useful way to visualize these sort of data to portray the effectiveness of finding rare (i.e., very low-frequency) items?

EDIT:
To be more concrete, I am showcasing the ability of some method to reconstruct biological sequences of a certain population. For validation using simulated data, I need to show the ability to reconstruct variants regardless of its abundance (frequency). So in this case I am visualizing the missed and found items, ordered by their frequency. This plot will not include reconstructed variants that are not in $Q$.
Comment: I don't completely understand.  Are the "outcomes" finding something? What are the "rare items"?
Comment: IMO you should include the graph you said looks horrible - it will give everyone a better idea of the data you are attempting to display.
Comment: OK, it looks like you tried a scatterplot on data where the y value can only be 0 or 1. Is that right? And you want to compare these sorts of plots across multiple methods on the same points?

But can each method be right or wrong in one way or two ways? That is, each point either is or is not (whatever). So a method could say a point is (whatever) or isn't (whatever) and either choice could be right or wrong?
Comment: @PeterFlom, I've edited to make it clearer. The 0-1 outcomes for each item indicate "not-found" and "found". A rare item is simple a very low frequency item.
Comment: @AndyW, edited to include image. Given the values on the y-axis don't really reflect the concept of found and not found, but at least for conveying what I _want_ to present (for this questions' purposes), you get the idea...
Comment: I have to agree w/ Peter & Andy, I have no idea what this is about. Can you state what your situation, data, and goals are? Our [FAQ](http://stats.stackexchange.com/faq) has some info about asking questions, & this blog post: [how to ask a statistics question](http://www.statisticalanalysisconsulting.com/how-to-ask-a-statistics-question/) may be helpful as well.
Comment: @PeterFlom, for these data, each method can only be wrong in one way. To be more concrete, I am showcasing the ability of some method to reconstruct biological sequences of a certain population. For validation using simulated data, I need to show the ability to reconstruct variants regardless of its abundance (frequency). So in this case I am visualizing the missed and found items, ordered by their frequency.
Comment: @gung, I made a slight edit to better describe what my intent is in the visualization.
Comment: If a method can be wrong in only one way (which seems like not finding an item) then you can devise a method that is never wrong by having it always say "found".

But now you say "ability to reconstruct variants" which seems like something else.

I see I am not the only one who is confused here....
Comment: @PeterFlom, I apologize for the confusion. I should have gathered my thoughts for a while before posting perhaps. A method can be wrong in more than one way. In my case it could reconstruct an incorrect variant. I have other plots for sensativity and ppv. But in addition to those more general measures, I would like to show a plot of the 'ground truth' with respect to a method at the frequency level.
Comment: I don't want to come off as sarcastic, but it would be more concrete if you could specify what the methods, sequences, & populations are, eg. You could also give a sample (perhaps the 1st few rows) of your data. $Q, F, \& O$ are not helpful to me; I have no idea what you're talking about.
Comment: Is that plot done in Excel? If so, you should be banished to hell. If so try ggplot2 in R. There is only so many ways you can plot data with a 0 or a 1 outcome (bar, pie, etc...) You haven't indicated what the x-axis variable is, is it time series?
Comment: The plot does not look that bad visually, not everyone loves ggplot2.
Comment: @ADP, for the record I think excel/libreoffice doesn't have the best looking plots, but for the sake of generating an example for a website it's fine.
Answer: What I have done in the past is basically what you've done with the addition of a loess.  Depending on the density of points, I would use translucent points (alpha), as shown below, and/or pipe symbols ("|") to minimize overlap.
library(ggplot2) # plotting package for R

N=100
data=data.frame(Q=seq(N), Freq=runif(N,0,1), Success=sample(seq(0,1),
size=N, replace=TRUE))

ggplot(data, aes(x=Freq, y=Success))+geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Frequency")+
  ylab("Probability of Detection")+
  theme_bw()

(I don't think the error bars should widen on the edges here, but there isn't an easy way I know of to do that with ggplot's internal stat_smooth function. If you used this method for reals in R, we could do it by estimating the loess and its error bar before plotting.)
(Edit: And plus-ones for comments from Andy W. about trying vertical jitter if the density of the data makes it useful and from Mimshot about proper confidence intervals.)
Comment: +1 but you should use confidence bounds from the inverse of the binomial distribution rather than you're implied Gaussian noise.
Comment: +1 - I would also suggest utilizing jitter for the dots (in addition to transparency). In this example I would replace `geom_point(size=2, alpha=0.4)` with `geom_jitter(size=2, alpha=0.4, position = position_jitter(height = .02))`.
Comment: @Mimshot do you know of a way within `ggplot2` to supply the correct CIs? I've got a plot with CIs outside of `[0,1]` which clearly are coming from the wrong calculation
Comment: This is a good answer (+1), but the range of the plot (and CIs) should definitely be restricted onto the interval $[0,1]$.  That would improve the look of the plot and also respect the support of the allowable probability value.
Comment: @Mimshot Can you show how to compute the confidence intervals correctly?
Answer: Also consider which scales are most appropriate for your use case. Say you're doing visual inspection for the purposes of modeling in logistic regression and want to visualize a continuous predictor to determine if you need to add a spline or polynomial term to your model. In this case, you may want a scale in log-odds rather than probability/proportion.
The function at the gist below uses some limited heuristics to split the continuous predictor into bins, calculate the mean proportion, convert to log-odds, then plot geom_smooth over these aggregate points.
Example of what this chart looks like if a covariate has a quadratic relationship (+ noise) with the log-odds of a binary target:
devtools::source_gist("https://gist.github.com/brshallo/3ccb8e12a3519b05ec41ca93500aa4b3")

# simulated dataset with quadratic relationship between x and y
set.seed(12)
samp_size <- 1000
simulated_df <- tibble(x = rlogis(samp_size),
                       y_odds = 0.2*x^2,
                       y_probs = exp(y_odds)/(1 + exp(y_odds))) %>%
  mutate(y = rbinom(samp_size, 1, prob = y_probs))

# looking at on balanced dataset
simulated_df_balanced <- simulated_df %>%
  group_by(y) %>%
  sample_n(table(simulated_df$y) %>% min())

ggplot_continuous_binary(df = simulated_df,
                         covariate = x,
                         response = y,
                         snip_scales = TRUE)
#> [1] "bin size: 18"
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2019-02-06 by the reprex package (v0.2.1)
For comparison, here is what that quadratic relationship would look like if you just plotted the 1's/0's and added a geom_smooth:
simulated_df %>%
  ggplot(aes(x, y))+
  geom_smooth()+
  geom_jitter(height = 0.01, width = 0)+
  coord_cartesian(ylim = c(0, 1), xlim = c(-3.76, 3.59))
# set xlim to be generally consistent with prior chart
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

Created on 2019-02-25 by the reprex package (v0.2.1)
Relationship to logit is less clear and using geom_smooth has some problems.
Answer: If you have so many points that they overlap and jittering is insufficient, you can add histograms for both levels of the binary variables (so one will be upside down). Here's an example combined with a logistic regression.

The idea and R code (popbio::logi.hist.plot) come from
Rot, M. D. L. C. (2005). Improving the presentation of results of logistic regression with R. Bulletin of the Ecological Society of America, 86(1), 41-48.
https://esajournals.onlinelibrary.wiley.com/doi/10.1890/0012-9623%282005%2986%5B41%3AITPORO%5D2.0.CO%3B2
Answer: The ggridges package offers more creative ways to avoid overplotting those ones and zeros. Modifying @MattBagg's example. Not optimal for this dataset, but you'll get the point.
library(ggplot2)
library(ggridges)

N=100
data=data.frame(Q=seq(N), Freq=runif(N,0,1),
                Success=sample(seq(0,1), size=N, replace=TRUE))

ggplot() +
  ggridges::geom_density_ridges(data = data, aes(x = Freq, y = Success, group = Success), scale = 0.2) +
  stat_smooth(data = data, aes(x = Freq, y = Success), size=1.5) +
  coord_cartesian(ylim = c(0, 1.25), xlim = c(0, 1), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 0.5, 1)) +
  labs(x = "Frequency", y = "Probability of Detection") +
  theme_bw()
#> Picking joint bandwidth of 0.123
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2021-06-30 by the reprex package (v2.0.0)
Answer: I agree that posting just a few lines of sample data would go a long way. If I understand the question, I think it would be simplest to plot the frequency by the proportion found.
First I will generate some sample data in R; please correct me if I haven't understood you correctly.
# Create some sample data
data=data.frame(Q=1:20,F=seq(5,100,by=5))
set.seed(1)
data$found<-round(sapply(data$F,function(x) runif(1,1,x)))
data$prop<-data$found/data$F
# Looks like:
Q   F found      prop
1   1   5     2 0.4000000
2   2  10     4 0.4000000
3   3  15     9 0.6000000
4   4  20    18 0.9000000
5   5  25     6 0.2400000
6   6  30    27 0.9000000
7   7  35    33 0.9428571
8   8  40    27 0.6750000
9   9  45    29 0.6444444
10 10  50     4 0.0800000
11 11  55    12 0.2181818
12 12  60    11 0.1833333
13 13  65    45 0.6923077
14 14  70    28 0.4000000
15 15  75    58 0.7733333
16 16  80    40 0.5000000
17 17  85    61 0.7176471
18 18  90    89 0.9888889
19 19  95    37 0.3894737
20 20 100    78 0.7800000

And now simply plot frequency (F) by proportion:
# Plot frequency by proportion found.
plot(data$F,data$prop,xlab='Frequency',ylab='Proportion Found',type='l',col='red',lwd=2)

Comment: That plot is horrible! Some smoothing, as in earlier answers, are necessary.


Is there any way to use results of a previous rendering inside a fragment shader?

Question: I'm using the fragment shader to render to a texture. I would like the rendering of the previous iteration of the shader to be used by the next one. How is that possible?
Comment: Sample here http://games.greggman.com/game/webgl-image-processing-continued/
Answer: To re-use the texture in the fragment shader:
either

unbind the current framebuffer object (bind the null framebuffer object or bind another framebuffer object) to which the texture is attached,

or

detach the texture from the framebuffer object (and attach null, attach another texture with framebufferTexture2D or attach a renderbuffer instead).

and then bind the texture as a normal texture to be used in the fragment shader (via  sampler2D and texture2D in the shader code).
To re-use the texture in the vertex shader, it is roughly the same, except that this feature (vertex textures) is non-standard (and has some limitations). Check MAX_VERTEX_TEXTURE_IMAGE_UNITS for support.
If you have an iterative algorithm that requires multiple rendering passes, one typical approach is to switch between two textures 1 and 2: during odd rendering passes, 1 is attached to the current framebuffer object and 2 is bound as a normal texture and during even rendering passes, it's the opposite. See also this answer on whether texture attachments should be changed in a single framebuffer object or whole framebuffer objects should instead be swapped.
Comment: framebuffers should be swapped. Changing an attachment should be a much more expensive operation. For a framebuffer to be *complete* a whole bunch of things have to be true. Every time you attach/detach all of those things need to be checked. Conversely if the framebuffer is already *complete* nothing needs to be checked so swapping framebuffers can be instant whereas swapping attachments is likely slow. On the other hand, the likelihood that either method is going to be a bottleneck is pretty low.
Comment: You detach an attachment by attaching 'null' to that attachment point. `gl.framebufferTexture2D(gl.FRAMEBUFFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);` attaches `texture` to `COLOR_ATTACHMENT0`. Change `texture` to `null` to bind `null` to `COLOR_ATTACHMENT0` which is effectively detaches `texture`.
Comment: How do you detach it from the framebuffer? Could you include some code example for that first paragraph?


how do you get to this result?

Question: I have a question of how to solve this
$$B = \{x ∈ \mathbb{Z} : -2^x \leq 1\}$$
this according to a friend of mine should be $\{0, 1, 2, ... , \infty\}$
but how he gets to this is how I dont understand, if someone can help me understand this it would be great
Thank you, I dont know what the name of this is in English so I wrote something, feel free to edit
Comment: Your friend is wrong: the answer is $\Bbb Z$, so: one $\infty$ too much and too few negative integers.
Comment: You friend would be right for $2^{-x}$.
Comment: $(-2)^x$ or $-(2^x)$ ?
Comment: no paranthesis on the question
Comment: Thus, $-2^x$ is *negative*, because $2^x$ is always positive for $x \in \mathbb Z$.
Comment: thank you, but I dont quite understand how you know its Z, but Z is also negative numbers
Answer: We have $-2^x \le 1$  iff $2^x \ge -1$
But always $2^x >0$, hence $B= \mathbb Z$
Comment: thanks, but then my friend is incorrect because he had a 0 in his answer? and second of all, do you confirm B=Z by checking the 2^x>0 or by testing x values? and if so, why is no negative numbers in the sequence?

how do you get to this result?


What database table can be emptied in order to make database lighter?

Question: Are log files in drupal take a lot of space? I want to create a dump file but it's over 1000mb. What tables can be harmlessly emptied from the database directly or through drupal interface?
Answer: Usually all tables with cache_ prefix are safe to be emptied.
The log table records can take a lot of space, depending on your settings on how many log messages you want to save. You can configure this at admin/config/development/logging.
How many nodes/users do you have on the website? Do you have enabled the core search module? What other modules do you have?
If your website has tens of thousands of nodes and users, 1GB is a normal size of the database dump.
Comment: I have enabled many modules: views, panels, search, context, video, music and others. But I did what you said sir and my dump really got smaller. Thanks!
Comment: You are welcome :) If this solved your problem, click on the green checkmark right next to the answer to accept it as a solution.


FastAPI - How to render Jinja2 template with variable that contains special characters?

Question: I'm trying to render this Jinja2 template:
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request, item: Optional[str] = None):
    item = "


App de tradução Text to Speech - Android

Question: Olá, sou iniciante em android e gostaria de fazer um tradutor simples, onde o usuário escreva qualquer coisa e clique no botão com a língua desejada para traduzir em voz alta, como seria o algorítimo de tradução para outras línguas ? Segue o meu código
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        edt_texto =(EditText)findViewById(R.id.edt_texto);
        btn =(Button)findViewById(R.id.btn);

        t2s = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {

                t2s.setLanguage(Locale.getDefault());

            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fala = edt_texto.getText().toString();

                t2s.speak(fala,TextToSpeech.QUEUE_FLUSH,null);
            }
        });
    }

Comment: TextToSpeech não traduz. Ele apenas faz o discurso sintetizado do texto.
Answer: Já está a utilizar outras línguas.
Ao usar:
t2s.setLanguage(Locale.getDefault());

Está a utilizar a linguagem seleccionada no dispositivo.
Pode, no entanto, definir uma linguagem diferente com:
t2s.setLanguage( new Locale("en", "US"));

ou
t2s.setLanguage( Locale.JAPAN );

ou ainda:
t2s.setLanguage( Locale.forLanguageTag("en-US") );

Comment: TextToSpeech não traduz. Ele apenas faz o discurso sintetizado do texto.
Comment: Peço desculpa, não tinha compreendido.
Também andei à procura de uma biblioteca de tradução para java mas, infelizmente, não encontrei nenhuma. A única alternativa será uma API de tradução, mas o utilizador terá que estar online para a utilizar.


Attaching a field of a node type in another form

Question: I have content type 'story' which have an upload field. I have workflow modules installed.
I want the upload field to be presented within the workfloaw form. So within form alter function I did:
field_attach_form('node', $node, $form['append'], $form_state);

then unset all other fields of the story node and appended validation and submit handlers:
$form['#validate'][] = 'node_form_validate';
$form['#submit'][] = 'node_form_submit';

on submission I get this error:
"EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7409 of /var/www/site/includes/common.inc)."
I tried by specifying 'bundle' and 'entity_type':
$form['#bundle'] = 'story';
$form['#entity_type'] = 'node';

and
$form['append']['#bundle'] = 'story';
$form['append']['#entity_type'] = 'node';

but did not help. What can I do?
Thanks.
Comment: From the D 7.22 changelog released today, this is linked there: http://drupal.org/node/1825844  ... it just smells that this might be relevant to you. I'm not certain you can have an "upload" Field from the FieldAPI be used seperate from its Entity without editing an existing entity (see linked page). You could always use FAPI `file_managed` element -- which probably defeats you workflow stuff :P
Answer: "EntityMalformedException: Missing bundle property on entity of type node."
means that Drupal does not recognize your entity as a valid one. In the entity info listing, there is a mapping from "bundle" to "type", for nodes.
Either your $node doesn't have a ->type, or Drupal no longer has the type registered. The type could have been removed, either from the UI, or by disabling a feature, and possibly in more interesting ways as well.
Comment: No, it is. I loaded it manually using `node_load()`
Comment: I've rewritten the answer, hopefully a more detailed answer is more helpful.


How to group and style tds in a table row?

Question: I'm trying to "group" a set of cells/ elements within a row/
 in a
 so that I can apply styles to them. The below code achieves what I somewhat want to do, but it has some limitations:

/* Styles I'm having issues with */
.row td:not(:first-child) {
  background-color: lightgray;
  /* potentially a box-shadow */
}

.row td:nth-child(2) {
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}

.row td:last-child {
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}

/* Base styles for table to make it look decent */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em;
}
table td {
  padding: 10px;
}

    A
    B
    C
    D

      1
      2
      3
      4

      5
      6
      7
      8

The above achives the visual effect I want (minus a box-shadow surrounding the grey box for each group), where the last 3 cells of each row are grouped. But it has a few issues:

This solution of stylying the last three cells involves me having to change the style of each , which means that styling is tedious (eg: I have to style the border radius for each corner, and can't specify just one simple border-radius style for the group).

Stylying the table in this way makes it hard to do things like add cell-spacing between each cell (as it adds a white gap), or to add a box-shadow that surrounds the entire grey box that groups the last three cells (with the above approach, each box-shadow would need to be applied to each , which makes each cell appear as seperate, eg, whereas the desired look for the box-shadow for the group would be like this).

Is there a way that I can group s together so that I can apply my styles hollistically to the group, and not to the individual s? Ideally, I would be able to do something like:

  1

    2
    3
    4

And then I would apply my border-radius: 10px and background-color: lightgray and box-shadow stylying to the .row-group class. But the HTML spec doesn't allow
s within
s and so the above doesn't work. I have also tried looking into <colgroup> and <col> tags, but these seem to apply styles per-column, and don't help me much with applying styles to a group of cells per-row. My only other thought is to use a sub-table, but I'm wondering if there is an easier way that will allow my to "group" and styles these s collectively, maybe I'm missing something and it can be done with <col>s?
Comment: wouldn't something like `td:not(:first-child)` work for you?
Comment: @AlvaroMenéndez that is what I've done in the code snippet at the start, but that has limitations (such as the box-shadow style I mentioned)
Comment: Sorry, I didn't check the code well. my bad. What about this? : https://jsfiddle.net/6cwqkomt/ It may work IF you know the width of your first child but if dynamic it would be easy with a few lines of javascript to calculate the width and add it as left property.
Comment: @AlvaroMenéndez Oh yes, that looks promising! Thank you. I can make my first cell a fix width (and yes, using JS wouldn't be too difficult either if it was dynamic). If you would like to, you can post that as an answer and I'll +1 it, and if after some time nothing else comes along that I think is a better solution I will accept it.
Answer: Posting my comment as an answer as You told me.
Basically we use the :after pseudoelement with the styles You are looking for (rounded grey box) and position it as absolute under your TD's. It will take all row width except the first:child width.

/* Styles I'm having issues with */
tbody tr {position:relative;}
tbody tr:after {
  content:'';
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:30px; /*First child width*/
  margin:auto;
  background-color: lightgray;
  border-radius:10px;
}
td {position:relative; z-index:1}
/* Base styles for table to make it look decent */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em;
}
table td {
  padding: 10px;
}

    A
    B
    C
    D

      1
      2
      3
      4

      5
      6
      7
      8

Edited: same snippet but adding a jquery script in case your first-child has a dynamic width:

$(document).ready(function(){
    var tdWidth = $("td:first-child").outerWidth();
  $( "" ).appendTo( "head" )
});
/* Styles I'm having issues with */
tbody tr {position:relative;}
tbody tr:after {
  content:'';
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  margin:auto;
  background-color: lightgray;
  border-radius:10px;
}
td {position:relative; z-index:1}
/* Base styles for table to make it look decent */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em;
}
table td {
  padding: 10px;
}

    A
    B
    C
    D

      1111111
      2
      3
      4

      5
      6
      7
      8


Can't find /dist directory in Netbeans 8

Question: I'm trying to export a project as a .jar in NetBeans 8, and I realize it's supposed to save automatically. All the sources I've found point out that it should be located in <myProjectFolder>/dist,but that directory does not exist-- is there a setting I must toggle?
Comment: What kind of project? Standard Java SE Project? If so just clean and build. It will appear.
Comment: What build system are you using? Ant, maven, ...?
Answer: You have to first clean and build to generate one.
As you do programming work you will see the result of your work , but this doesn't go into the dist folder until you clean and build.
Comment: Thanks. Coming from Xcode, I had no idea what "clean and build" meant.
Answer: Right click your project name in the NetBeans and you will see many options
starting with new, Build, clean and Build. click the clean and build and it will
create a dist folder which has the war file and you can access the dist folder
in the files at the top (projects Files Services)


how to define generic type based on passed arguments which is a union type

Question: I have following similar types:
class ActionFoo {
  action: 'foo';
  foo: string;
}
class ActionBar {
  action: 'bar';
  bar: number;
}

and some handler functions for each kind of defined "action", e.g.
function fooHandler(action: ActionFoo) {
  // do something
}
function barHandler(action: ActionBar) {
  // do something
}

The question is to create a util function to handling all these cases. I tried following, but the predicted types was wrong for handlers.
type ActionHandler<T extends { action: string }, O = any> = (action: T) => O; // type define for handler

function handleAction<T extends { action: string }, O = any>(
  action: T,
  handlers: {[key in T['action']]: ActionHandler<T, O>}
) {
  const handler = handlers[action.action];
  if (handler) return handler(action);
  throw new Error('not implemented');
}

type Action = ActionFoo | ActionBar;

function run(action: Action) {
  return handleAction(action, {
    foo: action => fooHandler(action), // ts compiler complaint type error (TS2345) for `action` for missing `foo` field
    bar: action => barHandler(action), // similar error
  });
}

Comment: Please [edit] this code to be a [mre] without any missing types or values, and where the only problems with the code are the ones you're asking about, when others run it in an IDE under `--strict` (or presumably under a different set of compiler options, but then you'd need to specify those).  Right now I see errors you're not asking about (class properties not initialized, error inside `handleAction`, and no `action` value in existence) and I don't see the error you are asking about.  Could you please fix address that so we can work on it?
Comment: @jcalz updated the last part. ts reports ts2345 on given type `ActionFoo | ActionBar`.
Answer: Finally, I got the solution. To let ts reasoning correct type for union type Action in ActionHandler, it has to combine the key type with the value type.
type ActionHandler<T extends { action: string }, O = any> = (action: T) => O; // type define for handler

function handleAction<T extends { action: string }, O = any>(
  action: T,
  handlers: { [K in T['action']]: ActionHandler<T & { action: K }, O> } // <--- added & { action: K } to combine value type with key type
) {
  const handler = handlers[action.action];
  if (handler) return handler(action);
  throw new Error('not implemented');
}


Thread - Violação de Acesso por Memória Compartilhada

Question: Eu utilizo uma biblioteca para recuperar os dados de uma Imagem (em unsigned char*), e utilizo ela para alocar em um Buffer (Textura) do OpenGL.
Como a imagem pode ser grande, o OpenGL por padrão cria uma outra thread para realizar essa atividade. Porém eu realizo free dessa imagem logo depois na thread principal, pois os dados não são mais necessários. Daí eu recebo o erro de violação de acesso no endereço (Visual Studio).

O C++ tem algum recurso para para verificar se uma memória não está sendo utilizada por outra Thread Implícita (ou seja que eu não possuo acesso)?

Detalhes:

A função que utilizo, que gera uma nova thread é glTexImage2D.
O erro ocorre randomicamente, depende muito se a thread principal executar free antes do Opengl alocar no Buffer

Detalhes Técnicos:
Exceção gerada em 0x07B8F54F (ig8icd32.dll) em ...[nomedoprograma.exe]: 0xC0000005: violação de acesso ao ler o local 0x13E1B000.
O endereço 0x13E1B000 é null. Trecho do código como solicitado
<code>int width, height, nrChannels;

unsigned char *dataImg = stbi_load(path, &width, &height, &nrChannels, 0);
GLenum format;
if (nrChannels == 1)
    format = GL_RED;
else if (nrChannels == 3)
    format = GL_RGB;
else if (nrChannels == 4)
    format = GL_RGBA;

if (dataImg) {
    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, dataImg);
    glGenerateMipmap(GL_TEXTURE_2D);
} else {
    log(StringBuilder::concat("STBI_LOAD::FAILED_TO_LOAD_IMAGE::", path));
}

stbi_image_free(dataImg);// <- ERRO AQUI
</code>
Comment: Onde você viu que a função glTexImage2D gera uma nova thread, até onde eu conheço o OpenGL é inteiramente projetado para trabalhar em single Thread.

Tente mover o código 'stbi_image_free(dataImg);' para dentro do if (dataImg), se não existe a imagem não faz sentido liberar recurso.
Comment: Não conheço soluções da linguagem para esse problema de concorrência que você colocou no ponto principal da pergunta. Mas há bibliotecas de concorrência que te ajudam a evitar isso. Semáforos e mutex são os que mais vejo em C. Talvez fosse interessante ler o que a documentação do compilador de C++ do Visual Studio tem a dizer sobre concorrência.
Comment: A hora não é propícia para chamar atenção, mas creio que dar um detalhe do excerto de código problemático, apresentando um [MCVE], possa gerar frutos quando mais usuários estiverem ativos. E conseguindo simular o seu problema também. Problemas de concorrência são [heisenbugs](https://en.wikipedia.org/wiki/Heisenbug). Ajudar a simular o problema consistentemente ajuda na solução
Comment: código adicionado, não sei se vai ajudar muito. Queria tornar o tópico mais amplo para problemas de memória compartilhada, não somente especifico do OpenGL.
Answer: Valeu pela dica nos comentários.
Segundo a documentação do OpenGL, glTexImage2D pode gerar erro durante a transferência se a imagem for de tamanho não divisível por 4.

You create storage for a Texture and upload pixels to it with glTexImage2D (or similar functions, as appropriate to the type of texture). If your program crashes during the upload, or diagonal lines appear in the resulting image, this is because the alignment of each horizontal line of your pixel array is not multiple of 4. This typically happens to users loading an image that is of the RGB or BGR format (for example, 24 BPP images), depending on the source of your image data.


Find distinct categories of products

Question: I have been asked to find the distinct categories of products but the problem is that the table category_code has value in this format 'category.sub_category.product'. Now my question is that how can I find a distinct category with values in such format?
Answer: Use split function to get category_code splitted using dot as a delimiter into an array(category,sub_category,product), then get element[0] for category:
<code>select distinct split(category_code,'\\.')[0] as category
  from your table
</code>

Find distinct categories of products


How do I install pygame on python 3.6?

Question: So I am having a bit of trouble with my python/pygame. I want to install pygame but i just dont know how to do it. I have been looking around  on the web now for a while and tried multiple things. It just does not work...
Comment: what's wrong with the pip install?
Comment: Which operating system do you use? How did you install Python 3.6--from python.org, through Anaconda, or something else?
Comment: i tried to use the pip install but it simply doesnt work. It does nothing.
Comment: I use windows 10 64 bit and i installed python through python.org @RoryDaulton
Comment: Here's some [tips](https://stackoverflow.com/questions/35991403/python-pip-install-gives-command-python-setup-py-egg-info-failed-with-error-c/39155105#39155105) I use to install python packages, hope it helps
Comment: When you say `pip install` does "nothing", what do you mean? I doubt it just returns 0. Is there any console output? Do you get any error messages? A timeout? Please read [ask].
Comment: Check out [this answer](https://stackoverflow.com/a/42663743/6220679).
Comment: The cause of your problems is most likely that the `/Scripts` directory which contains `pip` was not added to the PATH variable during the installation, so you can't just type `pip install name_of_the_package` in the command-line.
Comment: Don't give up. Try to enter `py -3.6 -m pip install pygame`, since that's the least error prone way to install pygame.
Answer: Here is how I installed pygame on my Windows 10 Home 64 bit system with Python 3.6. (I installed Python through Anaconda but this should also work if you used python.org.)

Download the latest version of pygame, 1.9.3, from https://pypi.python.org/pypi/Pygame/1.9.3. For the system I described the proper file is
pygame-1.9.3-cp36-cp36m-win_amd64.whl.
(That file is near the bottom of the list on the given page. Despite the "amd" in the file name, that is not just for AMD processors but for any 64-bit Windows 10.) Save the file to a directory you can reach with the Windows command line (also called the DOS box) or Windows PowerShell as an administrator.
Open a Windows command line (DOS box) or PowerShell in that directory as an administrator. One way to do that is to open that directory in File Explorer, click the "File" tab on the far left near the top, hover the mouse over "Open Windows PowerShell", then click "Open Windows PowerShell as Administrator." Another way is to press Windows-R, enter "cmd.exe", and "cd" to the proper directory.
In the command line/DOS box/PowerShell window, enter the command
pip install pygame-1.9.3-cp36-cp36m-win_amd64.whl
Test the installation by opening Python and entering
import pygame
Comment: PS C:\Program Files\Python36> pip install pygame-1.9.3-cp36-cp36m-win_amd64.whl
pip : The term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ pip install pygame-1.9.3-cp36-cp36m-win_amd64.whl
+ ~~~
    + CategoryInfo          : ObjectNotFound: (pip:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Comment: i dont know where i have to save  the file so i think that is the problem
Comment: Something seems to be wrong with your Python installation if `pip` is not accessible. Try finding `pip` by opening a DOS box and running `dir C:\pip.exe /s`. When you find its location, save the pygame file there and try again in that directory. If you cannot find pip then try re-installing Python.
Comment: i have been trying for over an hour and my pc is just not made for it i guess, i am also learning java so i might try with that
Comment: @Woutervandenbrandt: Did you find `pip.exe`?
Answer: On Linux, download the appropriate whl file from https://pypi.python.org/pypi/Pygame/1.9.3, extract it to the folder where you have python site-packages, and you are done.
Answer: If you're on a windows computer, you can do this:

type cmd in the search bar and click enter
type python --version
type pip --version
based on what the python version said, you would use the first number. I was on version 3.10, so I used pip3. now type pip"(python version)" install pygame.
You'll see some text highlighted green. Copy that and paste it in the search bar, click enter.
You should see two folders containing pygame in the file explorer.

I don't know what to do for mac.

How do I install pygame on python 3.6?


Falha na atualização das variaveis

Question: Estou a desenvolver um projeto em java, um "farm game" onde existem varias stats para o personagem que é criado.
Quando criamos uma personagem defenimos qual a sua classe, isto vai influenciar as suas stats.
O meu problema é aqui, quando escolho a class as stats não atualizam devidamente.
Codigo para escolher a classe:
import java.util.InputMismatchException;

public class Knight extends Game {

    public static PlayerClass chooseSubClass() {
        System.out.println("1 -» Heavy \n2 -» SwordsMaster \n3 -» Royal");
        int op = 0;
        while (op == 0) {
            try {
                op = Main.in.nextInt();
            } catch (InputMismatchException ex) {
                System.out.println(ex);
            }
        }
        PlayerClass playerClass;
        switch (op) {
            case 1:
                playerClass = new Heavy();
                break;
            case 2:
                playerClass = new Swordsmaster();
                break;
            case 3:
                playerClass = new Royal ();
                break;
            default:
                playerClass = chooseSubClass();
                break;
        }
        return playerClass;
    }
}

Caso se escolha heavy:
public class Heavy extends PlayerClass {

    public Heavy() {
        attackDamage += 50;
        armorPenetration += 20;
    }
}

As stats:
public class PlayerClass {

    public long attackDamage = 10;
    public long armorPenetration;
    public long magicDamage= 10;
    public long magicPenetration;
    public double attackSpeed= 1;
    public long money = 100;
    public int level = 1;

}

Codigo do player
public class Player extends PlayerClass{

    private String playerName;
    private PlayerClass playerClass;

    public String classes = "1 -» Knight \n2 -» Mage \n3 -» Archer";

    //Chooses the class and subclass of the player
    public void chooseClass() {

        System.out.println(classes);
        int op = Main.in.nextInt();
        switch (op) {
            case 1:
                playerClass = Knight.chooseSubClass();
                break;
            case 2:
                playerClass = Mage.chooseSubClass();
                break;
            default:
                chooseClass();
                break;
        }
    }

    // Setter method
    public void setPlayerName(String name) {
        playerName = name;
    }

    // Getter method
    public String getPlayerName() {
        return playerName;
    }
    public String getClassName() {
        return playerClass.getClass().getName();
    }

}

Answer: Analisei seu código rapidamente e pelo que pude perceber o problema está no fato de você  utilizar armorPenetration += 20 sem que a sua variável armorPenetration tenha um valor inicial. Nesse caso você deve escolher um valor padrão na criação da sua class, no caso PlayerClass.
Por exemplo, escolhendo 0 como valor inicial ficaria assim a classe:
public class PlayerClass {

public long attackDamage = 10;
public long armorPenetration = 0;
public long magicDamage= 10;
public long magicPenetration = 0;
public double attackSpeed= 1;
public long money = 100;
public int level = 1;

}

Nesses casos é sempre bom iniciar com um valor padrão quando criar uma nova classe.

Falha na atualização das variaveis


ALSA snd_pcm_drop() is not clearing complete buffer

Question: I am using ALSA API snd_pcm_drop() to clear the buffers. But when I continue audio later with snd_pcm_prepare(), I can hear part of previous audio which was supposed to be cleared.
This happens when I have a high value of snd_pcm_sw_params_set_stop_threshold(). If I am using a lower value, the partial audio from previous audio session wont be played.
What is happening here ? How to clear off the buffer completely ?
(I am new to ALSA)
Thanks
Comment: did you ever figure this out? I am seeing the same thing on the Raspberry Pi.
Comment: Is it an option to use snd_pcm_drain() instead of snd_pcm_drop()?
Comment: Sounds like a driver bug, but this is hard to say without more information.
Comment: @Multimedia Mike ,  As per documentation, snd_pcm_drain() will not drop the buffer immediately. So it is not useful to me.Anyway  I tried snd_pcm_drain(), but the observation is same.
Answer: I solved the same issue in this way:
snd_pcm_prepare(pcm_handle);
snd_pcm_drop(pcm_handle);

Answer: " can hear part of previous audio which was supposed to be cleared." because there is still some audio data left in the ALSA buffer, and the data is not big enough to play.Enlarge the audio package when you put into ALSA buffer may solve the problem.


Async a group of functions to over an array

Question: So I have an array of four JSON objects, and I want to add three new fields to each of these objects. To do this, I have one async function for each new field to add. Ideally, what I want to do is limit the async function to only 2 updates each. Then at the end, callback the original function and print out the newly formed array. But I can't get this to work, although I feel like I'm close...
<code>var async = require('async');

// My Array of JSON objects
var allIssues = [
    { id: '1', color: 'blue' },
    { id: '2', color: 'red' },
    { id: '3', color: 'gold' }
];

addExtraFeaturesToEachissue(allIssues, function(data) { console.log(data); });

function addExtraFeaturesToEachissue(allIssues, callback) {
    async.each(
        allIssues,
        async.applyEach([addheight, addWidth, addWeight]),
        callback(data)
    );
}

function addHeight(issue, callback) {
    setTimeout(function() {
        issue.fields['height'] = "1000";
        callback(issue);
    }, 100);
}
function addWidth(issue, callback) {
    setTimeout(function() {
        issue['width'] = "100";
        callback(issue);
    }, 300);
}
function addWeight(issue, callback) {
    setTimeout(function() {
        issue.['weight'] = "500";
        callback(issue);
    }, 200);
}
</code>
Comment: Please include the shape of the expected JSON array. Also, why are you opting for the async approach and delaying the filling of your JSON array using timeouts ?
Comment: [
    { id: '1', color: 'blue', 'width' : 100, 'weight' : '500', height : '1000' },
    { id: '2', color: 'red' ,'width' : 100, 'weight' : '500', height : '1000 },
    { id: '3', color: 'gold', 'width' : 100, 'weight' : '500', height : '1000 }
];

and they area actually more complicated functions, I'm just putting in a random timer to show that they do take a while to perform
Answer: supposing that the called functions will do some long task, honestly I don't see the need of using async, why don't just use Promise?
<code>// My Array of JSON objects
var allIssues = [
    { id: '1', color: 'blue' },
    { id: '2', color: 'red' },
    { id: '3', color: 'gold' }
];

addExtraFeaturesToEachissue(allIssues, function(data) { console.log(data); });

function addExtraFeaturesToEachissue(allIssues, callback) {
  var requests = allIssues.map((issue) => {
    return addHeight(issue).then(addWidth).then(addWeight);
  });
  Promise.all(requests).then((data) => console.log('done', data));
}

function addHeight(issue) {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
      issue.height = "1000";
      resolve(issue);
    }, 100);
});
}
function addWidth(issue) {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
        issue.width = "100";
        resolve(issue);
    }, 300);
  });
}
function addWeight(issue) {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
        issue.weight = "500";
        resolve(issue);
    }, 200);
  });
}
</code>
Edit I've just seen that you whant the complete array at the end. As suggested by Jared Smith, you can use Promise.all ;)


Executar função somente quando o user soltar o input type="range"?

Question: Estou usando um input[type="range"] como barra de progresso para um player de vídeo, é ao simular o evento para mudar o tempo do vídeo pois só quero que a ação seja executada somente depois que o usuário soltar o input, Exemplo:
$('input range').bind('change click mousemove', function() {
    var atual_val = $(this).val();
    vid.currenTime = atual_val;
});

Se eu apenas deixar dessa forma, cada vez que o usuário deslizar / arrastar o input, enquanto a ação é executa o player vai "bugar", logo eu quero que o vídeo só mude o currentTime após o usuário soltar a input, como posso fazer isso?
Answer: O que você pode fazer é usar o .on mouse event, como por exemplo:
.on('mouseup', handler) ou .on('mousedown', handler)

O JQuery permite o uso de atalhos para chamar estes eventos, como:
$( "#alvo" ).mouseup(function() {
  alert( "Atalho para .mouseup() foi chamado!" );
});

Sendo assi, seu exemplo ficaria desta forma
$('input range').mouseup(function() {
    var atual_val = $(this).val();
    vid.currenTime = atual_val;
});

Para maiores referências, consulte:
https://api.jquery.com/mouseup/
Comment: Funcionou perfeitamente bem xD


R How to delete selected row in data frame

Question: How to delete selected Rows in R.
I have Data frame (stock) like give below
100, A, B
101, B, C
102, A, B
103, B, C
104, A, B
105, B, C

And I have vector (vec) list like given below.
101
104
106

Want to delete all rows in data frame that are  present in the vector list.
I tried to achieve the same with subset function but I get error/ warning
subset(stock,stock$col1 == vec)

Warning messages:
1: In is.na(e1) | is.na(e2) :
longer object length is not a multiple of shorter object length
2: In `==.default`(stock$col1, vec) :
longer object length is not a multiple of shorter object length

How do I delete all rows in data frame that are  present in the vector list. Please advice.
Comment: possible duplicate of [Remove the rows of data frame whose cells match a given vector](http://stackoverflow.com/questions/11004203/remove-the-rows-of-data-frame-whose-cells-match-a-given-vector)
Answer: Use this code:
stock <- stock[-vec, ]

And yes, this is a duplicate and you probably should have spent more time searching the Stack Overflow database.
Comment: sorry, If it is duplicated. But I tired searching in stack over flow and found I should use subset and i tired with subset function accordingly. Since got error with subset  I made this post.  Thanks anyway I helped saved my day.
Answer:     stock<-stock[!stock$col1 %in% vec[,1],]


Heatmap for co-localization of factors in a set of clusters?

Question: I want to represent the co-occurrence of factor groups within clusters in a heat map that reflects the colocalization frequency of each pair of factors in clusters (yellow for more frequently colocalized, red means less). After trying different things, I came up with the code below. Is this a sensible way of representing this data set?
<code>set.seed(1)
x = c(
  paste("cluster-",sample(c(1:30000),800000, replace=TRUE),sep=""),
  paste("cluster-",sample(c(1:300),100000, replace=TRUE),sep=""),
  paste("cluster-",sample(c(600:900),100000, replace=TRUE),sep="")
     )
y = c(
  paste("factor-",sample(c(letters[1:19]),800000, replace=TRUE),sep=""),
  paste("factor-",sample(c(letters[1:3]),100000, replace=TRUE),sep=""),
  paste("factor-",sample(c(letters[6:9]),100000, replace=TRUE),sep="")
     )
d = data.frame(x,y)
t = table(d)
dat = rbind(t[,])
dats = dat/apply(dat,1,sum)
cdats = cor(dats)
heatmap(cdats)
</code>
PS: also, I would like to add a correlation score legend of the bottom right corner that shows the colour code for correlation from 1 to 0. How can I do that?
Answer: Some quick googling suggests it is not possible, but other packages extend the base heatmap packages capabilities. Verbatim from a r-help thread;

Take a look at:

heatmap.2 in gplots package
heatplot in made4 Bioconductor package
heatmap_2 in Heatplus Bioconductor package

efg
Earl F. Glynn Bioinformatics Stowers Institute for Medical Research

Also, over on Stack Overflow one of the users has developed a package, ggdendro,  to produce heatmap + dendrogram with ggplot2 (and the linked example has a legend). Also the lattice plot it was trying to replicate looks pretty good to begin with IMO (and has all the same elements of your example), and it produces the heat map legend already.
Also (just for overkill) given the nature of the visualization you may be interested in the corrplot package, which is based off the work of Michael Friendly (Friendly, 2002). See this answer for a quick example. Although it doesn't have the capabilities to produce the dendrogram, it does have some matrix re-ordering capabilities to group the variables. You can also reduce the information on display, such as only plotting the 95% ellipses and/or loess smoothers (if you rather not rely solely on bivariate correlations, which seems unreasonable in many situations between so many variables). Scagnostic measures may be of interest as well (Wilkinson & Wills, 2008).

Michael Friendly. 2002. Corrgrams. The American Statistician
56(4): 316-324.
PDF
Leland Wilkinson & Graham Wills. 2008. Scagnostic Distributions.
Journal of Computational and Graphical Statistics 17(2): 473-491.
This PDF is not available online, but another one that introduces the
concept is (Wilkinson et al.,
2005)


Changing properties in Seaborn displot

Question: It looks like a pretty straightforward task but I cannot find the right solution.
I do generate 10 collections with 100 samples in each and pack them into pandas.DataFrame assigning index numbers for each column.
How to plot all 10 lines with: same color='b' for all, set alpha=0.5, linewidth=1.0 ?
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
import random

sns.set_style("darkgrid")
sns.set_context("talk")
SEED = 202107
np.random.seed(SEED)
random.seed(SEED)

Num = 10
val = [np.random.randn() for _ in range(Num)]
my_data = [mu + np.random.randn(100) for mu in val]

df = pd.DataFrame()
for index in range(Num):
    name = str(index)
    df[name] = my_data[index][:]

sns.displot(df, kind='kde', legend=False)

plt.show()

Answer: The answer requires realizing that in wide-form mode, displot assigns the column dimension to a hue variable. So you control the color through the palette parameter and the easiest way to make everything one color is to pass a list of identical entries with the right length. The other properties can be controlled by matplotlib keyword arguments that get passed through to plt.plot:
g = sns.displot(
    df, kind='kde', legend=False,
    palette=["C0"] * Num, linewidth=1
)
plt.setp(g.ax.lines, alpha=.5)

It looks like there's a bug where alpha= is ignored, which you can workaround by modifying the artists after plotting (as above) or by defining the palette as [mpl.colors.to_rgba("C0", alpha=.5)] * Num.
Comment: Thanks so much! It does a good job. alpha property makes no effect.

Changing properties in Seaborn displot


The Nearest Points

Question: Given a set $R$ of $N$ points $R={(x_1, y_1, z_1), (x_2, y_2, z_2),....., (x_n, y_n, z_n)}$ and set $S$ of $M$ points  $S={ ((a_1, b_1, c_1), (a_2, b_2, c_2),...(a_m, b_m, c_m))}$.
for each point $p_i(i=1\space \text{to}\space N)$ in Set $R$, find the point $q_j(j=1\space \text{to}\space m)$ in set S such that the euclidean distance between $p_i$ and $q_j$ is minimum.
Note: euclidean distance between $(l_1, m_1, n_1)$ and $(l-2, m_2, n_2)$$=\sqrt{ abs(l_1-l_2)^2+abs(m_1-m_2)^2+abs(n_1-n_2)^2}$.
The constraints are large. The  total number of points in each set $R$ and $S$ is very large $(1\leq N, M\leq 50,000)$.
How to solve this problem..
Edit
Ross Millikan pointed out this can be done in polynomial time.(So i had made  a slight change in the problem statement).
By brute force it can be done in $O(N*M)$ time. But the problem is $(N*M)$ means $(50000*50000)=10^8$ steps. But I have to get output in less than $3$ seconds. So, is there better solution than $O(N*M)$ ?
Thanks!
for example::
The points of Set S {(1,0,0),(0,0,1),(0,1,0),
The points of Set R {(0,-1,0),(0,0,-1),
For the point p: (0 ,-1 ,0) in set R

Eucd distance between point p and (1,0,0) is: sqrt(2)
Eucd distance between point p and (0,0,1) is: sqrt(2)
Eucd distance between point p and (0,1,0) is: sqrt(4)
So minimum distance is (sqrt)2
So the point ie .Answer= q= (0 ,0 ,1)

For the point p: (0 ,0 ,-1) in set R

Eucd distance between point p and (1,0,0) is: sqrt(2)
Eucd distance between point p and (0,0,1) is: sqrt(4)
Eucd distance between point p and (0,1,0) is: sqrt(2)
So minimum distance is (sqrt) 2
So the point ie .Answer= q=  (0 ,1 ,0).

    Note ,if there are more than one points for which the distance is minimum,we just need any one of them.

Example 2:
The points of Set S {(-91,10,5),(3,-6,7),(4,5,8),(-89,1,4),
The points of Set R {(4,-6,7),(10,-30,17),(8,9,10),(3,4,-9),
For the point p: (4 ,-6 ,7) in set R

Eucd distance between point p and (-91,10,5) is:(sqrt) 9285
Eucd distance between point p and (3,-6,7) is:(sqrt) 1
Eucd distance between point p and (4,5,8) is:(sqrt) 122
Eucd distance between point p and (-89,1,4) is:(sqrt) 8707
So minimum distance is (sqrt) 1
So the point ie .Answer= q= (3 ,-6 ,7)
For the point p: (10 ,-30 ,17) in set R

Eucd distance between point p and (-91,10,5) is:(sqrt) 11945
Eucd distance between point p and (3,-6,7) is:(sqrt) 725
Eucd distance between point p and (4,5,8) is:(sqrt) 1342
Eucd distance between point p and (-89,1,4) is:(sqrt) 10931
So minimum distance is  (sqrt)725
So the point ie .Answer= q= (3 ,-6 ,7)
For the point p: (8 ,9 ,10) in set R

Eucd distance between point p and (-91,10,5) is:(sqrt) 9827
Eucd distance between point p and (3,-6,7) is:(sqrt) 259
Eucd distance between point p and (4,5,8) is:(sqrt) 36
Eucd distance between point p and (-89,1,4) is:(sqrt) 9509
So minimum distance is  (sqrt)36
So the point ie .Answer= q= (4 ,5 ,8)
For the point p: (3 ,4 ,-9) in set R

Eucd distance between point p and (-91,10,5) is:(sqrt) 9068
Eucd distance between point p and (3,-6,7) is:(sqrt) 356
Eucd distance between point p and (4,5,8) is:(sqrt) 291
Eucd distance between point p and (-89,1,4) is:(sqrt) 8642
So minimum distance is  (sqrt) 291
So the point ie .Answer= q= (4 ,5 ,8)

Comment: This is solvable in polynomial time:  For each of The $N$ points $p_i$, calculate the $M$ distances from the $q_j$ and sort them.  This is of order $NM \log M$.  Do you mean that you can't reuse one set of points?
Comment: @Ross Millikan:: I can reuse them.How it can be done in NMlogM time?
Comment: @RossMillikan:: By brute force it can be done in N*M times .We don't require MlogM approach..
But the problem is N*M means (50000*50000)=10^8 steps ..But i have to get output in less than 4 seconds.. What i exactly Want is better solution than O(N*M)
Comment: @ChopraJack: yes, you can find the highest in M tries.  That shows this problem is not in $NP$.  But even problems in $P$ can be intractable if the number of cases is large enough.
Answer: You can treat this as $n$ independent nearest neighbour search problems, namely, for each point in $R$, find its nearest neighbour among the $m$ points in $S$. The Wikipedia article lists lots of methods to do this efficiently. A popular approach is to use a $k$-D tree on the points in $S$. This takes $O(m\log m)$ time to build, and $O(\log m)$ time per nearest neighbour query if the points are randomly distributed. This gives a total time of $O((m+n)\log m)$.

The Nearest Points


Why is migration lookup failing during synchronisation of Group entities?

Question: On the basis of an RSS feed, I am required to create Group entities and Users out of the feed's items.
Each item contains sufficient data:
<item>
  <id>123</id>
  <title>An example</title>
  <description>Some <strong>HTML</strong> description.</description>
  <author>John Doe</author>
  <email>email@example.com</email>
  <pub_date>09/19/2017 - 08:30</pub_date>
</item>

I am using Core's migration module along with migrate_plus and migrate_tools and wrote two migration yml files, one for creating Group entities out of the items and one to generate users out of the author & email fields.
Taken separately, both work fine, that is I am getting Group entities and User entities as expected. The only issue which I can't get my head wrapped around is to set the respective User to be the Group's owner.
Here's the processing part of the Group yml file:
process:
  uid:
    plugin: migration_lookup
    migration: migrate_plus.migration.users
    no_stub: true
    source: email
  label: title
  field_description: description
  created:
    plugin: format_date
    from_format: 'm/d/Y - H:i'
    to_format: 'U'
    source: pub_date
  status:
    plugin: default_value
    default_value: 1
  type:
    plugin: default_value
    default_value: my_group_type

migration_dependencies:
  required:
    - migrate_plus.migration.users

All fields except for uid are set correctly. If I change the user id process part to something static like
  uid:
    plugin: default_value
    default_value: 1

it works as well and the Group's owner is set to the Super User account. So, I reckon there's something wrong with the migration_lookup processing part. I have read numerous tutorials on how to use that and it looks fine to me. I also read that migration_lookup fails silently if there is an error which makes it hard to debug.
Looking at the migration map table in the database it looks fine too:
mysql> select * from migrate_map_users limit 1;
+-----------------+----------------------+---------+-------------------+-----------------+---------------+------+
| source_ids_hash | sourceid1            | destid1 | source_row_status | rollback_action | last_imported | hash |
+-----------------+----------------------+---------+-------------------+-----------------+---------------+------+
| 009d...234ef457 | email@example.com |     118 |                 0 |               0 |             0 |      |
+-----------------+----------------------+---------+-------------------+-----------------+---------------+------+
1 row in set (0.00 sec)

Due to the migration dependency through
migration_dependencies:
  required:
    - migrate_plus.migration.users

the User migration should also run first, but it still doesn't work. Even after both migrations have run through, I tried an update migration run, but the Group's owner is never set (stays at being 0/Anonymous).
Is there soemthing wrong with my yml file? Are there other means to connect user and groups?
Answer: s/migrate_plus.migration.users/users/
The "migrate_plus.migration." is not part of the migration plugin ID - it is only part of the filename for the config entity.
Comment: Since all tutorials actually write just `users`, I tried that first, of course, but it never worked at all. Thus, I started using `migrate_plus.migration.users` and then the reference happened. For my specific problem, I ended up using `entity_lookup` instead of `migration_lookup` as a workaround.


Azure Service Bus Serialization Issues [C# / JAVA]

Question: I have some problems with serialization in c# and java cross platform:
We have 2 apps using ServiceBus, a C# one and a JAVA one. Lets say C# is in charge of publishing data to a queue / topic and JAVA is subscribing to it.
In C# we use the standard publish method which allows generic types publish so it automatically serializes data sent with TypeA class.
Then in java we have TypeA equivalent coded, but cannot find any API-Deserializer that can deserialize the object to our .
Is there any valid deserializer for JAVA or we must serialize (for example using JSON) in C#, publish it and deserialize in JAVA with any JSON deserializer?
Thanks in advance!
Answer: I believe one of the DataContractSerializers will be used if you don't do anything on the BrokeredMessage.  You didn't show any code, but I assume you are using BrokeredMessage class.  If you want cross-platform, you need to control the serialization.  I would serialize to JSON and either pass the JSON as a string, or use the stream overload and perhaps compress the stream, passing that in the constructor (if it was a big object).  The Java client would need to get a JSON deserializer and do the reverse.
As you have figured out - the DataContractSerializer is not easily reversible in Java.  I am sure you could do it, but using JSON or protobuf is a way easier and cross platform way.
Answer: For cross-language binary serialization, refer to this question.  However, I would use JSON in this case - I believe it would be much simpler.
Answer: I guess the answer is pertaining to your existing codebase. The most orthodox way would be use XmlSerializer on C#, and JAXB on Java. Of course, you need manual examination of code in both language to ensure full interoperability.
I think both languages support multiple ways of serialization/deserialization, you just need to find the one that are compatible to each other.
Answer:
Use your choice of serializing library in C# rather than default contract serializer. (I'll be using JSON and Newtonsoft.json)
Publish message as json text stream
<code>    var json = Newtonsoft.Json.Linq.JObject.FromObject(yourSerializableObject).ToString();
    var stream = new MemoryStream();
    var writer = new StreamWriter(stream);
    writer.Write(json);
    writer.Flush();
    stream.Position = 0;
    _topicClient.Send(new BrokeredMessage(stream, true) { ContentType = "application/json" });
</code>
Receive message as json text stream
<code>    var message = _subscriptionClient.Receive();
    Stream stream = message.GetBody<Stream>();
    StreamReader reader = new StreamReader(stream);
    string s = reader.ReadToEnd();
</code>


how i need to creating the csv from arrays and dictionaries in ios

Question: Here i need to create the csv file from arrays in to dictionaries, is there any way my arrays are
<code>{
    customerid = 1;
    jewelid = 1;
    reporttype = 1;
    sessionid = 2147483647;
    storeid = 1;
},{
    customerid = 1;
    jewelid = 1;
    reporttype = 0;
    sessionid = 2147483647;
    storeid = 1;
},{
    customerid = 1;
    jewelid = 1;
    reporttype = 0;
    sessionid = 2147483647;
    storeid = 1;
}
</code>
Answer: Thats not a CSV file. A CSV usually doesn't have brackets. Perhaps you are looking for the JSON format? But that's json neither, because the ending semicolons should be commas in JSON.
Anyway there are parser classes out there for CSV (eg CHCSVParser) and JSON (eg SBJSON).
See: Where can I find a CSV to NSArray parser for Objective-C?
// edit:
Like noted in the comments, most parsers do both ways: reading & writing. Have a look at CHCSVWriter.
Comment: He's looking for the other way around - array to CSV.
Comment: The parsers normally do both ways. There is a CHCSVWriter in the bundle, too.
Answer: You have to iterate through the array or dictionary and create the CSV manually by using an NSMutableString.


Alternative to Wilcoxon signed-rank test with sample size < 6

Question: I have to make a statistical analysis of only four (n=4) data pairs. I have tested two specific warm-ups for 4000m Team Pursuit in track cycling, and used a 250 m. sprint test as a meassurement of the warm-ups effect. I only had four track riders available as test subject, så they underwent both types of warm-up followed by the sprint test, on seperate days (repeated meassures). That gives me the four data pairs that i would like to analyse. I first thought of a Wilcoxon signed-rank test, but found out that it required at least six data pairs. I haven't been able to find an alternative test. "Please help Obi-Wan Kanobi (read: stat experts at Cross Validated), you are my only hope!"
ps. all four data pairs show the same warm-up as the most effective.
Comment: @ttnphns The problem is that with 4 pairs the smallest achievable two-tailed significance level is 0.125 ($2(\frac12)^4$). This applies equally to the permutation test as it does to the signed rank test. Similarly, when there are 6 pairs, the smallest achievable significance level for both tests is  $2(\frac12)^6=\frac{1}{32}=0.03125$, which is the first one below 5% (this is the source of the "requires six pairs" in the question -- which is actually only true if you insist on a two-tailed test with a significance level of 5%).
Comment: An exact, permutation-based significance can be done for every nonparametric test of a small sample size.
Comment: @Glen_b, thank you for reminding, and I'm aware of the problem. But could there be a solution for the OP, free of it?
Comment: Realistically a non-parametric test with a significance level of 0.05 is silly with n=4 and it would be hard to see how this could be a particularly compelling dataset - perhaps a little pilot trial that should not be overinterpreted? If there is previous data, then identifying a suitable parametric may be possible and may give some power. However, unless you are looking for enormous effect sizes, this is likely not a very conclusive experiment and ideally you should do a better experiment with a sample size suitable to detect realistically plausible effect sizes.
Comment: Hey folks. Thank you for taking your time to answer. As you might can tell I'm not a statistics expert of any kind, but your answers have been useful to me anyway. And Bjørn, you are right, this is a small trial, but smaller then planned to be. I had 8 subjects available to begin with, but injuries and sickness left me with only four and a statistical challenge I had no idea how to handle. Even though I can't say my results are statistical significant, they still show a tendensy which gives me something to elaborate on in my discussion chapter.
Answer: If you want to be able to use a significance level of 5% and you want a two tailed test, you're pretty much left with parametric tests.
However, this doesn't mean you're stuck with normal-theory tests.
For example you could treat times as (say) gamma-distributed and use a link-transformed sprint-time-for-first-warmup-type as an offset and an intercept-only model (a GLM). e.g. with a log-link you could use log-sprint-time-for-first-warmup-type as offset. Then a test of the intercept would be testing for change in sprint times. (Alternatively, generalized linear mixed-effect models might be used.)
There are numerous other possibilities (e.g. assume differences of log-times are normal).
You might even consider a normal theory test after all, and assume time differences are normal.
Comment: (+1) for the overview of some possibilities.


Unable to add css class to a Panels region

Question: I wish to add custom css classes to specific regions in a panels page.
I understand from this question Add a class to a panels pane
that I should be able to do this pretty trivially from the settings menu on the pane or region itself, however, there is absolutely no option to do so.
I am using panels 7.x-3.5 with Panels Bootstrap in Drupal 7.
No matter what layout setting I use - Boostrap, custom style etc - I never get any option to add css classes.
The boostrap settings has a handful of drop down boxes, the custom style settings has a heap of options but no ability to add css classes.
Do I need to over-ride a template somewhere? This should be trivial...
Answer: The option to add a CSS class is on the actual pane content. In a Bootstrap panel region the cog on the left gives you the option to set the grids up. If you need to add a class to that you will need to find the template file for the region you are using and copy it to your theme/sub-theme and add the class there or write a cTools plugin for that yourself. The ability to add a class is on the actual content panes that you add inside the region, so for instance if you were to add a view pane into the left region inside your panel you could place that view inside a bootstrap .well or bootstrap .alert using the cog on the right hand side (the cog on the content inside the region) of the content pane. So you can add the class to the content pane via settings that way.
If you need to add a class to the whole region you need to copy your template file for your layout to your theme. So if you were using Bootstrap three column stacked you would copy bootstrap-threecol-stacked.tpl.php to your templates folder in your theme. And modify the outer div for the region you want inside that template file. The regions are actually listed in that template file (you will see them inside the file). You can modify any of those template files inside the module, if you want to see what template files have what markup turn theme debug on and inspect your HTML of the page (see here for how to do that).
There is this module where you can add the alert or well into a region: https://www.drupal.org/project/panels_bootstrap_styles but I've never actually used that one and it looks limited. I would stick to the bootstrap panel layouts and proper templating, or cTools plugins for these types of things work great also. You can write them like that module does for just about any markup you need :)
You can also use a module like Semantic Panels to do this, it was a little overkill for something like adding a CSS class so I didn't add it before but you may find it easier to use.
Comment: If you look at the bootstrap panel layout template file (bootstrap-threecol-stacked.tpl.php) it has all of the regions in there, but I'm gong to add another way to do this with a preprocess function also that should work.
Comment: So say I want to add 4 different sets of classes for each of 4 regions on a panels layout I have to generate 4 separate template files... And if I want to do this on a number of pages? I am starting to regret using Drupal...
Answer: I guess adding css class programmatically in THEME_preprocess_html will work. the Add a class to panel page body element suggested a solution to do this using menu_get_item().
Comment: I don't want to add a css class to ALL panel panes, only to a specific one. For example, say I want to add classes: mypanel-region1, mypanel-region2 to different regions in the same panel but to any other panels.
Comment: You can filter it by some attributes. Like the current page url or anything else
Comment: Surely there is a more sensible way... that seems like an unreasonable amount of work to add a handful of custom css classes...
Comment: May be there is, but it is all came to my mind to add class in the absence of UI ;)


How to statistically compare the same classifier on two different sets of features?

Question: I have two sets of features (A and B) and I would like to compare the performance of the same classifier, trained on each of these sets.
The two sets correspond to features taken from the same population. Example: feature set A corresponds to the age and gender of a certain group of individuals and feature set B corresponds to their weight and height. So, in the end, the two feature sets will have the same size, and they will correspond to the same population, although they will represent different things.
What is the most suitable statistical test for this comparison?
The model is the same (apart from the hyperparameters, which I optimize), only the set of features are different.
I would like to perform a Monte-Carlo cross validation procedure (randomly sample the test set without replacement) and then perform a paired t-test, since the features come from the same individuals.
Is this a valid approach?
EDIT:
Would a permutation test be more appropriate?
Comment: What model do you have in mind?
Comment: I'm currently using XGBoost
Comment: Personally, I wouldn't try to mix the two approaches, I would stick to comparing predictive accuracy measures. But if I really had to introduce "significance", then  I would use bootstrapping.
Comment: Thank you for your reply! How would I use bootstrapping in this case? What I obtain in the end is 50 AUCs values for each model
Answer: If I were to do this, and I wanted to apply some concepts of statistics such as a confidence interval for my parameter (or some sort of "significance"), I would use a bootstrap approach.
In a loop:

Bootstrap your sample
Obtain a score (preferably a proper scoring rule) for models A and B from some hold-out set (a test set or CV)
Compute the difference of scores A - B and save the result

At the end you will have (hopefully) many scores, which represent the distribution of the differences. From here you can apply common approaches with bootstrap results.
For ex.

choose a level for $\alpha$, say $0.05$
compute the $0.025$ and $0.975$ quantile of the scores and also the mean score
compute the distance from the lower and upper quantiles to the mean
add/substract these distances from your original score to obtain a confidence interval around your original score

In general, if such an interval will contain 0, then you cannot reject the hypothesis, that the two models are significantly different, for the chosen $\alpha$.
Comment: @asere When using bootstrap to estimate some sort of variability score of your models or parameters, you would usually obtain the average score on your whole (original) training data, and then use resampling to estimate the variability around this "original" score. In the end, you are interested in how your model performs on the original training data, and not on resamples of it.
Comment: If I understood you correctly what you described in steps 1-3 is what I have so far. I take multiple random hold out sets and check the performance of both models on that hold out set. In the end I get two distributions of AUCs for each of the models. I haven't, but I can easily compute the differences. What is confusing me in you next steps is the "original score". What does this refer to?


Time returns 0 in Binary Search Tree

Question: I want to measure time it takes to search for x, problem is that its always 0.
I tried using different methods of calculating time with no luck.
If I calculate insertion then it works properly, so why is that it doesn't work in other case?
Functions work correctly because they are taken from site that explain BST, my task is to calculate and analyse time taken to complete functions.
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<windows.h>
#include <fstream>
#include <chrono>
#include <iomanip>
using namespace std;

struct node
{
    int key;
    struct node *left, *right;
};

struct node *newNode(int item)
{
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->key);
        inorder(root->right);
    }
}

struct node* insert(struct node* node, int key)
{
    if (node == NULL)
        return newNode(key);

    if (key < node->key)
        node->left  = insert(node->left, key);
    else
        node->right = insert(node->right, key);
}

struct node * minValueNode(struct node* node)
{
    struct node* current = node;

    while (current->left != NULL)
        current = current->left;

    return current;
}

struct node* search(struct node* root, int key)
{
    if (root == NULL || root->key == key)
        return root;

    if (root->key < key)
        return search(root->right, key);

    return search(root->left, key);
}

struct node* deleteNode(struct node* root, int key)
{
    if (root == NULL)
        return root;

    if (key < root->key)
        root->left = deleteNode(root->left, key);
    else if (key > root->key)
        root->right = deleteNode(root->right, key);
    else
    {
        if (root->left == NULL)
        {
            struct node *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct node *temp = root->left;
            free(root);
            return temp;
        }

        struct node* temp = minValueNode(root->right);
        root->key = temp->key;
        root->right = deleteNode(root->right, temp->key);
    }
return root;
}

int main()
{
    srand(time(NULL));
    clock_t start;
    struct node *root = NULL;

    for(int i=0; i<400000; i++)
    {
        root = insert(root,((rand()*rand())%20000));
    }

    double duration;

    start = std::clock();
    root = search(root, 19999);
    duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;

    cout << "Time: " << duration << endl;

    return 0;
}

Comment: Your code sample doesn't compile--it has errors. Please fix.
Comment: I was sure this is C until I saw `std::clock`.
Comment: Should work now
Comment: You may want to use 1E06 data items or run your BST 1E06 iterations and use the average.  Most computers can execute thousands of instructions in 1 second.  The *clock* may not be accurate enough or detailed enough to measure one insertion into the tree.  Search the internet for "c++ benchmarking".
Comment: BTW, you only need `struct node` when setting up the structure; otherwise `node` is a type similar to `int, double` and `char`.
Comment: Also, prefer to use `new` instead of `malloc`.  The `malloc` function does not call the `struct` or `class` constructors.  Remember to use `delete` to return the deleted or unused data.  See also `std::shared_ptr`, which handles deletion for you.
Comment: Your tree allows insertion of duplicate keys.  When searching, do you choose the first key that matches, the 2nd?  The third?  I was taught that Binary Search Trees should not contain duplicates.
Answer: Your insert function does not return anything if the parameter node is not NULL. This is Undefined Behaviour, and can result in literally anything, including demons flying out of your nose.
Seeing that no segfault occurs, it would seem that compiler gracefully assigns NULL to the pointer root in main, which means your tree has at most two elements (and there are many, many such trees lost in memory). Searching for element in two element tree certainly can take 0 seconds. But that's just random guessing - it's UB, it can be everything.

You should enable compiler warnings (for gcc that would be for example flags -Wall, -Wextra and -pedantic, preferably with -Werror to turn all warning into errors). Such issue would be very easy to find with compiler warnings.

Time returns 0 in Binary Search Tree


UILabel AutoResize Swift

Question: I'm learning Swift, but I Have two "stupid" Problems.
the first, I'd like autosize my UILabel
The second, I have another UIlabel and I want to put name and surname in it's field
I tried with
@IBOutlet weak var title: UILabel!

title.text = currentPerson?.name+""+currentPerson?.surname

But I have this error

Value of optional type 'String?' not unwrapped; did you mean to use
  "!" or "?" ?
Answer: Generally advised to ask 1 question per post so you get clear responses & don't mix topics, but...

In XCode storyboard "Attributes Inspector" you can change "Autoshrink" from "Fixed Font Size" to minimum font size or scale. Also change "Lines" from default 1 to 0. You'd also need to set some AutoLayout constraints to pin the label to superview or other elements in a way that will allow it to scale. Can't say more w/o seeing storyboard.
By using optional chaining to set the label text you're trying to set the label's .text property to an optional type String?  instead of a String. Those aren't equivalent. An optional of type String? might contain a String, or it might be nil. The UILabel expects you to use a String instance, so it's complaining about the mismatch.

One approach is to explicitly check the optional value against nil:
if currentPerson != nil {
   title.text = "\(currentPerson.name) \(currentPerson.surname)"
}
else {
   title.text = ""
}

Swift's optional binding is similar to the first option, but you create a temporary constant and can reference its properties. If currentPerson is not nil, then the if block executes.
// current convention would be to use "currentPerson" on both sides, which can be confusing. The left side is a temporary constant & the right side is the optional property you've declared somewhere above
if let aPerson = currentPerson {
  title.text = "\(aPerson.name) \(aPerson.surname)"
}
else {
   title.text = ""
}

Alternatively, as the error message suggests, you could Force Unwrap the optional value to access the name properties:
title.text = currentPerson!.name + " " + currentPerson!.surname

This assumes that currentPerson is never nil. If it is nil, your app will crash here.
Also note you can concatenate using + and " " or with string interpolation.
Comment: Thanks for Reply, the strings are concat. About my first problem. I Modified Autoshrink with minimum font Scale, I added constraints, even Bottom constraints. My Label is under my BottomNavigationBar. Can I use Playground with my StoryBoard, or I have to Build each time?

UILabel AutoResize Swift


How to set/display select options in selectbox using form-api

Question: I have created a form to allow users to enter some details but I also need to display some mysql data into that form. The contents and fields of the form are displayed in a tabular format. I just want to know the right way to display mysql data in one of the table columns along with the form fields.
How can i display select options in selectbox using form-api ?
Answer: You can make use of theme_table to make a table view.
You can use form-api inside the table as you wish.
Sample code for creating table.
$header = array();
$rows = array();
$rows[] = array(
    array('data' => 'Head1', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head2', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head3', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head4', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head5', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head6', 'colspan'=> 6, 'header' => 1),
);
$rows[] = array(
    array('data' => 'SubHead6-1', 'header' => 1),
    array('data' => 'SubHead6-2', 'header' => 1),
    array('data' => 'SubHead6-3', 'header' => 1),
    array('data' => 'SubHead6-4', 'header' => 1),
    array('data' => 'SubHead6-5', 'header' => 1),
    array('data' => 'SubHead6-6', 'header' => 1),
);

return  $output .= theme('table', $header, $rows);
To make a select box with db values you can try this
$query = "SELECT * FROM {tablemanme}";
$query_result =  db_query($query);
while (($data = db_fetch_object($query_result )) !== FALSE) {
  $opt[$data->id] = $data->name;
}

$form['select'] = array(
    '#type' => 'select',
    '#options' => $opt,
);

Good luck.
Comment: Hi my from fields are displaying in a tabular format but I am not able to display mysql data in the form
Comment: @SomyaGupta, What type of mysql data? Do you want to display it inside the form fields?
Comment: yes I want college names to be displayed as a form field within a table cell So the student can make a selection
Comment: @SomyaGupta, See my edit.
Answer: You can check on this link

http://public-action.org/content/drupal-7-form-api-drupal-7-multi-step-form-fapi-table-report
It gives you complete idea of displaying data in tabular form
Comment: OP is talking about D6
Comment: that's true, but in fact form API in D7 is 95% the same as the one in D6.


c# Builder Pattern

Question: I have below class which builds reference data for my app, do you think it's a good implementation of builder pattern. What can I change to improve it?
public class ReferenceDataBuilder
    {
        private readonly List<Task> _builderTasks;
        private IDataProvider _dataAccess;
        private ReferenceData _referenceData;
        private bool _withPersistence;
        private string _persistenceDirectory;

        public ReferenceDataBuilder(IDataProvider dataAccess)
        {
            ValidationUtils.ArgumentNotNull(dataAccess, "dataAccess");

            _dataAccess = dataAccess;

            _referenceData = new ReferenceData();
            _builderTasks = new List<Task>();
            createBuilderTasks();
        }

        public virtual ReferenceData BuildAsync()
        {
            Parallel.ForEach(_builderTasks, (task) => { task.Start(); task.Wait(); });
            return _referenceData;
        }

        private void createBuilderTasks()
        {
            _builderTasks.Add(new Task(() => _referenceData.Object1 = _dataAccess.GetObject1()));
            _builderTasks.Add(new Task(() => _referenceData.Object2 = _dataAccess.GetObject2()));
            _builderTasks.Add(new Task(() => _referenceData.Object3 = _dataAccess.GetObject3());
            _builderTasks.Add(new Task(() => _referenceData.Object4 = _dataAccess.GetObject4());
        }
    }

Usage:
new ReferenceDataBuilder(dataAccess).BuildAsync();

Thanks
Comment: check out http://codereview.stackexchange.com
Comment: @DLeh, Don't Migrate Crap.
Answer: To answer your question directly - I'm not commenting on whether or not it's good code, just whether or not it's a good implementation of the builder pattern:
It's not the builder pattern at all. The builder pattern relates to objects that have complex constructors. It separates construction of the class from the class itself.
In this case, here's your constructor:
_referenceData = new ReferenceData()

What your code is doing is retrieving data and populating this object with that data. It looks like construction of the object is really simple. Again, I'm only focusing on your exact question, not looking at anything else. Is it a good implementation of the builder pattern? It doesn't appear related to the builder pattern at all.

c# Builder Pattern


Migrations dependencies: run one by one?

Question: I have a few migrations in the same group for singe multi-language entity type set up. One migration handles one language. I need to make sure that default English language migration runs first and then it triggers all the others when I execute migration for the English language via admin UI of migrate tools module.
Can not find how make it through dependencies in yml for the default language, because dependencies list gets processed first. Is there proper way to do it?
Answer: First of all, you only need two yaml files, one for the default language and one for the translations.
If you are migrating for example a content type of bundle pages and your default language migration is eg. my_website_migrate_node_page, then in the my_website_migrate_node_page_translation define your dependencies as follows
migration_dependencies:
  required:
    - my_website_migrate_node_page
  optional: { }


How can I change a rendered user display to a combination of fields linking to the user?

Question: I have a set of users on the site I'm building that have First and Last name fields in their profile.  How can I create a display that will show the user link as "Last name value, First name value" all inline?
There are a number of places where this is how I want user references to show up.  In some other places, it would be ideal to show those fields, and a couple of others along with the user's image.  Then of course there is the full profile view.  I had though this must be done with different display types, but can not see anyway to bring fields inline with each other, or adjust what text makes up the link.
Comment: so instead of the user name you want to display last name and first name?
Comment: @NoSssweat exactly
Comment: ok... how are you going about this? Views or Template file?
Comment: Template files right now.
Comment: [How to rewrite output of a field in D7](https://www.drupal.org/node/1285272)
Answer: One option would be to use:
Real Name

The RealName module allows the administrator to choose fields from the
user profile that will be used to add a "real name" element (method)
to a user object. Hook_user is used to automatically add this to any
user object that is loaded. It will also optionally set all nodes and
comments to show this name.

For Example:

By default only the raw username is being shown, as you can see at the
top of the page.

Change this to:

Result:

source: Change Drupal Usernames with the Real Name Module
Answer: Another option is to use hook_username_alter
then in your template.php file located in /sites/all/themes/yourtheme
add the following code:
function yourtheme_username_alter(&$name, $account) {
  if (isset($account->uid)) {
    $this_user = user_load($account->uid);//loads the custom fields
    $name = $this_user->field_first_name['und'][0]['value'].' '.$this_user->field_last_name['und'][0]['value'];
  }
}

replace yourtheme with your actual theme name.
You might not want to alter the user name when you're in the user profile page. So you could do something like this:
function yourtheme_username_alter(&$name, $account) {
if (    arg(0) != 'user' // test if you are on user page
) {
  if (isset($account->uid)) {
    $this_user = user_load($account->uid);//loads the custom fields
    $name = $this_user->field_firstname['und'][0]['value'].' '.$this_user->field_lastname['und'][0]['value'];
       }
     }
}

replace yourtheme with your actual theme name.
Comment: That is what I had read that I was trying to remember!  When I'm not stuck in a meeting I'll try that out!
Comment: @UltraBob how did it go?
Answer: Your question is very similar to Combine two fields to be displayed together where you'll find several ideas, some of which are solving your problem even without custom code.
Hope will help
Comment: Thanks, I hadn't found that, but I need these coming out as links to the user profile, and I need them to show up in a lot of different places, including as the rendered preview in the paragraphs module.
Comment: And I never need the link to be the user name. I'd be totally fine with editing some kind of user.tpl.php template file to do this, if there is a way to make the change for just one display type. Is display suite really the best answer for that?
Comment: @UltraBob,  I guess you should give a try to Display Suite. Once done, you'll know how good it can support your work.
Comment: the thing about that is Display Suite is gigantic.  I am trying to keep things slim, and I find it hard to believe that the best way to make user links show up in a compact form that uses some fields from the profile is to install a do-everything module.
Comment: With that said, I am going to install it and have a look.  Maybe it will solve some of my other outstanding issues.


Connected component identification?

Question: Suppose I give a random 2 variable polynomial relation such as:
$$x^3+y^3=10$$
$$x^2 + 7yx^4 + x^2-15=0$$
Etc...
How do I determine how many individual pieces there are to the graph?
Answer: Turns out this is a great question. So great, that it is one of the Hilbert questions. See this page for an intro on the subject. It's been solved in some settings, but some parts remain open. It's fun stuff.

Connected component identification?


How to specify random effects in logisitc mixed effects regression with multiple observations per subject but only 1 outcome per ~50 DV measurements?

Question: I have a dataframe that looks something like this:

Each subject got somewhere between 40-120 lesions in a given procedure, and I want to know which dependent variable was associated with "injury". A subject either had post-op injury or didn't from the sum of the procedure. So 1 'injury' observation per subject, but there oculd be 100 'gsec'per subject.
egd_grade and injury were both assessed after the interventions were performed (post-surgery) whereas Randomization_Group was assigned beforehand. I'm stuck on how to specify the random effects here.
This is what I have:
glmer(factor(injury) ~ gsec + Randomization_Group + (1|subject_id),
      family = binomial,
      data)

Model is nearly unidentifiable: large eigenvalue ratio
 - Rescale variables?Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
Formula: factor(injury) ~ gsec + Randomization_Group + (1 | subject_id)
   Data: data

     AIC      BIC   logLik deviance df.resid
    52.7     75.0    -22.4     44.7     1949

Scaled residuals:
       Min         1Q     Median         3Q        Max
-0.0002550 -0.0002424 -0.0001830  0.0063440  0.0101318

Random effects:
 Groups     Name        Variance Std.Dev.
 subject_id (Intercept) 9253     96.19
Number of obs: 1953, groups:  subject_id, 44

Fixed effects:
                               Estimate Std. Error z value Pr(>|z|)
(Intercept)                  -1.712e+01  4.738e+00  -3.614 0.000302 ***
gsec                         -2.834e-04  1.123e-02  -0.025 0.979861
Randomization_GroupTreatment  6.054e-01  4.155e+00   0.146 0.884169
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) gsec
gsec        -0.677
Rndmztn_GrT -0.526 -0.009
optimizer (Nelder_Mead) convergence code: 0 (OK)
Model is nearly unidentifiable: large eigenvalue ratio
 - Rescale variables?

My question specifically pertains to the outcome metric injury. It's not like each "gsec" observation corresponded to a single yes/no injury, rather its the sum effect of every 'gsec' observation per subject/per randomization group that influence the outcome of injury yes/no.
The subject either had injury from the entire procedure or not at all.
It feels like I need to specify injury itself as a random effect but not sure how to also specify it as my outcome variable.
Answer:
boundary (singular) fit: see ?isSingular

The variable Randomization_Group seems to be a binary variable yet you are fitting random intercepts for it. This is likely to be the reason for the singular fit. Randomization_Group should be a fixed effect here.

It feels like I need to specify injury itself as a random effect but not sure how to also specify it as my outcome variable.

That wouldn't make sense at all. For one thing, it's another binary variable so fitting random intercepts for it would be wrong, but the main thing is that it's your outcome variable.

My question specifically pertains to the outcome metric injury. It's not like each "gsec" observation corresponded to a single yes/no injury, rather its the sum effect of every 'gsec' observation per subject/per randomization group that influence the outcome of injury yes/no. The subject either had injury from the entire procedure or not at all.

There's no question there, but it sounds like your concern is that the outcome is at at the subject level. There is nothing wrong with that.
Comment: Thanks for the input Robert. Randomization_Group is certainly a fixed effect. So factor(injury) ~ gsec +  (1 | subject_id) is a perfectly valid model where gsec is a repeated measure per subject and injury is a binary outcome at the subject level? I edited my original post for the corrected regression which has some convergence issues.


nginx rewrite rule for two php parameters

Question: Can you please help me to write rewrite rule for ngnix :
<code>    Actual URL : http://www.example.com/all-dog-breeds.php?Greater-Swiss-Mountain-Dog/82

   expected URL : http://www.example.com/Greater-Swiss-Mountain-Dog/82
</code>
I tried below rewrite URL but it is not working:
<code>   rewrite ^/([A-Za-z-]+)\/([0-9]+)\/?$ /all-dog-breeds.php?$1/$2;

   rewrite ^/(.*)/(.*)$ /all-dog-breeds.php?$1/$2 last;
</code>
Comment: The first one should work if you remove the `^/` from the beginning.
Comment: As per your suggestion I tried but no success. I tried on        http://www.example.com/all-dog-breeds.php?Dachshund/9
Comment: Try `^www\.example\.com/([A-Za-z-]+)/([0-9]+)/?$`
Comment: No success. http://www.k9studfinder.com/all-dog-breeds.php?Dachshund/9
Comment: Judging from the answer that has been posted in the meantime, I was doing it the wrong way round.
Answer: The regex you're actually looking for is: https://regex101.com/r/uC1wH6/3
I believe that should be converted into Apache as follows:
<code>rewrite ^\/[^\/]*\?(.*)$ /$1
</code>
Comment: If all is successful I would appreciate if you accept by clicking the checkmark to the left of this answer.


Theme Organic Group Entity Reference on user profile

Question: On my site, users join groups via an Entity Reference field using the "Check boxes/radio buttons" widget.
The widget default is to display only the group Title. Like so (where the list bullet is a checkbox):

Group1Name
Group2Name
Group3Name

The group content type has an additional field called "description" that I'd like to include on this display. Like so:

Group1Name: Group1Description
Group2Name: Group2Description
Group3Name: Group3Description

Is there a way to theme either the checkbox widget or something in Organic Groups to display this additional field?
Answer: You need to create an entity reference view and use it for your og_groups_ref field. In Views where you click Add, you will see entity reference view next to block, page etc.
Create the view to show the groups and the add fields for title and description. Place description above title and exclude it from display.
Rewrite the title in field settings to something like [title] -[description]. The replacement patterns are in the view field settings.
Go back to your groups audience field settings (main settings, not widget) and choose entity reference view instead of organic groups or simple. Use the view you created.
Comment: Adding the caveat that view field permission needs to be set for non-members at admin > config > group > permissions > node