Proving this space is not totally bounded.

Question: The real line $\Bbb R $ endowed with the metric $d(x,y)=\min(1,|x-y|)$ is a bounded metric space that isn't totally bounded.
I can easily prove that this is bounded. But to prove this is not totally bounded we have to find $P>0$ such that there is no finite cover of $P$ balls. Can someone help me to prove this?
Comment: Use any $P\le 1$: in that case the $P$-balls in this metric are identical to those in the usual metric.
Comment: I assume "$P$ balls" means balls of radius $P$.
Comment: Just take $P=\frac12$, or anything else smaller than $1$.
Comment: @BrianM.Scott Don't you mean $<1$?
Comment: @5xum: No, $1$ also works. If $x$ is in the open $1$-ball about $y$, then $|x-y|<1$.
Comment: Maybe $P$-ball means open ball of radius $P$.  Then we can even use $P=1$.
Comment: Oh, open balls, yeah. Then it works.
Comment: What means cover?
Comment: @5xum My problem is the way to prove it .Could you please write a complete proof
Answer: If $P\leq 1$, then $B(x,P)=\{y \in \mathbb R \mid |x-y|<P\}=(x-P,x+P)$ is an open interval centered at $x$ as if $d(x,y)<1$, as the metric $d$ becomes the usual metric on $\mathbb R$.
Since $\mathbb R$ is unbounded with the usual metric (while, as you have proved, it is bounded with this metric) we can see that we can't find finitely many balls of radius $P$ that cover the entire space.
In fact, if we could, that would mean that there exist $x_1,\dots,x_N$ such that $\mathbb R \subset \bigcup_{i=1}^N (x_i-P,x_i+P)$. But the length of the union of such intervals is at most $2NP$, while $\mathbb R$ has infinite length.
Comment: What i dont understand is that how can you tell R is unbounded with this metric ? It becomes the usual metric when d(x,y)<1 but it is not the usual metric
Comment: You are right, it is not and $\mathbb R$ is bounded. But if you take $P<1$, then the balls of this metric are the same balls of the usual metric (that is, open intervals) so you can't cover the entire space only with finitely many.
Comment: Can you tell me the way to prove R with this metric is complete? do we have to show that every cauchy sequence converges or is there an easier way?
Comment: You should ask a new question, if you need help with that. Anyway, I would show that every Cauchy sequence converges.
Comment: Thanks.I asked a new question anyway :)


Two cones intersect such that the vertex of one coincides with the center of the other's base. Find the volume of their intersection.

Question: One cone of radius $4$ and height $12$, and another cone of radius $6$ and height $12$, intersect such that the vertex of one coincides with the center of the base of the other. Find the volume of the intersection of the cones.
Comment: Can you please point a direction of approach you've already tried?
Comment: Hint: it is the sum of two cones.
Answer:
From similar triangles in the diagram, we have
$$\frac x4=\frac b{12},\>\>\>\>\>\frac x6=\frac a{12}\implies x(\frac14+\frac16) = \frac{a+b}{12}=1$$
which yields $x = \frac {12}5$. Thus, the intersecting volume is
$$\frac13 \cdot 12\cdot (\pi x^2)= \frac{576\pi}{25}$$
Comment: Interestingly enough, $x$ depends not of the common height of the cones, but on their radii solely.
Comment: @MichaelHoppe - but conditional on the two cones having the same height
Comment: Of course, a nice result.


How to perform multivariate linear regression on this example?

Question: So I have a table with points in time $t_i$ and measurements $s_i$ give by
\begin{bmatrix}t_i&0&1&2&3\\s_i&4.30&1.48&0.56&0.24\end{bmatrix}
As a regression function I'm given
$\hspace{6cm}s(t)=a_1 2^{-2t}+a_2 2^{-t}$
Now I'm supposed to find the parameters $a_1$ and $a_2$ with the least square method, which means I have to minimize
$\hspace{6cm} \sum\limits_{i=0}^{3}|s(t_i)-s_i|^2$.
I think I understood linear regression quite well and would be able to perform it on this example but the multiple variables of the function really throws me off.
Comment: This problem is actually linear in the coefficients, so there is no real difficulty. In fact after a change of variable it is a polynomial regression.
Answer: Do you know how to do multivariate linear regression? Let one of your variables be $x_1 = 2^{-2t}$ and the other be $x_2 = 2^{-t}$, calculate those on your whole dataset and now your problem is simply optimizing $y(x) = a_1x_1 + a_2x_2$.
Comment: In my lecture notes there is just a general approach to the least square problem described. I was confused about the terminology so acutally I'm asking about multivariate linear regression.
Answer: $$s(t)=a \:2^{-2t}+b\: 2^{-t}$$
Compute the variable : $\quad x_i=2^{-t_i}$
$$s(x)=a \:x^2+b\: x$$
In order to find the approximates of $a,b$ one have to make a linear regression.
This means linear with respect to the sought parameters $a,b$, of course not with respect to the functions $x^2,x$.
I suppose that all is explain in your textbook on a more general manner.

If you are trouble by the function $x^2$ you can do this (not recommended, only for information) :
$$\frac{s(t)}{2^{-t}}=a \:2^{-t}+b$$
Compute $\quad y_i=\frac{s_i}{2^{-t_i}}$
$$y(x)=a \:x+b$$
You are certainely more familiar with this simpler form.

The result is slightly different from above. This is not surprising because the fitting is with respect to $y$ instead of to $s$.


App crashes when you click several times

Question: In my app the first screen is a list and when you click once it goes ok but if,my mistake or something, you click two or more times before it loads the application crashes. I've been doing a little research but I didn't found anything. I post my code:
    @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.firstscreen);
    // Set title
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.mainheader);

    // Get Arguments
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        app = extras.getString("app");
    }

    //Set simulation date
    TextView date = (TextView) findViewById(R.id.date);
    date.setText(convertDateToString());
    // set the list adapter: ListView myList =
    ListView lv = (ListView) findViewById(R.id.list);
    DataBaseAccess ddbb = new DataBaseAccess();
    //Download the data from a database
    lv.setAdapter(new MyAdapter(this, R.layout.listitem, myLines,
            flowAlarm, precAlarm));
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent i = new Intent(Main.this, ShowGraph.class);
            String[] obj = list.split(";");
            i.putExtra("object name", obj[position]);
            i.putExtra("app name", app);
            i.putExtra("app list", list);
            startActivity(i);

        }
    });

    // Set buttons clickable
    Button exit = (Button) findViewById(R.id.exit);
    exit.setOnClickListener(this);
    Button calendar = (Button) findViewById(R.id.calendar);
    calendar.setOnClickListener(this);
}

Does anyone knows why?
My LogCat:
02-29 13:51:27.975: E/AndroidRuntime(9082): FATAL EXCEPTION: main
02-29 13:51:27.975: E/AndroidRuntime(9082): java.lang.NullPointerException
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.chart.XYChart.toRealPoint(XYChart.java:699)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.tools.Pan.apply(Pan.java:64)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.TouchHandler.handleTouch(TouchHandler.java:106)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.GraphicalView.onTouchEvent(GraphicalView.java:284)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.View.dispatchTouchEvent(View.java:3952)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1711)
02-29 13:51:27.975: E/AndroidRuntime(9082):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1145)
02-29 13:51:27.975: E/AndroidRuntime(9082):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1695)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.os.Looper.loop(Looper.java:130)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.app.ActivityThread.main(ActivityThread.java:3701)
02-29 13:51:27.975: E/AndroidRuntime(9082):at java.lang.reflect.Method.invokeNative(Native Method)
02-29 13:51:27.975: E/AndroidRuntime(9082):at java.lang.reflect.Method.invoke(Method.java:507)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
02-29 13:51:27.975: E/AndroidRuntime(9082):at dalvik.system.NativeStart.main(Native Method)

The ShowGraph activity oncreate:
    @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Get arguments
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        object = extras.getString("object name");
        app = extras.getString("app name");
        list = extras.getString("app list");
    }
    // Set title
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.headermenu);
    // Download the info from the database

     downloadInfo(app, object);

    // set the button listeners
    listeners();
    //Set checkbox listeners
    setCheckListeners();

    // Drag Meteo
    vg = (RelativeLayout) findViewById(R.id.graph);
    vg.setOnTouchListener(new View.OnTouchListener() {
        RelativeLayout vg1 = (RelativeLayout) findViewById(R.id.graph1);

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                // int x = (int) event.getX() - offset_x;
                int y = (int) event.getY() - offset_y;
                int w = getWindowManager().getDefaultDisplay().getWidth();
                int h = vg.getHeight() - 40;
                if (y < 0)
                    y = 0;
                if (y > h)
                    y = h;
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        new ViewGroup.MarginLayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT));
                lp.setMargins(0, y, 0, 0);
                selected_item.setLayoutParams(lp);
                int height = vg1.getHeight();
                RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                        new ViewGroup.MarginLayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT));
                lp2.setMargins(0, 0, 0, Math.abs(vg.getHeight() - y));
                // lp2.height = y-height;
                vg1.setLayoutParams(lp2);
                break;
            default:
                break;
            }
            return true;
        }
    });
    RelativeLayout img = (RelativeLayout) findViewById(R.id.vg);
    img.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                offset_x = (int) event.getX();
                offset_y = (int) event.getY();
                selected_item = v;
                break;
            default:
                break;
            }
            return false;
        }
    });
}

I tried with the AsyncTask as I read here but is not working:
Android: Disable ListView once clicked?
Comment: Blindly assumption. If I am not wrong. Just check your variables which are you passed to ShowGraph class in list item click whether its not null when you passed it. put condtion to check it and start activity in this condition. Also at ShowGraph.java class when you get values just check any one of them is not null.
Comment: What is showing in your logcat?
Comment: I've edited the question with the logcat, although I'm not able to pur it into code source form, don't know why!sorry...is a bit messy to see it like that! I edit also the list content in xml layout!
Comment: what is the `Exception` you get? you didn't include that in your logcat post...
Comment: You put ListLayout twice, instead of ListAdapter. Problem isn't in layout, so put here onStart code of your ShowGraph activity
Comment: I've checked and they are not null, but I found out that it crashes in  ShowGraph activity, before it loads not in Main activity the exception I get is NUllPointerException.
Comment: I've updated with the ShowGraph activity!
Answer: In ShowGraph:
if (extras != null) {
    object = extras.getString("object name");
    app = extras.getString("app name");
    list = extras.getString("app list");
}

If your extras is null, then your app is null too. So problem can be there
 downloadInfo(app, object); //NullPointerException

ALSO
1) Note that onCreate() called once (only for first click). It's better to move your "get Intent" part to onStart() method.
2) Use debbuging tools or try to hide your code with comments block by block, to find where is NullPointerException exactly.
 /*Like this
 downloadInfo(app, object);
 */

Comment: No, it's not that, in downloadInfo I check if they are null or not and  do different things in each case. If I click just once it works perfecctly but if I click twice it crashes! Thanks!
Comment: But what do you mean exactly by: try to hide your code with comments block by block?
Answer: What I did to solve it was to add a ProgressDialog in order to prevent people clicking several times when the new activity was loading.
MyDialog = ProgressDialog.show(ShowGraph.this, ""," Loading. Please wait ... ", true);


How to find the distance between two concentric contours, for different angles?

Question: I have an image with two contours, where one contour is always 'inside' another. I want to find the distance between the two contours for 90 different angles (meaning, distance at every 4 degrees). How do I go about doing it?
Here's an example image:

Thank you!
Comment: Have a look at the shapely package and this answer: http://stackoverflow.com/questions/36314240/how-to-draw-a-line-from-the-centroid-of-a-contour-to-the-perimeter-of-the-contou/36412705#36412705
Comment: 1.) You take the inner contour and calculate the centroid. 2.) For each angle, you create a line. 3.) For each line, you calculate the intersection points with both contours as shown in the other example.  Have I missed anything?
Comment: You can obtain the coordinates of your contour by using the OpenCV findContours function: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
Comment: `CV_CHAIN_APPROX_NONE` doesn't return all coordinates. There's absolutely no native  function in Opencv that returns all coodrinates.

http://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html#gsc.tab=0
Comment: If you use cv2.approxPolyDP, you'll get all Polygon which can be used for shapely. Example given in answer below.
Comment: by the way, this question is now to open to more optimal answers, so anyone can answer rest of question, thanks
Comment: @mattsmith5: answers below assume angles around the centroid, and distances along the line from the centroid. Distances between the two contours could be (usually are) smaller than in the one chosen direction. Please edit the question to clarify the definition of angle and the way the distance should be measured.
Answer: Take this image of two sets of two shapes:

We want to find the distance between the edges of each set of shapes, including where the edges overlap.

First things first, we import the necessary modules:

import cv2
import numpy as np

To do that, we will first need to retrieve every shape in the image as lists of contours. In the above particular example, there are 4 shapes that need to be detected. To retrieve each shape, we will need to use a mask to mask out every color besides the color of the shape of interest:

def get_masked(img, lower, upper):
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(img_hsv, np.array(lower), np.array(upper))
    img_mask = cv2.bitwise_and(img, img, mask=mask)
    return img_mask

The lower and upper parameters will determine the minimum HVS values and the maximum HSV values that will not be masked out of the image. Given the right lower and upper parameters, you will be able to extract one image with only the green shapes, and one image with only the blue shapes:

With the masked images, you can then proceed to process them into more clean contours. Here is the preprocess function, with values that can be tweaked whenever necessary:

def get_processed(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (7, 7), 7)
    img_canny = cv2.Canny(img_blur, 50, 50)
    kernel = np.ones((7, 7))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=2)
    img_erode = cv2.erode(img_dilate, kernel, iterations=2)
    return img_erode

Passing in the masked images will give you

With the images masked and processed, they will be ready for opencv to detect their contours:

def get_contours(img):
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    return [cnt for cnt in contours if cv2.contourArea(cnt) > 500]

The list comprehension at the return statement is there to filter out noise by specifying that every contour must have an area that is greater than 500.

Now, we will define some basic functions that we will later use:

def get_centeroid(cnt):
    length = len(cnt)
    sum_x = np.sum(cnt[..., 0])
    sum_y = np.sum(cnt[..., 1])
    return int(sum_x / length), int(sum_y / length)

def get_pt_at_angle(pts, pt, ang):
    angles = np.rad2deg(np.arctan2(*(pt - pts).T))
    angles = np.where(angles < -90, angles + 450, angles + 90)
    found= np.rint(angles) == ang
    if np.any(found):
        return pts[found][0]

The names of the functions are pretty self-explanatory; the first one returns the center point of a contour, and the second one returns a point in a given array of points, pts, that is at a given angle, ang, relative to a given point, pt. The np.where in the get_pt_at_angle function is there to shift the starting angle, 0, to the positive x axis, as it by default will be at the positive y axis.

Time to define the function that will return the distances. First, define it so that these five parameters can be passed in:

def get_distances(img, cnt1, cnt2, center, step):

A brief explanation on each parameter:

img, the image array
cnt1, the first shape
cnt2, the second shape
center, the origin for the distance calculations
step, the number of degrees to be jumped per value

Define a dictionary to store the distances, with the angles as key and the distances as values:

    angles = dict()

Loop through each angle you want to retrieve the distance of the edges of the two shapes, and find the coordinate of the two contours that are the ct angle of the iterations, angle, relative to the origin point, center, using the get_pt_at_angle function we defined earlier.

    for angle in range(0, 360, step):
        pt1 = get_pt_at_angle(cnt1, center, angle)
        pt2 = get_pt_at_angle(cnt2, center, angle)

Check if a point exists in both contours that is at the specific angle relative to the origin:

        if np.any(pt1) and np.any(pt2):

You can use the np.linalg.norm method to get the distance between the two points. I also made it draw the text and connecting lines for visualization. Don't forget to add the angle and value to the angles dictionary, and you can then break out of the inner for loop. At the end of the function, return the image that has the text and lines drawn on it:

            d = round(np.linalg.norm(pt1 - pt2))
            cv2.putText(img, str(d), tuple(pt1), cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 0))
            cv2.drawContours(img, np.array([[center, pt1]]), -1, (255, 0, 255), 1)
            angles[angle] = d

    return img, angles

Finally, you can utilize the function defined on an image:

img = cv2.imread("shapes1.png")

img_green = get_masked(img, [10, 0, 0], [70, 255, 255])
img_blue = get_masked(img, [70, 0, 0], [179, 255, 255])

img_green_processed = get_processed(img_green)
img_blue_processed = get_processed(img_blue)

img_green_contours = get_contours(img_green_processed)
img_blue_contours = get_contours(img_blue_processed)

Using the image of four shapes, you can tell that the img_green_contours and img_blue_contours will each contain two contours. But you might be wondering: how did I choose the minimum and maximum HSV values? Well, I used a trackbar code. You can run the below code, adjusting the HSV values using the trackbars until you find a range where everything in the image is masked out (in black) except for the shape you want to retrieve:
import cv2
import numpy as np

def empty(a):
    pass

cv2.namedWindow("TrackBars")
cv2.createTrackbar("Hue Min", "TrackBars", 0, 179, empty)
cv2.createTrackbar("Hue Max", "TrackBars", 179, 179, empty)
cv2.createTrackbar("Sat Min", "TrackBars", 0, 255, empty)
cv2.createTrackbar("Sat Max", "TrackBars", 255, 255, empty)
cv2.createTrackbar("Val Min", "TrackBars", 0, 255, empty)
cv2.createTrackbar("Val Max", "TrackBars", 255, 255, empty)

img = cv2.imread("shapes0.png")

while True:
    h_min = cv2.getTrackbarPos("Hue Min", "TrackBars")
    h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
    s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
    s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
    v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
    v_max = cv2.getTrackbarPos("Val Max", "TrackBars")

    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])

    mask = cv2.inRange(img_hsv, lower, upper)
    img_masked = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow("Image", img_masked)
    if cv2.waitKey(1) & 0xFF == ord("q"): # If you press the q key
        break

With the values I chose, I got:

Loop through the blue shape contours and green shape contours in parallel, and depending on which color shape you want the origin to be at the center of, you can pass that color contour into the get_centeroid function we defined earlier:

for cnt_blue, cnt_green in zip(img_blue_contours, img_green_contours[::-1]):
    center = get_centeroid(cnt_blue)
    img, angles = get_distances(img, cnt_green.squeeze(), cnt_blue.squeeze(), center, 30)
    print(angles)

Notice that I used 30 as the step; that number can be changed to 4, I used 30 so the visualization would be more clear.

Finally, we can display the image:

cv2.imshow("Image", img)
cv2.waitKey(0)

Altogether:
import cv2
import numpy as np

def get_masked(img, lower, upper):
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(img_hsv, np.array(lower), np.array(upper))
    img_mask = cv2.bitwise_and(img, img, mask=mask)
    return img_mask

def get_processed(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (7, 7), 7)
    img_canny = cv2.Canny(img_blur, 50, 50)
    kernel = np.ones((7, 7))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=2)
    img_erode = cv2.erode(img_dilate, kernel, iterations=2)
    return img_erode

def get_contours(img):
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    return [cnt for cnt in contours if cv2.contourArea(cnt) > 500]

def get_centeroid(cnt):
    length = len(cnt)
    sum_x = np.sum(cnt[..., 0])
    sum_y = np.sum(cnt[..., 1])
    return int(sum_x / length), int(sum_y / length)

def get_pt_at_angle(pts, pt, ang):
    angles = np.rad2deg(np.arctan2(*(pt - pts).T))
    angles = np.where(angles < -90, angles + 450, angles + 90)
    found= np.rint(angles) == ang
    if np.any(found):
        return pts[found][0]

def get_distances(img, cnt1, cnt2, center, step):
    angles = dict()
    for angle in range(0, 360, step):
        pt1 = get_pt_at_angle(cnt1, center, angle)
        pt2 = get_pt_at_angle(cnt2, center, angle)
        if np.any(pt1) and np.any(pt2):
            d = round(np.linalg.norm(pt1 - pt2))
            cv2.putText(img, str(d), tuple(pt1), cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 0))
            cv2.drawContours(img, np.array([[center, pt1]]), -1, (255, 0, 255), 1)
            angles[angle] = d

    return img, angles

img = cv2.imread("shapes1.png")

img_green = get_masked(img, [10, 0, 0], [70, 255, 255])
img_blue = get_masked(img, [70, 0, 0], [179, 255, 255])

img_green_processed = get_processed(img_green)
img_blue_processed = get_processed(img_blue)

img_green_contours = get_contours(img_green_processed)
img_blue_contours = get_contours(img_blue_processed)

for cnt_blue, cnt_green in zip(img_blue_contours, img_green_contours[::-1]):
    center = get_centeroid(cnt_blue)
    img, angles = get_distances(img, cnt_green.squeeze(), cnt_blue.squeeze(), center, 30)
    print(angles)

cv2.imshow("Image", img)
cv2.waitKey(0)

Output:
{0: 5, 30: 4, 60: 29, 90: 25, 120: 31, 150: 8, 180: 5, 210: 7, 240: 14, 270: 12, 300: 14, 330: 21}
{0: 10, 30: 9, 60: 6, 90: 0, 120: 11, 150: 7, 180: 5, 210: 6, 240: 6, 270: 4, 300: 0, 330: 16}

Note: For certain shapes, some angles might be absent in the dictionary. That would be caused by the process function; you would get more accurate results if you turn down some of the values, like the blur sigma
Answer: In the following code, I have just given you the example for the vertical line, the rest can be obtained by rotating the line. Result looks like this, instead of drawing you can use the coordinates for distance calculation.

import shapely.geometry as shapgeo
import numpy as np
import cv2

img = cv2.imread('image.jpg', 0)
ret, img =cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)

#Fit the ellipses
_, contours0, hierarchy = cv2.findContours( img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
outer_ellipse = [cv2.approxPolyDP(contours0[0], 0.1, True)]
inner_ellipse = [cv2.approxPolyDP(contours0[2], 0.1, True)]

h, w = img.shape[:2]
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours( vis, outer_ellipse, -1, (255,0,0), 1)
cv2.drawContours( vis, inner_ellipse, -1, (0,0,255), 1)

##Extract contour of ellipses
cnt_outer = np.vstack(outer_ellipse).squeeze()
cnt_inner = np.vstack(inner_ellipse).squeeze()

#Determine centroid
M = cv2.moments(cnt_inner)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print cx, cy

#Draw full segment lines
cv2.line(vis,(cx,0),(cx,w),(150,0,0),1)

# Calculate intersections using Shapely
# http://toblerity.org/shapely/manual.html
PolygonEllipse_outer= shapgeo.asLineString(cnt_outer)
PolygonEllipse_inner= shapgeo.asLineString(cnt_inner)
PolygonVerticalLine=shapgeo.LineString([(cx,0),(cx,w)])

insecouter= np.array(PolygonEllipse_outer.intersection(PolygonVerticalLine)).astype(np.int)
insecinner= np.array(PolygonEllipse_inner.intersection(PolygonVerticalLine)).astype(np.int)
cv2.line(vis,(insecouter[0,0], insecinner[1,1]),(insecouter[1,0], insecouter[1,1]),(0,255,0),2)
cv2.line(vis,(insecouter[0,0], insecinner[0,1]),(insecouter[1,0], insecouter[0,1]),(0,255,0),2)

cv2.imshow('contours', vis)

0xFF & cv2.waitKey()
cv2.destroyAllWindows()

Comment: it would be kind of nice if we can extend for different contours, eg triangle, rectangle, that may interwine/overlap occasionally, I think your answer above will work pictures like this also, just want to extend it every 4 degrees, etc https://ibb.co/6XBJn7q
Comment: also performance wise, wondering if there is faster way to measure distances without physically drawing line intersection etc if possible
Comment: @mattsmith5 I'm a little bit short on time at the moment and it seems that some other guys have already provided appropriate answers (which is the way SO works), so I'll pass on that one. But thanks for the compliments!
Answer: I borrowed the general idea using Shapely and the basic code from tfv's answer. Nevertheless, iterating the desired angles, calculating the needed end points for the correct lines to be intersected with the shapes, calculating and storing the distances, etc. were missing, so I added all that.
That'd be my full code:
import cv2
import numpy as np
import shapely.geometry as shapgeo

# Read image, and binarize
img = cv2.imread('G48xu.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]

# Find (approximated) contours of inner and outer shape
cnts, hier = cv2.findContours(img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
outer = [cv2.approxPolyDP(cnts[0], 0.1, True)]
inner = [cv2.approxPolyDP(cnts[2], 0.1, True)]

# Just for visualization purposes: Draw contours of inner and outer shape
h, w = img.shape[:2]
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours(vis, outer, -1, (255, 0, 0), 1)
cv2.drawContours(vis, inner, -1, (0, 0, 255), 1)

# Squeeze contours for further processing
outer = np.vstack(outer).squeeze()
inner = np.vstack(inner).squeeze()

# Calculate centroid of inner contour
M = cv2.moments(inner)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])

# Calculate maximum needed radius for later line intersections
r_max = np.min([cx, w - cx, cy, h - cy])

# Set up angles (in degrees)
angles = np.arange(0, 360, 4)

# Initialize distances
dists = np.zeros_like(angles)

# Prepare calculating the intersections using Shapely
poly_outer = shapgeo.asLineString(outer)
poly_inner = shapgeo.asLineString(inner)

# Iterate angles and calculate distances between inner and outer shape
for i, angle in enumerate(angles):

    # Convert angle from degrees to radians
    angle = angle / 180 * np.pi

    # Calculate end points of line from centroid in angle's direction
    x = np.cos(angle) * r_max + cx
    y = np.sin(angle) * r_max + cy
    points = [(cx, cy), (x, y)]

    # Calculate intersections using Shapely
    poly_line = shapgeo.LineString(points)
    insec_outer = np.array(poly_outer.intersection(poly_line))
    insec_inner = np.array(poly_inner.intersection(poly_line))

    # Calculate distance between intersections using L2 norm
    dists[i] = np.linalg.norm(insec_outer - insec_inner)

    # Just for visualization purposes: Draw lines for some examples
    if (i == 10) or (i == 40) or (i == 75):

        # Line from centroid to end points
        cv2.line(vis, (cx, cy), (int(x), int(y)), (128, 128, 128), 1)

        # Line between both shapes
        cv2.line(vis,
                 (int(insec_inner[0]), int(insec_inner[1])),
                 (int(insec_outer[0]), int(insec_outer[1])), (0, 255, 0), 2)

        # Distance
        cv2.putText(vis, str(dists[i]), (int(x), int(y)),
                    cv2.FONT_HERSHEY_COMPLEX, 0.75, (0, 255, 0), 2)

# Output angles and distances
print(np.vstack([angles, dists]).T)

# Just for visualization purposes: Output image
cv2.imshow('Output', vis)
cv2.waitKey(0)
cv2.destroyAllWindows()

I generated some examplary output for visualization purposes:

And, here's an excerpt from the output, showing angle and the corresponding distance:
[[  0  70]
 [  4  71]
 [  8  73]
 [ 12  76]
 [ 16  77]
 ...
 [340  56]
 [344  59]
 [348  62]
 [352  65]
 [356  67]]

Hopefully, the code is self-explanatory. If not, please don't hesitate to ask questions. I'll gladly provide further information.
----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
NumPy:         1.20.2
OpenCV:        4.5.1
Shapely:       1.7.1
----------------------------------------


Show that $M_{n}=\left(\cfrac{N}{N-1}\right)^n X_n(N-X_n)$ is a martingale

Question: Suppose we have a random variable $(X_n)$ such that :

$X_0=k\in\{1,..,N\}$

The conditional distribution of $X_{n+1}$ conditioned to $\mathcal F_n=\sigma(X_1,..,X_n)$ follows the binomial distribution with parameters $(N,X_n/N)$

Let $M_{n}=\left(\cfrac{N}{N-1}\right)^n X_n(N-X_n)$.  Prove that $M_n$ is a martingale with respect to $\mathcal F_n$.
Attempt: I already proved that $X_n$ is a martingale.
$E(M_{n+1} \, | \, F_n)=E\left( \left(\cfrac{N}{N-1}\right)^{n+1}X_{n+1}(N-X_{n+1}) \, | \, F_n \right)$
I noticed that $X_{n}(N-X_{n})=N*Var(X_{n+1})$
And we know that $E[X_{n+1}\, | \, F_n]=X_n$
But I got stuck there, did I start this wrong? Am I missing a property that I can use?
Comment: How did you show that $\{X_n\}$ is an $\mathcal{F}_n-$martingale? I think you need additional assumptions on $X_n$ than what you have given. Such as what values do $X_n$ take.
Comment: @AndrewShedlock because $X_{n+1}$ follows the binomial distribution $(N,X_n/N)$ when conditioned to $F_n$ you just take the product $N \frac{X_n}{N}=X_n$ by definition of the binomial distribution the expected value is the product of its paramaters
Answer: Since $X_{n+1} \, | \, X_n = x \sim B(N, x/N)$, we have:
$$\begin{align*}
E(X_{n+1} (N - X_{n+1}) \, | \, X_n = x) &= N E(X_{n+1} \, | \, X_n=x) - E(X_{n+1}^2 \, | X_n = x) \\
&= Nx - \left( {x}\left( 1- \frac{x}{N} \right) + x^2 \right) \\
&= (N-1)x\left( 1 - \frac{x}{N} \right)
\end{align*}$$
Where in the second equality we used $E(X^2) = Var(X) + E(X)^2$ and in the last equality we expanded and collected terms. Since $M_n$ is a Markov process, it follows that $$E(X_{n+1} (N - X_{n+1}) \, | \, \mathcal{F}_n) = E(X_{n+1} (N - X_{n+1}) \, | \, X_n) = (N-1)X_n \left( 1 - \frac{X_n}{N} \right)$$
Thus,
$$\begin{align*}
E(M_{n+1} \, | \, \mathcal{F}_n) = \left( \frac{N}{N-1} \right)^{n+1} (N-1) X_n \left( 1 - \frac{X_n}{N} \right) = \left( \frac{N}{N-1} \right)^{n}  X_n \left( N - {X_n} \right) = M_n
\end{align*}$$
Thus $M_n$ is an $\mathcal{F}_n$-martingale.


Imputing missing values in time series using SAS

Question: If I have missing values in a time series that has 40 quarters (ten cycles or ten years) of data, what is the best SAS procedure to use to impute the missing values?
Part 2:  I have 390 series (40 quarters each) that follow similar patterns -- most have missing data points (2-3 each), how do I make use of the other  390 series to help impute missing values in any one series? What SAS procedure would I use for that?  In the end I want a complete set of 15600 data points.
Comment: I'm not an expert in the topic, but I think some more details about the nature of your data could be necessary to peek proper way of imputation.
Answer: For each of your 390 series you have 40 readings. Simply automatically identify an ARIMA Model for each series enabling Intervention Detection to provide estimates of the missing values. If there are 3 missing values the software will identify three Pulses which will yield an estimate if the missing values. The problem is that SAS assumes an ARIMA MODEL first and then estimates the Pulses. In truth the identified ARIMA Model may be flawed by the missing values. An alternative procedure which we have used is to identify the missing values first and then iterate to the ARIMA Model. The whole idea is to run BOTH PROCEDURES to determine which one is optimal for each of your 390 series.
Comment: Regarding the nature of the data: The data come from crop storage facilities where  the annual harvest causes a cyclic spike in December with usage during the year till the next harvest. Observation are quarterly but the cycles are annual.
Answer: You can use proc expand to impute missing values, but I'm not sure if it can do what you're asking in Part 2.
Comment: It seems that the cubic spline used in proc expand is inadequate due to the Jagged nature of the cyclic pattern in the data.  With December observations revealing the peak storage levels after harvest, the September observations are usually the low point with steady usage occurring throughout the year.


Eclipse content assist not working in some parts of code

Question: I am using Eclipse Juno and have never had any problems with it, until its content assist stopped working only in some parts of my code. The code below shows what I am talking about:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        mWTBatch.setText(mRTBatch.getText());
        mWTExp.setText(mRTExp.getText());
        mWTName.setText(mRTName.getText());
        mWTQuantity.setText(mRTQuantity.getText());
        /* Here content assist is not working */
    }
});
mWTBatch.setText("Here it is working again");

That piece of code is just inside one method of my class. The problem is that it works fine inside the methods of the class, but inside objects (new ActionListener(){}) it returns "No default proposals". It has this behavior when I either use "Ctrl+space" or type in "." after the object I want to get suggestions of. I've already searched here for the solution and googled it, but couldn't find a solution. What I have already tried:

Window->Preferences->Editor->Content Assist->Advanced and checked Java Proposals. It didn't work. I've even tried checking it myself, without just using Restore Defaults. I also tried the Java Proposals from the other table. Nothing. At last I tried checking all the fields with Java on them. Nothing worked.
Deleted my workspace folder completely, created a new one and imported my project. As my project is from an SVN repository, I used the project folder inside "trunk" folder, but I didn't copy the content of the folder to the workspace when importing (I need it to be in the repository). Still nothing, it continues with the error.
I reproduced the same situation of the code above into another class of another different project, which was also imported to the workspace, but having its content copied to it. The content assist worked for that one.
I then re-imported the project with the original code from above, but this time copying its content to workspace (which means it had nothing to do anymore with the SVN repository). It didn't work either.
I also checked if the ctrl+space is bind to the content assist on Eclipse settings and it is ok. I also checked if it had nothing to do with advanced key settings of Windows 7 language bar. I had seen on the internet that could be problem, but it was not my case.
I even created a new Java project on the workspace and copied the .java files one by one from the old project, set all the build path manually and in the end it still doesn't work.

For all the first four cases above I also cleaned the projects and closed and reopened them, not forgetting to refresh it after each action. Could anyone tell me how to fix this? I would like to find a solution to this problem, so that others may not be like me on it for 2 days. I think my question is important because I am gathering here many probable solutions found by googling the problem and from StackOverflow, and nothing solved it.
Comment: Same problem. Can't figure this out. One day it works, other day it is dead. Off to log a comment on www.ihateeclipse.com
Comment: I've seen content-assist fail in some cases when the code had syntax errors.
Comment: I can't believe there still isn't a single answer to this question basically on the entire internet in 2022, so many people have asked on so many different sites and there is still not one single logical explanation on the entire internet to what causes this behavior and how to fix it
Answer: I stumbled into this issue the other day, and after seeing this post thought that I'd have to just live with it.
However, I did find a "hack" around it:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(   //<---------------------- Problem arises because we're inside a function declaration ...
    new ActionListener()
    {   // <--------------------------------------------- ... yet we're trying to write a function
        public void actionPerformed(ActionEvent e)
        {
            /* Here content assist is not working */
        }
    }
);
mWTBatch.setText("Here it is working again");

From the content assistants point of view, this is just plain wrong, so we need to give it a little help:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(
    new ActionListener()
        // <--------------------------------------------- CURLY BRACKET MISSING
        public void actionPerformed(ActionEvent e)
        {
            /* Here content assist IS WORKING */
        }
    }
);
mWTBatch.setText("Here it is still working");

This will obviously give you an error upon compilation time, but it gives you full access to the content assist for the rest of the function declaration.
Also, it doesn't matter which curly bracket you remove from inside the function declaration, so long as it's an opening curly bracket.
One other point, if you only remove the opening curly bracket like I did above, then eclipse will NOT add in another closing curly bracket automatically for most cases (because by the time you enter the new opening curly bracket, you've equalised the opening vr closing of curly brackets). You can get around this by deleting the closing curly bracket as well, but then you have to remember to put two curly brackets back.
Hope that helps the 1165 views this question has garnered over the past year =)
Answer: Do a search for "Change display language" on the start menu. 'Change keyboards' > Look to see if you have any non English keyboards under 'General' > 'Installed services'. If you have more than just English then pressing Ctrl+Spacebar is changing your focus from Eclipse to the language selector on the taskbar. Remove any other keyboard languages from the list if that is your problem.


GIT repository over LAN

Question: I am trying to setup a GIT repository over LAN in Ubuntu OS.
I am able to setup a GIT repository, but not sure HOW TO expose the repository to other users in LAN.
As it requires a server, I installed OpenSSH-Server. But don't know HOW TO configure it.
Please point me to correct resource to read from.
Thanks.
Answer: First of all, you need to check your openssh setup on Ubuntu server: see this HowTo.
Then you can follow this article, which mainly recommend:
$ sudo apt-get install python-setuptools
$ mkdir ~/src
$ cd ~/src
$ git clone git://eagain.net/gitosis.git
$ cd gitosis
$ sudo python setup.py install
$ sudo adduser \
  --system \
  --shell /bin/sh \
  --gecos 'git version control' \
  --group
  --disabled-password \
  --home /home/git \
  git

go into your /etc/ssh/ssh_config file and add git to the list of Allowed Users that can login.
copy your id_rsa.pub file over to your server somewhere (in our example we're using /tmp) and then run this command:
 $ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
     Initialized empty Git repository in ./
 $ sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

From your local machine, test it out with this:
 git clone git@YOUR_SERVER:gitosis-admin.git

Configure gitosis for a new project. Use your favorite editor to create a new block under the gitosis one. It should look like this:
[group myrailsapp]
members = myNameAsInTheRsa.pub
writable = myNewApp

A couple of things to watch out in the above block.
First, make sure your name matches what's in your public key (that is, open your id_rsa.pub file and see that what the name says.
Second, make sure you spell writable correctly!
Once you're done, commit and push the changes up to the server.
$ git commit -a -m "created a new repository!"
$ git push

Comment: Note: if you are having problems with the `eagain.net`, you can use Github.
Answer: Your best way of doing that would be to run a ssh server and limit your users to use git-shell for their login shell. You can install an ssh server properly by either running one of the following commands if it has not already been installed.
sudo apt-get install openssh-server

or
sudo apt-get install dropbear

Then add /usr/bin/git-shell to /etc/shells by doing
sudo echo $(which git-shell) >> /etc/shells

Then use the git-shell instead of bash as your user shell. This will restrict the user to only being able to perform git operations when logging into the server instead of providing the user with a full she..

GIT repository over LAN


Linear regression on aggregate and missing data

Question: I have data from a survey with $m$ quantitative questions (rating 0 to 100). There was $n$ participants. I want to apply linear regression to predict $X_m$ based on $X_1,\dots,X_{m-1}$.
There are, however, two problems:

Some participants did not answer some questions.
I have only aggregated data. Each aggregation is the average rating per a group of size $n_i$ where $n=\sum n_i$.

Thus, my data are like this:

$n_{1,i}\dots n_{m,i}$ where $n_{j,i}\leq n_i$ is the number of respondents who did answer the $j$th question within the $i$th group.
$\bar X_{1,i}\dots\bar X_{m,i}$ for each $i$ are the average ratings per group. Note that some of them are not defined (NaN) because the corresponding $n_{i,j}$ may be zero (no answers to that question).
Answer: It sounds like your design dramatically limits the possible missing data methods you may apply.
If the only data available to you are the averaged responses and the number of responses for each question and group. What you do not know is: exactly how many responses were omitted for each item, what the variability of the mean response was for each item, or any individual level information whatsoever.
In light of this, the best solution is the use a complete case analysis with observations at the subject by item-response level. This is the analysis you would have done had you had complete data. I assume some form of frequency weighted linear regression is what you intend to do. The imbalance in counts will only affect the precision of analyses. You can make a strong case that non-response is more a feature of the questionnaire design than it is of the data analysis. If the tool garners missing data and does not advise how to analyze those data, then any approach developed in this sample would be dependent on the features of that sample and therefore have little generalizability.
Recount the sensitivities to non-response on survey items: recall, invasive, fatigue, lack of clarity in item wording, and so on. In many cases, non-response is very much a subtle response: "Jeez, I can't remember", "How dare you ask me that question", "I already told you that", "Heck if I know". Data analysis can tell you very little of how good a survey is. So it bothers me very little that missing data are a feature of your data analysis, we are already at the point that we must accept the questionnaire, including it's tendency to render non-response among some respondents.


Como Transformo meus resultados em arquivo txt, excel ou word?

Question: tenho um código em Python que faz o seguinte: Extrai vários dados de tabelas em excels distintos e realiza uma regressão linear entre eles. Até aí tudo bem mas como faço meus resultados saírem limpos em um arquivo TXT, excel ou word? Obrigado
Segue meu código simplificado pois há bem mais dados:
import numpy as np
import numpy as np
import pandas as pd
import plotly as py
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.formula.api as smf

# Carrega a taxa de cambio em Dólares por Real
CAMBIO = pd.read_csv("CAMBIO.csv", parse_dates=["DATE"], index_col="DATE", na_values=".")
CAMBIO['USREAL'] = pd.to_numeric(CAMBIO['USREAL'])
CAMBIO['USREAL'] = CAMBIO.USREAL.shift().apply(np.log) - pd.to_numeric(CAMBIO['USREAL']).apply(np.log)
CAMBIO.columns = ["ReturnsCAMBIO"]
CAMBIO = CAMBIO.asfreq('B')
CAMBIO = CAMBIO.reset_index()

# INICIO DA SEQUENCIA de REGRESSOES

#REGRESSAO DE BBAS3
BBAS3 = pd.read_csv("BBAS3.csv", parse_dates=["Date"], index_col="Date",
na_values="null")
BBAS3.drop(["Open", "High", "Low", "Adj Close", "Volume"], axis=1, inplace=True)
BBAS3['Close'] = BBAS3.Close.shift().apply(np.log) -
pd.to_numeric(BBAS3['Close']).apply(np.log)
BBAS3 = BBAS3.interpolate()
BBAS3.columns = ["ReturnsBBAS3"]
BBAS3 = BBAS3.asfreq('B')
BBAS3 = BBAS3.reset_index()
BBAS3 = BBAS3.dropna(axis=0, how='any')

#CONCATENAMOS NA MATRIZ SENSIBILIDADE BBAS3
sensitivityBBAS3 = pd.concat([CAMBIO, BBAS3], axis=1)
sensitivityBBAS3 = sensitivityBBAS3.dropna(axis=0, how='any')

#REALIZAMOS A REGRESSAO COM TODOS FATORES BBAS3
y = sensitivityBBAS3['ReturnsBBAS3']
X = sensitivityBBAS3[["ReturnsCAMBIO"]]
RegressionBBAS3 = sm.OLS(y, X).fit()
print(RegressionBBAS3.summary())

Queria que esse sumamary de RegressionBBAS3 fosse exportado para txt ou excel ou word.
Comment: Mostre a saída de RegressionBBAS3 pra ver como está os dados, enfim com dica (pois não tenho como testar) tente criar um DataFrame com essa saída e depois exportar para Excel usando o Pandas. Exemplo: Criar DataFrame `planilha_final = pd.DataFrame(RegressionBBAS3)` | Salvar em Excel `planilha_final.to_excel('planilha_pandas.xlsx', index=False)` espero que algo disse que falei te ajude!
Answer: Para criar um ficheiro existe a função open():
file = open("file.txt","w")
file.write(RegressionBBAS3.summary())
file.close()


Como centralizar os elementos dentro de uma div?

Question: Estou tentando alinhar os elementos que são três círculos com suas respectivas descrições dentro da minha div ao centro usando a seguinte classe:
.center {
    margin:auto;
}

No entanto, não obtive sucesso desta forma, fica tudo a esquerda. Esses sãos os elementos que eu gostaria de alinhar:

.ligado {
    background: yellow;
    width: 150px;
    height: 150px;
    border-radius: 50%;
}

.desligado {
    background: gray;
    width: 150px;
    height: 150px;
    border-radius: 50%;
}

.center {
    margin:auto;
}

Quarto

Cozinha

Sala de estar

Como eu poderia centralizar os elementos dentro da div?
Answer:

.ligado {
    background: yellow;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin: 0 auto; //adicionado
}

.desligado {
    background: gray;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin: 0 auto; //adicionado
}

.center {
    // modificado
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    flex-direction: column;
    text-align: center;
}

Quarto

Cozinha

Sala de estar

Comment: Resolveu meu problema
Answer: Eu ia descartar essa resposta pois fiquei na dúvida se o
 também tinha que ficar ou não centralizado na página. Mas acabei deixando da mesma forma que na pergunta, espero que seja o que precisa.
Única coisa que mudei no seletor .center foi um fundo bege para destacar o próprio
, usei um display: table e alinhei o texto ao centro text-align: center;.
Aos seletores .ligado e .desligado adicionei margin: auto; para centralizar os elementos.

.ligado {
  background: yellow;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  margin: auto;
}

.desligado {
  background: gray;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  margin: auto;
}

.center {
  background: beige;
  display: table;
  text-align: center;
}

Quarto

Cozinha

Sala de estar

Answer: Vejamos...
Vou expor uma alternativa à resposta do @guilherme-henriques que usa do conceito dos flexbox do CSS 3. Esta alternativa também se alinha mais com seu modo de centralizar.
Antes do CSS 3, era o modo "padrão". Hoje não é mainstream ou cool. Coisas da moda..

Centralizando um

Um
, para ser centralizado, deve ter um tamanho definido em relação a horizontal (largura). Suas margens devem estar configuradas para serem automáticas. Exemplo:

#fundo {

  width: 100%;
  height: 100px;

  background-color: #c33;

  }

#exemplo {

  width: 200px;
  height: 50px;
  margin: auto;

  background-color: #fff;
  text-align: center;

  }

<div> centralizado horizontalmente

Note que isto não centraliza os elementos internos do
. Entretanto, este ainda será referência para os elementos internos a ele - incluindo outros elementos
. Assim:

Os elementos internos de um
 pai não são centralizados ao centralizar o
 pai. Precisam ser centralizados independentemente seguindo as regras já expostas: tamanho definido e margem automática.
Para textos e imagens, você deve usar o alinhamento de texto (text-align: center).

O tamanho de um
 pai deve ser maior que o dos elementos internos para que se possa observar o fenômeno da centralização horizontal. Se forem de tamanhos iguais (e.g width: 100%; em elementos pai e filho), a centralização ocorre porém sem que se possa observar visualmente. Estarão sobrepostos.
Veja o exemplo a seguir. Note que diferenciei as alturas para que se se observe a centralização e a sobreposição.

#fundo {

  height: 100px;

  background-color: #c33;

  }

#exemplo {

  height: 50px;
  margin: auto;

  background-color: #fff;
  text-align: center;

  }

<div> centralizado horizontalmente

Na maioria dos casos da vida real, um programador sem experiência (ou mesmo sem base teórica forte) poderia supor que nada ocorreu. Fundos idênticos quanto a cor e alturas idênticas corroboram para a premissa errada. É um erro comum.
Assim, considere utilizar margin: auto; em todos os elementos
 que deseja centralizar dentro do elemento pai. Pode ser individualmente:
.desligado {
    ...
    margin: auto;
}

.ligado {
    ...
    margin: auto;
}

... Ou geral:
.center > div { // ">" indica elementos-filho (descendente direto)
    margin: auto;
}

Exemplo funcional
Segue seu código de forma funcional. Repare que .center foi usado apenas para florear e tornar visual a observância do fenômeno.

.ligado {
  background: yellow;
  width: 150px;
  height: 150px;
  border-radius: 50%;
}

.desligado {
  background: gray;
  width: 150px;
  height: 150px;
  border-radius: 50%;
}

.center {
  text-align: center;     /* centraliza o texto */
  background-color: #eee; /* fornece cor ao fundo para melhor visualizar */
  padding: 2em;           /* 'desgrudado' das bordas do div-pai */
}

.center > div { /* centraliza os elementos 'div' dentro do 'div.center' */
  margin: auto;
}

Quarto

Cozinha

Sala de estar

Answer: Tanto o elemento div quanto o elemento p são elementos de bloco, logo colocando margin: auto na div pai .center não irá alinhá-los ao centro. É preciso usar um seletor que irá alinhar tudo ao centro:
.center > *{
    margin: auto;
    text-align: center;
}

O código acima irá aplicar margin: auto a todos os elementos de bloco com largura fixa, centralizando-os, e também os seus textos internos.

.ligado {
    background: yellow;
    width: 150px;
    height: 150px;
    border-radius: 50%;
}

.desligado {
    background: gray;
    width: 150px;
    height: 150px;
    border-radius: 50%;
}

.center > * {
    margin: auto;
    text-align: center;
}

Quarto

Cozinha

Sala de estar


TLS-PSK vs TLS-PKI

Question: I have read that once TLS-PSK encryption is about equally secure as TLS-PKI. The level of security by both depends on the data entered to configure the encryption. Could you please confirm this?
Here are the points I am interested in:

What is the difference between TLS-PKI and TLS-PSK?
Which encryption standard is more secure and why?
If I wish to protect my access to the servers, will TLS-PSK suffice? TLS-PSK is significantly faster to configure.

Thank you.
Answer:
PSK you give the key to someone first
PKI you request the key after receiving something
PSK is used more for things like access
PKI is used more for things like authentication

past that your level of security is dependent on your encryption algorithm.
Comment: Hi Jacob, thank you for this explanation. In other words, if I configure secure connection just for me, I do not have to strive to have PKI. PSK should work just fine. After the communication is established, possible intruder will have the same difficulty penetrating the encryption.
Comment: Yes, just like if you and your home router want to talk, PSK is perfect, but distributing that key to 100s of employees is why PSK fails, key rotation is also a factor.

TLS-PSK vs TLS-PKI


Why is Rank() OVER PARTITION BY returning too many results

Question: I want the results of my query to be the top 3 newest, distinct Campaign Names for each Campaign Type.
My query at the moment is:
DECLARE @currentRecord varchar(160);
SET @currentRecord = '316827D2-B522-E811-816A-0050569FE3BD';

SELECT DISTINCT
       rs.CampaignName,
       rs.CampaignType,
       rs.receivedon,
       rs.Rank
FROM
    (SELECT
         fs_retentioncontact,
         receivedon,
         regardingobjectidname AS CampaignName,
         fs_campaignresponsetypename AS CampaignType,
         RANK() OVER (PARTITION BY fs_campaignresponsetypename, regardingobjectidname
                      ORDER BY receivedon DESC) AS Rank
     FROM
         dbo.FilteredCampaignResponse) rs
INNER JOIN
    dbo.FilteredContact ON rs.fs_retentioncontact = dbo.FilteredContact.contactid
WHERE
    (dbo.FilteredContact.parentcustomerid IN (@currentRecord))
    AND Rank <= 3
ORDER BY
    CampaignType, receivedon DESC;

There may be multiple results for each campaign name as well as campaign response because they are linked to individual contacts but I only want to see the 3 latest unique campaigns for each campaign type.
My query is not partitioning by each individual campaign response type (there are 6 different ones) as I was expecting.  If I remove the regardingobjectidname from the PARTITION BY I only get a single row in the results when I should be getting 18 rows. This particular company has over 700 campaign responses across the 6 campaign types.
My query is returning 102 rows so it seems to be removing duplicates on campaign name which is part of what I need but not the whole story.
I have read quite a few posts regarding rank() on here e.g.
 how-to-use-rank-in-sql-server
[ using-sql-rank-for-overall-rank-and-rank-within-a-group]2
but I am not able to work out what I am doing wrong from their examples. Could it be the positioning of the 'receivedon' in the ORDER BY? or something else?
Comment: FYI, SQL Server 2008 has been out of support for well over a year now, and you should really be looking into upgrade paths ASAP. Also why use `IN (@currentRecord)`? `@currentRecord` is a scalar value so you might as well use the `=` operator there.
Comment: It's almost always an error to repeat a column/expression in the `ORDER BY` that appears in the `PARTITION BY` - by **definition** you know that all rows within each partition have the *same value* for that column/expression. It thus provides no assistance in assigning an ordering to those rows.
Comment: @Larnu - my query is not about the IN(@currentRecord) as that works perfectly well for the reason that it is there.  It is part of a much more complicated query and does a good job that the equals would not do.
Comment: Thank you @Damien_The_Unbeliever for offering help.  I have adjusted the query (and in the post above) but I am still not getting what I need.  Is Rank() the correct expression to be using to get the top 3 of each group?
Answer: I have finally worked out from reading a post on another site how to get the top 3 of each group.  I shall post my answer in case it helps anyone else.
I had to use ROW_NUMBER() OVER (PARTITION BY instead of RANK() OVER (PARTITION BY and I also moved the INNER JOIN and WHERE clause (to filter for the correct company) from the outer query to the inner query.
DECLARE @currentRecord varchar(160)
SET @currentRecord='316827D2-B522-E811-816A-0050569FE3BD'
SELECT distinct rs.CampaignName
                ,rs.CampaignType
                , rs.receivedon
                ,RowNum
FROM(
        SELECT fs_retentioncontact
        , receivedon
        , regardingobjectidname AS CampaignName
        ,fs_campaignresponsetypename as CampaignType
        ,ROW_NUMBER() OVER (PARTITION BY fs_campaignresponsetypename ORDER BY fs_campaignresponsetypename, receivedon DESC) AS RowNum
        FROM FilteredCampaignResponse
        INNER JOIN dbo.FilteredContact ON fs_retentioncontact = dbo.FilteredContact.contactid
        WHERE(dbo.FilteredContact.parentcustomerid IN (@currentRecord)))rs

WHERE RowNum <=3
ORDER BY CampaignType,receivedon DESC;


how to minimize the probability of error in a Bayes decision rule

Question: According to the Bayes decision rule for a 2 class classification problem:
$d(x) = w_1 : P(w_1 |x) ≥ P(w_2|x) $
And $P(error|x) = min[P(w_1 |x), P(w_2|x)]$
where $P(w_i |x) = p(x|w_i) * P(w_i)$

By simply following this decision rule $d(x)$, we minimize the probability of error? There is no complicated way or additional formulas for minimizing error?
In case we want to minimize this error further how do we do it? Increase the number of samples perhaps? Or maybe increase the dimension of feature space i.e., use more features, thereby changing the likelihood $p(x|w_i)$, or combination of both.
Answer: Given some underlying model for the data, you cannot do better than the Bayes optimal rule (hence the "optimal").  It's actually fairly easy to see.  Suppose you have an observation $x$ and want to classify it into one of $k$ groups.  Then whatever your decision might be, if you are assigning it to a class other than the most likely one, you will be wrong more often than the Bayes classifier.  Simply collecting more data or something else can't change this.


Axiom of Choice in Constructive Mathematics. Is it really an axiom?

Question: The status of the Axiom of Choice varies between different branches of Constructive Mathematics.
For example, in Constructive Set Theory the Axiom of Choice is not accepted because it implies the law of excluded middle, but in Intuitionistic Type Theory the Axiom of Choice is a theorem that can be proved.
Are there other branches of Constructive Mathematics in which the Axiom of Choice is really an axiom (I mean, it's assumed true but not provable)?
Comment: @Peter "[we] can basically not do any reasonable math anymore." is an absolutely false statement; there is a very rich theory of intuitionistic logic and type theory in which LEM is not taken as given
Comment: @Peter Even if the situation with modern constructive math were as simple as Brouwer-style denial of LEM, it would still be pretty reactionary to call it "not math".
Comment: @Peter You are conflating "not accepting" with "denying". At any rate, even if you accept excluded middle philosophically, it can sometimes be useful to do a proof which relies on a principle which contradicts LEM if you need to work internally in some topos where this anti-classical axiom holds.
Comment: @Shaun Oh yes, thank you for point out this mistake !
Comment: The axiom of choice is known to be indepent of ZF , that means ZFC can neither prove nor disprove it (assuming ZF is consistent). Not accepting the law of the excluded middle means to go back in math for centuries. We lose many many extremely important results and can basically not do any reasonable math anymore. I do not consider math denying the law of the excluded middle math anymore.
Comment: @Peter In algebraic geometry, there are some objects that behaves extremely like ordinary sets, except that if you really follow this analogy, the corresponding statement of excluded middle does not hold. If you prove a theorem about sets without excluded middle, you can now immediately translate this and obtain a theorem about these complicated objects in algebraic geometry for free. Note that this happens even if you accept excluded middle and choice. Does that expel this part of algebraic geometry from maths?
Comment: You mean ZF, right, @Peter?
Comment: Are you alluding to topoi, @Trebor?
Answer: The version of the axiom of choice probable in intuitionist type theory bears only a formal resemblance to what set theorists think of as the axiom of choice. The issue is that one can't really interpret the existential quantifier $\exists$ as $\sum$ in many cases when doing ordinary mathematics. $\DeclareMathOperator{refl}{refl}$
For instance, consider $\{x \in \{1\} \mid \exists y \in \mathbb{Z} (y^2 = x)\}$. How many elements does this set/type have?
In set theory, the answer should clearly be 1. But in type theory, if we interpret the above as $\sum (x : \{1\}) \sum (y : \mathbb{Z}) (y^2 = x)$, then the type has two distinct elements, $(1, (1, \refl_1))$ and $(1, (-1, \refl_1))$. This is why propositional truncation types are necessary.
The same thing happens in a much more dramatic fashion when one tries to develop, for instance, the Dedekind reals.
The solution is to use propositional truncation or, more generally, to allow quotient types (or other forms of higher inductive types). Then, one can interpret $\exists$ in a manner consistent with ordinary mathematics. When one does so, the axiom of choice is not provable.
That said, many versions of constructive mathematics take weak versions of choice as axioms, including countable choice, dependent choice, and COSHEP (category of sets has enough projectives).
Comment: I believe Bishop's constructive analysis uses countable choice. Also, the effective topos validates countable choice among other things, so doing constructive mathematics in that setting (basically the ordinary computability interpretation) has it, too. The full axiom of choice implies excluded middle, so you can't assume it without being non-constructive.
Comment: Thank you for your interesting explanation about axiom of choice in intuitionistic type theory! You said that many versions of constructive mathematics take weak versions of choice. Can you tell me an example of a version of constructive mathematics that take countable choice as an axiom? Does it exists a version of constructive mathematics that take the full axiom of choice as an axiom?


Update existing content using migrate module

Question: I'm using migrate module in Drupal 7 to update existing content of a Drupal site. My source and destination tables are the same. I got some documentation from http://drupal.org/node/1117454. Here's my code:
<?php
class MTestMigration extends Migration {

  public function __construct() {
    parent::__construct();
    $this->systemOfRecord = Migration::DESTINATION;
    $this->team = array(
      new MigrateTeamMember('foo', 'email@example.com', t('Site builder')),
    );

    $this->description = t('Migrate basic slides');

    $source_fields = array(
      'nid' => t('The node ID of the slide'),
      'title' => t('The node title of the slide'),
      'uri' => t('the slide image'),
    );

    $q = db_select('node', 'n');
    $q->leftjoin('field_data_field_image', 'i', 'i.entity_id=n.nid');
    $q->leftjoin('file_managed', 'f', 'i.field_image_fid=f.fid');
    $q->fields('n', array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'sticky', 'tnid', 'translate'))
      ->fields('f', array('uri'))
      ->condition('n.type', 'slide', '=');
    $this->source = new MigrateSourceSQL($q, $source_fields);

    $node_options = MigrateDestinationNode::options(LANGUAGE_NONE, 'filtered_html');
    $this->destination = new MigrateDestinationNode('slide', $node_options);

    $this->map = new MigrateSQLMap($this->machineName,
        array(
          'nid' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'description' => 'Unique Node ID',
            'alias' => 'n',
          )
        ),
        MigrateDestinationNode::getKeySchema()
    );

    $this->addFieldMapping('is_new')
      ->defaultValue(TRUE);

    $this->addFieldMapping('nid', 'nid');
    $this->addFieldMapping('title', 'title');
    $this->addFieldMapping('is_new')->defaultValue(TRUE);
    $this->addFieldMapping('uid', 'uid');
    $this->addFieldMapping('revision')->defaultValue(TRUE);
    $this->addFieldMapping('revision_uid', 'uid');
    $this->addFieldMapping('created', 'created');
    $this->addFieldMapping('changed', 'changed');
    $this->addFieldMapping('status', 'status');
    $this->addFieldMapping('promote', 'promote');
    $this->addFieldMapping('sticky', 'sticky');
    $this->addFieldMapping('comment', 'comment');
    $this->addFieldMapping('language')->defaultValue('und');

    $this->addFieldMapping('path')->issueGroup(t('DNM'));
    $this->addFieldMapping('pathauto_perform_alias')->defaultValue('1');

    $this->addFieldMapping(NULL, 'name');
    $this->addFieldMapping(NULL, 'vid');
    $this->addFieldMapping(NULL, 'type');
    $this->addFieldMapping(NULL, 'language');
    $this->addFieldMapping(NULL, 'tnid');
    $this->addFieldMapping(NULL, 'translate');

    $this->addFieldMapping('field_image', 'uri')
      ->arguments(FILE_EXISTS_RENAME);
  }
}

I end up with this error:

Column not found: 1054 Unknown column 'map.destid1' in 'field list':
  SELECT n.nid AS nid, n.vid AS vid, n.type AS type, n.language AS
  language, n.title AS title, n.uid AS uid, n.status AS status,
  n.created AS created, n.changed AS changed, n.comment AS comment,
  n.promote AS promote, n.sticky AS sticky, n.tnid AS tnid, n.translate
  AS translate, f.uri AS uri, map.destid1 AS migrate_map_destid1,
  map.needs_update AS needs_update FROM {node} n LEFT OUTER JOIN
  {field_data_field_image} i ON i.entity_id=n.nid LEFT OUTER JOIN
  {file_managed} f ON i.field_image_fid=f.fid LEFT OUTER JOIN
  {migrate_map_mtest} map ON n.nid = map.sourceid1 WHERE (n.type =
  :db_condition_placeholder_0) AND (map.sourceid1 IS NULL OR
  map.needs_update = 1) ; Array ( [:db_condition_placeholder_0] => slide
  ) in MigrateSourceSQL->rewind()
Comment: Did you clear caches before you ran? Have you changed the destination or the key at all? Is Migrate the best option for this?
Answer: This post about Unknown column 'destid1' in 'field list' helped me when I had the same problem.
In particular, drop the migrate_map_* and migrate_message_* tables since they may have been created incorrectly beforehand.
Comment: Also if you want to do `Updates` to content in Migrate > 2.5 (for example Migrate 2.6) you can supply `$options['track_changes'] = TRUE;` for any `MigrateSource` class. More about that here: http://timonweb.com/using-hash-value-trackchanges-to-detect-source-data-changes-in-migrate-for-drupal-7
Comment: The link in the previous comment moved to https://timonweb.com/posts/using-hash-value-track_changes-to-detect-source-data-changes-in-migrate-for-drupal-7/


Import SSL Cert into IIS when I have the text of the certificate and private key


Rotas Angular-JS

Question: boa noite! Sou novo na tecnologia de single page application, e estou encontrando dificuldades para renderizar minhas páginas no Angular um, eu possuo um botão que ao clicar, dispara uma rota, a mesma tenta ser carregada, mas não aparece na página, já pequisei e tentei de tudo, não sei onde estou errando
Meu modulo:
var modulo = angular.module("clienteCtrl", ["ngRoute"]);

Minha rota:
    modulo.config(function($routeProvider){
  $routeProvider

  .when("/", {
        templateUrl: "view/formCadastrarCliente.html"
      })

  .when("/cadastrarCliente", {
    templateUrl: "view/formCadastrarCliente.html"
  });

});

Meu HTML:
    <!DOCTYPE html>

<meta charset="UTF-8">
<title>Bem - Vindo</title>

<link rel="stylesheet" href="css/application.css">

Cadastrar cliente

Ao clicar no link, a url fica assim: http://localhost:8080/POC_Cast/index.html#!#%2FcadastrarCliente
Mas a página não carrega o conteúdo.
Obrigado desde já!
Comment: @Sorack eu não manjo, só lembro dos tropeços. ;)
Comment: Você já tentou usar assim: `Cadastrar cliente`
Comment: @Sorack, já sim! Mas fazendo isso, ao clicar no link, o navegador exibe o erro 404. O mais estranho, é que do jeito que eu postei, ele tenta renderizar o conteúdo, mas não aparece nada na página.
Comment: Veja bem, a URL tá esquisita... tenta `./#/cadastrarCliente` OU `./cadastrarCliente`
Comment: @Sorack, Então, eu editei a minha pergunta com a minha nova rota, no .config... Se eu colocar para carregar o template no .when("/"), ele é carregado, logo que o documento html carrega no navegador... mas eu não entendi, na verdade me confundiu mais, eu queria que renderizasse apenas quando eu clicasse no link... ;(
Comment: Viu, tava revendo uns exemplos... mude o `href` para `#!/cadastrarCliente`
Comment: @Sorack, obrigado pela ajuda, de verdade! Era o erro que o amigo abaixo comentou, bem difícil mesmo de descobrir, mesmo pesquisando, não havia encontrado nada relativo! Mais uma vez eu agradeço e uma boa noite! xD
Comment: Opa tranquilo hehehe o @OnoSendai manja bastante de Angular mesmo, pode confiar
Answer: Modifique
modulo.config(function($routeProvider){
    $routeProvider

para
modulo.config(function($routeProvider, $locationProvider) {

        $locationProvider.hashPrefix('');

        $routeProvider

Seu erro se dá por uma mudança do comportamento padrão do angular 1.6.
Comment: Obrigado, @OnoSendai! Era isso, eu não sabia dessa novidade ... na verdade, preferia o outro comportamento padrão do angular.
Comment: @MatheusMinguini Sempre um prazer ajudar! Eu passei pelo mesmo problema a umas semanas atrás, por isso que lembrei da causa. =)
Comment: obrigado mais uma vez! :D

Rotas Angular-JS


Jekyll Front Matter custom fields in _posts .md (markdown) not available in liquid tag

Question: I am aware you can add and use html page custom front matter fields using the {{ page.myField }} directive but this does not work for posts (i.e. when iterating site.posts in a for loop.
My problem, what I have done ...

I created a new post in _posts with a .md extension containing new custom front matter fields (it is published and refixed correctly with date) - Seems correct
I loop all the posts using  {% for post in site.posts %} - This works
When I attempt to use these using a liquid tag {{ post.myNewField }} it is not available but does show in post.content as text not a useable field.

Surely this cannot be correct.  I would assume anything in the markdown in the front matter section should be accessible, as it seems to say so in the Jekyll front matter instruction
http://jekyllrb.com/docs/frontmatter/#custom-variables
Please help.
Comment: And your post is starting with a yaml-header, containing the variables, enclosed with three dashes (---)?
Comment: Not really easy to reproduce. Where did 3. happends ?   Did you use `{{ post.myNewField }}` in the post itself, in `layouts/post.html` ?   Maybe you have a github repository url to submit.
Comment: yes posts start with '---' yaml-header.  3 happens in another index.html file, the for loop is inside this html file.  github repo is private.
Comment: I am thinking this link may help me http://wolfslittlestore.be/2013/10/rendering-markdown-in-jekyll/
Answer: cause of my issue was UTF-8-DOM encoding when it should have just been UTF-8 encoding for .md files


Managing subcomponents that are not in DOM subtree

Question: Consider a component which needs to manage subcomponents that are not children in its own DOM tree, but must be added to the top-level document.
A typical example would be an autocompletion field that needs to show autocompletion matches in a floating menu below the input field. The floating menu necessarily needs to be added as a child of the document body element to escape any "overflow: hidden" constaints in the tree that would prevent it from showing. The floating menu needs to be removed after it's no longer used.
In such a case, the logical approach seems to be to mount a component in an arbitrary div, and later unmount it when it's no longer needed. However, this introduces an interesting state-flow problem when events are used to trigger such an unmount.
Here is an excerpt from my current code, to illustrate the problem:
componentDidUpdate: function(prevProps, prevState) {
  if (prevState.matches !== this.state.matches) {
    if (this._floater) {
      this._floater.remove();
      this._floater = null;
    }

    if (this.state.matches.length > 0) {
      this._floater = Floater.create(
        <Floater
          parentElement={this.getDOMNode()}
          open={true}>
          <SelectableList
            items={this.state.matches}
            limit={10}
            onSelectionChange={this.handleSelectionChange}/>
        </Floater>
      );
    }
  }
},

handleSelectionChange: function(items) {
  this.setState({matches: [], selectedItem: items[0]});
},

Here, Floater is a generic component that can contain any other component; it sets itself to absolute, positions itself and so on. Floater.create() is a convenience method to create a floater component and insert it into the document.
Floater.remove() currently looks like this:
remove: function() {
  var self = this;
  if (this.isMounted()) {
    window.setTimeout(function() {
      React.unmountComponentAtNode(self.getDOMNode().parentNode);
    }, 10);
  }
},

The reason it's using a timeout is to allow the parent component to be able to remote it after a state update. Selecting something in the SelectableList would trigger a handleSelectionChange in the parent, which would call remove() to unmount the component while it's still being used. It's ugly, although it does work.
Is there a better, more idiomatic way?
Comment: Pete Hunt came up with a `ReactLayeredComponentMixin` that moves a Modal, similar to your Float, into a Mixin and exposes a `renderLayer` function on components that use it to let components effectively render to two separate containers: http://jsfiddle.net/LBAr8/ Since it's a Mixin and uses life cycle methods, you might call it more idiomatic.
Answer: Just a head-up for visitors to this question.
As of React v16, there is a specific feature to handle this very case.
It is called Portal

Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.


WSL2 Nginx - Works on localhost but throws 403 Forbidden on 127.0.0.1 or custom host name

Question: I've been trying to setup nginx on my WSL2 running Ubuntu 20.04. I got pretty much the entire site setup. It's working when I visit http://localhost. But I want it to be on http://app.test. So I added it to the host and tried, it throws 403 Forbidden error. I've not changed anything in nginx.conf. This is what I have so far:
Server Block
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/app.test;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

Permissions
The /var/www/html/app.test folder belongs to www-data:www-data user and group.
Hosts
I've added the following in both my Windows Hosts and on Ubuntu Hosts
127.0.0.1 app.test

Findings

I checked the access.log. I've included 2 of the logs below. The first one is a non-working one (when visiting app.test) and the other is a working one (when visiting localhost).

127.0.0.1 - - [05/Nov/2020:00:07:09 +0400] "GET / HTTP/1.0" 403 564 "-" "Mozilla/5.0 (Windows NT 10.0;..."

::1 - - [05/Nov/2020:00:01:13 +0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0;...

I tried running a curl command from Windows Terminal, I get 200 OK for http://localhost but 403 Forbidden for http://127.0.0.1

Thanks!
Comment: Finding 1: no, both of those requests returned status 403. Finding 2: curious (i.e., impossible), but I'm not sure how it relates to "app.test". Overall, given your setup, the request for ```http://app.test``` should result in serving ```/var/www/html/app.test/index.html```. The URL has no path component, so you're requesting ```/``` (as ```GET /``` in the access log confirms), and the index directive maps the request for a directory to a request for the index.* file(s) you specified. Just take your time and re-read the documentation. It's quite logical.
Comment: @Buster I made a mistake with finding 1, I've edited the question now. I'm guessing it's a problem with the hosts because I'm working with WSL2
Answer: This is the solution for me.
Just add:
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 app.test
::1 app.test

WSLUbuntu:/etc/hosts
127.0.0.1 app.test


I don't get the same graph, before and after solving for Y?

Question: Now, if I draw the following:
$3x+y=3$
$2x^2-y^2=-1$
With Wolfram I get the following graph.

But if I draw the following functions that I have solved for Y
$y=\sqrt{1+2x^2}$
$y=3-3x$
I seem to be loosing some information

Is all this simply a consequence of the following math:
$y^2=1+2x^2$
$y=\sqrt{1+2x^2}$
Squaring could cause this, but is that what I have done here?
The reason for my post is that when doing the calculations by hand I still find the two intersections. But it is not visible in the graphs of the very same functions that on paper gives me what I want.
Comment: You're right. To complete things, plot the *negative* square root as well.
Comment: Note, for example, that $(0,-1)$ is on the graph of $y^2=1+2x^2$ but not on the graph of $y=\sqrt{1+2x^2}$. When you say you get both intersections by hand, surely that's because somewhere along the way you "square both sides" thereby re-introducing the other branch of the hyperbola.
Comment: Remember that $\sqrt{y^2} = |y|$, not $y$. So your first function should not be $y=\sqrt{1+2x^2}$, but rather $|y|=\sqrt{1+2x^2}$.
Answer: The "missing information" comes from improperly taking the square root. The actual equation B, solved for y, is: $$ y \ =\ \pm\ \sqrt{1 \ +\ 2x \ ^2\ } $$ This will give you the second part of the graph that you were previously missing.


Palavras cruzadas vertical

Question: Problema: Escreve uma função chamada find_word_vertical que aceita uma lista de 2 dimensões de caracteres e, uma string como argumentos de input. Esta função procura nas colunas da lista de duas dimensões para encontrar uma palavra igual à palavra de input. Se uma igualdade é encontrada, a função retorna uma lista contendo o index da linha e o index da coluna em que o principio da palavra encontrada começa, caso contrário retorna o valor None.
Por exemplo, com os seguintes inputs:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'
find_word_vertical(crosswords,word)

Então a função deve retornar:
[1,0]

A minha solução:
def find_word_vertical(crosswords,word):
    num = len(crosswords)
    joinner = ''
    w = str()
    vert_cross = list()
    for i in range(num):
        w = joinner.join(vert_cross)
        if word in w:
            for let in w:
                if let == word[0]:
                    row_ind = w.find(let)
                    col_ind = i-1
                    return [row_ind, col_ind]
        for j in range(num):
            vert_cross.append(crosswords[j][i])

A saída dá-me None, mas a resposta correta seria [0, 1]
O que é que está mal no meu código?
Answer: Seu código está funcionando para a palavra 'cat', mas não funcionará caso a palavra procura esteja na última coluna ('mkt', por exemplo) porque sua comparação está sendo feita na iteração seguinte da coluna (a primeira apenas appenda o conteúdo)
Segue uma sugestão de solução:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'

def find_word_vertical(cw,w):
    for i in range(len(cw)):
        wstr = ''.join([cw[j][i] for j in range(len(cw))])
        if wstr.find(w) != -1:
            return (i,wstr.find(w))
    return (-1,-1)

print find_word_vertical(crosswords,word)

Aproveitando, como dica, existe a lib pprint que para esse caso ajuda a visualização no interpretador:
>>> import pprint
>>> pprint.pprint(crosswords)
[['s', 'd', 'o', 'g'],
 ['c', 'u', 'c', 'm'],
 ['a', 'c', 'a', 't'],
 ['t', 'e', 't', 'k']]
>>>

Palavras cruzadas vertical


Filling fields with data on fly

Question: I have some records imported to my module tables. Some of this data is synchronized to drupal nodes by creating and updating existing nodes of specified type. This operation is made each time new import is done. The problem is, it produces around 60 000 nodes for every new import and it is quite a lot. Synchronization takes hours and operations through Views takes ages or crashes.
Is there any way to store my data only in my tables and serve them from this tables as nodes? Some way to only creates nodes with some properties set as language and title, and then provide data for fields directly from my tables?
Answer: If revisions aren't important you might want to fork your website codebase and try installing Field SQL NoRevisions module, by removing the "revisions" of Nodes node creation and fetching takes half the number of normal drupal queries.
Depending on you Read vs Write ratio you also might want to consider moving some of your Fields into a NoSQL database such as MongoDB.
Comment: It is considerable, but what I actually want and need is some kind of way to store and provide values for fields for my node types all by my own module and its tables. This way creating nodes would take lot less time, because only type, title and few more properties would be set. Any way of doing that?
Comment: ... well thats why I said try Field SQL NoRevisions, by not having "revisions", now you just have "title", "body", "whatever" field contents and no revisions.

Node can be costly. Can you do what you want with a custom Entity that sets specific Properties on its table .... an entities properties can be stored on the table the entity is defined. Entities, aren't necessarily nodes though.

Filling fields with data on fly


What is the systemd equivalent of ntsysv?

Question: Now that RHEL 7 uses systemd the ntsysv tool only shows some of the services on my system. Is there a systemd equivalent to ntsysv whch will allow me to show/configure all services on my system?
I am a commandline only user, so I would like a TUI.
Comment: I have created one for systemctl. https://github.com/chunhualiu/sc
Comment: I have edited your question for clarity [based on your comment below](http://serverfault.com/questions/594530/what-is-a-good-ntsysv-replacement-now-rhel-7-uses-systemd#comment703387_594537). I have also removed the "I don't want `systemctl`" restriction -- If there is a specific reason why you cannot use `systemctl` for this please edit your question to *explain your reason for rejecting this tool as an option* - I assume you have a reason, but it's incumbent on *you* to explain it.
Comment: @ChunhuaLiu Very cool! Very nice overview.
Answer: Check the cheat sheet for native commands. But you can still use chkconfig. And ntsysv still shows up in the EL7 package list.
Comment: Problem with chkconfig and ntsysv is that it doesnt show all services anymore. Some are on it, some are not. Its quite confusing when chkconfig/ntsysv shows 3 services and systemctl shows a lot more.

I know how to use systemctl, but what I really want is a ntsysv TUI replacement that shows me the list graphically. Call me lazy, but for me its a must-have tool on my CentOS 6.x servers. I can go through a list of services with arrow buttons, now instead I need to use my superman laser eyes to to avoid mistakes.
Comment: Lazy much? Systems progress, progress with them or be left behind.
Comment: Its not about being lazy or not. Its about usability of a tool. I am looking for something like ntsysv, a TUI so I can go through a list of services "graphically".  Progress is subjective, for me systemd feels like a mess. My first linux distro was slackware 2.1.
Comment: Point taken; I guess this is "Linux way" and I have to live with it. Of course there are many other ways to achieve what I want, just not the way I want to. I do use CHEF for all my servers and all my servers are single purpose, nevertheless I still liked ntsysv. Its a simple overview tool with a simple function. Systemd is like Windows 8 for me, over complicating things that we are very used to do simply; In the name of "progress" and "innovation". Uhuh.
Comment: @laapsaap If you have a need for this, it's a strong indicator that you are trying to do too many things with a system. Ideally each server should be dedicated to a single purpose.
Comment: @MichaelHampton Not necessarily. I have application servers out there that have a full complement of related services. I understand what the OP wants, but really, this is a one-time or infrequent task. If you have configuration management of any sort, you won't even need to go this far.
Answer: I had the same problem.
I use to check the installed services using ntsysv (Red Hat family distros) and rcconf (Debian family distros). However with this new standard I was forced to find another solution. Then I found in the Arch Linux repository a python script to do the check.  I just downloaded the tarball and uncompressed it in /usr/local/bin.  I am using RHEL 7.2 and to me, it seems working fine.
To invoke the program just execute in the command-line: serman2
EDITED:
The new version of ntsysv now is showing systemd services (Red Hat and Cent OS 7), so just invoke ntsysv via command line.
Comment: I just found this in EL7.3 too. I was happy to see it.


Show system message to user when node is published

Question: I'm looking for a way to show a system message to the author of a node, when the node gets published (by an administrator)
I think this must be achievable with Rules, but browsing the web and searching on stackoverflow did not help me out (yet).
This is what I did in Rules so far:
EVENT:After updating existing content of type Event
CONDITIONS: Content is published
ACTIONS: Show a message on the site
Comment: you could create a custom system message set up using Views, Rules, VBO, etc - I have done this for sites in the past. It's nice because it can be adapted to serve any number of scenarios within a site
Comment: Yes, I know that's a possibility but it's too much work for what I need it. On my website, this is almost the only scenario where I want to send my users a message. Though, I'm interested in how you did create your own system message. Feel free to contact me about it!
Comment: nothing should be too much work if it accomplishes the goal you desire. too lengthy for a comment, and this site doesn't lend itself to personal contact - depending the complexity the site needs, it can be done with Flags or a custom content type, and then 2 or 3 rules, 1 view and maybe a small custom module for expediency. It's really not as complicated as you might think
Answer: System messages are displayed only once and for the current user. In your case it would be more appropriate to send an email when the node has been published.
To do so, create a following Rule:

Event: After updating existing content; restrict it by content type if necessary.
Condition: User has role(s), select administrator role and check Negate to make sure this rule does not get triggered for administrators.
Condition: Data comparison, for Data to compare select that node-unchanged:status equals to Unpublished. This is to make sure old node was unpublished.
Condition: Data comparison, for Data to compare select that node:status equals to Published. This is to make sure new node is published.
Action: Send mail and configure it the way you want.

Hope this helps.
Comment: Sure, glad it helped.


SQL Server - Local storage (ssd's) vs SAN

Question: I plan to change the hardware for may SQL Server, and upgrade it to SQL Server 2016 Enterprise. AlwaysOn AG will be built on top of two nodes + dr.
There are two options I have in mind for storage:
have only local disks, ssd's in RAID1, with separate disks for Windows, Data, Logs and TempDb
have a hybrid of local storage with ssd's for TempDb (RAID1) and the rest of the disks for Windows, Data and Logs to be provisioned from a SAN over the network
I personally prefer the option with everything on local storage, because:
you get rid of single point of failure (the SAN)
faster speeds on the local storage, the SAN will not have ssd's
no network bottlenecks
Are there any major disadvantages of using local storage?
Is using SAN for storage a better option?
Regardless of the solution, the hardware will be rented from a hardware provider. So there will be no buying involved.
Thanks in advance!
Comment: There are way too many variables for an easy answer. What type of SAN?  "Single point of failure" on a high-end SAN is quite likely a lot more reliable than even a cluster of commodity x86 servers.  How are backups done - is that integrated into the SAN, or would you have to configure backups either way?  How good are your SAN administrators?  Are they doing things like creating huge 31-drive RAID6 arrays and slicing that one 60+ TB RAID array into tens or hundreds of LUNs - so everyone using the SAN has to fight for disk IO time with everyone else's IO? Then can't figure out why it's S-L-O-W?
Comment: The infrastructure will be hosted by a third party hardware provider. All the maintenance will be done by them. So, I have no idea how good their SAN admins are, or how constant the SAN speed will be.(even if they guarantee a specific speed)
The backup plan is going to be done separately, regardless of whatever backup they do. I am aiming for local storage because there are less "parts" and I'm hoping for a higher speed.
Comment: SANS is single point of failure, IMHO
Comment: Local disks have their points of failure too. Redundant hot swap controllers are standard on storage arrays. You might not need that with a database replicated application.  Its perfectly fine to rely on OS and application availability solutions, but understand that during design.
Answer: With regards to Microsoft's docs you should go with either local storage, or shared storage or Microsoft Storage Spaces Direct.
Assuming you what achieve availability for MSSQL Server, I would suggest you go with local storage, identical setup for each host wich would provide you same performance for the case of node failure.
Go with shared storage if you would go with MSSQL FCI.
As for SAN, its SPoF. IMHO
Answer: As always, there are many advantages and disadvantages to everything.
A storage array provides high capacity and centralized management. You can snapshot some volumes and present them to another host. Although, the switches and cabling to connect the SAN up can be a bit more complicated.
Local storage has fewer server nodes involved and may be more familiar to the OS administrator. There may be limits on capacity that fit in the server, unless you add something like a disk enclosure.
Database level replication makes it relatively easy to use two independent storage systems for a business continuity solution.  A storage level failure then can be mitigated by activating DR.   Single point of failure analysis is still a good idea, checking for redundant power supplies and SAN paths and such.  The DR copy, possibly at a remote site, makes recovery faster if the primary copy becomes unavailable.
Comment: We plan to rent the equipment for a period of max 2 years. So, the size of the storage it is not that hard to predict. Probable we will add more than the estimates from the start, just to be safe.
I was more worried about possible risks for going with everything on local storage. Things that I did not take in consideration and can bite back after a while.
Answer: Personally, I'm seeing SANs becoming a relic of a bygone era. Don't get me wrong - like the mainframe they have their place, and used appropriately they are hugely valuable. Large virtualisation installations wouldn't be possible without them. Once upon a time, shared storage like a SAN was the only way to make things like SQL Server or VMWare ESXi highly available.
But to get a SAN that is as fast as local SSD storage is very expensive. To get one that's as fast as NVMe storage (such as an Intel P3608) is into six figures. Compared to $8,000 for a top of the line 4TiB P3608 - cheaper if you don't need 4TiB of storage.
With SQL Server's Always On providing a good high availability option, with local storage, synchronous or asynchronous replication options, and with Distributed Availability Groups, there is no reason to be deploying a SAN for the vast majority of new SQL Server installations.
There is no downside as to using local storage as far as SQL Server is concerned.
Comment: What about costs? AGs are part of Enterprise only, Standard has BAGs but they are limited (very limited).