Where can I store private images for my app (outside of internal storage)?

Question: Is there a place where I can store and manage my own images outside of internal storage? I don't want other apps to be able to see or access these images. Should I use external storage? Does such a place exist in the new MediaStore? It's fine if they're deleted when the app is deleted.
This solution needs to support API 21 or higher.
I know there are a lot of questions like this, but they're 10+ years old and a lot has changed since then.
Use case / background
I have an app where all data is stored locally on the device (no external servers).
Users can choose custom background images for journal entries. A user could choose to use a different image for each journal entry they create. They can create as many journal entries as they want. They may revisit those journal entries. So, I need to store an unknown amount of images for the lifetime of the app. I've been saving a copy of the images the user picks from the gallery in my local app storage via context.filesDir.
I noticed a crash Fatal Exception: android.database.sqlite.SQLiteDiskIOException disk I/O error (code 4874 SQLITE_IOERR_SHMSIZE) and after googling, I found This error may indicate that the underlying filesystem volume is out of space.
My concern is that my app is running out of internal storage space because of the user images I'm storing.
Where should I be storing these images? I originally chose internal storage because I wanted my users' images to be reasonably private (since I don't know if they're storing sensitive images or not). I also wanted to make sure the images would always be available even if the source image (chosen from user's media) is deleted. However, I hadn't considered the limits imposed on internal storage. Silly me!
Comment: How would storing your images in a different directory have any effect on a user whose device ran out of space? That's out of your control.
Comment: How many images are you saving that is eating up device storage? consider only keeping the latest image for reference instead of creating a whole list of duplicate images on the device.
Comment: check out [official doc](https://developer.android.com/training/data-storage)
Comment: @GavinWright Yes, device space is outside of my control, but this is about internal storage. All apps are allocated a dynamic & unknown amount of internal storage. Presumably, my app ran out of internal storage because I let users fill it up with an unknown amount of large picture files. Since databases are kept in internal storage by default & since my app has used up its allocation of internal storage, I'm getting crashes. If I'd used external storage, my app would still be able to perform DB operations and other functions (saving to shared preferences). That's a big difference.
Comment: @snachmsm The official doc doesn't mention a way to store images privately other than internal storage, which is what I'm asking about. If I use `MediaStore API` any other app can see the images.
Comment: @DarShan I'll update the question to be more clear. My use case lets the users add custom backgrounds for journal entries. A user could choose to use a different image for each journal entry they create. So, it's an unknown amount of images and every one of these images must be stored somewhere.
Answer: Android does not provide a place for you to store private photos (can't be accessed by user or other apps) outside of app's internal storage. I know you can contradict me by saying that one can store them in external storage by using getExternalStoragePublicDirectory() but the problem is that it is a shared directory accessible to users and other apps and remains when the app is deleted.
So I suggest you to use getFilesDir() as the directory returned by it is hidden from users and is deleted when the app is deleted. And also implement a image compressing tool in your app which automatically compresses images when uploaded by the user and then save it to internal storage. This won't solve the problem completely but I guess it's a start.
Answer: Darshil's answer is correct. Using the recommended Storage Access Framework for your use case, you should use getFilesDir() which will return your app's internal storage, which is private to your app.

Where should I be storing these images?

problem is due to limit of resource which is out of our control. However if you really want to store all the images, you can take some approaches:
1. Online: Use some cloud servers for storing user data. This might cost you a lot.
2. Offline: Tell user that you have only the limited amount of storage and storing more images will require to delete some older ones.
3. Both: Store in device. When internal storage is running low, tell user to buy some type of premium subscription to store unlimited cloud photos.
Answer: With limited storage space appearing to be the root cause, I recommend intentionally using the UI to encourage users to use existing images over new images. (This can work in addition to the technical options provided in other answers)
For example, present the user with a list of available background images and a link to add a new image. In this case, using an existing image is 1 click with a preview, where getting a new image requires opening a new view and searching for the new image.

Using the UI in this way doesn't directly get you more space, but it can help you to more efficiently use the space you have by guiding users to use existing images. It also gives you a place to warn about (or limit) adding more images when there is not more space available.
*If you still must have more storage on the device outside of the app, you could try using the public space with encryption for "privacy".
Comment: While this seems to be an honest attempt at solving the problem presented, it does not directly answer the question


Fitting t distribtution to financial data

Question: The data I am using can be found here: http://uploadeasy.net/upload/nwv0.rar
The variable is called "alvsloss".
I want to fit a distribution to my financial data. First of all, I started with a normal distribution.
The pdf is given by:
$\frac{1}{\sqrt{2\pi\sigma^2}}\operatorname{exp}\left\{-\frac{\left(x-\mu\right)^2}{2\sigma^2}\right\}$
and the log likelihood, which is to be maximized is given by
$  \ln\mathcal{L}(\mu,\sigma^2) = \sum_{i=1}^n \ln f(x_i;\,\mu,\sigma^2) = -\frac{n}{2}\ln(2\pi) - \frac{n}{2}\ln\sigma^2 - \frac{1}{2\sigma^2}\sum_{i=1}^n (x_i-\mu)^2. $
which gives as a solution
$   \hat{\mu} = \overline{x} \equiv \frac{1}{n}\sum_{i=1}^n x_i, \qquad \hat{\sigma}^2 = \frac{1}{n} \sum_{i=1}^n (x_i - \overline{x})^2. $
I can calculate this in R via mean(alvsloss) and sd(alvsloss) and get the values
-0.0004270872 and 0.02304159 or I do
library(MASS)
fitdistr(alvsloss, "normal")

and get the solution
       mean             sd
  -0.0004270872    0.0230371244
 ( 0.0004535429) ( 0.0003207033)

Ok, I understand this. But now my problem arises, I tried this with the t distribution. The pdf is given by:
$\textstyle\frac{\Gamma \left(\frac{\nu+1}{2} \right)} {\sqrt{\nu\pi}\,\Gamma \left(\frac{\nu}{2} \right)} \left(1+\frac{x^2}{\nu} \right)^{-\frac{\nu+1}{2}}\!$
the log likelihood can be found here:
http://de.scribd.com/doc/52587265/80/STUDENT%E2%80%99S-t-DISTRIBUTION formula (17.8) (x is denoted by $\epsilon$. I am not understanding where the $h_i$ comes from in the formula, but this is not my main question.)
First question: Seems to me, that there is no closed form solution?
Second question:
I do fitdistr(alvsloss, "t")
and get the output
  m               s               df
  -0.0004919768    0.0130128873    2.6340459185
 ( 0.0003182568) ( 0.0003453702) ( 0.1620424078)
Es gab 28 Warnungen (Anzeige mit warnings())

I am not understanding, why I have three parameters now? The df is the $\nu$, but the others? What is R doing? Since there is no closed form solution, I guess it is using a iterated algorithm? Should I bother about the 28 warnings? They are:
Warnmeldungen:
1: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
2: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
3: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
4: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
5: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
6: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
7: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
8: In log(s) : NaNs wurden erzeugt
9: In log(s) : NaNs wurden erzeugt
10: In log(s) : NaNs wurden erzeugt
11: In log(s) : NaNs wurden erzeugt
12: In log(s) : NaNs wurden erzeugt
13: In log(s) : NaNs wurden erzeugt
14: In log(s) : NaNs wurden erzeugt
15: In log(s) : NaNs wurden erzeugt
16: In log(s) : NaNs wurden erzeugt
17: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
18: In log(s) : NaNs wurden erzeugt
19: In log(s) : NaNs wurden erzeugt
20: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
21: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
22: In dt((x - m)/s, df, log = TRUE) : NaNs wurden erzeugt
23: In log(s) : NaNs wurden erzeugt
24: In log(s) : NaNs wurden erzeugt
25: In log(s) : NaNs wurden erzeugt
26: In log(s) : NaNs wurden erzeugt
27: In log(s) : NaNs wurden erzeugt
28: In log(s) : NaNs wurden erzeugt

And how can I plot this distribution with the estimated parameters?
Comment: Your file is 20K. Why on earth would you save it as binary and compress it as rar when you could simply upload an ascii or CSV file and save people a lot of mucking about? In fact, the *best* thing to do would be to grab a small subset of your data with the same issues and put the data in your question, so your question doesn't break when that file becomes inaccessible.
Answer: The estimators correspond to $(\mu,\sigma,\nu)$, the parameters of a Student-$t$ distribution with location parameter $\mu\in{\mathbb R}$, scale parameter $\sigma>0$ and $\nu>0$ degrees of freedom. This density is simply given by
$$f(x;\mu,\sigma,\nu)=\dfrac{1}{\sigma}\dfrac{\Gamma \left(\frac{\nu+1}{2} \right)} {\sqrt{\nu\pi}\,\Gamma \left(\frac{\nu}{2} \right)} \left(1+\dfrac{(x-\mu)^2}{\nu \sigma^2} \right)^{-\frac{\nu+1}{2}}.$$
There seems to be no closed expression for these estimators.
The warnings in this case are harmless. You can check this by finding the MLE using the command optim as follows
# log-likelihood function
loglik <-function(par){
if(par[2]>0 & par[3]>0) return(-sum(log(dt((alvsloss-par[1])/par[2],df=par[3])/par[2])))
else return(Inf)
}

# optimisation step
optim(c(0,0.1,2.5),loglik)

The following code shows how to plot the fitted density together with a kernel density estimator.
# fitted density
param = optim(c(0,0.01,2.5),loglik)$par
fit.den <- function(x) dt((x-param[1])/param[2],df=param[3])/param[2]

curve(fit.den,-0.15,0.15)
points(density(alvsloss),type="l",col="red")

Comment: So many novelists, singers and artists have been visiting this site (with good answers!) of late. It's eerie!
Comment: @Carlos Gardel thanks for your answer, but I don't get the following: You are doing the log-likelihood maximization manually, but -sum(log(dt((alvsloss-par[1])/par[2],df=par[3])/par[2])) does not look like the log likelihood function of the t distribution?
Comment: or why do you use alvsloss-par[1])/par[2] and df=par[3])/par[2] ?
Comment: @StatTistician did you figure out why Carlos uses sum(log(dt((alvsloss-par[1])/par[2],df=par[3])/par[2])? I am trying to understand it as well as I have the same issue


JRadioButton: how to replace text by IconImage?

Question: I want to replace the text in my radio button list by an icon.
I've tried this:
rotateButton = new JRadioButton(rotateIcon.getImage());

But this replaces the radio button and text by the icon. I would like to keep the radio button and display the image.
What should I do?

What I'm currently getting is:

But I want it to end up with this:
Answer: Create a JRadioButton with no text and put a JLabel with the image next to it. You can also create a class to hide complexity.
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeListener;

public class RadioButtonWithImage extends JPanel {

private JRadioButton radio = new JRadioButton();
private JLabel image;

public RadioButtonWithImage(Icon icon) {
    image = new  JLabel(icon);
    add(radio);
    add(image);
}

public void addToButtonGroup(ButtonGroup group) {
    group.add(radio);
}

public void addActionListener(ActionListener listener) {
    radio.addActionListener(listener);
}

public void addChangeListener(ChangeListener listener) {
    radio.addChangeListener(listener);
}

public Icon getImage() {
    return image.getIcon();
}

public void setImage(Icon icon) {
    image.setIcon(icon);
}

} // end class RadioButtonWithImage

Answer: public JRadioButton(String text, Icon icon) and simple example here
Comment: Sorry but its not what I'm looking for, I want to keep the existing radio and replace the text by an image. as here is replaces the default radio button by the image and keeps the text
Comment: this answer is wrong, shouldn't be the accepted one. A correct answer is given by @andrew-thompson.
Comment: *"that for me harder as any of PL as I know.."*  Programming languages are a lot more *logical* than English.  ;)
Comment: @Eng. Fouad: I also see him in most of Swing questions seems like mKorble is a swing enthusiast.
Comment: @Eng.Fouad because there are lots of fun, kind people with peaceFullFaces, and learning English language :-) that for me harder as any of PL as I know,
Comment: @Jason Rogers I'm not see any problem in that, what's your issue
Comment: Since I'm not being clear with words I did a paint work of whats my problem and added it in the edit
Answer: I just reproduced your described behavior using this source:
import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.gravatar.com/avatar/" +
            "a1ab0af4997654345d7a949877f8037e?s=128");
        Image image = ImageIO.read(url);
        final ImageIcon imageIcon = new ImageIcon(image);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JRadioButton radioButton = new JRadioButton("A.T.", imageIcon);
                JOptionPane.showMessageDialog(null, radioButton);
            }
        });
    }
}

It seems like a bug to me, though I cannot recall seeing a radio with an icon.  How are they supposed to look?

Time to reach into my 'box of hacks'.
import javax.swing.*;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        String url = "http://www.gravatar.com/avatar/" +
            "a1ab0af4997654345d7a949877f8037e?s=128";
        final String html = "";
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JRadioButton radioButton = new JRadioButton(html);
                JOptionPane.showMessageDialog(null, radioButton);
            }
        });
    }
}

This technique will not work if:

The use-case requires other types of icons (pressed, roll-over, selected etc.)
The button is disabled (it will render incorrectly).
Comment: looks like as Java6 forgot for that, but I think that you are here one of Htlm Gurus ... +1
Comment: @mKorbel  Oh no!  I may seem an HTML guru to Java programmers, but to the web-designers, I'm a newbie.  But thanks for saying it.  ;)  Yours was a good answer by the way, +1.  I like answers that include links to the latest JDocs. :)
Comment: I'm really confused how quickly can coding todays newbie
Answer: There is no radio button constructor that allows an image as the content argument instead of a text. The only way to replace the text of a radio button by an image it is generate html and pass it as an argument to the default constructor.
import javax.swing.*;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        URL url = Windows_ChordsGenerator.class.getResource("/images/img1.png");

        final String html = "";

        JRadioButton radioButton = new JRadioButton(html);
       }
}

Comment: Great answer! This is more useful than @Andrew Thompson's answer because it uses an image actually bundled with the jar, and not an external resource.


How to convert array to object and group by index number / steps in js

Question: I have an array
[
a1,
b1,
c1,
d1,
a2,
b2,
c2,
d2
]

and I would like to convert it to:
{
    0: {
        name: a1,
        tel: b1,
        mail: c1,
        address: d1
        },
    1: {
        name: a2,
        tel: b2,
        mail: c2,
        address: d2
        }
}

Basically group them every 4 array steps. What is the best way to do this?
Appreciate any help. Thank you
Answer:

const d = ['a1','b1','c1','d1','a2','b2','c2','d2']

const f = ([name, tel, mail, address, ...rest]) =>
  [{name, tel, mail, address}, ...rest.length?f(rest):[]];

console.log(Object.fromEntries(f(d).map((e,i)=>[i,e])));

Or, without recursion:

const d = ['a1','b1','c1','d1','a2','b2','c2','d2']
const keys = ['name','tel','mail','address'];

console.log(d.reduce((a,c,i,r)=>(i%keys.length?0:a[i/keys.length|0]
  = Object.fromEntries(keys.map((k,j)=>[k,r[i+j]])),a),{}));
Comment: Your solution is more elegant and simple!
Answer: I provide a solution by iteration,however I think deserialization is more better choice,waiting for more elegant solution

let data = [
`a1`,
`b1`,
`c1`,
`d1`,
`a2`,
`b2`,
`c2`,
`d2`
]

let keys = ['name','tel','mail','address']
let result = {}
for(let i=0;i<data.length;i=i+keys.length){
  result[i/4] = {}
  for(key of keys){
    result[i/4][key] = data[i]
  }
}
console.log(result)
Comment: After converting my solution to output an object instead of an array, I think you win on simplicity :)


Undefined method 'id' error using collection_select with form_for in Ruby on Rails

Question: I'm having an issue with collection_select using form_for in rails. This issue is coming from a method that was working until I namespaced my routes to be only for admin. The admin routes are working as they should as far as I can tell, but it is the only connection I can find to why this method stopped working.
Error:
undefined method `id' for #<Array:0x00007faf084c7048>

Here is the trace if that would help:
actionview (18.104.22.168) lib/action_view/helpers/tags/base.rb:41:in `public_send'
actionview (184.108.40.206) lib/action_view/helpers/tags/base.rb:41:in `value'
actionview (18.104.22.168) lib/action_view/helpers/tags/collection_select.rb:18:in `block in render'
actionview (126.96.36.199) lib/action_view/helpers/tags/collection_select.rb:18:in `fetch'
actionview (22.214.171.124) lib/action_view/helpers/tags/collection_select.rb:18:in `render'
actionview (18.104.22.168) lib/action_view/helpers/form_options_helper.rb:203:in `collection_select'
actionview (126.96.36.199) lib/action_view/helpers/form_options_helper.rb:835:in `collection_select'
app/views/admin/users/_edit_my_profile.html.erb:28
actionview (188.8.131.52) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (188.8.131.52) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (184.108.40.206) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (184.108.40.206) lib/action_view/helpers/form_helper.rb:1004:in `fields_for'
actionview (184.108.40.206) lib/action_view/helpers/form_helper.rb:2195:in `fields_for'
app/views/admin/users/_edit_my_profile.html.erb:16
actionview (184.108.40.206) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (184.108.40.206) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (126.96.36.199) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (184.108.40.206) lib/action_view/helpers/form_helper.rb:450:in `form_for'
app/views/admin/users/_edit_my_profile.html.erb:3
actionview (188.8.131.52) lib/action_view/base.rb:247:in `public_send'
actionview (22.214.171.124) lib/action_view/base.rb:247:in `_run'
actionview (18.104.22.168) lib/action_view/template.rb:154:in `block in render'
activesupport (126.96.36.199) lib/active_support/notifications.rb:205:in `instrument'
actionview (22.214.171.124) lib/action_view/template.rb:345:in `instrument_render_template'
actionview (220.127.116.11) lib/action_view/template.rb:152:in `render'
actionview (188.8.131.52) lib/action_view/renderer/partial_renderer.rb:285:in `block in render_partial_template'
activesupport (126.96.36.199) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (18.104.22.168) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (220.127.116.11) lib/active_support/notifications.rb:203:in `instrument'
actionview (188.8.131.52) lib/action_view/renderer/partial_renderer.rb:280:in `render_partial_template'
actionview (220.127.116.11) lib/action_view/renderer/partial_renderer.rb:271:in `render'
actionview (18.104.22.168) lib/action_view/renderer/renderer.rb:81:in `render_partial_to_object'
actionview (188.8.131.52) lib/action_view/renderer/renderer.rb:27:in `render_to_object'
actionview (18.104.22.168) lib/action_view/renderer/renderer.rb:22:in `render'
actionview (126.96.36.199) lib/action_view/helpers/rendering_helper.rb:38:in `block in render'
actionview (220.127.116.11) lib/action_view/base.rb:273:in `in_rendering_context'
actionview (22.214.171.124) lib/action_view/helpers/rendering_helper.rb:34:in `render'
app/views/admin/users/profile.html.erb:14
actionview (22.214.171.124) lib/action_view/base.rb:247:in `public_send'
actionview (220.127.116.11) lib/action_view/base.rb:247:in `_run'
actionview (184.108.40.206) lib/action_view/template.rb:154:in `block in render'
activesupport (184.108.40.206) lib/active_support/notifications.rb:205:in `instrument'
actionview (22.214.171.124) lib/action_view/template.rb:345:in `instrument_render_template'
actionview (220.127.116.11) lib/action_view/template.rb:152:in `render'
actionview (126.96.36.199) lib/action_view/renderer/template_renderer.rb:61:in `block (2 levels) in render_template'
activesupport (184.108.40.206) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (184.108.40.206) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (220.127.116.11) lib/active_support/notifications.rb:203:in `instrument'
actionview (18.104.22.168) lib/action_view/renderer/template_renderer.rb:56:in `block in render_template'
actionview (22.214.171.124) lib/action_view/renderer/template_renderer.rb:71:in `block in render_with_layout'
activesupport (188.8.131.52) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (126.96.36.199) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (18.104.22.168) lib/active_support/notifications.rb:203:in `instrument'
actionview (126.96.36.199) lib/action_view/renderer/template_renderer.rb:70:in `render_with_layout'
actionview (22.214.171.124) lib/action_view/renderer/template_renderer.rb:55:in `render_template'
actionview (22.214.171.124) lib/action_view/renderer/template_renderer.rb:11:in `render'
actionview (22.214.171.124) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (220.127.116.11) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (184.108.40.206) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (188.8.131.52) lib/action_view/base.rb:273:in `in_rendering_context'
actionview (18.104.22.168) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (126.96.36.199) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (220.127.116.11) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (220.127.116.11) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (126.96.36.199) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (188.8.131.52) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (184.108.40.206) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (126.96.36.199) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
/home/taylor/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (18.104.22.168) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (188.8.131.52) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (220.127.116.11) lib/action_controller/metal/instrumentation.rb:86:in `cleanup_view_runtime'
activerecord (220.127.116.11) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (126.96.36.199) lib/action_controller/metal/instrumentation.rb:45:in `render'
actionpack (126.96.36.199) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (184.108.40.206) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (184.108.40.206) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (220.127.116.11) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (18.104.22.168) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (220.127.116.11) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (18.104.22.168) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (220.127.116.11) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
actiontext (184.108.40.206) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (220.127.116.11) lib/action_text/engine.rb:55:in `block (4 levels) in <class:Engine>'
activesupport (22.214.171.124) lib/active_support/callbacks.rb:126:in `instance_exec'
activesupport (18.104.22.168) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
activesupport (220.127.116.11) lib/active_support/callbacks.rb:137:in `run_callbacks'
actionpack (220.127.116.11) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (22.214.171.124) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (126.96.36.199) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (22.214.171.124) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (22.214.171.124) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (18.104.22.168) lib/active_support/notifications.rb:203:in `instrument'
actionpack (126.96.36.199) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (188.8.131.52) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
activerecord (22.214.171.124) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (188.8.131.52) lib/abstract_controller/base.rb:165:in `process'
actionview (22.214.171.124) lib/action_view/rendering.rb:39:in `process'
actionpack (188.8.131.52) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (188.8.131.52) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (184.108.40.206) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (126.96.36.199) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (22.214.171.124) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (18.104.22.168) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (184.108.40.206) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (220.127.116.11) lib/action_dispatch/routing/route_set.rb:842:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (22.214.171.124) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (126.96.36.199) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (22.214.171.124) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (188.8.131.52) lib/active_record/migration.rb:601:in `call'
actionpack (188.8.131.52) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (184.108.40.206) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (188.8.131.52) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (18.104.22.168) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (22.214.171.124) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (22.214.171.124) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (220.127.116.11) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (188.8.131.52) lib/rails/rack/logger.rb:37:in `call_app'
railties (126.96.36.199) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (22.214.171.124) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (18.104.22.168) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (188.8.131.52) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (184.108.40.206) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (184.108.40.206) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (184.108.40.206) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (22.214.171.124) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (220.127.116.11) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (184.108.40.206) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (22.214.171.124) lib/action_dispatch/middleware/host_authorization.rb:98:in `call'
webpacker (5.4.0) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (188.8.131.52) lib/rails/engine.rb:539:in `call'
puma (5.3.2) lib/puma/configuration.rb:249:in `call'
puma (5.3.2) lib/puma/request.rb:77:in `block in handle_request'
puma (5.3.2) lib/puma/thread_pool.rb:338:in `with_force_shutdown'
puma (5.3.2) lib/puma/request.rb:76:in `handle_request'
puma (5.3.2) lib/puma/server.rb:438:in `process_client'
puma (5.3.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'

Here is the full method:
<%= user_builder.fields_for :team_attributes, [:admin, @team] do |team_builder| %>
            <%= team_builder.hidden_field :profile, :value => "profile" %>
        <% if @company.teams.all == [] %>

Create a New Team:

        Team Name: <%= team_builder.text_field :name %>
        Team Description: <%= team_builder.text_area :description %>

        <% else %>
            <% if @show_form == "new-position" %>
               Change Team: <%= team_builder.collection_select :id, @company.teams, :id, :name, {:include_blank => true} %>
               Or <%= link_to "Create a new Team", "/email@example.com}/edit/new-team-and-new-position" %>

            <% elsif @show_form == "new-team" %>
            <% else %>
               Change Team: <%= team_builder.collection_select :id, @company.teams, :id, :name, {:include_blank => true} %>
               Or <%= link_to "Create a new Team", "/email@example.com}/edit/new-team" %>

            <% end %>
        <% end %>

The error is being throw here:
Change Team: <%= team_builder.collection_select :id, @company.teams, :id, :name, {:include_blank => true} %>

I threw in a pry and returned the value of @company and the value of @company.teams:
#<Company:0x00007faf085aa668
 id: 1,
 name: "My Company",
 industry: "Retail",
 address: "1546 Jefferson Dr.",
 city: "Townesville",
 state: "AA",
 phone_number: "1232345678",
 email: "firstname.lastname@example.com",
 user_id: 1,
 created_at: Mon, 07 Jun 2021 22:31:22.169619000 UTC +00:00,
 updated_at: Mon, 07 Jun 2021 22:31:22.169619000 UTC +00:00>

[#<Team:0x00007faf00375170
  id: 2,
  name: "Marketing",
  description:
   "Develops and implements marketing campaigns from previously developed strategies.",
  company_id: 1,
  user_id: 1,
  created_at: Mon, 07 Jun 2021 22:52:15.767441000 UTC +00:00,
  updated_at: Mon, 07 Jun 2021 22:52:15.767441000 UTC +00:00>]

This is attempting to create a drop down of a company's team dependent on if the company has teams and depending on what links the user has clicked to reveal parts of the full form. Basically this error is preventing some substantial functionality and progress towards completing this application. If anyone needs more information to help please let me know. Thanks for the help.
Comment: Shouldn't the collection be the 3rd argument and not 2nd as you are passing? (teams) https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Comment: If you aren't using form_for, yes. If you're using form_for the first argument is coming team_attributes https://apidock.com/rails/ActionView/Helpers/FormBuilder/collection_select. Please correct me if I'm wrong
Comment: sorry, you're right.
Comment: I just added some information that might be relevant. It's about namespacing. Joel Blum, would mind taking another look?
Answer: Solved it :/ there was no need for me to namespace @teams in fields_for when the original form_for was already namespaced. Don't go to fast people lol


k-means for set of integers

Question: I'm not really sure where to begin with this but for a simplified example I have a set of integers:
{11, 22, 3, 12, 1, 23, 21, 13, 2}
I need to partition these into K=3 clusters with initial means of the clusters of 1,2,3 using Euclidean distances.
I'm assuming I need to find the distance of each value and compute new means but no idea how to go about it and everything I've read online doesn't seem to help
If someone can give me the basic ideas on how to tackle this much appreciated.
Comment: See also: [How can I group numerical data into naturally forming "brackets"?](http://stats.stackexchange.com/q/67571/7290)
Answer: In R, you could do something like this:
> kmeans(c(11, 22, 3, 12, 1, 23, 21, 13, 2), c(1, 2, 3))
K-means clustering with 3 clusters of sizes 1, 2, 6

Cluster means:
  [,1]
1  1.0
2  2.5
3 17.0

Clustering vector:
[1] 3 3 2 3 1 3 3 3 2

Looking at ?kmeans the centers argument can be:

either the number of clusters, say k, or a set of initial (distinct) cluster centres

To get a vector that shows which cluster each element belongs to, use $cl, e.g.
> kmeans(c(11, 22, 3, 12, 1, 23, 21, 13, 2), c(1, 2, 3))$cl
[1] 3 3 2 3 1 3 3 3 2

k-means for set of integers


Images break with wrong href; clearing caches fixes!

Question: Occasionally we will have graphics in various places on the site that are missing because of incorrect hrefs with unknown subdomains and are fixed when the caches are cleared. So an incorrect link
https://www.xfdg.oursite.com/sites/image.jpg
is only fixed after cache clear
(correct https://www.oursite.com/sites/image.jpg)
We are also seeing results in Google that have weird unknown subdomain URLs, in the same manner as the image URLs.
I had read in https://www.drupal.org/node/1992030 "Protecting against HTTP HOST Header attacks (prevent your site from thinking it is someone else)" that it could be spam or errors with Drupal's cron could be responsible.
Any ideas or directions?
Answer: More than likely, your server is responding to multiple domain names, either by being the default or matching/wildcards for ServerName and ServerAlias (using Apache terminology here; nginx and IIS have similar concepts).
So, the problem is that you get a request to www.example.com, and Drupal serves that page up and uses that URL as the base.  It will also respond to www.subdomain.example.com and use that as the URL base.  The problem you see is that the subdomain request is the one being cached, and the hostname isn't part of the cache ID.
The general solution to this is to set up a canonical redirect in either the Apache config or the .htaccess file for your site (I prefer in server config).  The rule would look something like this
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>

When you do that, Apache will redirect everything to https://www.example.com.  Over time, Google will fix its indexes because of the 301 redirect, or you can try to submit manual delists.


Default datapath of tessdata folder

Question: I found a strange issue like even If I explicitly mention the datapath to be /data/local/tmp/tesseract/, the TessBaseAPI uses the tessdata inside /data/data/tesseract/ (Only if it exists). If tesseract directory does not exist inside /data/data folder then the given path is taken.
I almost searched the entire TessBaseAPI.java file, but I couldn't find the default path.
Following are the code:
<code>String TESSBASE_PATH = "/data/local/tmp/tesseract/";
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init(TESSBASE_PATH, "eng");
</code>
Can you please let me know from where the default datapath is taken?
Answer: There's no "default" data path. Only the path supplied to the init() method is used. Check out the code for the init() method--you'll see where the API throws an IllegalArgumentException if the data file is not in the specified location.

Default datapath of tessdata folder


How to improve standard visualizations of a univariate distribution?

Question: I have a data set with about 15,000 labeled observations of a single continuous value.
What is the best way to plot this type of data? I'm playing around with various histograms and density plots, but I can't seem to figure out the best way to plot this data set. Any suggestions?
My data looks like this:
<code> label     value
-------   -------
  foo       1.2
  bar       6.2
  baz       0.2
  qux       4.7
  ...       ...
</code>
This data set contains 15,000 values, each with a unique label.
I am looking as to how best to create a visualization of the distribution of the data and see outliers.  Here are two candidate plots I've generated. Both simplified the data more than I would like. Are there any additional ways I could plot the data and somehow integrate the labels into this plot?
Comment: The edit makes the question much clearer, thanks.  Now that the format and general nature of your data are revealed, could you tell us *what* you would like the visualization to show?  (Obviously, unless there is some structure to the labels, no visualization will be capable of displaying 15000 distinct labels in any clear or meaningful way.)  It would help to say something about what the values mean.  Are there natural limits to their ranges?  What is their actual range?  Are they perhaps proportions, or are they direct measurements of something?
Comment: *Now* we have a clear, answerable question!  (+1).  I look forward to reading the solutions people have to offer.  (Automatic labeling of plots is notoriously tricky and manual labeling is a pain, so there is a real problem to be addressed here.)
Comment: Could you clarify what you mean by a "discrete" variable with a single "continuous" value?  This seems self-contradictory.
Comment: I have variables such as x, y, z, etc. Each variable has a continuous values such as x=0.2, y=1.2, z=2.9.
Comment: Normally we think of such data as arrayed in a table in which variables (or "attributes" or "fields") are given by columns and cases (or "observations") are given by rows.  You're saying you have a table with 15,000 columns.  How many rows are in it?
Comment: I have a table that contains two columns. Column 1 is a unique field and Column 2 is a value. There are 15,000 unique fields.
Comment: Are you saying you have *1* *observation*?  That is, a 15,000-tuple (like 1 person, and a measurement for their (1) height, (2) weight, (3) age, (4) sex, (5) education level, (6) ... etc).
Comment: Precisely. I have 15,000 tuples. For example, `(Mike, 6'1"),(Bob, 5'2"),(Tim, 5'9"), etc.` I've tried to example my data set more clearly above.
Comment: @whuber I've tried to address your additional questions in my most recent edits. Thanks.
Comment: OK, let's grant that these visualizations oversimplify the data (but they are a good start!).  What precisely would you like to see in a better visualization?  What does it need to show that your examples do not show?  E.g., do you want to label apparent outliers in the boxplot?  Do you want to see more detail in the shape of the distribution in the density plot?  Something else?
Comment: I would like to somehow integrate the labels into this plot.
Answer: A simple idea that satisfies both seeing the entire distribution and one that potentially allows space to place the labels is to use a jittered, one-dimensional dot plot.

In this example I have just arbitrarily chosen points with a value over 25 to label, as well as made the points semi-transparent (even with jittering there is still a substantial amount of over-plotting).
This isn't perfect (I had to arbitrarily choose what points where deemed to be "outliers" and hence which ones would get the label). Also in the software I produced this in (SPSS) I can't restrict the jittering only to the Y dimension of the graph, so many points in this instance fall outside the logical ranges (this is random data generated using a log-normal distribution, so all values are positive).
Although I hope this is a useful demonstration for plotting, given the look of your examples you would surely want to consider transforming the data. I'm sure other members of the site can produce some examples in ggplot2 for demonstration if you need help producing a similar graph in R as well (I look forward to other suggestions as well!)
Comment: +1 Nice idea for using the second dimension to create space for the labels.
Answer: Expanding on the idea from @Andy, I would suggest this alternative which incorporates the two original plots included in the questions:
I created this plot in R with the following code:
<code>data <- data.frame( Id = paste('case',1:5000), x = exp(rnorm(5000)))

outlier.cut = 10

outliers <- data[ data$x > outlier.cut, ]
outliers <- outliers[ order(outliers$x), ]

png(filename = "labeled_outliers.png", width = 600, height = 600)

plot(density(data$x), main = 'Labeled Outliers')
rug( data$x, side = 1)
rug( data$x, side = 3)

unit <- (par('usr')[4]-par('usr')[3]) / ( dim(outliers)[1] + 5 )
outliers$y <- ( c(1:dim(outliers)[1]) * unit)

text(outliers$x, y = outliers$y, label = outliers$Id, cex = .65, srt = 0)

dev.off()
</code>


Interpreting contour plots for two variables

Question: This contour plot shows the relationship between the finished square feet of a home and the tax-assessed value for homes in a County.

Things I couldn't understand:

what does it mean when lots of bands are close to one another? And when they are very far to one another?
what does the weird(or asymmetric) shape of a contour band represent?
Comment: Could you explain what you mean by "grouped"?  I don't see any apparent examples of combining or grouping contour levels in your example.
Comment: Lots of bands close to each other, as can be seen from 1000 to 2000 of x-axis.
Answer: tldr: Denser implies steeper gradient.
On a contour plot, the value on the continuous plot is the same. What might be helpful in your plot is to give numbers to the contour lines.
Imagine a Z-axis, rising out of the screen. If we plot,
$z^2 = x^2 + y^2$,
For a circle with radius 1, the circle will be created a unit distance out of the screen. For radius, 2 it will be 2 unit distance out of the scree and so on. If we project these circles in the air on to the X-Y plane and give them number we would get, concentric contours.
It would be safe to assume, that the incremental contours as we head out keep increasing (they could be decreasing but just to explain assume they increase). Eg. Inner-most is 1, then the next one 2 and so on.
The densely crowded regions indicate, that the gradient is steeper in that region. The gradient is usually referred to in the direction perpendicular to the contours. (In the circle case it will be the the radial direction).
For eg. For Finished Square feet = 1000, if tax assessed goes from 150000 to 170000 (roughly), we change around 4-5 contour lines. i.e.
$abs(\frac{\delta{z}}{\delta{y}}_{x=1000, y= approx 160k} )= \frac{5}{20k} $ (5 is because roughly 5 contours crossed in 20k change).
z is contours, y is tax-assessed value and x is Finished square feet.
If you perform similar calculation for x=1000 and y between, 300k to 320k,
$abs(\frac{\delta{z}}{\delta{y}}_{x=1000, y= approx 310k} )= \frac{1}{20k} $ (1 is because roughly only 1 contour crossed in 20k change).
Note: Values are rough eyeballing and just to give sense of magnitude.
So denser is steeper gradient.
Notice, I have used, absolutes. This is because, going towards the central portion is opposite in direction to going out and we just wanted to demonstrate magnitude of gradient. Based on value of the contours increasing or decreasing the sign can be determined. Hope this helps.
Further references:

Khan academy Link

Gradient Video


How can I evaluate the performance of a program to calculate the exact Kolmogorov-Smirnof PDF

Question: For reasons abstruse I wish to create a program to calculate the exact density function for the $D$ statistic from Kolmogorov-Smirnov's two-sample test. Here is the code I have so far.
dDx <- function(d, nx, ny) {

  # KS.D: Takes an ordering vector which represents the sorting of two samples
  # as 0s and 1s, and returns the Kolmogorov-Smirnov D statistic.
  KS.D <- function(sample) {
    nx <- length(sample[sample==1])
    ny <- length(sample[sample==0])
    D <- 0
    for (i in 1:(nx + ny)) {
      D <- max(D, abs( length(sample[1:i][sample[1:i] == 1])/nx - length(sample[1:i][sample[1:i] == 0])/ny) )
      }
    return(D)
    }

  # n2Ck: takes two scalars n >= k, and returns an n choose k by n matrix,
  #       where each row identifies a unique size k combination of n
  #       variables. The resulting matrix includes all possible combinations.
  n2Ck <- function(n,k) {
    m <- choose(n,k)             # number of n2Ck(n,k) combinations
    out <- matrix(0,m,n)
    ones <- combn(n,k)
    for (i in 1:m) {
      out[i,][ones[,i]] <-1
      }
    return(out)
    }
  precision <- 12
  out <- d
  m <- choose(nx+ny,nx)             # number of n2Ck(nx+ny,nx) combinations
  combinations <- n2Ck(nx+ny,nx)  # possible combinations of (routes through) nx+ny samples
  index <- 0
  for (val in d) {          # d loop is for each supplied d value
   index <- index + 1
   count <- 0
   for (i in 1:m) {         # i loop counts the number of times d occurs out of all possible combinations
     D <- KS.D(combinations[i,])
     # The below conditional is sensitive to precision!
     if (round(D,digits=precision) == round(val,digits=precision)) {
       count <- count + 1
       }
     }
   out[index] <- count/m
   }
 return(out)
 }

How can I evaluate how accurate my code is? (I.e. that it produces numerically correct results?)
I can certainly feed it the sample space of $D$ for given parameters nx and ny (thanks to whuber).
But how can I evaluate the accuracy of the program's output? Are there tables of published value I can compare against?
PS: It's 100% true that I am not a CS type, and there probably a zillion ways to improve my code aside from the accuracy question: such feedback most welcome as comments.
Comment: @ close votes: The question is not about coding, it asks how to evaluate the performance of *a* program that is supposed to calculate this density.
Comment: It's probably worth explicitly mentioning (either in the title, the opening paragraph, or both) that you're dealing with the two sample test; typically if not explicitly qualified people will tend to assume you mean the one-sample test. There are exact small sample tables in a few places; indeed these can be generated fairly directly.
Comment: Here's code to check the pmf  for any specific instance:  `f=function(x,m,n) ks.test(head(x,m),tail(x,n))$statistic; sdf=function(y,x) setdiff(x,y); m=3;n=4;x=combn(m+n,m);xy=rbind(x,apply(x,2,sdf,x=1:(m+n)));table(apply(xy,2,f,m=m,n=n))`
Comment: You'll probably want to put that on separate lines, in which case replace the `;` with returns (i.e. the "Enter" key)
Comment: The output table consists of a set of labels -- the sample space -- and a set of counts (unnormalized pmf, sorry to be unclear). Divide those counts by their sum to get the normalized pmf. If you assign the last line to `a`, then `a/sum(a)` generates the normalized probabilities, the first of which is `0.02857143`, the second is `0.08571429` and so on
Comment: @Glen_b-ReinstateMonica Thank you! I see that your code produces elements of the sample space for this test, but I am struggling to find the probability density output? For example, my `dDx(.25,3,4)` gives `0.02857143`, and `dDx` sums to 1 across the whole sample space... is that accurate?
Answer: There are tables in Conover's Practical Nonparametric Statistics, 3E, tables A19 and A20.
You can also calculate values as follows:
f=function(x,m,n) ks.test(head(x,m),tail(x,n))$statistic
sdf=function(y,x) setdiff(x,y)
m=3
n=4
x=combn(m+n,m)
xy=rbind(x,apply(x,2,sdf,x=1:(m+n)))
a=table(apply(xy,2,f,m=m,n=n))
a/sum(a)

The output for this case is:
         0.25 0.333333333333333 0.416666666666667               0.5 0.666666666666667              0.75                 1
   0.02857143        0.08571429        0.22857143        0.25714286        0.17142857        0.17142857        0.05714286

Just replace $m$ and $n$ as needed.
If $m$ and $n$ are not small this will be slower than it might be. It is possible to speed it up (e.g. the function f does unnecessary calculation that could be avoided by making the relevant call to get the statistic that can be found in the code for ks.test). Indeed there's a bunch of ways to make it faster, but it might still be useful enough to check cases.


jQuery UI in Backbone View adds elements, but doesn't respond to events

Question: I'm building an app in which I'm using Django on the backend and jQuery UI/Backbone to build the front. I'm pulling a Django-generated form into a page with jQuery.get() inside of a Backbone View. That part works fine, but now I want to add some jQuery UI stuff to the form (e.g. a datepicker, some buttons that open dialogs, etc). So, here's the relevant code:
var InstructionForm = Backbone.View.extend({
  render: function() {
    var that = this;
    $.get(
      '/tlstats/instruction/new/',
      function(data) {
        var elements = $(data);
        $('#id_date', elements).datepicker();
        that.$el.html(elements.html());
      }
    };
    return this;
  }
});

The path /tlstats/instruction/new/ returns an HTML fragment with the form Django has generated. What's happening is that input#id_date is getting the hasDatePicker class added and the datepicker div is appended to my  element (both as expected), but when I click on input#id_date, nothing happens. No datepicker widget appears, no errors in the console. Why might this be happening?
Also, somewhat off-topic, but in trying to figure this problem out on my own, I've come across several code examples where people are doing stuff like:
$(function() {
  $('#dialog').dialog(...);
  ...
});

Then later:
var MyView = Backbone.View.extend({
  initialize(): function() {
    this.el = $('#dialog');
  }
});

Isn't this defeating the purpose of Backbone, having all that jQuery UI code completely outside any Backbone structure? Or do I misunderstand the role of Backbone?
Thanks.
Answer: I think your problem is right here:
$('#id_date', elements).datepicker();
that.$el.html(elements.html());

First you bind the datepicker with .datepicker() and then you throw it all away by converting your elements to an HTML string:
that.$el.html(elements.html());

and you put that string into $el. When you say e.html(), you're taking a wrapped DOM object with event bindings and everything else and turning into a simple piece of HTML in a string, that process throws away everything (such as event bindings) that isn't simple HTML.
Either give .html() the jQuery object itself:
$('#id_date', elements).datepicker();
that.$el.html(elements);

or bind the datepicker after adding the HTML:
that.$el.html(elements);
that.$('#id_date').datepicker();

Comment: Does anyone have an idea how to rerender the view, which already had bindings? Rerender is important when the model state changes.
Comment: @mojovski: Maybe you should ask a new question, that would let you provide more context than a comment on something a year old.


What Bayesian prior for normally distributed data with knowledge observations are too high

Question: I'm just learning Bayesian statistics with stan, so bear with me.
Let's say we have one observed data points:
tweets_dat <- list(J = 1,
                    y = 2500,
                    sigma = ?)

I've left the error term incomplete as I don't yet know exactly what should go there.
I know that the mean from my observations should have mean of 1100. In other words, if the 2500 is quite high. I estimate it is two standard deviations away from the mean.
How can I make my model reflect this prior knowledge?
I'm using a simple Bayesian model, but it's not clear to me how to put that two standard deviations information into the model or into the standard error in such a way that I arrive at the right parameters in the end.
data {
  int<lower=0> J;
  real y[J];
  real<lower=0> sigma[J];
}

parameters {
  real theta[J];
}

model {
  y ~ normal(theta, sigma);
}

Answer: if you feel that the mean of your observations should be around 1100 and that 2500 is about two standard deviations away from the mean, then I think this quite accurately reflects your prior knowledge (i.e. mu should be centered around 1100 and sigma should have expected value close to (2500-1100)/2 = 700).
Looking at your Stan code, it seems like you consider sigma a part of your data, not of your parameters. That is appropriate if you know exactly what your standard deviation is going to be, but doesn't sound like your case. It seems more appropriate to put sigma in the parameters block. Second, it seems like both sigma and theta are now real arrays of length J. This means that every observation has its own mean and sigma, which is probably not what you want to accomplish.
The simplest choice of prior is completely uninformative, but this is rarely a practical choice of prior. A step up from there would be to choose a conjugate prior (see: https://en.wikipedia.org/wiki/Conjugate_prior - this page includes quite a few examples of priors for the Normal distribution). In this case, if you're using Stan, then you can pick almost any prior that reasonably reflects your assumptions. For instance, you could use a Normal prior for the mean and a Exponential prior for the standard deviation, each chosen such that they reflect your prior knowledge. For example, you could choose the following model to start, and then adjust as you see fit after checking its inferences.
data{
  int<lower=0> J;
  real y[J];
}

parameters{
  real mu;
  real<lower=0> sigma;
}

model{
  y ~ normal(mu, sigma);
  mu ~ normal(1100, 100);
  sigma ~ exponential(1/700);
}

As you see, the prior for mu was chosen quite vaguely, with a wide range of plausible values (high standard deviation). One possible way to improve on this is if you have more specific knowledge that you can use. Or, you can set this as another parameter to be estimated from the data (essentially creating a hierarchical model).
Another way to potentially improve on this model is to take a half-Cauchy prior for sigma (see Stan manual 2.15.0 page 124-128 for more on the choice of priors, or look here: https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations). These tend do do quite well in practice when you don't have much prior information. The problem in this case is that the Cauchy distribution doesn't have a defined expected value, so you can't use it to reflect your knowledge that sigma is 'about 700'.
I hope this helps. Just remember, regardless of the model you're trying to set up, if you have a dataset with only a single observation, it will always be difficult to learn a lot from the data.


How does Chisq.test() calculate degrees of freedom from two vectors as opposed to a matrix?

Question: I'm confused about the way chisq.test() is handling a very simple table from an exercise where I asked students to "sample" a bag of M&Ms and compare their scope to the pooled samples from their lab mates, to wit:
> mm1<-data.frame(
+     color = c("red", "orange", "yellow", "green", "blue", "brown"),
+     mySample = c(9,10,9,15,13,7),
+     myTable = c(26,28,34,44,44,18))
> chisq.test(mm1$mySample,mm1$myTable)

    Pearson's Chi-squared test

data:  mm1$mySample and mm1$myTable
X-squared = 18, df = 16, p-value = 0.3239

Warning message:
In chisq.test(mm1$mySample, mm1$myTable) :
  Chi-squared approximation may be incorrect
> chisq.test(mm1[,2:3])

    Pearson's Chi-squared test

data:  mm1[, 2:3]
X-squared = 0.67269, df = 5, p-value = 0.9844

The second version is what I'd expect (5 d.f.), but why is the first one failing and where is it coming up with 16 d.f.? I don't think any of my expected values should be less then 5 for this set, so I don't think that error reflects the small-cell issue that's discussed here. Is.atomic() and is.numeric() on mm1\$mySample and mm1\$myTable all evaluate to TRUE, so what's the difference between passing them as an X and Y and passing them as a matrix?
Comment: The second thing one does in `R`, after trying to read the help page (which often is terse and opaque), is to examine the code.  The key line is `x <- table(x, y)` (line 25 of the code).  Take a look at what that gives when `x` is `mySample` and `y` is `myTable`.
Comment: Link following @whuber's comment for various ways to check source code in R: (https://stackoverflow.com/q/19226816/1834244) (several different options offered over the different answers)
Comment: This is flagged as off-topic. But this is not really a Q about how to use R (which *is* off-topic), but about understanding what it does, which is on-topic
Comment: "to the pooled samples from their lab makes" (presumably you mean *lab mates* there) -- I hope those pooled samples exclude their own data.
Answer: Short answer: the second version works as it treats the input data as though they were a contingency table.
From help(chisq.test)

If x is a matrix with at least two rows and columns, it is taken as a two-dimensional contingency table: the entries of x must be non-negative integers.

The "two vector" version (your first bit of code) is treating the input in a different fashion from what you expect. This treats each row like a single observation (e.g. there are five observations with the observed values 7, 9, 10, 13, and 15) and then comparing those responses with the second vector.
This is a little bit more opaque in the help file: see added emphasis below.

Otherwise, x and y must be vectors or factors of the same length; cases with missing values are removed, the objects are coerced to factors, and the contingency table is computed from these

You can see how these data are being treated in the output below:
Code example of what two vector analysis is doing
mm1 <- data.frame(
         color = c("red", "orange", "yellow", "green", "blue", "brown"),
         mySample = c(9,10,9,15,13,7),
         myTable = c(26,28,34,44,44,18))

check_chisq <- chisq.test(mm1$mySample, mm1$myTable)

Have a look at this stored object and you will see the "observed" data being interrogated:
check_chisq$observed

            mm1$myTable
mm1$mySample 18 26 28 34 44
          7   1  0  0  0  0
          9   0  1  0  1  0
         10   0  0  1  0  0
         13   0  0  0  0  1
         15   0  0  0  0  1

Hence this is being treated as a 5x5 table: which then gives 16 d.f. but as you can see is not what you want in this example.
Comment: Thanks, @james, this is exactly the help I needed. I'm going to use this exchange as an example for the class!


Exact sequence of sheaves and associated sequence of graded modules

Question: Let $(X,\mathcal{O}_X)$ with $X=\mathbb{P}^n$ and consider a exact sequence of sheaves of $\mathcal{O}_X$-modules
$$0 \to \mathcal{F} \to \mathcal{H} \to \mathcal{G} \to 0 $$
Suppose that we apply the functor $\Gamma^*(\mathcal{F})=\bigoplus_{m \in \mathbb{Z}}\Gamma(X,\mathcal{F}(n))$ to the sequence (the functor in general is only left exact) and it remains exact and splits.
What can we say about the original sequence?  Does the sequence split?
Comment: This follows from results in EGA II 2.6 and 2.7 for quasi-coherent sheaves. Do you want general $\mathcal{O}_X$ modules or quasi-coherent sheaves?
Comment: briefly put: $()^{\sim}$ is an exact functor and left inverse to $\Gamma^*$.
Answer: I will give an outline of the proof of this fact if all the sheaves involved are quasi-coherent. I claim that the original exact sequence does split.
First recall the Proj construction. Let $A=\oplus_{n\geq 0}A_n$ be a graded commutative ring, then $\operatorname{Proj} A$ is as a set the set of homogeneous prime ideas of $A$ not containing the trivial ideal $A_+=\oplus_{n>0}A_n$. For any homogeneous $f\in A$, $\operatorname{Proj}A$ has distinguished affine open $\operatorname{Spec}(A_f)_0$ (where $A_f$ is the localization by $f$ and $(A_f)_0$ means take the degree zero part of that). For a graded $A$ module $M$, we associate a quasicoherent sheaf $\tilde{M}$ by having its value on $(A_f)_0$ be $(M_f)_0$.
First I claim for for any quasi-coherent sheaf $\mathscr{M}$ on $\mathbb{P}^n$, $\widetilde{\Gamma^*(\mathscr{M})}$ is canonically isomorphic to $\mathscr{M}$. To show this you must use that the graded ring defining $\mathbb{P}^n$ is finitely generated and generated in degree $1$.
Let $A$ still be a graded ring as above. Then if $N$ and $M$ are graded $A$ modules, and $N\to M$ a morphism, it induces a map $\tilde{N}\to \tilde{M}$. One shows that if $N\to M$ is injective (resp surjective, bijective) in degrees $n\gg 0$, then $\tilde{N}\to \tilde{M}$ is an injection (resp surjection, isomorphism).
Together the last two paragraphs are enough to imply your desired statements, as the isomorphisms $\widetilde{\Gamma^*(\mathscr{M})}\cong \mathscr{M}$ get you a splitting map $\mathcal{G}\to \mathcal{H}$ from that on the level of $\Gamma^*$. Then by the last paragraph, this identifies $\mathcal{G}$ with $\mathcal{H}\oplus \mathcal{F}$ as it does on the level of $\Gamma^*$.


Export Kendo Grid to Excel in MVC 5

Question: I need to export webgrid data into excel, I am using mvc 5 application.click the button, webgrid data into export into excel file. webgrid generated dynamic data,
here is my view code.

    @(Html.Kendo().Grid(Model)
        .Name("grid")
        .Scrollable()
        .Columns(columns =>
        {
            columns.AutoGenerate(column =>
            {
                column.Width = "100px";
            });

         })
         .HtmlAttributes(new { style = "width: 900px;" })
         .Groupable()
         .Filterable()
         .Sortable(sortable => sortable
             .AllowUnsort(true)
             .SortMode(GridSortMode.MultipleColumn)
          )
         .Pageable(pageable => pageable
             .PageSizes(true)
          )
         .Resizable(resize => resize
             .Columns(true)
          )
         .DataSource(dataSource => dataSource
             .Ajax()
             .PageSize(20)
             .ServerOperation(false)
          )
      )

Comment: What is your question ?  Are you wanting someone to write you the code to export a kendo grid to excel for you ?
Comment: I added my view code.
Answer: The easier way to go about it, would be to use the KendoUI Grid's built-in ability to export to Excel.
Just add the excel button to the toolbar with a
.ToolBar(tools => tools.Excel())

and then add the Excel configuration
.Excel(excel => excel
    .FilterName("My Kendo Grid Export.xlsx")
    .Filterable(true)
)

See official telerik sample at KendoUI Demos Site
If you need to support older than IE10 or Safari, you will need to make use of the ProxyURL configuration.  See documentation at KendoUI Documentation and API Reference
Your other options is creating the spreadsheet using server side controls, ala GemBox (Gembox Excel File Creation Sample) or the Microsoft Office Excel Object Library (MS Office Object Library Sample) or any number of other alternatives which have a litany of Stack Overflow Q/A already (Stackoverflow Search for '.Net create Excel file').
Comment: Thanks for your reply. I tried this. I got an error: does not contain definition for excel in kendo and no extension method
Comment: Which version of KendoUI are you using ? I think the native export to excel was added in the first 2015 release.
Comment: Version 2014.1.528.545
Comment: Try upgrading to the latest version.
Comment: how can I upgrade  to the latest version?
Comment: Go and download it from the vendors site.
Comment: Can you please give me the link
Comment: How to add kendoui in project. there is no Kendo.MVC dll
Comment: This has now gone way beyond the scope of your initial question.  If you have additional questions about Visual Studio (or whatever IDE you are using) and using DLLs, please create another question.
Comment: Export to Excel button not working. when i click, button color changes to orange color
Comment: Is there anything in the browser console as far as an error goes ?  Is there a callback to the server? Which browser/version are you using ?
Comment: this is  I found in Console.

HTML1300: Navigation occurred.
    File: RunReports
    DOM7011: The code on this page disabled back and forward caching. For more        information, see: http://go.microsoft.com/fwlink/?LinkID=291337
    File: RunReports
    HTML1300: Navigation occurred.
    File: localhost:11040
    DOM7011: The code on this page disabled back and forward caching. For more  information, see: http://go.microsoft.com/fwlink/?LinkID=291337
Comment: Since you are using their MVC controls and grid, I would switch over to the professional help from Telerik.
Answer: It display data only in one excel sheet. I need to display 20 records in each excel sheet.

Export Kendo Grid to Excel in MVC 5


Given $4$ places, find the number of ways you can fill four Reds, one Blue, one Green and one Yellow

Question: Given $4$ blanks, find the number of ways you can fill four Reds, one Blue, one Green and one Yellow  so that none of the blanks is filled up with $4$ reds and none of the blanks remain empty.
I cannot get the final answer. All I can do is find the number of ways four blanks can be filled using the colours but then the permutation in each case is something which I cant do.
Here's what I did : add up the coefficient of $x^4, \cdots, x^7$ in $(x^4+\cdots x^0)(x^1+x^0)^3$. But that doesn't help.
Comment: Huh, so? What to do next, then?
Comment: Can I put multiple colors on a same place or just put three extra colors out of this calculation?
Comment: @TrầnThúcMinhTrí you can put multiple colours at the same place. But just ensure that you can't have all 4 A's at the same place, else, anything is fine as long as there's ≥ 1 colour at every location
Comment: I am imagining the blanks as distinguishable boxes; but within each box, order is unimportant.  So for instance (RRG)(RB)(R)(Y) is the same as (RGR)(RB)(R)(Y); but (RRG)(RB)(Y)(R) is different.  Is that the correct interpretation?
Comment: @paw88789 it's a correct interpretion. Btw, it's not necessary that all 7 colours have to be used up. Like, you can also take (RR)(Y)(B)(R) as well or anything you wish
Answer: I would just count by hand, starting with the choices for the red.  The reds can be distributed $(3,1,0,0), (2,2,0,0), (2,1,1,0), (1,1,1,1)$.  Now the two zeros have to get one of the other colors, so you get $(3X,1,Y,Z), (3,1X,Y,Z), (3,1,XY,Z)$ as possibilities.  There are six ways to assign colors to $XYZ$ and each can be assigned to the boxes in $12$ ways except $(3,1,XY,Z)$ is $24$ because it does not have the two single colors to swap, but assigning $Y$ and $Z$ can be swapped so we have $2\cdot 6 \cdot 12+ 6 \cdot 12=216$.  Keep going with the rest.
Comment: I have fixed it and believe I have this case correct. I made some mistakes before the original post, so it takes a lot of care.
Comment: How's it $6\cdot 24$? In that case, you are considering $(3,1,XY,Z)$ and $(3,1,YX,Z)$ as two different cases. But they are rather similar
Comment: You are correct.  The counting is hard and I missed that.  I will fix.
Comment: sure, that's not an issue. Thanks for your help (always)
Comment: Sir, sorry to interrupt but I think that it should be $2\cdot 3! \cdot 4!$ instead of $2\cdot 6 \cdot 12$ because the four places can be arranged in $24$ ways. Also, the last case will be like $4!\cdot 3$. So, I think that the answer should be $4!3!+4!3!+4!3=360$ if I am not wrong.
Comment: Sir, sorry to interrupt but I think that it should be $2\cdot 3! \cdot 4!$ instead of $2\cdot 6 \cdot 12$ because the four places can be arranged in $24$ ways. Also, the last case will be like $4!\cdot 3$. So, I think that the answer should be $4!3!+4!3!+4!3=360$ if I am not wrong.
Comment: For the first two patterns arranging the four places in $4!$ ways double counts because you could assign the $Y$ and $Z$ colors in reverse, then swap those two places.  One or the other of the $3!$ and $4!$ has to be divided by $2$ to account for this.  The last case is similar but it is the $X$ and $Y$ that can be swapped.
Comment: Yeah, that. We have to divide by $2$.
Comment: So, $(2X,2,Y,Z)$ can be done in $\frac{4!\cdot 3!}{2!}$ ways, am I correct?


Utilizar retorno de uma função em outra função

Question: Tenho uma função em Python que recebe um texto e retorna 2 listas, uma com todas as palavras que aparecem e a outra retorna é o numero de vezes que cada palavra aparece.
def frequencia(lista):
    conv = str(lista)
    plSeparadas = separa_palavras(conv)

    listUnic = []
    listFreq = []

    for p in plSeparadas:
        if p in listUnic:
            i = listUnic.index(p)
            listFreq[i] = listFreq[i] + 1
        else:
            listUnic.append(p)
            listFreq.append(1)

            xList = listUnic
            xFreq = listFreq

    return xList, xFreq

Preciso utilizar esse numero de vezes (xFreq) em uma outra função para calcular o valor médio da frequência :
def mediaFreq(listaFrequencia):
    ordenar = sorted(listaFrequencia)
    tamanho = len(ordenar)
    media = tamanho/2
    return(media)

Como posso fazer isso ??
Answer: Se o método retorna uma tupla de valores é necessário usar uma tupla para receber o retorno do mesmo.
Aplicando no seu código, seria algo como:
xList, xFreq = frequencia(lista)
media = mediaFreq(xFreq)

Exemplo:
def teste():
    return 'A', 'B'

x, y = teste()

print(x)  # A
print(y)  # B

Veja funcionando no repl.it.
Answer: def frequencia(lista):
    conv = str(lista)
    plSeparadas = separa_palavras(conv)

    listUnic = []
    listFreq = []

    for p in plSeparadas:
        if p in listUnic:
            i = listUnic.index(p)
            listFreq[i] = listFreq[i] + 1
        else:
            listUnic.append(p)
            listFreq.append(1)

            xList = listUnic
            xFreq = listFreq

    return xList, xFreq
def mediaFreq(listaFrequencia, xFreq):
    ordenar = sorted(listaFrequencia)
    tamanho = len(ordenar)
    media = tamanho/2
    return(media)
xList, xFreq = frequencia(lista)
media = mediaFreq(listaFrequencia, xFreq)

É só vc atribuir os valores de retorno da sua função frequencia à duas variáveis, as quais podem ter qualquer nome, apenas coloquei xList e xFreq pra facilitar o entendimento.
Como elas foram RETORNADAS da função frequencia, agora elas estão em escopo global e podem então, ser passadas por parâmetro para sua função mediaFreq da seguinte forma:  media = mediaFreq(listaFrequencia, xFreq). Não podemos esquecer de alterar os parâmetros de entrada de mediaFreq.
Answer: Como você compacta no return, você tem que descompactar no código main do seu script.
Por que as variantes só armazenadas na função elas tem que ser descompactadas no código principal.
Dessa forma o returno da função será armazenado nas variantes globais do código principal.
xList, xFreq = frequencia(lista)


Pegar o retorno de uma função em outra no controller do laravel

Question: Tenho duas funções no controller e preciso passar o retorno de uma função para a outra e salvar numa variável, mas está dando erro.
função que retorna
public function getarrcontatos(Request $request){
        $value = $request->get('value');
        $ArrContatos = [];
        $ArrContatos = $value;
        return ($ArrContatos);

    }

Função que recebe
    public function store(Request $request){

          $input = $request->all();
          $obscontatos = $request->input('obscontatos');
          $result = count($obscontatos);

        for ($i = 0; $i < $result ; $i++) {

          $listacontatos = getarrcontatos();

        $contatoarray = array('idContato'=>$listacontatos[1],'ccContato'=>$listacontatos[2], 'idObsContato'=>$listacontatos[0]);
       DB::table('contatoObsCc')->insert($contatoarray);

      }

  }

Estas duas funções estão no controller do Laravel.
A linha de código que da errp é:  $listacontatos = getarrcontatos();
Comment: Estão na mesma controller?
Comment: Sim, estão no mesmo Controller
Answer: Como você esta referenciando uma função dentro da mesma classe que foi instanciada é necessário utilizar $this na chamada ao método, assim, seu código da função que recebe tem que ficar desta forma:
public function store(Request $request){
    $input = $request->all();
    $obscontatos = $request->input('obscontatos');
    $result = count($obscontatos);

    for ($i = 0; $i < $result ; $i++) {

        // O método é referenciado por $this e o parâmetro $request é enviado
        $listacontatos = $this->getarrcontatos($request);

        $contatoarray = array('idContato'=>$listacontatos[1],'ccContato'=>$listacontatos[2], 'idObsContato'=>$listacontatos[0]);

        DB::table('contatoObsCc')->insert($contatoarray);

    }
}

Para mais informações sobre o uso de $this da uma olhada na resposta da seguinte pergunta:
Quando usar self vs $this em PHP?
Comment: eu fiz dessa forma, mas a variável $listacontatos fica vazia.
Comment: Utilize no método store antes de qualquer instrução a diretiva Log::info($request); para gravar no arquivo de log a saída e termos uma ideia do que esta sendo enviado ao método, acredito que ele não esta trazendo a chave 'value' e por isso o método getarrcontatos esta retornando vazio.
Answer: Falta colocar o $this
Coloca assim:
 $listacontatos = $this->getarrcontatos();


rational points at rational inputs of x+ sinx

Question: Let $f(x)=x+\sin x$
The question: Is there an $x\in Q$ for which output of the function is also rational? (Apart from the trivial case of $x=0$ where $f(0)=0$)
My attempt: Clearly the range of $f(x)$ is set of real numbers and from graph of the function it is very clear that rational outputs do come for many real numbers, but except for x$=0$ I couldn't find any other value.
Kindly help me. Any help would be greatly appreciated.
Comment: Does this answer your question? [Is sin(x) necessarily irrational where x is rational?](https://math.stackexchange.com/questions/299124/is-sinx-necessarily-irrational-where-x-is-rational)
Comment: Isn't this the same as asking for which rational values of $x$ the value of $\sin x$ is rational, which is well known?
Comment: There are many questions of this type - did you have a look already? I found [this one](https://math.stackexchange.com/questions/299124/is-sinx-necessarily-irrational-where-x-is-rational) first.
Answer: A sum of a rational number and a irrational number is irrational but $sin(x)$ is irrational (except 0) for all rational (also transcendental but not matter) then $x+sin(x)$ is irrational(also transcendental but not matter) for all rational
Comment: I wanted a rigorous proof on this so that it is established well beyond any doubt.
Comment: I think that my proof is perfectly rigorous
Comment: It's ok don't downvote
Comment: I don't down vote .


How can I interrupt or debounce an inotifywait loop?

Question: I have a little script that watches files for changes using inotifywait. When something changes, a batch of files are sent through a process (compiled, compressed, reorganised, etc) that takes about ten seconds to run.
Consider the following example:
touch oli-test
inotifywait -mq oli-test | while read EV; do sleep 5; echo "$EV"; done

If you run touch oli-test in another terminal a few times, you'll see that each loop completes before it moves on. That scenario is very real to me. If I forget to save a file while it's already processing, or notice a mistake, the events stack up and I'm waiting minutes.
It strikes me that there are two techniques that would make this workflow objectively better. I'm not sure what is easiest or best, so I'm presenting both:

Interrupt previous run-throughs, and restart immediately. The scripted process is currently just an inline set of commands. I could break them out to Bash functions, I'm not wild about breaking them further out than that.
Debounce the list of things waiting to be processed so that if five events happen at once (or while it's already processing), it only runs once more.

(Or both... because I'm certain there are cases where both would be useful)
I am also open to approaches that are different from inotifywait but they need to give me the same outcome and work on Ubuntu.
Answer: Here is a compact solution:
inotifywait -q -m -e modify -e create -e close_write --format "%w%f" /etc/nginx/ |\
while read -r path; do
    echo $path changed
    echo "Skipping $(timeout 3 cat | wc -l) further changes"
    service nginx reload
done

The first read waits for a line of data, so this won't eat your CPU.  The timeout 3 cat reads any further change notifications that come in over the next 3 seconds.  Only then does nginx get reloaded.
Comment: Wow, this is a really elegant solution! I never would have thought of that. Thanks for sharing!
Comment: This is really slick, and i wish i found it earlier because I've gone through a lot of iterations on this type of code and it's well suited behavior for bash but I never found a remotely elegant way to handle it. Recently I implemented project watching and rebuilding into my typescript project manually, and it didnt turn out so well because each time i broke my code, depending on the nature of breakage  it would often also break the watching, which hurts since the watching is what's supposed to streamline the process of unbreaking the code.
Answer: To interrupt, you can shift things around so the processing runs in a  background subshell and each new inotifywait event nukes the background processes:
inotifywait -mq oli-test | while read EV; do
    jobs -p | xargs kill -9
    (
        # do expensive things here
        sleep 5  # a placeholder for compiling
        echo "$EV"
    ) &
done

Comment: @Marcel Ish. If there are other background jobs in *this bash session* then yes, they'll all get thrown on the bonfire. But try it out, `sleep 2000 &` in one terminal, `jobs -p` in another. It won't nuke other sessions' jobs.
Comment: This looks like a good solution for what you are trying to do.
Comment: Anyway to silence the output of the pipe to `kill` ? Ive tried redirecting to /dev/null and no matter what type of redirect i've tried, I still see output.
Comment: @surgiie Untested but kill might be outputting to STDERR (fd/2). That's usually the case when you can't redirect something.  Try subbing that line for `jobs -p | xargs kill -9 2>/dev/null`
Comment: @Oli I tried this as well but was still seeing the output for kill :

 `./myscript: line 12:   542 Killed ( mycommand)`
Comment: Will this kill other background jobs?
Answer: Use of bc is totally superfluous, it's a sub-optimal implementation. It can be done all in integers.
A version with pure bash (no external commands except inotifywait), if we really want millisecond precision. (We can do even nano second precision, but will limit maximum debounce period)
#!/bin/bash

getTime() {
   # EPOCHREALTIME has the EPOCH time in nanoseconds
   # (i.e: 1653121901,339206 )
   # We extract the milliseconds and seconds up to 9
   # digits precision.
   # Using the value above, this is "121901339"
   # (Those values fit neatly into a bash integer)

   [[ $EPOCHREALTIME =~ ([^,].....),(...).*$ ]] && \
       echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"

   # (This way of pattern extraction avoids the use of sed on this cases.
   # Totally recommended)
}

lastRunTime=$(getTime)

# We avoid the pipe, so the main program always
# runs on the same process.
while read path event file; do

    currentTime=$(getTime)
    delta=$(( $currentTime - $lastRunTime ))

    if [[ "${delta}" -gt 1000 ]] ; then
        echo "run"
        lastRunTime=$(getTime)
    fi
done < <(inotifywait -mr ./web -e create -e delete -e modify)
# This way the extra process is the generator command, which is generally what you
# want.

That said, if you are interested on the events you receive, this treats all events as if they were the same. You don't know which events happened during the debouncing period.
This can be solved by using an associative array to store all the different events between debounce periods.
#!/bin/bash

getTime() {
   [[ $EPOCHREALTIME =~ ([^,].....),(...).*$ ]] && echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
}

declare -A Changes
lastRunTime=0

while read path event file; do

    currentTime=$(getTime)
    delta=$(( $currentTime - $lastRunTime ))

    Changes["$event $file"]=1

    if [[ "${delta}" -gt 1000 ]] ; then
        for change in "${!Changes[@]}"; do
           echo "run $change";
        done
        Changes=()
        lastRunTime=$currentTime
    fi
done < <(inotifywait -mr ./web/  -e create -e delete -e modify)

Comment: Perhaps change the regex to `([^,].....)[^[:digit:]](...).*$` to accommodate a decimal separator that isn't COMMA.
Answer: I write this solution, working only if bc (calculator) application is installed, because bash can't handle float numbers.
lastRunTime=$(date +'%H%M%S.%N')

inotifywait -mr ./web -e create -e delete -e modify | while read file event tm; do

    currentTime=$(date +'%H%M%S.%N')
    delta=$(bc <<< "$lastRunTime - $currentTime")
    echo "$currentTime, $lastRunTime, $delta"

    if (( $(echo "$delta < -1.0" | bc -l) )); then
        echo "run"
        lastRunTime=$(date +'%H%M%S.%N')
    fi

done

Explain: here we set last run datetime as NOW, and next run allowed only if last run datetime < delta (in my case is -1.0).
Example:
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
120845.009293691, 120842.581019388, -2.428274303
run
120845.018643243, 120845.017585539, -.001057704
120845.026360234, 120845.017585539, -.008774695

Comment: nice but why not just avoid decimals?


Encrypted Periodic Backups in RavenDB

Question: We've started using the Periodic Backup bundle in RavenDB to export data from our Server to an Azure Blob Storage Container incrementally.
The source database uses the Encryption bundle to safeguard data at rest, however after testing the restore it's clear the backup files either do not encrypt the data or keep a copy of the key in the backup file for restoration.
How can we configure RavenDB to encrypt backups, periodic or otherwise, so they remain safeguarded at rest?
Answer: Periodic Backup uses an export method to do that, which store the data in clear text.
Actually backing up (Raven.Backup.exe) will store the data in encrypted format, but will include the key in the backup (there is an option to prevent that), so you can actually restore the backup on another machine.
Comment: Further to this; running `Raven.Backup.exe -?` shows no flag to prevent that; nor is there indication of this in the [GitHub Repo](https://github.com/ravendb/ravendb/blob/master/Raven.Backup/Program.cs)
Comment: When restoring, you can edit the Database.Document file to include the  secret key
Comment: Thank you Ayende, how would you restore the backup if you've removed the key?  Is there an option when restoring to provide the key?
Comment: Raven.Backup doesn't expose this. That is how it behaves by default.
The studio (assuming you are human, not a machine), does behave in this way.
Answer: To expand on @Ayende Rahien's answer, use Raven backup instead of export.
If you need to remove the encryption key in the backup, use a curl Raven backup request with DatabaseDocument containing an empty SecuredSettings property.
Example from the link above:
curl -X POST "http://localhost:8080/databases/Northwind/admin/backup?incremental=false" \
     -d "{\"BackupLocation\":\"c:\temp\backup\Northwind\",\"DatabaseDocument\":{\"SecuredSettings\":{},\"Settings\":{\"Raven/ActiveBundles\": \"Encryption\"},\"Disabled\":false,\"Id\":null}}"

Here's the posted data in a readable format:
{
  "BackupLocation": "c:\temp\backup\Northwind",
  "DatabaseDocument": {
    "SecuredSettings": {}, // <- EncryptionKey not present here.
    "Settings": {
      "Raven/ActiveBundles": "Encryption"
    },
    "Disabled":false,
    "Id":null
  }
}

Encrypted Periodic Backups in RavenDB


how to call tab bar view controller by click button function in swift

Question: I have one common tab bar view controller.And i have connected 3 other view controllers with navigation bar and i connect this 3 view controllers with the main tab bar view controller.So now i have 3 tab bar items with 3 view controller.
that is  1.Home   2. Cart   3. feedback
This is fine !!
And now my first view controller(That is my first tab bar view controller) have some table view data and with detail view controller.And in my detail view controller i have one button called "Go to cart".
So the actual flows is when user press Go to cart.It have to move to cart` view controller.Now its moving.
But the problem is .I was not able to see my tab bar items and other items .But if i go normal to each tab bar items its showing all tab bar items.
When i go from my Detail view button click to my Cart tab bar view controller..Then no tab bar items are showing down.
I did all this like demo prototype.Like i drag from button to cart tab bar view controller navigation bar.
But its not showing tab bar why.Please help me out.I don't have any single solution to solve this.I am using swift 2.2.
Thanks.

Updated :
So i need to code some thing like this Right?.But i don't know how to handle this code:
 let barViewControllers = sender.destinationViewController as! UITabBarController

    let nav = barViewControllers.viewControllers![1] as! UINavigationController

    let destinationViewController = nav.topViewController as! CartVC

But i am getting crash in first line :
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

Please help me out
Comment: How are you going from detail to cart?...add some code & images of storyboard...people might be able to help.Check this how to ask questions http://stackoverflow.com/help/how-to-ask
Comment: @sanman  Now please check my updated post.And thanks
Comment: Your add to cart segue is modally presenting the cart viewcontroller instead of changing tab on tabbar controller.That is why you are not able to see any tabbar at bottom.
Comment: @sanman now what should i have to do .To see the tab bar
Comment: ok.But if i call to tab bar view controller means.Its showing the first tab bar option view controller.Because my cart tab bar view controller is second option.So if i call to main tab bar view controller.My first option`Home Tab`.That `home tab bar view controller only showing` instead of my cart tab bar view controller
Comment: You can access your tabbar controller in "detailVC" using self.tabBarController.From tabbarcontroller access your "cart vc" to set value for its variables if you need to.Then change the selected index of tabbarcontroller using self.tabBarController.selectedIndex = 1; Ask if you have any queries.
Comment: Please give me some code example.I am new to iOS dev.I din get you what you saying.BY  button click in detailVC.i NEED TO WRITE SOME CODE TO PASSS TO CART VC.but i don't how how to handle that.
Comment: @sanman now please see my update and please help me out.Thnaks
Answer:
Here is storyboard similar to yours.
Now remove segue from button "go to cart" which is similar to "show second tab" button in my storyboard
Instead provide action to it as follows,
 @IBAction func showSecondTab(sender: AnyObject) {
            let  navController = self.tabBarController?.viewControllers![1] as! UINavigationController
            ///secondviewcontroller in your case is cart
            let secondViewController = navController.viewControllers[0] as! SecondViewController
            //set values you want to pass
            //lets say I want to pass name to secondVC
            secondViewController.name = "ABCD"

            self.tabBarController?.selectedIndex = 1
    }

you can print and check values in viewDidLoad of secondVC(CartVC).


How To Create a case Or Incident In Microsoft Dynamics Online CRM using php-crm-toolkit

Question: I am trying to create case it's giving me

"Fatal error: Uncaught AlexaCRM\CRMToolkit\SoapFault: You should specify a parent contact or account. in D:\wamp64\www\php_crm\vendor\alexacrm\php-crm-toolkit\src\Client.php on line 1159"

My code:
<?php
/**
 * Use init.php if you didn't install the package via Composer
 */
require_once 'vendor/autoload.php';

use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;

$options = [
    'serverUrl' => '**********************',
    'username' => '****************',
    'password' => '*************',
    'authMode' => '***********************',
];

$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );

// create a new contact
$incident = $service->entity( 'incident' );
$incident->title = 'Test Created With Proxy';
$incident->description = 'This is a test incident';
$incidentId = $incident->create();

?>

Comment: ehm, so what's your question? what you tried?
Comment: I want to create "Customer Services - All Cases" using php.      I'm using https://github.com/AlexaCRM/php-crm-toolkit
Answer: Finally I created case by using following code:
<?php
/**
 * Use init.php if you didn't install the package via Composer
 */
require_once 'vendor/autoload.php';

use AlexaCRM\CRMToolkit\Client as OrganizationService;
use AlexaCRM\CRMToolkit\Settings;
use AlexaCRM\CRMToolkit\Entity\EntityReference;

$options = [
    'serverUrl' => '**************************',
    'username' => '**********************',
    'password' => '*****************',
    'authMode' => 'OnlineFederation',
];

$serviceSettings = new Settings( $options );
$service = new OrganizationService( $serviceSettings );

$contact = $service->entity( 'contact' );
$contact->firstname = 'John';
$contact->lastname = 'Doe';
$contact->emailaddress1 = 'firstname.lastname@example.com';
$guid = $contact->create();

$incident = $service->entity('incident');
//echo '
';print_r($incident);echo '
';
$incident->title = 'Test Created With Proxy';
$incident->description = 'This is a test incident';
$incident->customerid = new EntityReference( 'contact', $guid );
//$incident->ID = $guid;//contactid responsiblecontactid primarycontactid
$incidentId = $incident->create();
?>

Answer: The error is very clear, you must mention the customer of the incident - Parent account or contact. Check the below code, it should work (not sure about php syntax).
$incident = $service->entity( 'incident' );
$incident->title = 'Test Created With Proxy';
$incident->description = 'This is a test incident';
$incident->customerid = new EntityReference( 'contact', 'GUID HERE' );
$incidentId = $incident->create();

Reference
From the above link, adding another snippet for lookup field assignment:
$customer = $client->entity( 'contact' );
$customer->ID = 'GUID HERE';
$incident-> customerid = $customer;

Comment: Thanks Arun, Could you please let me know how we can get ?
Comment: @PHPMagentoDeveloper guid is the primary key of any CRM record - basically looks like 32 digits with dashes. https://arunpotti.wordpress.com/2016/07/10/how-to-get-guid-of-the-record-in-crm-online/
Comment: Also I am getting following error
Notice: Undefined property via __set() - incident does not support property: customerid_contact in D:\wamp64\www\php_crm\test.php on line 37 in D:\wamp64\www\php_crm\vendor\alexacrm\php-crm-toolkit\src\Entity.php on line 264"
Comment: @PHPMagentoDeveloper really sorry, I typed it from my memory.. also am not sure if you use web api or odata api. Try with customerid
Comment: Thanks for your help, getting Fatal error: Uncaught Error: Class 'EntityReference' not found
Comment: @PHPMagentoDeveloper updated the answer, I don't have any other clue :) is it easy to setup php environment or any codepen kinda thing available to test these myself first..?
Comment: Great Thanks for reply, Do we have any document for this?
Comment: How can i get the contact id from crm?
Comment: Hi Arun,    Coould you please have a look on following issue?

https://github.com/AlexaCRM/php-crm-toolkit/issues/90


is there a way to use java reflection to find a class using the classes variable?

Question: for example
say you had
public abstract class class1 {

}

public class2 extends class1 {
String id = "id_for_class2"
}

public class3 extends class1 {
String id = "id_for_class3"
}

public class4 extends class1 {
String id = "id_for_class4"}

And you want to use reflection to get a specific class based on the id.
Is there a way to do this using reflection in java?
Comment: While it's possible to do what you describe, you really should use [ServiceLoader](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/ServiceLoader.html) for finding implementations of an abstract class or interface.
Comment: Your `id` fields are instance fields. So you'd have to create instances of a class you don't known, in order to read the field. If you know the classes in advance, there is no point in comparing an additional id. What would be the advantage of `c = hypotheticalMethod("id_for_class3")` over `c = class3.class`? Also worth reading [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378)
Answer: You can get the value of id like below. This example will print "id_for_class2".
import java.lang.reflect.Field;

public class Main {

    public static void main(String[] args) throws Exception{

        class1 a = new class2();

        Class cls = a.getClass();
        Field field = cls.getDeclaredField("id");
        Object value = field.get(a);

        System.out.println(value);
    }
}


Dúvida sobre instruções que podem ser implementadas no Hibernate

Question: Pergunta de prova de curso técnico de Linguagem de programação Java:

Quais instruções podem ser usadas no Hibernate?
a- JDBC
b- JPA
c- SQL
d- JNDI
Comment: Pergunta muito mal-elaborada, por sinal.
Answer: Considerando "instruções" como instruções de manipulação e persistência de dados a resposta correta é:

C- SQL
Answer: O Hibernate é um ORM (Object Relational Mapping), uma "técnica de mapeamento objeto relacional que permite fazer uma relação dos objetos com os dados que os mesmos representam".
Fazendo algumas pequenas pesquisas no google:
JDBC - "Classes e interfaces para enviar instruções SQL para bancos qualquer banco de dados relacional". - JDB é um conector de bancos, assim como o hibernate, mas com outras técnicas e implementações, permitindo executar comandos SQL. (Andremoniy, 2015).
JPA - "Uma API padrão da linguagem Java que descreve uma interface comum para frameworks de persistência de dados" - JPA refere apenas às especificações, equanto Hibernate é uma implementação das especificações (Bowersox, 2012).
SQL - É a linguagem de pesquisa declarativa padrão para banco de dados relacional (base de dados relacional) - SQL são os comandos do banco em sí. É o que é executado, seja explicitamente por comando ou "por baixo dos panos" por ORM. Assim, essa é sua resposta.
JNDI - "Uma API para acesso a serviços de diretórios. Ela permite que aplicações cliente descubram e obtenham dados ou objetos através de um nome" - Assim como o JDBC, o JDNI permite conectar à bancos e executar comandos SQL. (jpkrohling, 2011).
Sempre que possível, faça o dever de casa!
Answer: HQL é o mecanismo no Hibernate para definir instruções do tipo SQL. No entanto, não acho correto dizer que o SQL pode ser lido pelo Hibernate e sim o HQL.


Rational function in integral form

Question:
If $f$ is quadratic function such that $f(0)=1$ and $\int\frac{f(x)}{x^2(x+1)^3}dx $ is a rational function, find the value of $f'(0)$.

I already tried solving this question by using general quadratic equation $ax^2+bx+c$ and then using partial fraction method but it became very complicated.
Answer: We have that $b=f'(0)$ and $c=f(0)=1$. Moreover, by using partial fraction method we get
$$\frac{ax^2+bx+1}{x^2(x+1)^3}=\frac{A}{x}+\frac{B}{x^2}+\frac{C}{x+1}+\frac{D}{(x+1)^2}+\frac{E}{(x+1)^3}.$$
If the integral is a rational function, i.e. a ratio of polynomials, then $A=0$ and $C=0$ because the integrals of those terms yield logarithms. Hence
$$ax^2+bx+1=B(x+1)^3+Dx^2(x+1)+Ex^2,$$
that is
$$(B+D)x^3+(3B+D+E-a)x^2+(3B-b)x+ (B-1)=0.$$
Are you able to find $b$?
Comment: No, for rational function $p/q$, $p$ and $q$ are POLYNOMIALS!
Comment: so what wrong with logs here ,I mean a particular fraction terms are in form of logs but still overall by taking lcm we get the form $\frac{p(x)}{q(x)}$ so that is what rational function is right?! or may be my concept about what rational function is, is not clear
Comment: thanks I understand now
Comment: yes you were great! thanks once again!! ☺

Rational function in integral form