Downsampling and Upsampling for Gaussian Image Pyramids in Swift

Question: Introduction
I'm interested in writing a function that outputs for me the next level in a Gaussian Pyramid(I eventually want to get to creating a Laplacian Pyramid) for use in image processing. (Link for reference https://en.wikipedia.org/wiki/Pyramid_(image_processing)#Gaussian_pyramid)
The Downsampling Problem
Now the easy part of this is that when you down/upsample, a 5-tap filter is convoled with the image before resizing.
However, the interesting part about making image pyramids is that you have to downsample and upsample an image by a factor of .5 or 2, depending on which direction you're going. Swift has a few ways of doing this, such as using CIAffineTransform and CILanczosTransform, however I'm wondering if there are ways to do it a bit more naively because I do not care about the quality of the resized image. For this post, I'm going to use Lenna(512x512) as an example, seen below:

If we want to downsample an image by a factor of two, we would take all of the odd numbered pixel data to form a new image. In MATLAB this is performed as follows(after the gaussian blur):
If I is your input image and is NxM in size, with 3 colors mappings stored for P(a 512x512x3 matrix), then the decimated image by a scale of .5 is
R = I(1:2:end, 1:2:end,:)
All the new image is the previous with the odd numbered numbered columns and rows of the image. This yields the following, a 256x256 photo that is the first level of the gaussian pyramid:

does such a thing exist in swift? Is it doable in Core Image, or maybe a OpenGL custom filter?
The Upsampling Problem:
Upsampling is really only used when creating a Laplacian Pyramid. However the naive idea to do this is to do the following:
Initialize R, a blank image context of the size you want to upsample to. In this case we will be upsampling the downsampled Lenna photo as seen above, so R must be a 512x512 blank image.
Next, multiply the pixel values of the downsampled image, I by 4. This can be done in swift by convolving the image with the 3x3 matrix [0,0,0;0,4,0;0,0,0]. Then one can uniformly distribute the pixels of the image into the larger blank image, R. This looks like:

Finally, one can employ the same 5 tap gaussian blur on this image to recover the upsampled image:

I'd like to know if it's possible to employ a similar method of upsampling in swift.
Another thing that I'm unsure of is if it really matters on the technique to resize an image for gaussian/laplacian filtering. If not, then certainly I could just use the fastest built in method than trying to make my own.
Comment: Have you seen this: https://developer.apple.com/reference/metalperformanceshaders/mpsimagegaussianpyramid
Comment: I have, but I really wanted to see if there was an alternative before using custom filters to get what I want. These things are not so uncommon in image processing so I figured there would be a way via apple's built in beforehand.
Comment: I'm not sure if it has all the functions you need, but you could try the `Accelerate Framework` https://developer.apple.com/videos/play/wwdc2013/713/
Answer: The GPUImage processing library can give you some up-sampling and possibly lead to your Laplacian Pyramid.
pod 'GPUImage'
SHARPEN UPSAMPLING:
UIImage *inputImage = [UIImage imageNamed:@"cutelady"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc]initWithImage:inputImage];
GPUImageSharpenFilter *stillImageFilter = [[GPUImageSharpenFilter alloc] init];
[stillImageSource addTarget:stillImageFilter];
[stillImageFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];

LANCZOS UPSAMPLING:
UIImage *inputImage = [UIImage imageNamed:@"cutelady"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageLanczosResamplingFilter *stillImageFilter = [[GPUImageLanczosResamplingFilter alloc] init];
[stillImageSource addTarget:stillImageFilter];
[stillImageFilter useNextFrameForImageCapture];
[stillImageSource processImage];
[stillImageSource forceProcessingAtSizeRespectingAspectRatio:CGSizeMake(200, 200)];
UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];
cell.imageView.image = currentFilteredVideoFrame;

Comment: The thing to note here is that you're using a Lanczos Transform -- a specific kind of resampling method -- to rescale the image. This transform is ideal if you want to preserve as much detail as possible, but that's not quite what I'm asking -- in fact, to create an image pyramid, you almost don't care about preserving detail.
Answer: I have made some progress, and I pretty much consider this an answer to my question, although some things are a tad different and I don't think this method is very fast. I would love to hear from anyone to see how to make this code faster. In the below, it seems like resizing the image is taking up the most time, I get a TON of calls to the ovveride outputImage section and I have no idea why that is. Unfortunately when I do run the Laplacian Pyramid function below, it takes around 5 seconds to complete on a 275x300 photo. This is just no good, and I'm at a bit of a loss as to how to speed it up. My suspicion is that the resample filter is the culprit. However I am not well versed enough to know how to make it faster.
First, the custom filters:
This first one resizes an image by a simple rescaling. I think it's the best technique of rescaling in this case because all that is done is a replication of pixels when resized. For example, if we have the following block of pixels and perform a 2.0 scale, then the mapping looks like the following:
[ ][ ][x][ ] ----->[ ][ ][ ][ ][x][x][ ][ ] (Thanks to Simon Gladman for the idea on this one)
public class ResampleFilter: CIFilter
{
    var inputImage : CIImage?
    var inputScaleX: CGFloat = 1
    var inputScaleY: CGFloat = 1
    let warpKernel = CIWarpKernel(string:
        "kernel vec2 resample(float inputScaleX, float inputScaleY)" +
            "   {                                                      " +
            "       float y = (destCoord().y / inputScaleY);           " +
            "       float x = (destCoord().x / inputScaleX);           " +
            "       return vec2(x,y);                                  " +
            "   }                                                      "
    )

    override public var outputImage: CIImage!
    {
        if let inputImage = inputImage,
            kernel = warpKernel
        {
            let arguments = [inputScaleX, inputScaleY]

            let extent = CGRect(origin: inputImage.extent.origin,
                                size: CGSize(width: inputImage.extent.width*inputScaleX,
                                    height: inputImage.extent.height*inputScaleY))

            return kernel.applyWithExtent(extent,
                                          roiCallback:
                {
                    (index,rect) in
                    let sampleX = rect.origin.x/self.inputScaleX
                    let sampleY = rect.origin.y/self.inputScaleY
                    let sampleWidth = rect.width/self.inputScaleX
                    let sampleHeight = rect.height/self.inputScaleY

                    let sampleRect = CGRect(x: sampleX, y: sampleY, width: sampleWidth, height: sampleHeight)

                    return sampleRect
                },
                                          inputImage : inputImage,
                                          arguments : arguments)

        }
        return nil
    }
}

This one is a simple difference blend.
public class DifferenceOfImages: CIFilter
{
    var inputImage1 : CIImage?  //Initializes input
    var inputImage2 : CIImage?
    var kernel = CIKernel(string:  //The actual custom kernel code
        "kernel vec4 Difference(__sample image1,__sample image2)" +
            "       {                                               " +
            "           float colorR = image1.r - image2.r;         " +
            "           float colorG = image1.g - image2.g;         " +
            "           float colorB = image1.b - image2.b;         " +
            "           return vec4(colorR,colorG,colorB,1);        " +
        "       }                                               "
    )
    var extentFunction: (CGRect, CGRect) -> CGRect =
        { (a: CGRect, b: CGRect) in return CGRectZero }

    override public var outputImage: CIImage!
    {
        guard let inputImage1 = inputImage1,
            inputImage2 = inputImage2,
            kernel = kernel
            else
        {
            return nil
        }

        //apply to whole image
        let extent = extentFunction(inputImage1.extent,inputImage2.extent)
        //arguments of the kernel
        let arguments = [inputImage1,inputImage2]
        //return the rectangle that defines the part of the image that CI needs to render rect in the output
        return kernel.applyWithExtent(extent,
                                      roiCallback:
            { (index, rect) in
                return rect

            },
                                      arguments: arguments)

    }

}

Now for some function definitions:
This function just performs a gaussian blur on the image, according to the same 5 tap filter as described in Burt & Adelson's paper. Not sure how to get rid of the awkward bordering pixels that seem to be extra.
public func GaussianFilter(ciImage: CIImage) -> CIImage
{

    //5x5 convolution to image
    let kernelValues: [CGFloat] = [
        0.0025, 0.0125, 0.0200, 0.0125, 0.0025,
        0.0125, 0.0625, 0.1000, 0.0625, 0.0125,
        0.0200, 0.1000, 0.1600, 0.1000, 0.0200,
        0.0125, 0.0625, 0.1000, 0.0625, 0.0125,
        0.0025, 0.0125, 0.0200, 0.0125, 0.0025 ]

    let weightMatrix = CIVector(values: kernelValues,
                                count: kernelValues.count)

    let filter = CIFilter(name: "CIConvolution5X5",
                          withInputParameters: [
                            kCIInputImageKey: ciImage,
                            kCIInputWeightsKey: weightMatrix])!

    let final = filter.outputImage!

    let rect = CGRect(x: 0, y: 0, width: ciImage.extent.size.width, height: ciImage.extent.size.height)

    return final.imageByCroppingToRect(rect)

}

This function just simplifies the use of resample. You can specify a target size of the new image. This turns out to be easier to deal with rather than setting a scale parameter IMO.
public func resampleImage(inputImage: CIImage, sizeX: CGFloat, sizeY: CGFloat) -> CIImage
{
    let inputWidth : CGFloat = inputImage.extent.size.width
    let inputHeight : CGFloat = inputImage.extent.size.height

    let scaleX = sizeX/inputWidth
    let scaleY = sizeY/inputHeight

    let resamplefilter = ResampleFilter()
    resamplefilter.inputImage = inputImage
    resamplefilter.inputScaleX = scaleX
    resamplefilter.inputScaleY = scaleY
    return resamplefilter.outputImage
}

This function just simplifies the use of the difference filter. Just note that it's
imageOne - ImageTwo.
public func Difference(imageOne:CIImage,imageTwo:CIImage) -> CIImage
{
    let generalFilter = DifferenceOfImages()

    generalFilter.inputImage1 = imageOne
    generalFilter.inputImage2 = imageTwo

    generalFilter.extentFunction = { (fore, back) in return back.union(fore)}
    return generalFilter.outputImage

}

This function computes the level dimensions of each pyramid, and stores them in an array. Useful for later on.
public func LevelDimensions(image: CIImage,levels:Int) -> [[CGFloat]]
{
    let inputWidth : CGFloat = image.extent.width
    let inputHeight : CGFloat = image.extent.height

    var levelSizes : [[CGFloat]] = [[inputWidth,inputHeight]]
    for j in 1...(levels-1)
    {
        let temp = [floor(inputWidth/pow(2.0,CGFloat(j))),floor(inputHeight/pow(2,CGFloat(j)))]
        levelSizes.append(temp)
    }
    return levelSizes
}

Now on to the good stuff: This one creates a Gaussian Pyramid a given number of levels.
public func GaussianPyramid(image: CIImage,levels:Int) -> [CIImage]
{
    let PyrLevel = LevelDimensions(image, levels: levels)

    var GauPyr : [CIImage] = [image]
    var I : CIImage
    var J : CIImage

    for j in 1 ... levels-1
    {
        J = GaussianFilter(GauPyr[j-1])
        I = resampleImage(J, sizeX: PyrLevel[j][0], sizeY: PyrLevel[j][1])
        GauPyr.append(I)

    }
    return GauPyr
}

Finally, this function creates the Laplacian Pyramid with a given number of levels. Note that in both Pyramid functions, each level is stored in an Array.
public func LaplacianPyramid(image:CIImage,levels:Int) -> [CIImage]
{
    let PyrLevel = LevelDimensions(image, levels:levels)

    var LapPyr : [CIImage] = []
    var I : CIImage
    var J : CIImage

    J = image
    for j in 0 ... levels-2
    {
        let blur = GaussianFilter(J)
        I = resampleImage(blur, sizeX: PyrLevel[j+1][0], sizeY: PyrLevel[j+1][1])
        let diff = Difference(J,imageTwo: resampleImage(I, sizeX: PyrLevel[j][0], sizeY: PyrLevel[j][1]))
        LapPyr.append(diff)
        J = I

    }
    LapPyr.append(J)
    return LapPyr
}

Comment: The only thing I haven't done yet is making a function that obtains the new image from a image pyramid. Will do that later and edit it in.
Comment: Just wanted to throw in, that one will need a way of preserving the negative sign that floats between Laplacian Level, since any calculation you do with them MUST  be preserved.


Write a Java program that downloads the first 100 comics of the webcomic XKCD. Be sure to use https:// for all URLS

Question: This is what I have so far, and I am having trouble downloading 1-100 comics starting at https://xkcd.com/1/ and I know I am supposed to be going to the source code for the website. However, I cant seem to figure out how to get all the first 100 comics into my designated file I set it to save to. For example, I want https://xkcd.com/1/(view-source:https://xkcd.com/1/), https://xkcd.com/2/(view-source:https://xkcd.com/2/), and all the way up to comic 100. I know the img src is at line 50, but once again I don't know how to approach it.
 public static void main(String[] args) {
    URL imgURL = null;
    for (int web = 1; web <= 100; web++) {
    try {
        imgURL = new URL("https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg");
        InputStream stream = imgURL.openStream();
        Files.copy(stream, Paths.get("file/WebComics" + web + ".png"));
        System.out.println("Done!");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error!");
    }
    }
}

}
Comment: @UnmannedPlayer How would i go about parse the resulting HTML?
Comment: Use a library like jsoup or something. Going to line 50 is not going to help if they change their page layout. Get the page title and then search for the `img` tag that contains `alt` same as the page title and pull out the `src` attribute. That's the image.
Comment: I forgot to mention that all of the downloads are of the same picture from the first comic, but I need them to download all of the them up to 100 comics.
Comment: Curious, is this homework question? Seems like all you really need is HTTPConnection and parse the resulting HTML (not jump to line 50 but really parse).
Answer: Add jsoup library jar to your project, and then try this:
static void do_page(int id) throws IOException {
    Document doc = Jsoup.connect("https://xkcd.com/" + id).get();
    Elements imgs = doc.select("#comic img");
    for (Element e: imgs) {
        System.out.println(e.attr("src"));
    }
}

Then call the do_page function in a loop:
for (int i = 1; i <= 100; i++) {
    do_page(i);
}

Now, instead of  printing it, you can use JSoup again to probably download the images like you see fit.
Comment: That actually makes a lot of sense! Thank you!


Proving the convergence of a sequence to a limit from another sequence.

Question: Currently taking Real Analysis 1. Working on proving convergence of sequences. I have been struggling general with how to approach these problems, so the ask is help with the problem in addition to a critique of the approach.
The Problem: Let $u_{n}$ for $n\in \Bbb N$ be a sequence of real numbers such that $$\lim_{x\to +\infty} (u_{n+1} - u_{n}) = l$$ where $l$ is a real number.$$$$
Show that $$\lim_{x\to +\infty}(u_{n}/n)=l$$
After spending some time thinking about this, I feel as though I have some grasp of why this might be the case. If the distance between $u_{n}$ and $u_{n+1}$ approaches a limit then the distance from one term to the other is approaching a constant rate. It would makes sense that if this is the case then the ratio of $u_{n}/n$ would also approach a constant because the rate of growth of $u_{n}$ with respect to $n$ is constant from the previous statement.
In terms of actually approaching the problem I think that I need to find an N such that $| u_{n}/n-l|<\epsilon$ from the knowledge that $|(u_{n+1} - u_{n})-l|<\epsilon$
Trying to solve the problem at this point has mostly been shots in the dark, random applications of the triangle inequality, and frustration.
As stated previously in addition to help with the problem I would appreciate some insight into the thought process that led you to the solution.
Answer: The idea is to write $u_n$ in terms of the differences $u_{n+1}-u_n$. We can write $u_n=u_1+(u_2-u_1)+\cdots+(u_n-u_{n-1})$. Let $v_1=u_1, v_2=u_2-u_1$ etc. Then $\frac {u_n} n=\frac {v_1+v_2+\cdots+ v_n} n$. It is given that $v_n \to l$. The fact that $\frac {u_n} n \to l$ now follows from Cesaro's Theorem.


I want to add an include_path but don't have access to php.ini

Question: I was thinking I could use the php function set_include_path() to accomplish this, but I'm not sure which file I should include this in. template.php perhaps?
Answer: settings.php would probably be the safest bet.
The template.php file might well be included too late in the process to have the desired effect, but settings.php is included indirectly from drupal_bootstrap() so should take effect before any of your custom code has a chance to run.
If you don't want to alter settings.php in that way you could implement hook_boot() in a custom module which is also invoked very early on in the page build process.


Is there a D7 module that allows you to create a content type to be displayed as a system message?

Question: Is there a D7 module that allows you to create a content type to displayed as a system message?
Something like http://drupal.org/project/user_alert but gets displayed in the system messages.
Answer: It would be a lot of overhead for this one function but Rules can trigger arbitrary system messages with Token support. Here is a screencast by Node One which can give you an overview and he does the system message example in a couple of the videos. I think that if you need extended interaction with users Rules can be very helpful.
Comment: I considered this. However, is there a way to keep the message shown to all users until the node is deleted?
Comment: I'm not sure what your particular case is. However, in the video, Johan Falk shows a case of using the Flag module Rule integration. A rule can be created where if a flag on the node is set, the rule will trigger a system message to be shown until the flag is either unset or the node is deleted or another condition is met such as the node is unpublished.


Android App start with virtual keyboard open

Question: I am writing an app for android phones and after my splash screen it shows an activity which has several spinners and edittext views.
On an android device without a keyboard it shows the virtual keyboard which then blocks the rest of the app and looks unprofessional to me.  I have tried to hide the virtual keyboard in the activity.onCreate method however no avail there.
My next option I tried was setting an onFocus listener to the actual offending edittext view and still no avail.
How can I prevent the virtual keyboard from showing until a user explicitly clicks an edittext view?  Is there a way to give one of the spinner's focus since one of the spinners is before the edittext view in the layout xml?
Answer: You can try setting the android:windowSoftInputMode attribute of your Activity's entry in the AndroidManifest.xml file.  I think setting it to stateHidden might do what you want.
Here's the
docs from the Android site.
Answer: You can try this:
Oncreate of your activity, set the input type to TYPE_NULL. Then, onTouch, set it to TYPE_CLASS_TEXT.
    myText.setInputType(InputType.TYPE_NULL);

    myText.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        myText.setInputType(InputType.TYPE_CLASS_TEXT);
        myText.onTouchEvent(event); // call native handler
        return true; // consume touch even
        }
    });

Comment: Thanks a bunch, now my app acts more professional.  I noticed other apps like the yahoo email app work in the same way, nice to have apps look alike to add to feel and functionality.
Comment: I believe this is a nice workaround, but that mportuesisf answer is a better one.
Answer: There are several more answers with another experience on this issue in thread Automatic popping up keyboard on start Activity


How many is a "large" data set?

Question: Assumed infinite storage where size/volume/physics (metrics,gigabytes/terrabytes) won't matter only the number of elements and their labels, statistically pattern should emerge already at 30 subsets, but can you agree that less than 1000 subsets is too little to test, and at least 10000 distinct subsets / "elements", "entries" / entities is "a large data set". Or larger?
Thanks
Comment: With infinite storage, no data set is "large" :-)
Comment: You should specify what kind of data contains your data set and what statistical analysis you want to do with them.
Comment: As @akappa says, without context, it is impossible to say.
Comment: @marc_s, Processing and fetching would make the "large" dataset "slow."
Answer: I'm not sure I understand your question, but it sounds like you are attempting to ask about how many elements of data set you need to sample in order to ensure a certain degree of accuracy (30 is a magic number from the Central Limit Theorem that comes in to play frequently).
If that is the case, the sample size you need depends on the confidence level and confidence interval.  If you want a 95% confidence level and a 5% confidence interval (i.e. you want to be 95% confident that the proportion you determine from your sample is within 5% of the proportion in the full data set), you end up needing a sample size of no more than 385 elements.  The greater the confidence level and the smaller the confidence interval that you want to generate, the larger the sample size you need.
Here is a nice discussion on the mathematics of determining sample size
and a handy sample size calculator if you just want to run the numbers.

How many is a "large" data set?


avoiding creation of temporary or permanent tables

Question: I want to manipulate with a table, but the only solution I found so far is to create some tables first and join them together to the desired results.
I'm trying to avoid creating tables and dropping them at the end of my MySQL query which btw, I'm running on phpmyadmin page.
Here is the data: I have one table containing user_id, columnA_unixtime, columnB_unixtime -- meaning that for each user there are two unix_time stored in the database for two different events.

user_id     eventA_join     eventB_join
1           1321652009      1321652009
2           0               1321652257
3           0               1321668650
4           1321669261      0

what I want to have is a table showing how many users joined the two events for each day. Something like this (just a sample)

day       eventA    eventB
11/18/11    3       2
11/19/11    11      8
11/20/11    6       3
11/21/11    17      11

Here is the code I'm using so far:
CREATE TABLE table1(
day VARCHAR(256),
eventA_count INT);

INSERT INTO table1 (day, eventA_count)
(SELECT DATE(FROM_UNIXTIME('eventA_join') ) AS 'day', COUNT('user_id') AS 'eventA_count'
FROM org_table
WHERE 'eventA_join' > 0
GROUP BY day);

CREATE TABLE table2(
day VARCHAR(256),
eventB_count INT);

INSERT INTO table2 (day, eventB_count)
(SELECT DATE(FROM_UNIXTIME('eventB_join') ) AS 'day', COUNT('user_id') AS 'eventB_count'
FROM org_table
WHERE 'eventB_join' > 0
GROUP BY day);

SELECT t.day, t1.eventA_count, t2.eventB_count FROM
(SELECT DISTINCT day FROM table1
UNION
SELECT DISTINCT day FROM table2) t
LEFT JOIN table1 t1
ON t.day = t1.day
LEFT JOIN table2 t2
ON t.day = t2.day

DROP table2;
DROP table1;

As far as I tried I couldn't use table variables in phpmyadmin and neither I could use template tables because there was no way to refer to template tables multiple times (error #10327 Can't reopen temporary table) when I try to join them together.
Is there anyway I avoid creating tables but gain what I'm looking for? Any thoughts?
Edit: both tables are getting data from 'org_table' which is now corrected in the code.
Answer: This is pretty easy to do with a single query.  You just need UNION ALL:
select date, sum(IsEventA) as EventA, sum(isEventB) as EventB
from ((select user_id, DATE(FROM_UNIXTIME('eventA_join') as date,
              1 as IsEventA, 0 as IsEventB
       from table1
       where eventA_join > 0
      ) union all
      (select user_id, DATE(FROM_UNIXTIME('eventB_join') as date,
              0 as IsEventA, 1 as IsEventB
       from table1
       where eventB_join > 0
      )
     ) t
group by date
order by 1

Comment: Yes. This works well and shows me how to make an aggregation of two selects. Thank you @Gordon


How to Redirect to specific action route

Question: this is my custom route.but when I want to use RedirectToAction("action","controller"); it cause error.
No route in the route table matches the supplied values.
I visited some related post but I could not solve it. How do I can fix it?
routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{filter1}/{filter2}/{filter3}/{filter4}/{filter5}/{filter6}/{filter7}/{filter8}/{filter9}/",
    defaults: new
    {
        controller = "Shop",
        action = "Category",
        id = UrlParameter.Optional,
        filter1 = UrlParameter.Optional,
        filter2 = UrlParameter.Optional,
        filter3 = UrlParameter.Optional,
        filter4 = UrlParameter.Optional,
        filter5 = UrlParameter.Optional,
        filter6 = UrlParameter.Optional,
        filter7 = UrlParameter.Optional,
        filter8 = UrlParameter.Optional,
        filter9 = UrlParameter.Optional,
    }
);

Comment: How about trying `RedirectToAction("action", "controller", new { id = 0 })`? I think there's no route matches with action defined in `RedirectToAction`, you should create another mapping if you want to use multiple filters (or dynamic filters).
Comment: Is that route mapping the only one route in the `RouteConfig`? If my guess is right, there you should consider dividing routes into multiple mappings with different filters instead of using single default route for multiple filters.
Comment: What you are trying will not work because of [this](https://stackoverflow.com/a/5061320/4228458).
Comment: @TetsuyaYamamoto tnx. it not worked. How can I create another mapping filter.
Comment: Not related, but only the last parameter can be marked as `UrlParameter.Optional`
Comment: @CodingYoshi, That has nothing to do with OP's issue.
Answer: I had a similar problem once with RedirectToAction and found out that you need a valid route registered that leads to that action. in your case, the error occurred because your optional parameter is more than expected amount.
I test code and it works :
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{filter1}/{filter2}",
    defaults: new
    {
        controller = "Employees",
        action = "Index",
        id = UrlParameter.Optional,
        filter1 = UrlParameter.Optional,
        filter2 = UrlParameter.Optional
    }
);

if you want your own route you can use [Route("")] on top of your Action, at the first Add routes.MapMvcAttributeRoutes(); to RegisterRoutes method in RouteConfig.cs. After that do like below:
[Route("{TestEmployee}/{Index}/{id?}/{filter1?}/{filter2?}/{filter3?}/{filter4?}/{filter5?}/{filter6?}/{filter7?}/{filter8?}/{filter9?}")]
public ActionResult Index()
{
    //do something
}

then you can redirectToAction :
return RedirectToAction("Index", "TestEmployee" ,routeValues: null);

Comment: how about the situation that routeValues is not null!? I used a string like "1/3/def" (my parameters) but it doesn't work.
Comment: @mjyazdani Have you mapped correct MapRoute in your RouteConfig.cs ?!


How to disable ESLint react/prop-types rule in a file?

Question: I'm using React and ESLint with eslint-plugin-react.
I want to disable the prop-types rule in one file.
var React = require('react');
var Model = require('./ComponentModel');

var Component = React.createClass({
/* eslint-disable react/prop-types */
    propTypes: Model.propTypes,
/* eslint-enable react/prop-types */
    render: function () {
        return (

                {this.props.title}

        );
    }
});

Comment: Does this answer your question? [Turning off eslint rule for a specific file](https://stackoverflow.com/questions/34764287/turning-off-eslint-rule-for-a-specific-file)
Comment: ESLint has nothing to do with your code being react or whatnot.  I have provided a detailed answer of all the cases (single/multiple line/file, some/all rules) in this *answer*: https://stackoverflow.com/a/56719951
Answer: if you have only one file you want to disable prop-type validation you can use:
/* eslint react/prop-types: 0 */

in cases where you have multiple files you can add to your .eslintrc file in your root directory a rule to disable prop-type validation:
{
 "plugins": [
     "react"
  ],
  "rules": {
    "react/prop-types": 0
  }
}

for further rules you can checkout this link that solved my issue and for your convenience you can also read up from eslint-plugin-react's github documentation about how to disable or enable it with various options.
Comment: `"react/prop-types": "off"` also works (and it is a little more readable)
Comment: Fro me, it works: 'react/prop-types': false
Answer: Just put this on top of your file:
/* eslint-disable react/prop-types */

Comment: Tried it out and it works fine. It's a cleaner approach. Thanks.
Comment: Instead of `0`, I'd recommend putting `off`. When working with many developers of different skill levels, it's best to be explicit rather than requiring they know `0` stands for `off`.
Answer: I had to do:
/* eslint react/forbid-prop-types: 0 */

this did not work for me:
/* eslint react/prop-types: 0 */

To disable globally in your .eslintrc file (old version v6.0 or below):
{
    "rules": {
        "react/forbid-prop-types": 0
    }
}

To disable globally in your .eslintrc file (new version above v6.0):
{
    "rules": {
        "react/prop-types": 0
    }
}

Comment: The part about rules is incorrect as of 2020. Use

 `    "rules": {
        "react/prop-types": 0
    }
`
Answer: I had to wrap the whole component with the eslint ignore comments.
var React = require('react');
var Model = require('./ComponentModel');

/* eslint-disable react/prop-types */
var Component = React.createClass({

    propTypes: Model.propTypes,

    render: function () {
        return (

                {this.props.title}

        );
    }
});
/* eslint-enable react/prop-types */

Comment: My hero. This solved why I wasn't able to get `/* eslint-disable react/no-multi-comp */` when just wrapping my first component in it.
Comment: `/* eslint-disable react/prop-types */` should be placed at the very beginning of a file
Answer: Sometimes I have small components in the same file as the major one. There propTypes seems overkill. Then I usually do something like this
// eslint-disable-next-line react/prop-types
const RightArrow = ({ onPress, to }) => (<TouchableOpacity onPress={() => onPress(to)} style={styles.rightArrow}><Chevrons.chevronRight size={25} color="grey" /></TouchableOpacity>);

Answer: I had to do this in my .eslintrc config file to disable prop types validation error.
"rules": {
  "react/prop-types": "off"
}

Answer: To disable or modify the react/prop-types rule in your .eslint.cjs file, you can use the following configurations:
Option 1: Disable the rule completely
"rules": {
  "react/prop-types": "off"
}

or
"rules": {
  "react/prop-types": 0
}

or
"rules": {
  "react/prop-types": false
}

Option 2: Set the rule to generate warnings instead of errors
"rules": {
  "react/prop-types": ["warn"]
}

Choose the option that best suits your requirements and add it to your .eslint.cjs file. This will configure ESLint to either disable the react/prop-types rule or modify its severity to generate warnings instead of errors.
Answer: I needed React relaunch for this to be taken into account without error
rules: {
  'react/prop-types': 'off'
}

Answer: You can change it to be warning, put it inside eslint rules:
'react/prop-types': ['warn'],
Answer: Best is to add this line :
'react/prop-types':[disabled] //* disabling eslint prop-types all warnings

add this line to your module.exports object in .eslintrc.cjs in file.
Comment: OP asked how to disable the rule for just one file.


Assigning nodes to a hierarchical tree/network programatically

Question: I have a network/tree which looks like this.

I have used a binary tree to represent this model. However manually assigning the right and left parameters of a node become cumbersome for levels > 4.
Is there a method by which I can do the above assignment programatically
Answer: You can at first create a 2D array of nodes where row i corresponds to level i on your paper, and column j corresponds to the j-th node in that level:
for i = 1 to n:
   for j = 1 to i:
      A[i][j] = new Node()

Then, the relationship between nodes is, the A[i][j] node has left child being A[i+1][j] and has right child being A[i+1][j+1].
for i = 1 to n-1:
   for j = 1 to i:
      A[i][j].left = A[i+1][j]
      A[i][j].right = A[i+1][j+1]

for j = 1 to n:
   A[n][j].left = null
   A[n][j].right = null


SQLAlchemy Enum type field with integer value

Question: There is an events model in which I want to add a memebers_count field to indicate the number of participants. The user will make a single selection from the select form. I am using Enum type for this. The model looks like this
class MembersQuantity(enum.Enum):
    two = 2
    three = 3
    four = 4
    five = 5
    six = 6
    ...
    nineteen = 19
    twenty = 20
    thirty = 30
    forty = 40
    fifty = 50
    unlimited = 1000

events = Table(
    "events",
    metadata,
    Column("id", Integer(), primary_key=True),
    ...
    Column("members_count", Enum(MembersQuantity, values_callable=lambda obj: [e.value for e in obj]),
           nullable=False,
           default=MembersQuantity.two.value,
           server_default=MembersQuantity.two.value),
    ....

An error appears in the console when this entry is made
  File "/home/jekson/Projects/own/wplay/./models/events.py", line 64, in <module>
    Column("members_count", Enum(MembersQuantity, values_callable=lambda obj: [e.value for e in obj]),
  File "<string>", line 2, in __init__
  File "/home/jekson/virtualenvs/wplay/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "/home/jekson/virtualenvs/wplay/lib/python3.9/site-packages/sqlalchemy/sql/sqltypes.py", line 1383, in __init__
    self._enum_init(enums, kw)
  File "/home/jekson/virtualenvs/wplay/lib/python3.9/site-packages/sqlalchemy/sql/sqltypes.py", line 1425, in _enum_init
    length = max(len(x) for x in self.enums)
  File "/home/jekson/virtualenvs/wplay/lib/python3.9/site-packages/sqlalchemy/sql/sqltypes.py", line 1425, in <genexpr>
    length = max(len(x) for x in self.enums)
TypeError: object of type 'int' has no len()

I assumed the problem was that [e.value for e in obj] should be str instead of int and replaced it with [str(e.value) for e in obj] but got another error
sqlalchemy.exc.ArgumentError: Argument 'arg' is expected to be of the type '<class 'str'>' or '<class 'sqlalchemy.sql.elements.ClauseElement'>' or '<class 'sqlalchemy.sql.elements.TextClause'>', got '<class 'int'>'

What's wrong?
Answer: The question is what do you want to persist? two or 2 or <MembersQuantity.two: 2>?
SQLAlchemy docs states:
Above, the string names of each element, e.g. "one", "two", "three", are persisted to the database;
the values of the Python Enum, here indicated as integers, are not used;
the value of each enum can therefore be any kind of Python object whether or not it is persistable.

So if you have integer values of enum, you probably should have something like this:
    Column(
       "members_count",
       Enum(MembersQuantity),
       nullable=False,
       default=MembersQuantity.two.name,
       server_default=MembersQuantity.two.name
    )

The name attribute is two, while value attribute is 2 in your case. But you persist only left-hand side strings.
You should use values_callable only if you want to persist values instead of names, but docs states also: value_callables - a callable which will be passed the PEP-435 compliant enumerated type, which should then return a list of string values to be persisted. So you can persist value only if it is a string.
Here is a topic that can be helpful: How to use Enum with SQLAlchemy and Alembic?
Comment: Here you can find a hack for `int`: https://michaelcho.me/article/using-python-enums-in-sqlalchemy-models
Comment: Same result as in my question
`sqlalchemy.exc.ArgumentError: Argument 'arg' is expected to be one of type '' or '' or '', got '` So, easiest way is set values as string I think. I have made sure that using `int` in `enum` is inappropriate
Comment: You gave me great advice, found a solution that works for me in this answer https://stackoverflow.com/a/38786737/9262339


Profiling the function 'library' in R

Question: I'm trying to track down the reason(s) why a package I've put together loads slowly (relative to others, it's noticeably slower), and running this (using ggplot2 as an example) doesn't appear to give any insight:
Rprof(line.profiling=TRUE)
library(ggplot2)
Rprof(NULL)
summaryRprof("Rprof.out", lines = "show")

which yields:
$by.self
              self.time self.pct total.time total.pct
<no location>       0.5      100        0.5       100
$by.total
              total.time total.pct self.time self.pct
<no location>        0.5       100       0.5      100
$by.line
              self.time self.pct total.time total.pct
<no location>       0.5      100        0.5       100

$sample.interval
[1] 0.02

$sampling.time
[1] 0.5

(And Rprof.out contains a mess of information I'm having trouble understanding.)
Can anyone suggest how I might "profile" what the function library is doing?  Or, am I taking the wrong approach?
Comment: To be clear, I'm definitely not suggesting `ggplot2` loads slowly.
Comment: I might be able to provide some guesses if you pointed to the source code of your package. I'd look at what you're doing in NAMESPACE, how many objects in your package environment, etc.
Comment: Thanks @hadley.  I'm sure you have better things to do, but the code base is here: https://github.com/abarbour/strain
Comment: I'd wonder about the number of Imports: would be interesting to profile that vs. Depends. If it made a big impact you could replace with specific importFrom calls.
Comment: It turns out importing `Matrix` was consuming most of the load time: `sapply(c("Matrix","lubridate","corpcor","zoo"), function(x) system.time(do.call("library",list(x))))`
Answer: As it says in the Line profiling section of ?summaryRprof:

If the code being run has source reference information retained (via
  keep.source = TRUE in source or KeepSource = TRUE in a package
  DESCRIPTION file or some other way), then information about the
  origin of lines is recorded during profiling.

By default, source code is not kept when building/installing packages.  You'll see non-trivial timings if you just run summaryRprof().  You could always re-install the base package with KeepSource=TRUE, but that might be more trouble than it's worth.
Another alternative would be to use debugonce(library) to step through the library call line-by-line.  This is a bit brute-force, but it might be more obvious than combing through the profiling output.
Comment: Interesting points.  To clarify, when you write "the base package" do you mean the package I'm trying to study, or the package `base` so that the source code for `library` is retained?
Comment: @AndyBarbour: the latter.

Profiling the function 'library' in R


Qt simple error when trying to delete a word in QTextEdit

Question: I'm trying to delete a word after pressing space on a QTextEdit.
Here's my code:
window.h:
#ifndef WINDOW_H
#define WINDOW_H
#include <QApplication>
#include <QWidget>
#include <QTextEdit>
#include <iostream>
using namespace std;

class Window: public QWidget
{
    Q_OBJECT

public:
    Window();

public slots:
    void write();

private:
    QTextEdit *textEdit;
};

#endif // WINDOW_H

window.cpp:
#include "window.h"

Window::Window()
{
    setFixedSize(500, 500);
    textEdit = new QTextEdit(this);
    textEdit->resize(500, 500);
    QObject::connect(textEdit, SIGNAL(textChanged()), this, SLOT(write()));
}

void Window::write()
{
    QString word = textEdit->toPlainText();
    if (word[word.length()-1] == ' ')
    {
        for(int i=0;i<word.length();i++)
        {
            textEdit->textCursor().deletePreviousChar();
        }
    }
}

main.cpp
#include <QApplication>
#include <QTextEdit>
#include <iostream>
#include <QString>
#include "window.h"
using namespace std;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();

    return app.exec();
}

So with this code, when I'm writing "abc ", it should erase all the word, but instead it return me an error:
Answer: In your write(), if word.length() equals to 0, word[word.length()-1] refers to word[-1], which is invalid.
You should check if word.length() >= 1 in your if statement:
if (word.length() >= 1 && word[word.length()-1] == ' ')

Comment: Wow it works, thank you very much, I was actually blocked for hours on this one, you just save me thanks
Comment: Glad to help. You can `accept` my answer if it solves your problem. :-)


Mask subdomain.mydomain1.com to subdomain.mydomain2.com

Question: I have got customer.mydomain1.com and want to mask it to customer.mydomain2.com
so that customer.mydomain1.com shows content of customer.mydomain2.com and url stays the same as of customer.mydomain1.com
I am using the code below used in my .htaccess, which gives me ERROR 400
RewriteEngine On
RewriteCond %{HTTP_HOST} ^customer.mydomain1.com
RewriteRule ^(.*) http://customer.mydomain2.com/$1 [P]

Comment: @Webdesigner yes both of them are on same server. yes i do have apache config access! How to fix it?
Comment: What dose your log files say about the error? Please give us a little bit more information. Without it is a little bit hard for us. Read the doumentaion: https://httpd.apache.org/docs/current/en/mod/core.html#ServerAlias
Comment: Are both sites on the same Server? If not you only can do a redirect. If yes, you also need access to your apache config. Do you have it?
Comment: Why not add customer.mydomain1.com as a ServerAlias to your Vitualhost of customer.mydomain2.com so if you call customer.mydomain1.com you get content of customer.mydomain2.com and disable virtual host from customer.mydomain1.com
Comment: @Webdesigner i tried gave me Error 500
Comment: The [P] Flag requires that mod_proxy is enabled... but IMHO ServerAlias is  what you need.
Answer: I would just use a ServerAlias this works for any domain that have an A-Record or a CNAME pointing to your server:
<VirtualHost *:80>
    ServerName customer.mydomain2.com
    ServerAlias www.customer.mydomain2.com customer.mydomain1.com example.com

    DocumentRoot /var/www/path/to/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log common
</VirtualHost>

No need to use mod_rewite with mod_proxy (required for the the [P] Flag)
All listed domains (you can add as many you like)

customer.mydomain2.com
www.customer.mydomain2.com
customer.mydomain1.com
example.com

will respond with the content of /var/www/path/to/public_html so just replace it with the public directory of your customer.mydomain2.com server.
Answer: I can't see anything wrong in your config.  I used this configuration on my local apache, so both domains are on the same HTTPD:
<VirtualHost *:80>
    ServerName customer.mydomain1.com
    ErrorLog "logs/error_mydomain1_log"
    CustomLog "logs/access_mydomain1_log" common

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^customer.mydomain1.com
    RewriteRule ^(.*) http://customer.mydomain2.com/$1 [P]

</VirtualHost>

<VirtualHost *:80>
    ServerName customer.mydomain2.com
    ErrorLog "logs/error_mydomain2_log"
    CustomLog "logs/access_mydomain2_log" common

    DocumentRoot "/home/tools/apache/htdocs/mydomain2"
    DirectoryIndex index.html
</VirtualHost>

Then changed my local hosts file to define both domains, and it works. I get the content of index.html if I access http://customer.mydomain1.com.  I also created other files, in sub-directories on customer.mydomain2.com and when I access them, customer.mydomain1.com stays in the address bar.
So either there is something in your configuration, or some module is missing.  Does $(apachectl -t) return "Syntax OK"?  Did you restart your apache?


Azure Web Service / SSL - Renewal Procedure

Question: I have a WCF service being hosted on the Microsoft Azure Web Services platform.
I wish to secure the service using SSL. I have the domain with GoDaddy and will be buying an SSL certificate through them.
I am fairly happy with the procedure to add a certificate to Azure. My question is the following:
After 1 year, what is the procedure. My original cert (A) will expire, and a new certificate (B) will take over as the SSL cert. During the period of the change I will need to maintain uptime of my service via SSL.
How does this work? Can somebody explain to me how I can make sure that there is a sufficient overlap between A and B.
Comment: Put the date on your Outlook calendar? I'm really not seeing the point of this question.
Comment: I can only assume I haven't made myself clear. What I don't understand is can there be two certificates valid at the same time so that there is safe overlap? Or is that not how SSL works? @MichaelHampton
Comment: You renew it before it expires, like your driver license. :)
Answer: I resell SSL Certificates for Thawte on a fairly regular basis.
When you go to renew your SSL Certificate around the time that it is near it's expiration, most if not all Certificate Authorities(CA) will add the remaining validity time onto your newly renewed certificate.
For example, if you had an SSL certificate for secure.domain.com.au and you received a notice from your CA that it was going to expire in 90 days from now, you could renew it straight away and your new certificate would have the 12 months of validity (or whatever period you renew it for) PLUS the 90 days that you had remaining in the first place.
Swapping out an old SSL certificate with a new one should not cause any interruption to your service, as long as the certificate is valid.
Hope that helps!
Comment: Ah ok, that was the answer I was looking for. Thanks Vasili
Answer: You have to renew it before it expires, and change the certificate on the server before the expiration date.
Source: I'm a very technologically knowledgable 12 year old seventh grader who has worked on countless technologies, both client side and server side.


How to prove $ (3\Bbb N + 1) \cap (5\Bbb N + 3) = 15\Bbb N + 13$

Question: I'm trying to prove that: $$ (3\Bbb N + 1) \cap (5\Bbb N + 3) = 15\Bbb N + 13$$ What have I done so far is, I'm getting an x from left hand side of equation:
$$
x \in (3\Bbb N + 1) \cap (5\Bbb N + 3)
$$
Then I'm simplifying it, like this:
$$
x \in (3\Bbb N + 1) \land x \in (5\Bbb N + 3)
$$
$3\Bbb N + 1$ is basicly $1 \pmod{3}$, and $(5\Bbb N + 3)$ is basicly $3 \pmod{5}$, thus:
$$
x \equiv 1 \pmod{3} \; and \; x\equiv 3 \pmod{5}
$$
Use CRT (i.e. https://en.wikipedia.org/wiki/Chinese_remainder_theorem) to conclude result for left hand side:
$$
a_1 = 1, \; m_1 = 5, \; y_1 \equiv 2 \pmod{3}
$$
After that,  Use CRT to conclude result for right hand side:
$$
a_2 = 3, \; m_2 = 3, \; y_2 = 2 \pmod{5}
$$
Then plug in:
$$
\begin{split}
x &\equiv a_1m_1y_1 + a_2m_2y_2 \pmod{M} \\
  & \equiv 1\cdot 5\cdot 2 + 3\cdot 3 \cdot 2 \pmod{3\cdot 5} \\
  & \equiv 28 \pmod{15}
    \equiv 13 \pmod{15}
\end{split}
$$
then find for the right side;
$$
x \in (15\Bbb N + 13) \implies x = 13 \pmod{15}
$$
Did I prove it right? Is there an easier way to do this?
Comment: Essentially, this is the Chinese Remainder Theorem, so there shouldn't be an easier way
Answer: Let $n=3x+1$ and assume $x=5y+z$, then $n=15y+3z+1$. If $n\equiv 3 \pmod{5}$, then $z\equiv -1 \pmod5$. Thus we can take $z=-1$ and obtain the result you want.
Answer: Consider the ring isomorphism $\phi:\Bbb Z_{15}\rightarrow \Bbb Z_3\times \Bbb Z_5: x\mapsto (x \mod 3,x\mod 5)$.
Then $x=13$ is mapped to $\phi(13) = (13\mod 3,13\mod 5)= (1,3)$.


System design to aggregate in near-real time, the N most shared articles over the last five minutes, last hour and last day?

Question: I was recently asked this system design question in an interview:

Let's suppose an application allows users to share articles from 3rd
  party sites with their connections. Assume all share actions go
  through a common code path on the app site (served by multiple servers
  in geographically diverse colos). Design a system to aggregate, in
  near-real time, the N most shared articles over the last five minutes,
  last hour and last day. Assume the number of unique shared articles
  per day is between 1M and 10M.

So I came up with below components:

Existing service tier that handles share events
Aggregation service
Data Store
Some Transport mechanism to send notifications of share events to aggregation service

Now I started talking about how data from existing service tier that handles share events will get to the aggregation servers? Possible solution was to use any messaging queue like Kafka here.

And interviewer asked me why you chose Kafka here and how Kafka will work like what topics you will create and how many partitions will it have. Since I was confuse so couldn't answer properly. Basically he was trying to get some idea on point-to-point vs publish-subscribe or push vs pull model?

Now I started talking about how Aggregation service operates. One solution I gave was to keep a collection of counters for each shared URL by 5 minute bucket for the last 24 hours (244 buckets per URL) As each share events happens, increment the current bucket and recompute the 5 min, hour, and day totals. Update Top-N lists as necessary. As each newly shared URL comes in, push out any URLs that haven't been updated in 24 hours. Now I think all this can be done on single machine.

Interviewer asked me can this all be done on one machine? Also can maintenance of 1M-10M tracked shares be done on one machine? If not, how would you partition? What happens if it crashes and how will you recover? Basically I was confuse how Aggregation service will actually work here? How it is getting data from Kafka and what is going to do actually with those data.

Now for data store part, I don't think we need persistent data store here so I suggested we can use Redis with partitioning and redundancy.

Interviewer asked me how will you partition and have redundancy here? And how Redis instance will get updated from the entire flow and how Redis will be structured? I was confuse on this as well. I told him that we can write output from Aggregation service to these redis instance.

There were few things I was not able to answer since I am confuse on how the entire flow will work. Can someone help me understand how we can design a system like this in a distributed fashion? And what I should have answered for the questions that interviewer asked me.
Answer: The intention of these questions is not to get ultimate answer for the problem. Instead check the competence and thought process of the interviewee. There is no point to be panic while answering these kind questions while facing tough follow up questions. Intention of the follow up questions is to guide you or give some hint for the interviewee.
I will try to share one probable answer for this problem. Assume I have s distributed persistent system like Cassandra. And I am going to maintain the status of sharing at any moment using my Cassandra infrastructure. I will maintain a Redis cluster  ahead of  persistence layer for LRU caching and maintain the buckets for 5 minutes, 1 hour and a day. Eviction will be configured using expire set. Now my aggregator service only need to address minimal data present within my Redis LRU cache. Set up a high through put distributed Kafka cluster will pump data from shared handler. And Kafka feed the data to Redis cluster and from there to Cassandra. To maintain the near real time output, we have to maintain the Kafka cluster throughput matching with it.
Comment: how redis cluster will work for all these different buckets? I am kinda confuse on that. Can you elaborate on that please?
Comment: Similar thoughts can be shared for a follow-up question for designing a search auto-complete system for generating suggestions based real time data like an exlusive news.


Determine the wireless network name from command-line in Linux

Question: I use my personal laptop at both home and work and in both cases I mount nfs shares.
I would like to automate the mounting of these shares as they are done every time I turn the laptop on.
I can't make it a normal start up command to be run, because the wireless connection takes about a half a minute to locate the network and connect.
So I wanted to write up a script that checks every 5 seconds if I'm connected to a network and what the essid of the network so i can mount the appropriate shares.
I looked at iwconfig, and ifconfig and none of them return an essid, only an actual ip address.
ifconfig return...
eth0      Link encap:Ethernet  HWaddr 00:26:2d:a2:01:a8
      UP BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
      Interrupt:16

eth1      Link encap:Ethernet  HWaddr f0:7b:cb:45:36:b2
      inet addr:192.168.0.48  Bcast:192.168.3.255  Mask:255.255.252.0
      inet6 addr: fe80::f27b:cbff:fe45:36b2/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:30972 errors:0 dropped:0 overruns:0 frame:124824
      TX packets:31414 errors:16 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:25279206 (25.2 MB)  TX bytes:4716009 (4.7 MB)
      Interrupt:17

lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:336 errors:0 dropped:0 overruns:0 frame:0
      TX packets:336 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:48501 (48.5 KB)  TX bytes:48501 (48.5 KB)

iwconfig returns...
lo        no wireless extensions.

eth0      no wireless extensions.

eth1      IEEE 802.11  Access Point: Not-Associated
      Link Quality:5  Signal level:210  Noise level:165
      Rx invalid nwid:0  invalid crypt:0  invalid misc:0

vboxnet0  no wireless extensions.

iwlist didn't return anything useful...
iwlist scan
lo        Interface doesn't support scanning.

eth0      Interface doesn't support scanning.

eth1      Interface doesn't support scanning.

vboxnet0  Interface doesn't support scanning.

Does anyone know an easy method to find out the essid of the Wireless network you are connected to via the linux command-line.
Update
I added the ifconfig and iwconfig information above, also I'm running Ubuntu 10.10 and it was upgraded from 10.04 which seems to have messed with the wireless somewhat.  They no longer use the old Network-Manager app from gnome.  I can't connect to my schools wireless network either, so there is definitely something funky with the wireless to begin with.
iwevent return...
iwevent
Waiting for Wireless Events from interfaces...
16:34:33.614296   eth1     Set ESSID:"" [13]
16:34:33.615723   eth1     Custom driver event:
16:34:33.615774   eth1     New Access Point/Cell address:Not-Associated
16:34:33.714835   eth1     Set ESSID:"" [62]
16:34:41.869636   eth1     Set Mode:Managed
16:34:41.869688   eth1     Set ESSID:"" [254]
16:34:42.815795   eth1     Custom driver event:
16:35:07.141450   eth1     Set Mode:Managed
16:35:07.141502   eth1     Set Frequency:24.12 GHz
16:35:07.141601   eth1     Set ESSID:"" [121]
16:35:08.160515   eth1     New Access Point/Cell address:00:1E:00:00:19:0E
16:35:08.252912   eth1     Registered node:00:1E:52:79:19:0E
16:35:08.252970   eth1     Custom driver event:

Answer: Running iwgetid you get the wireless device and the SSID of the connected network. It's part of the wireless-tools - that you appear to have already installed because iwevent is also part of it.
Comment: specifically `iwgetid -r` will return the just the SSID of the network
Comment: `-r` is undocumented in my man-page, but has the same output as `--raw`.
Answer: iwlist scan should certainly work to give you the ssid.  However from your example I don't see any actual wireless interfaces.  I suggest pasting the output of iwconfig and ifconfig into your question to help figure that out.
Also from looking at the iwevent man page, I think perhaps your best bet is to run a loop that calls iwevent instead.  When the ssid changes, iwevent will detect and report the new ssid.
Note that polling every 5 seconds sounds pretty intensive, perhaps you should run it less frequently?
Comment: Posted additional info in the original question.  Also the polling would stop once the connection was made, or it would time out after a few minutes.  I didn't plane on it being a continuous service.
Comment: So eth1 is your wireless interface.  Weird that it appears to be working yet reports 'Access Point: Not-Associated'.  Does iwevent do anything useful?
Comment: I ran iwevent, then turned my wireless off and back on.  Posted the contents above.  The essid still showd blank for whatever reason.
Comment: What happens if you force the essid with `iwconfig eth1 essid Foo` and then look at the iwevent output?
Comment: When running the command to connect to my works wireless this is returned...  `08:56:26.981161   eth1     Set ESSID:"" [121]
08:56:27.977062   eth1     New Access Point/Cell address:00:1E:00:00:19:0E
08:56:28.032219   eth1     Registered node:00:1E:52:79:19:0E
08:56:28.032277   eth1     Custom driver event:`   I'm probably just going to use the ip that I connect to.  To much of a hassle troubleshooting why the essid isn't appearing.
Answer: I have an old script that handles a check like this using iwconfig that's worked for several years now across several Fedora distributions:
   iwconfig wlan0 | grep ESSID | awk -F: '{print $2}'
Then I just make a decision in the script based on what that returns, like start up a home service or a work service.  Does your iwconfig command not return info like that once associated with an access point?
Comment: iwconfig doesn't return any access point information.  I posted the command output above with the ifconfig.
Comment: If iwconfig is not showing any ESSID then it's possible that your wireless card can't provide it to iwconfig.  If that's the case then the solution from @christopher-evans here is your best bet - make the decision based on the IP/subnet.
Answer: using iw
Assuming that your interface is wlp3s0, you can get the SSID with iw and grep:
iw wlp3s0 info | grep -Po '(?<=ssid ).*'

The flags -P and -o are explained in this answer.
Comment: or `iw wlp3s0 info | grep -Po 'ssid \K.*'`
or `iw wlp3s0 info | pcregrep -o1 'ssid (.*)'`
Answer: You could also have your script work based on the ip address it gets, and have your home network give out a different subnet.
Or base it on the default route's mac address.  You will just need to update it if the router ever changes.
Comment: This seems like the best solution at the moment, thanks.


more than one SUM from different table with different condition

Question: I have 2 tables .. one is named as "claim"
+----------+----------+--------+------------+
| claim_id | store_id | amount | created_by |
+----------+----------+--------+------------+
|        2 |        2 | 926.00 |          2 |
|        3 |        2 | 746.50 |          5 |
|        4 |        2 | 989.26 |          5 |
|        5 |        2 | 766.00 |          6 |
|        6 |        2 | 403.00 |          6 |
|        7 |        2 | 130.00 |          6 |
+----------+----------+--------+------------+

the other one "claim_progress"
+----------+------+------+---------------------+
| claim_id | type | paid | created             |
+----------+------+------+---------------------+
|        2 | S    |    0 | 2019-09-12 20:37:26 |
|        3 | S    |    0 | 2019-09-12 21:52:32 |
|        4 | S    |    0 | 2019-09-12 22:33:16 |
|        5 | S    |    0 | 2019-09-12 22:53:58 |
|        6 | S    |    0 | 2019-09-12 22:58:55 |
|        7 | S    |    0 | 2019-09-12 23:01:40 |
|        5 | A    |    0 | 2019-09-21 04:02:58 |
|        6 | A    |    0 | 2019-09-21 04:03:02 |
|        5 | PP   |  150 | 2019-09-21 04:03:10 |
|        5 | PP   |   45 | 2019-09-21 04:03:22 |
+----------+------+------+---------------------+

sure they have more columns to each but I simplify it down to such. claim_progress is the table to keep track of payment status of the claim table from type S for submission then A for approved then PP as progress payment and lastly P for paid. the column "created_by" is the user id that file for the claim.
What I'm trying to do here to get a one liner that produce 2 columns of the total of amount and the remaining unpaid about claim by "created_by". By that I need to get the latest progress of each claim identifying it to be either type "A" or "PP".
The total approved claim by "created_by" 6 should be 1169 and total paid would be 195. BTW here's what I've got so far but I wonder if it's redundant or even wrong?
SELECT SUM(c.amount),cp3.paid FROM claim c
JOIN (SELECT claim_id,MAX(created) as maxprogress FROM claim_progress GROUP BY claim_id) cp ON (c.id=cp.claim_id)
JOIN claim_progress cp2 ON (cp.maxprogress=cp2.created)
JOIN (SELECT claim_id,SUM(paid) AS paid FROM claim_progress GROUP BY claim_id) cp3 ON (cp3.claim_id=cp.claim_id)
WHERE cp2.type IN ('A','PP') AND c.created_by='6'

Hopefully the DB masters in here could shed some light. Thank you.
EDIT:
I'm expecting the answer to be like this
+----------------+------+
| total_approved | paid |
+----------------+------+
|           1169 |  195 |
+----------------+------+

Comment: Edit your question and *show* what the result set should look like.  How do two numbers in a column called "paid" get added together to become "unpaid"?
Comment: I do not see how the state duplicate is really going to help the OP.  There are multiple things that need to be resolved here.
Comment: I have edited the question to include my expected output
Comment: Sorry Gordon .. I've edited the unpaid to be paid ... the expected figure of 195 is paid .. sorry again Gordon
Comment: Can you explain 1169? If I add the three created_by = 6 I get 766 + 403 + 130 = 1299
Comment: hi kjmerf .. only claim with latest type of A or PP in claim_progress to summed. created_by 6 has only 2 approved claims
Answer: I feel like I should be able to compress this down by another join, but I just can't figure it out, and it's time to power down for the week. You're incredibly close to optimal here. This is a little bit more streamlined, but is doing essentially the same thing you were:
select sum(c.amount) Total_approved, sum(Progress.paid) paid from claim c
join (select claim_id, sum(paid) paid, max(created) created from claim_progress where type = 'A' group by Claim_id, type) Approved on c.claim_id = approved.claim_id
Left join (select claim_id, sum(paid) paid, max(created) created from claim_progress where type = 'PP' group by Claim_id, type) Progress on c.claim_id = progress.claim_id
Where c.created_by = 6

Hope this helps!
Comment: that one works .. kinda sleepy though I gotta consume more time in understanding how your statement works! thanx combee!


Setting dynamic path in the redis.conf using the Environment variable

Question: I have a environment variable MY_HOME which has a path to a directory /home/abc
Now, I have a redis.conf file In which I need to set this path like this
**redis.conf**

pidfile $MY_HOME/local/var/pids/redis.pid
logfile $MY_HOME/local/var/log/redis.log
dir $MY_HOME/local/var/lib/redis/

like we do in command line, so that my config file picks the path based on the Environment variable.
Comment: Looks like there is no support for environment variables in Redis configuration (at least not in 2.6.16), unfortunately.
Answer: Because Redis can read its config from stdin, I do something very similar to what @jolestar suggested.  I put placeholder variables in my redis.conf and then replace them using sed in my Redis launcher.  For example:
==========
$MY_HOME/redis/redis.conf
==========
...
pidfile {DIR}/pids/server{n}.pid
port 123{n}
...

Then I have a script to start Redis:
==========
runredis.sh
==========
DIR=$MY_HOME/redis
for n in {1..4}; do
    echo "starting redis-server #$n ..."
    sed -e "s/{n}/$n/g" -e "s/{DIR}/$DIR/g" < $DIR/redis.conf | redis-server -
done

I've been using this approach forever and it works out well.
Answer: i also find a solution,but redis config is no support for env var.
i think have 2 method:

start up redis by a script,script get env var and change redis config.
start up redis by command line,and send env var as parameter.
Answer: this is not supported by Redis. I recommend you use envsubst to substitute in the values of environment variables in redis.conf, on-the-fly, just-in-time, when executing redis-server.
example redis.conf:
tls-port ${MY_PORT_ENV_VAR}
# this is valid too:
# tls-port $MY_PORT_ENV_VAR

example usage of envsubst, when executing redis-server:
# echo "my env var port is: $MY_PORT_ENV_VAR"
# my env var port is: 8089
envsubst < ~/.redis.conf | redis-server -

by default, envsubst will replace all recognized environment variables in the input. however, you can limit this to a specific set of variables. This is done by passing a an argument. For example:
envsubst '${MY_PORT_ENV_VAR}' < ~/.redis.conf | redis-server -

this will only substitute MY_PORT_ENV_VAR in the input.
Comment: The idea is inject env vars in runtime , not statically
Comment: I think I see what you mean, writing a redis.conf from a template redis.conf could be seen as static and not dynamic, but if you do not write a new redis.conf to use and instead modify the canonical redis.conf on-the-fly when executing redis-server, as I show in my edited answer, this is as dynamic as it gets, considering Redis doesn't support having environment variables in redis.conf. @alexey-melezhik


TestNG Silently kill ExecutorService

Question: I am trying to write a test which uses ScheduledExecutorService to send request to the mocked downstream service in testng but the test return immediately without actually wait for the ScheduledExecutorService to run any background job.
      @Test
    public void test() {
        int theadNum = 32;
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(theadNum);
        Runnable runnable = () -> System.out.println("Thead: " + Thread.currentThread().getName());
        scheduledExecutorService.scheduleAtFixedRate(runnable, 0, 1L, TimeUnit.SECONDS);
    }

Does anyone know why? I guess it's because TestNG does not honor the background thread and only honor the main one but no luck with the search. Any doc or know issue will be appreciated.
Answer: generally java method won't wait for thread to stop. Try awaitTermination for the main java to wait for all the thread to complete.
scheduledExecutorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
Eg: scheduledExecutorService.awaitTermination( 1, TimeUnit.SECONDS);
for more information check javadoc

TestNG Silently kill ExecutorService


Get values from a multi value field?

Question: I have a field that can accept unlimited values. I want to print those values in my template.
The array that I can see with the devel module looks like this....
Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [value] => Dustin
                    [format] =>
                    [safe_value] => Dustin
                )

            [1] => Array
                (
                    [value] => Jen
                    [format] =>
                    [safe_value] => Jen
                )

            [2] => Array
                (
                    [value] => Mike
                    [format] =>
                    [safe_value] => Mike
                )

        )

)

What I'd like to do is have these values printed to a comma separated list like: Dustin, Jen, Mike
I've been using this code to print single value fields in my template but that of course isn't going to work well with a multi value field..
<?php echo $entityform->field_team_name['und'][0]['value']; ?>

Answer: field_get_items() will do the trick:
if ( ($items = field_get_items('entityform', $entityform, 'field_team_name')) ) {
  $values = array_map($items, function($x) { return $x['safe_value']; });
  $comma_separated = implode(', ', $values);
}

Comment: of course, make sure those `['values']` are safe to just print/echo out to the world :)
Comment: Thanks Clive. I have it working now. This is what I ended up with...

`foreach ($items as $item) {
    $members = $prefix . $item['safe_value'];
    $prefix = ', ';
    print $members;
  }`
Comment: Thanks for the help. Its not printing out anything though. I'm printing the $comma_separated variable but its empty.. hmm.
Comment: Hmmm yeah that should work as far as I can tell...does the condition for `field_get_items` get entered? If so, what's in `$items` at that point?
Comment: Yep, $items spits out an array..

Array
(
    [0] => Array
        (
            [value] => Dustin
            [format] =>
            [safe_value] => Dustin
        )

    [1] => Array
        (
            [value] => Jen
            [format] =>
            [safe_value] => Jen
        )

    [2] => Array
        (
            [value] => Mike
            [format] =>
            [safe_value] => Mike
        )

)
Comment: Strange, can't think of any reason why that wouldn't work. You could always loop through: `foreach ($items as $item) { $val = $item['safe_value']; }`, but the anonymous function should really do the same thing

Get values from a multi value field?


Where can I buy knife-edges for a guitar tremolo bridge?

Question: I recently discovered that I need to replace the edge-knives on my chrome black EDGE III tremolo but I cannot find them on the Ibanez website nor on Ebay. Could anyone tell me where to buy them (online or in a shop)?
P.S. I live in Brussels, Belgium
Answer: I don't believe that they are individually replaceable.  I think you will have to order a complete replacement tremolo.
See this site, which says:

The knife edges of the trem become worn or damaged and require filing to get back to a proper knife edge. This is also rarely seen on
  Edge variants [except Edge3!] and some "mating" of the parts is
  actually preferred as they form to each other, and their specific use.
  Cheaper trems are more prone to wear. The same procedure as fixing a
  fat/flat knife edge, see this section to walk you through shaping the
  knives. Note, you do NOT want the knives sharp. You cannot replace
  knife edges if you screw them up or they're shot, Ibanez refuses to
  sell them, or the trem baseplates alone, you need to replace the
  bridge at that point.

Here is the knife edge-sharpening page mentioned.
Answer: Perhaps you'll have to order a new base plate.
Some people try to resharp their, but it has to be really well done, and will probably affect the intonation.
Comment: Fabio - this doesn't add anything over the existing answer.


Create a basic Socket.IO Websocket server

Question: I am writing a websocket server with socket.io but don't know why is not working.
This works:
var WebSocketServer = require('ws').Server;

wss = new WebSocketServer({port: 8100});

wss.on('connection', function(ws) {

    console.log('New connection!');

    ws.on('close', function() {
        console.log('Client connection closed!');
    });

    ws.on('message', function(message) {

    });

});

This runs, but my client is not able to connect:
var io = require('socket.io').listen(8100);

io.sockets.on('connection', function (socket) {
    console.log('New Connection');
});

Source: Book Professional Node.js
The client is a Javascript basic web browser client that works with any server so please disregard client side issues.
Comment: `The client is a Javascript basic web browser client that works with any server` Ah, if life was that easy. Socket.io provides its own client. I don't remember details now but it is possible that the `connection` event is fired not at the moment of conenction but when the client sends some data. That would be done in order to unify the event between different pushing strategies.
Comment: @JackM. `socket.io` is _not_ a WebSocket server.  It is a higher level protocol built on a combination of http and WebSockets, utilizing multiple transports (one of which is WebSockets).  You have to use a `socket.io` client (they exist for many platforms, as it is a well-documented protocol).
Comment: If I can't use my own websocket client with socket.io, then this library is absolutely obsolete for my purpose.
Comment: Well that's up to you. Anyway you've asked the question, so my question to you is: did you try it with the client socket.io provides? I might be wrong and the issue may lay somewhere else.
Comment: Try explicitly setting the transports to websocket only, since socket.io has fallbacks.
Comment: @Ben how do I do that ? I didn't get your point sorry
Answer: Try setting the transports explicitly to websocket only, since socket.io has fallback options for long-polling and flash sockets.
var io = require('socket.io')(8100, {transports: ['websocket']})

io.sockets.on('connection', function (socket) {
    console.log('New Connection');
});

Comment: I feel that the problem is the code really because when my client tries to connect to a wrong port number, you can feel it holding for a second. If you try to connect to the right port 8100, it immediately get Disconnected - Status 3 (without status 1)
Comment: Is there any reason not to use WS?
Comment: I don't know how to scale (multiple server instances) with WS and not sure if it works well behind proxy/load balancer
Comment: I see. Scaling is far easier with socket.io as you can just plug it into redis. What's the reason for not using the socket.io client?
Comment: The client is a Windows/Mac application, not a webpage.
Comment: You might want to change your question then, since you've stated that it's a Javascript client.


What is the bijection in this combinatorics example?

Question:
Example 3.19. A medical student has to work in a hospital for five
  days in January. However, he is not allowed to work two consecutive
  days in the hospital. In how many different ways can he choose the
  five days he will work in the hospital?
Solution. The difficulty here is to make sure that we do not choose
  two consecutive days. This can be assured by the following trick. Let
  $a_1$, $a_2$, $a_3$, $a_4$, $a_5$ be the dates of the five days of January that the
  student will spend in the hospital, in increasing order. Note that the
  requirement that there are no two consecutive numbers among the $a_i$,
  and $1 \le a_i \le 31$ for all $i$ is equivalent to the requirement that
  $1 \le a_1 \lt a_2 - 1 \lt a_3 - 2 \lt a_4 - 3 \lt a_5 - 4 \le 27$. In other words,
  there is
  an obvious bijection between the set of 5-element subsets of $[31]$
  containing no two consecutive elements and the set of 5-element
  subsets of [27]. Instead of choosing the numbers $a_i$, we can choose the
  numbers $1 \le a_1 \lt a_2 - 1 \lt a_3 - 2 \lt a_4 - 3 \lt a_5 - 4 \le 27$, that is, we
  can simply choose a five-element subset of $[27]$, and we know that
  there are $27 \choose 5$ ways to do that.

Note: the bracket notation $[27]$ means $\{1,2,3,...27\}$.
Was reading this example from A Walkthrough Combinatorics (Bona), but I'm not really sure I follow what the "obvious bijection" is suppose to be.  Does this mean that if I choose a random five-element subset of $[27]$ say: $\{1,7,27,25,6\}$ then ordered it from least to greatest: $\{1,6,7,25,27\}$ then to map to the actual days (the five-element subset of $[31]$) I would compute:
$\{1+0, 6+1, 7+2, 25+3, 27+4\} = \{1, 7, 9, 28, 31\}$ So the bijection (to map from a five element set of [27]) is to add 0,1,2,3,4 to that ordered set? (Sorry not really sure how to express this in more mathematical notation)
Answer: The following does not precisely answer your bijection question, but it gives what we believe is a quick way to see what the answer is.
There will be $26$ non-work days. To represent them, put $26$ $\times$ (or perhaps beer bottles) in a row, somewhat separated from each other, like this:
$$\times\quad\times \quad\times\quad\times\quad \times\quad\times \quad\times\quad \dots\dots \quad\times\quad\times\quad\times \quad\times$$
These determine $27$ "gaps." We are including the two invisible endgaps.
We need to choose $5$ of these gaps as workdays.
Remark: If one really wants to use the word, there is a bijection from the set of workday choices to the set of choices of $5$ gaps: once we know the workdays, we will know which gaps were chosen and vice-versa.
Comment: Thanks your use of the X's and gaps really helped me see this.
Comment: As I thought more on it, I think the bijection would be $f(a_1,a_2,a_3,a_4,a_5) = (a_1,a_2+1,a_3+2,a_4+3,a_5+4)$ To go from say gap numbers to day numbers.
Comment: Yes, that is correct, you advance by $1$ each time. And of course the idea generalizes.
Comment: I was still pondering this question, and I think I understand better the "invisible" endgaps.  Those are the start/end of some other Months (e.g. Dec & Feb) so that's why we include them.  Had Jan wrapped around on itself then one end gap would be lost.
Comment: There is a difference of treatment, albeit not a very large one, between ordinary permutations and the "circular" permutations, precisely because of the "wraparound."


Orbit of an element of the group $G$ as opposed to the orbit of an element of a set acted upon by the group $G.$

Question: At this point of this lecture by Professor Macauley at Clemson University the following statement appears:

Every element in a group traces out an orbit.

I am trying to familiarize with group theory concepts a bit (beginner), and find this confusing because I thought orbits applied to group actions so that considering the action of group $G$ on a set $X,$ and taking an element of $x \in X,$ its orbit would be all the results of the binary operation of this action of $G$ on $x:$
$$G.x=\{g.x\;|\;g\in G\}$$
However, if $x$ is not an element of some set $X,$ separate from the set $G$ that defines the group, but rather is an element of $G,$ i.e. $x\in G,$ wouldn't then, $G.x=G?$ In other words, the result of all the elements of the set constituting $G,$ acting on any given element $x\in G,$ would reproduce the entire set $G?$ The orbit of any given element would then be the entire group, resulting in a single partition without much interest...
Am I confusing "acting on" with "composed with" as in $Gx =\{g\;\circ\; x\;|\;g\in G\}, $ where $\circ$ denotes the group operation, and where every element of the group appears once and only once?
Comment: He is calling orbit of $g$ to $\{e,g,g^2,g^3,...\}$
Answer: There is a way to make the "orbit" as he calls it the actual orbit of an action (the "orbit" of $g \in G$ is the orbit of $e$ under the left multiplication action of $\langle g\rangle$ on $G$) but this isn't necessary.  I don't think he really means for you to be thinking about actions at this point, he just means the path you get through the Cayley diagram by multiplying by $g$ repeatedly.  Since this path path forms a loop starting at $e$ he's just informally calling this loop an orbit.


Unconditional basis for a Hilbert space $H$ that is not a Riesz basis for $H$.

Question: I would like to find a sequence $\{x_n\}\subset H$ that is an unconditional basis for a Hilbert space $H$ that is not a Riesz basis for $H$.
This is Exercise 7.15 in A Basis Theory Primer, Christopher Heil. I am just reading this book and not a homework.
Thought:
Heil's book Theorem 7.13 says a sequence $\{h_n\}\subset H$ is a Riesz basis for $H$ if and only if $\{h_n\}$ is a bounded unconditional basis for $H$.
Thus, any example $\{x_n\}$ desired must be unbounded:
$$
 \text{Either}\quad \inf_{n}\|x_n\|_{H}=0\ \text{ or }\ \sup_{n}\|x_n\|_{H}=\infty\quad \text{must hold.}
$$
 Further, from the assumption for any $x\in H$ we have the unique representation
$
x=\sum_{n}a_n(x)x_n
$
with scalars $a_n(x)$, and the series is unconditionally convergent. This means, in view of Orlicz's Theorem,
$$
\sum_{n}|a_n(x)|^2\|x_n\|_H^2<\infty.
$$
So, roughly if we decide to construct $\{x_n\}$ such that $\sup_{n}\|x_n\|_{H}=\infty$, we want $x_n$ that gives coefficient functionals of small value $a_n(x)$ for any $x\in H$? But this doesn't really tell me much...
Answer: Just start with the standard basis $(e_n)$ for $\ell^2$. Now consider $(ne_n)$, which is still an unconditional basis for $\ell^2$, as each $x\in \ell^2$ can be written as
$$ x = \sum_n x_n e_n = \sum_n \frac{x_n}n \cdot ne_n $$
But it is unbounded, as $\|ne_n\| = n\|e_n\| = n$.
Comment: Ah. Likewise, if we consider $(\frac{e_n}{n})$ then $\inf_n\|e_n/n\|=0$, huh...thank you!


Unbiased Estimators and Heteroskedasticity

Question: Consider a consumption model with bivariate data points $(Y_i,X_i)$, $i=1,...,n$, with $Y_i$ consumption and $X_i$ income. The univariate model is
$$Y_i=\beta X_i+u_i,$$
where $E(u_i|X_i)=0, \textrm{var}(u_i|X_i)=\sigma^2$ and $\textrm{cov}(u_i,u_j|X_i)=0$.
The estimates of $\beta$ are given as follows:
$$\hat{\beta_1}=\frac{\sum_{i=1}^{n}Y_i}{\sum_{i=1}^{n}X_i}$$
and
$$\hat{\beta_2}=\frac{\sum_{i=1}^{n}X_iY_i}{\sum_{i=1}^{n}X_i^2}.$$
a.) Show that $\hat{\beta_1}$ and $\hat{\beta_2}$ are unbiased. Also, which estimator is 'better'.
I am not sure how to prove that both are unbiased. I think we begin by taking the expected values and then reduce it to the form of $E(\hat{\beta_1})=\beta$ and $E(\hat{\beta_2})=\beta$, but I am not sure how to do this mathematically.
To consider which is better, since both are unbiased, I would look at which has the smallest variance, but to avoid calculating this, would it be sufficient to talk about the Gauss-Markov conditions and state which one satisfies these conditions? Because the one that satisfies these conditions will have the smallest variance possible. How would I show which $\hat{\beta}$ does not satisfy the Gauss-Markov conditions? I believe that it is $\hat{\beta_1}$ but I am unsure of how to show this.
b.) If the residuals of the model are linearly related to the income so that $\textrm{var}(u_i|X_i)=\sigma^2X_i$, how could one verify if this is the case? Ie.: suggest a test.
I think you would write the OLS, take residuals and square them, then run the regression. If we assume linearity then we could use the Breusch-Pagan test with the following model:
$$\hat{u_i^2}=\sigma^2+\sum_{j=1}^{k}\delta_jX_{ij}+\epsilon_i,$$
where $\sum_{j=1}^{k}\delta_jX_{ij}+\epsilon_i=0$ by the homoskedasticity assumption.
So this would give us:
$H_0: \delta_1=\delta_2=...=\delta_k=0$ and $H_1: \textrm{at least one } \delta \neq 0$.
Then I think you would run an F-test since this is a joint hypothesis. We would then reject the null hypothesis if we cannot accept that all $\delta$'s equal $0$. And we would conclude that there exists heteroskedasticity in at least one of the $X$'s.
Is this correct? And how would be compute the F-statistic?
c.) If heteroskedasticity does exist, then will the estimators still be unbiased? And which estimator is now considered 'better'?
I would say that the estimators are still unbiased as the presence of heteroskedasticity affects the standard errors, not the means. So the condition for unbiasedness still holds ($E(u_i|X_i)=0$). I think the  better estimate is now $\hat{\beta_1}$ because it is an optimum WLS (weighted least squares) estimator as it minimises $\textrm{var}(u_i|X_i)=\sigma^2X_i$, however I am unsure of how to prove this formally.
d.) If consumption also depends on interest $R_i$ so that the consumer may need to borrow in order to fund certain forms of expenditure), this would give the following model:
$$Y_i=\beta X_i+\gamma R_i+u_i,$$
where $E(u_i|X_i, R_i)=0, \textrm{var}(u_i|X_i, R_i)=\sigma^2X_i$ and $\textrm{cov}(u_i,u_j|X_i, R_i)=0$.
What happens to the unbiasedness of the the $\hat{\beta_1}$ and $\hat{\beta_2}$ estimators? What conditions will make them unbiased?
Here, I think that again $\hat{\beta_1}$ and $\hat{\beta_2}$ are still unbiased as the $E(u_i|X_i, R_i)=0$. However, I am not sure because of the introduction of the $R_i$ term. Also, to make them unbiased would it be correct to say that the only condition that would make them unbiased is when $E(u_i|X_i)\neq0$? Or are there other conditions which can cause unbiasedness?
Any help with any of these questions will be really appreciated. Thank you!
Answer: Both in case of unbiasedness and variance-connected issues (like efficiency or heteroskedasticity), first of all, I would recommend plugging your equation on $Y$ into formulas for $\beta_{1} / \beta_{2}$. Thus (for $Y_{i} = \beta X_{i} + u_{i}$) we get:
$$
\beta_{1}= \beta + \frac{\sum_{i=1}^{n}u_{i}}{\sum_{i=1}^{n}X_{i}} \\
\beta_{2}= \beta + \frac{\sum_{i=1}^{n}X_{i}u_{i}}{\sum_{i=1}^{n}X_{i}^{2}} \\
$$
Since $E(u_{i}|X_{i}) = 0$, you can see clearly (Tipp: $E(u_{i})=E(E(u_{i}|X_{i})$), why both estimators are unbiased and why there is a problem if we put $Y_{i} = \beta X_{i} + \gamma R_{i} + u_{i}$ inside $\beta_{1} / \beta_{2}$ (instead of $Y_{i} = \beta X_{i} + u_{i}$).
Moreover, calculating OLS estimator ($\beta_{OLS}=(X^{'}X)^{-1}X^{'}y$) for given model, we obtain $\beta_{2} = \beta_{OLS}$. Gauss-Markov Theorem gives you that $\beta_{2}$ has lower variance (both estimators are linear in $Y$ and $\beta_{2}$ is BLUE). Alternatively, you could calculate variance of both estimators directly from definition:
$$
Var(\beta_{2})=Var(\beta + \frac{\sum_{i=1}^{n}X_{i}u_{i}}{\sum_{i=1}^{n}X_{i}^{2}})=...
$$
remembering that $Var(u_{i}) = E(Var(u_{i}|X_{i})) + Var(E(u_{i}|X_{i}))$. This approach might be particularly useful in c) since you can no longer use Gauss-Markov Theorem (we have heteroskedasticity!). I hope you can manage to do this; to find out which variance is lower you can use Cauchy-Schwarz inequality (https://en.wikipedia.org/wiki/Cauchy%E2%80%93Schwarz_inequality).
Last but not least, heteroskedasticity has nothing with biasedness or unbiasedness of your estimators (the only thing you should provide is $E(u_{i}|X_{i})=0$). Back to b), your approach to use Breusch - Pagan test seems correct for me.
Comment: upvote is appreciated :)


Mapbox, cannot setGeoJSON in jquery callback

Question: I want through Jquery retrieve some geojson points data (i.e. set some markers in a map) from a database. The
function remm(id){
$.ajax({
    type: "POST",
    url:"geojson2.php",
    data:{
        u_id:id[0],
        t_id:id[2]
    },
    success: function(geo,status){
        myLayer.setGeoJSON([geo]);
        console.log(geo);
    }
  });
}

When testing the points data retrieved from geojson2.php in GeoJSON Lint it works perfectly well. However, when calling the myLayer.setGeoJSON() function it only takes all existing markers away. myLayer is instantiated at the beginning of the javascript file and looks like:
    L.mapbox.accessToken = 'key';
    var map = L.mapbox.map('map', 'mapbox.streets').setView([40, 0], 1);
    var myLayer = L.mapbox.featureLayer().addTo(map);

Since it takes away all markers from the map it doesn't seem to be that myLayer is not available to the callback function in the ajax. What could be the issue and what am I missing?
Answer: The .setGeoJSON method replaces GeoJSON - it's setting GeoJSON, not adding individual features. If you'd like to add new features instead of replacing your existing content, I'd recommend creating a new layer for the features you're loading with jQuery, with the L.mapbox.featureLayer method, and adding it to the map.
Comment: I've tried that but that doesn't work either. Is the instantiated 'map' and 'myLayer' objects global so that all functions, no matter where you have it in the script (regular js functions, jquery callbacks, etc.)? @tmcw
Comment: > Is the instantiated 'map' and 'myLayer' objects global so that all functions, no matter where you have it in the script

That's entirely dependent on where you instantiate them. If you create them with `var` and instantiate them outside of any function or closure, then yes, they're global.
Comment: They are only instantiated directly inside the script with var so I guess they become global in that case. Thanks! @tmcw


Content is not filter with conceptual filter in views

Question: I am using two view one view as a page with exposed filter and  another view as a block and filter the content by getting Value from url in  contextual filter.
Exp:http://exp.com/?field_project_catagory_tid=112&field_nd_portfolio_tid=92
If we use one conceptual filter this http://exp.com/field_project_catagory_tid=112
and  get the value with use the PHP code in conceptual filter
$path=drupal_get_path_alias($_GET["field_project_catagory_tid"]);
return $path;
Its working fine and listed the value in both the views
If  we pass one more filter then we have url Like this
 http://exp.com/?field_project_catagory_tid=112&field_nd_portfolio_tid=92
and it works collectively fine.
but if user filter with one filter it is not working.
Then how we can use the value in contextual filter for individually also. Please help
Answer: Use all instead of id in any one parameter if you want to consider only one parameter.
e.g. like
http://exp.com/?field_project_catagory_tid=112&field_nd_portfolio_tid=all

or
http://exp.com/?field_project_catagory_tid=all&field_nd_portfolio_tid=112

Comment: thanks for reply.
let me know how we can use the all in conceptual filter because it will comes from exposed filter, and getting from url with

PHP code

$path=drupal_get_path_alias($_GET["field_project_catagory_tid"]); return $path;


SVG.js - Export nested SVG with Clip intact?

Question: I have an SVG.js canvas that has a nested artwork and a nested UI on top that controls it. I want to export the artwork which is clipped. The issue is the Defs and SVG headers are at the root so they're not part of the nested SVG's getter response (svgString below).
canvas = SVG()
artwork = canvas.nested()
artworkLimit = canvas.rect(100,100)
clip = canvas.clip().add(artworkLimit)
artwork.clipWith(clip)
/* Add Artwork Here */
svgString = artwork.svg()

I was hoping that there was a built-in way to export a nested SVG as you see it on the screen but I can't find it in the docs. I've also tried putting the clip within the nested SVGs and making containers but the Defs is always on the root SVG object.
Is the intended solution?

A. Export the whole SVG.js object but using the SVG(function(node){...}) modifier to exclude the the UI and retain the Defs and headers and also undo the rotation, scaling and panning that is done to the artwork. Downside is the user will see it flicker as it has to reposition.
B. Just use the nested artboard SVG getter and do some find and replace on the string to insert the headers and defs. Fiddly but helps the user experience
C. Rebuild the artwork layer in a second SVG canvas/object and export that instead. Canvas can be off screen and no change in what the user sees. (Also better for bitmaps exports)
D. Something better?
Answer: If the original svg can be destroyed after the export you can use the following. Otherwise you might need to clone first:
const nestedNode = reference to your nested nestedNode.addTo(someDiv)
rootSvg.defs().addTo(nestedNode)
nestedSvg.svg() // svg string

So you are basically moving the nested svg into its own div (which makes it a root svg) and then transplant the defs.
If your root svg has a viewbox you might need to copy that as well.
Comment: I tried the suggested code above but it was applying the clip-path at the root SVG and the Defs were within that and it didn't work.

So I needed a intermediary container SVG first and then adding the nestedNode to that, then reset all the pan and scaling of the artwork layer.
Comment: Yeah I wasn't sure about your setup. But as long as the concept works... :D


Exportar Kendo Grid para PDF

Question: Tenho uma função que exporta para Excel e preciso de outra que exporte para PDF.  A função deverá exportar os dados de uma Kendo Grid.
<code>function ExportarParaExcel(grid) {
   grid = grid == undefined ? "#grid" : grid;
   var grd = $(grid).data("kendoGrid");
   grd.bind("excelExport", function (e) {
       var rows = e.workbook.sheets[0].rows;
       for (var ri = 0; ri < rows.length; ri++) {
           var row = rows[ri];
           var dd = '';
           if (row.type == "group-header" || row.type == "header" || row.type == "group-footer" || row.type == "footer") {
               for (var ci = 0; ci < row.cells.length; ci++) {
                   var cell = row.cells[ci];
                   if (cell.value) {
                       cell.value = ajustaAcentosXLS(cell.value);
                   }
               }
           }
       }
   });
   grd.saveAsExcel();
}
</code>
Answer: Encontrado a solução na pergunta do Stack Overflow (Inglês). Ela soluciona o problema de exportação, mas identifiquei que para o meu caso, é melhor exportar somente para Excel.

Exportar Kendo Grid para PDF