问:
How to perform a numerical Laplace and inverse Laplace transforms in Matlab for a discrete input signal?
I want to evaluate the Laplace transform of a discrete stochastic signal I sample from a communication device.

As a simple test case I am trying to use the ilaplace to get the original signal, but I am not sure if my approach is plausible.

```<code>x = sym('x','real');
y = sym('y','real');
t=linspace(0,1000,1000);
f=sin(t);
s = x+i*y;
F_s=sum(f.*exp(-s*t));
ilaplace(F_s)
</code>```

The above might seem silly, though in my real problem I am trying to estimate the medium Green's function which is of the formilaplace(2*F_s/(-s*F_s +f(0))).

I have also tried to use a signal symbolic variable s and it gives me a train of deltas which I am not sure it's correct and what is the error estimation.

```<code>syms s;
F_s = symfun(sum(sin(t).*exp(-s*t)), s);
ilaplace(F_s)
</code>```

答:
Ok so that's the way I used matlab to do laplace transform for a discrete signal and recovered it back using ilaplace for validation purposes:

```<code>t=linspace(0,10,500);
f=exp(-t/0.2);
syms s;
F_s = sum(f.*exp(-s*t));
f_t = (ilaplace(F_s));
F_t = (int(f_t));

y=subs(F_t,t);
Ft_recovered = diff(double(y));

subplot(2,2,1)
plot(t,f)
title('numerical input exp(-t/0.2)')

subplot(2,2,2)
ezplot(F_s)
title('numerical laplace')

subplot(2,2,3)
plot(t(1:end-1), Ft_recovered)
title('recovered signal')

subplot(2,2,4)
syms x;
fx = symfun(exp(-x/0.2),x);
ezplot(laplace(fx))
title('symbolic laplace transform')
</code>```

The big difficulty here is to make matlab to evaluate the sum of the dirac delta train, so I did this trick: integrated the expression to convert it to train of heaviside functions, evaluate it and then plot the derivative:

```<code>F_t = (int(f_t));
y=subs(F_t,t);
Ft_recovered = diff(double(y));
</code>```

Body:
I want to evaluate the Laplace transform of a discrete stochastic signal I sample from a communication device.

As a simple test case I am trying to use the ilaplace to get the original signal, but I am not sure if my approach is plausible.

x = sym('x','real');
y = sym('y','real');
t=linspace(0,1000,1000);
f=sin(t);
s = x+i*y;
F_s=sum(f.*exp(-s*t));
ilaplace(F_s)

The above might seem silly, though in my real problem I am trying to estimate the medium Green's function which is of the formilaplace(2*F_s/(-s*F_s +f(0))).

I have also tried to use a signal symbolic variable s and it gives me a train of deltas which I am not sure it's correct and what is the error estimation.

syms s;
F_s = symfun(sum(sin(t).*exp(-s*t)), s);
ilaplace(F_s)

Body:
Ok so that's the way I used matlab to do laplace transform for a discrete signal and recovered it back using ilaplace for validation purposes:

t=linspace(0,10,500);
f=exp(-t/0.2);
syms s;
F_s = sum(f.*exp(-s*t));
f_t = (ilaplace(F_s));
F_t = (int(f_t));

y=subs(F_t,t);
Ft_recovered = diff(double(y));

subplot(2,2,1)
plot(t,f)
title('numerical input exp(-t/0.2)')

subplot(2,2,2)
ezplot(F_s)
title('numerical laplace')

subplot(2,2,3)
plot(t(1:end-1), Ft_recovered)
title('recovered signal')

subplot(2,2,4)
syms x;
fx = symfun(exp(-x/0.2),x);
ezplot(laplace(fx))
title('symbolic laplace transform')

The big difficulty here is to make matlab to evaluate the sum of the dirac delta train, so I did this trick: integrated the expression to convert it to train of heaviside functions, evaluate it and then plot the derivative:

F_t = (int(f_t));
y=subs(F_t,t);
Ft_recovered = diff(double(y));


来源: stackexchange

问:
Some questions about cuda streams
Code:

```<code>__global__ void K1() {
    int p=1;
    for(int i=0; i<100000; ++i)
        for(int j=0; j<100000; ++j)
            p*=(i+100)*j;
    printf("K1\n");
}
__global__ void K2() {
    printf("K2\n");
}

int main() {
    int *ptr;
    cudaStream_t s1, s2;
    cudaStreamCreate(&s1);
    cudaStreamCreate(&s2);
    K1<<<1, 1, 0, s1>>>();
    cudaHostAlloc(&ptr, 1, 0);
    K2<<<1, 1, 0, s2>>>();
    cudaDeviceSynchronize();
    return 0;
}
</code>```

Output:

```<code>K2
K1
</code>```

Questions:

Is s1 same as default stream?

As per this documentation,

Two commands from different streams cannot run concurrently if any one of the following operations is issued in-between them by the host thread:

a page-locked host memory allocation,

shouldn't K2 start after K1 finish?

答:
On the first point, s1 is not the default stream.

On the second point, running you code on a small system, I got the following profiler timeline.

My understanding is that there is a delay between the scheduling of a kernel launch and its actual launch. This is no surprise as launch are asynchronous with streams. As a result, the cudaHostAlloc occurs before any kernel launch. It occurs inbetween kernel call schedules, BUT before any kernel call.

ContentLicense: CC BY-SA 3.0

Title: Some questions about cuda streams

Body:
Code:

__global__ void K1() {
    int p=1;
    for(int i=0; i<100000; ++i)
        for(int j=0; j<100000; ++j)
            p*=(i+100)*j;
    printf("K1\n");
}
__global__ void K2() {
    printf("K2\n");
}

int main() {
    int *ptr;
    cudaStream_t s1, s2;
    cudaStreamCreate(&s1);
    cudaStreamCreate(&s2);
    K1<<<1, 1, 0, s1>>>();
    cudaHostAlloc(&ptr, 1, 0);
    K2<<<1, 1, 0, s2>>>();
    cudaDeviceSynchronize();
    return 0;
}

Output:

K2
K1

Questions:

Is s1 same as default stream?

As per this documentation,

Two commands from different streams cannot run concurrently if any one of the following operations is issued in-between them by the host thread:

a page-locked host memory allocation,

shouldn't K2 start after K1 finish?


问:
Selenium Webscraping Twitter - Getting hold of tweet timestamp?
When inspecting a twitter results page, within the following class:

```<code>    <small class="time">
         ....
    </small>
</code>```

Is a timestamp for each tweet 'data-time':

```<code><span class="_timestamp js-short-timestamp js-relative-timestamp" data-time="1510698047" data-time-ms="1510698047000" data-long-form="true" aria-hidden="true">12m</span>
</code>```

Within selenium i am using the following code:

```<code>tweet_date = browser.find_elements_by_class_name('_timestamp')
</code>```

But looking at a single entry only returns, in this case, 12m.

How is it possible to access one of the other properties within the class within selenium?

答:
Thank you to SuperStew who found the key to the answer - get_attribute()

My final solution for anyone wondering:

```<code>tweet_date = browser.find_elements_by_class_name("_timestamp")
</code>```

And then for any date in that list:

```<code>tweet_date[1].get_attribute('data-time')
</code>```

Body:
When inspecting a twitter results page, within the following class:

         ....

Is a timestamp for each tweet 'data-time':

12m

Within selenium i am using the following code:

tweet_date = browser.find_elements_by_class_name('_timestamp')

But looking at a single entry only returns, in this case, 12m.

How is it possible to access one of the other properties within the class within selenium?

Body:
Thank you to SuperStew who found the key to the answer - get_attribute()

My final solution for anyone wondering:

tweet_date = browser.find_elements_by_class_name("_timestamp")

And then for any date in that list:

tweet_date[1].get_attribute('data-time')


问:
Can't extract mp3 audio from video with ffmpeg
My input is a .mp4 video file.

Try #1

```<code>ffmpeg -i pipe:0 -y -map a media/73.mp3
</code>```

Error:

```<code>ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] stream 1, offset 0xa8: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none(smpte170m/smpte170m/bt709), 320x176, 300 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:0':
  Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: mp42isomavc1
creation_time   : 2012-03-13T08:58:06.000000Z
encoder         : HandBrake 0.9.6 2012022800
  Duration: 00:00:10.03, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none(smpte170m/smpte170m/bt709), 320x176, 300 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time   : 2012-03-13T08:58:06.000000Z
  encoder         : JVT/AVC Coding
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream #0:2(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream #0:3(und): Subtitle: mov_text (text / 0x74786574), 0 kb/s
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream mapping:
  Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
  Stream #0:2 -> #0:1 (aac (native) -> mp3 (libmp3lame))
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] stream 1, offset 0xa8: partial file
pipe:0: Invalid data found when processing input
Finishing stream 0:0 without any data written to it.
Finishing stream 0:1 without any data written to it.
[mp3 @ 0x7f977b846800] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:1 --
Conversion failed!
</code>```

Try #2

```<code>ffmpeg -i pipe:0 -y -vn -acodec copy media/72.mp3
</code>```

Error:

```<code>[...]
[mp3 @ 0x7febed835200] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
</code>```

Try #3

```<code>ffmpeg -i pipe:0 -y -codec:a libmp3lame -qscale:a 2 media/74.mp3
</code>```

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

Try #4

```<code>ffmpeg -i pipe:0 -y -vn -acodec mp3 media/78.mp3
</code>```

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

Try #5

```<code>ffmpeg -f mp4 -i pipe:0 -y media/80.mp3
</code>```

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

What am I doing wrong? This seems like a fairly easy thing to do...

答:
Try this for extract .mp3:

```<code>ffmpeg -i video.mp4 -map 0:a -acodec libmp3lame out.mp3
</code>```

And maybe this useful for extract .m4a:

```<code>ffmpeg -i video.mp4 -map 0:a -c copy out.m4a
</code>```

Body:
My input is a .mp4 video file.

Try #1

ffmpeg -i pipe:0 -y -map a media/73.mp3

Error:

ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] stream 1, offset 0xa8: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none(smpte170m/smpte170m/bt709), 320x176, 300 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:0':
  Metadata:
major_brand     : mp42
minor_version   : 0
compatible_brands: mp42isomavc1
creation_time   : 2012-03-13T08:58:06.000000Z
encoder         : HandBrake 0.9.6 2012022800
  Duration: 00:00:10.03, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none(smpte170m/smpte170m/bt709), 320x176, 300 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time   : 2012-03-13T08:58:06.000000Z
  encoder         : JVT/AVC Coding
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream #0:2(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 160 kb/s
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream #0:3(und): Subtitle: mov_text (text / 0x74786574), 0 kb/s
Metadata:
  creation_time   : 2012-03-13T08:58:06.000000Z
Stream mapping:
  Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
  Stream #0:2 -> #0:1 (aac (native) -> mp3 (libmp3lame))
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f977d800000] stream 1, offset 0xa8: partial file
pipe:0: Invalid data found when processing input
Finishing stream 0:0 without any data written to it.
Finishing stream 0:1 without any data written to it.
[mp3 @ 0x7f977b846800] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:1 --
Conversion failed!

Try #2

ffmpeg -i pipe:0 -y -vn -acodec copy media/72.mp3

Error:

[...]
[mp3 @ 0x7febed835200] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

Try #3

ffmpeg -i pipe:0 -y -codec:a libmp3lame -qscale:a 2 media/74.mp3

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

Try #4

ffmpeg -i pipe:0 -y -vn -acodec mp3 media/78.mp3

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

Try #5

ffmpeg -f mp4 -i pipe:0 -y media/80.mp3

No error with ffmpeg, but the file is invalid (it's size is 334 bytes)

What am I doing wrong? This seems like a fairly easy thing to do...

Body:
Try this for extract .mp3:

ffmpeg -i video.mp4 -map 0:a -acodec libmp3lame out.mp3

And maybe this useful for extract .m4a:

ffmpeg -i video.mp4 -map 0:a -c copy out.m4a


问:
android.view.InflateException: Binary XML file line #0: Binary XML file line #0:
I am new in android studio and I would like a little help, I'm sorry for my bad English, it's not my native language.

When wanting to enter an activity in my application. This is over, here is the code:

```<code>W/System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kefagothstudios.cleaner/com.kefagoth.cleaner.ui.RubbishCleanActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.ferdi.cleaner.widget.textcounter.CounterView
</code>```

this is the activity that fails

```<code><FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:counter="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/layout_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/header_height"
        android:background="@color/yellow_background"
        android:orientation="vertical"
        android:visibility="visible">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <com.ferdi.cleaner.widget.textcounter.CounterView
                android:id="@+id/textCounter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\ 250"
                android:textColor="@color/white"
                android:textSize="52.0sp"
                counter:autoStart="false" />

            <TextView
                android:id="@+id/sufix"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_right"
                android:layout_marginTop="10.0dip"
                android:layout_toRightOf="@+id/textCounter"
                android:text="KB"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_middle" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/sufix"
                android:layout_marginLeft="@dimen/margin_right"
                android:layout_marginTop="10.0dip"
                android:layout_toRightOf="@+id/textCounter"
                android:text="Freeable"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_middle" />
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:background="?android:windowBackground"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <fr.castorflex.android.circularprogressbar.CircularProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="50.0dip"
            android:layout_height="50.0dip"
            android:indeterminate="true"
            android:visibility="gone"
            counter:cpb_colors="@array/gplus_colors" />

        <com.ferdi.cleaner.views.RotateLoading
            android:id="@+id/rotate_loading"
            android:layout_width="88.0dip"
            android:layout_height="88.0dip"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/progressBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="4.0dip"
            android:singleLine="true"
            android:text="@string/scanning"
            android:textAppearance="?android:textAppearanceSmall" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/memory_ads_container"
        android:layout_below="@+id/header"
        android:clipToPadding="false"
        android:divider="@null"
        android:dividerHeight="4.0dip"
        android:fadingEdge="none"
        android:fastScrollEnabled="true"
        android:fitsSystemWindows="true"
        android:focusable="true"
        android:layoutAnimation="@anim/listview_layout_animation"
        android:listSelector="@android:color/transparent"
        android:padding="12.0dip"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="none"
        android:visibility="gone" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="@string/empty_cache"
        android:textAppearance="?android:textAppearanceLarge"
        android:visibility="gone" />

    <FrameLayout
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/memory_ads_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="fill_horizontal|bottom|center"
        android:layout_marginBottom="@dimen/button_height"
        android:visibility="visible">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adsView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/bottom_lin"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentBottom="true"
        android:layout_margin="@dimen/button_margin"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:visibility="visible">

        <Button
            android:id="@+id/clear_button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/btn_normal_green"
            android:gravity="center"
            android:text="Clean Junk"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_large" />
    </LinearLayout>
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:app1="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_container2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroud_layout"
    android:orientation="vertical"
    android:visibility="gone">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="192.0dip"
            android:layout_height="192.0dip"
            android:layout_centerInParent="true"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <com.ferdi.cleaner.views.HoloCircularProgressBar
                    android:id="@+id/clean_done_holoCircularProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    app1:marker_progress="0.0"
                    app1:marker_visible="false"
                    app1:progress="1"
                    app1:progress_background_color="@color/circle_progress_background"
                    app1:progress_color="@color/circle_progress"
                    app1:stroke_width="6.0dip" />

                <ImageView
                    android:id="@+id/clean_done_iv_bg"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="16.0dip"
                    android:background="@drawable/circle_floating_bg"
                    android:rotation="-30.0"
                    android:visibility="visible" />

                <ImageView
                    android:id="@+id/clean_done_iv_done"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="40.0dip"
                    android:src="@drawable/ic_cleaning"
                    android:visibility="visible" />
            </FrameLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:gravity="center">

                <ImageView
                    android:id="@+id/clean_up_rocket"
                    android:layout_width="112.0dip"
                    android:layout_height="112.0dip"
                    android:layout_gravity="center" />
            </LinearLayout>
        </FrameLayout>

        <TextView
            android:id="@+id/clean_up_done_tv_notice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16.0dip"
            android:layout_marginRight="16.0dip"
            android:layout_marginTop="32.0dip"
            android:gravity="center"
            android:text="@string/boost_notice"
            android:textColor="@color/white"
            android:textSize="12.0sp"
            android:visibility="gone" />

        <FrameLayout
            android:id="@+id/clean_up_done_icon_container"
            android:layout_width="144.0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingTop="32.0dip">

            <TextView
                android:id="@+id/clean_up_tv_done"
                android:layout_width="wrap_content"
                android:layout_height="48.0dip"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="32.0dip"
                android:background="@drawable/button_done_selector"
                android:gravity="center"
                android:minWidth="64.0dip"
                android:paddingBottom="8.0dip"
                android:paddingLeft="32.0dip"
                android:paddingRight="32.0dip"
                android:paddingTop="8.0dip"
                android:text="DONE"
                android:textColor="@color/phoneboost_text_top_color_white_blue"
                android:textSize="16.0sp"
                android:typeface="sans"
                android:visibility="gone" />
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>
</code>```

my application stops and I do not understand why, I would like an idea of why it stops, in advance thank you very much

答:
One issue that I definitely see is 'Android won't be able to identify app1 namespace'.

```<code><com.ferdi.cleaner.views.HoloCircularProgressBar
                    android:id="@+id/clean_done_holoCircularProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    app1:marker_progress="0.0"
                    app1:marker_visible="false"
                    app1:progress="1" app1:progress_background_color="@color/circle_progress_background"
                    app1:progress_color="@color/circle_progress"
                    app1:stroke_width="6.0dip" />
</code>```

But since the inflateException mentions the issue with com.ferdi.cleaner.widget.textcounter.CounterView as well, there seems to be another issue with the attribute definition of the counter view. Please verify the same.

Body:
I am new in android studio and I would like a little help, I'm sorry for my bad English, it's not my native language.

When wanting to enter an activity in my application. This is over, here is the code:

W/System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kefagothstudios.cleaner/com.kefagoth.cleaner.ui.RubbishCleanActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.ferdi.cleaner.widget.textcounter.CounterView

this is the activity that fails

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:counter="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/layout_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/header_height"
        android:background="@color/yellow_background"
        android:orientation="vertical"
        android:visibility="visible">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <com.ferdi.cleaner.widget.textcounter.CounterView
                android:id="@+id/textCounter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\ 250"
                android:textColor="@color/white"
                android:textSize="52.0sp"
                counter:autoStart="false" />

            <TextView
                android:id="@+id/sufix"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_right"
                android:layout_marginTop="10.0dip"
                android:layout_toRightOf="@+id/textCounter"
                android:text="KB"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_middle" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/sufix"
                android:layout_marginLeft="@dimen/margin_right"
                android:layout_marginTop="10.0dip"
                android:layout_toRightOf="@+id/textCounter"
                android:text="Freeable"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_middle" />
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:background="?android:windowBackground"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <fr.castorflex.android.circularprogressbar.CircularProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="50.0dip"
            android:layout_height="50.0dip"
            android:indeterminate="true"
            android:visibility="gone"
            counter:cpb_colors="@array/gplus_colors" />

        <com.ferdi.cleaner.views.RotateLoading
            android:id="@+id/rotate_loading"
            android:layout_width="88.0dip"
            android:layout_height="88.0dip"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/progressBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="4.0dip"
            android:singleLine="true"
            android:text="@string/scanning"
            android:textAppearance="?android:textAppearanceSmall" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/memory_ads_container"
        android:layout_below="@+id/header"
        android:clipToPadding="false"
        android:divider="@null"
        android:dividerHeight="4.0dip"
        android:fadingEdge="none"
        android:fastScrollEnabled="true"
        android:fitsSystemWindows="true"
        android:focusable="true"
        android:layoutAnimation="@anim/listview_layout_animation"
        android:listSelector="@android:color/transparent"
        android:padding="12.0dip"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="none"
        android:visibility="gone" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="@string/empty_cache"
        android:textAppearance="?android:textAppearanceLarge"
        android:visibility="gone" />

    <FrameLayout
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/memory_ads_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="fill_horizontal|bottom|center"
        android:layout_marginBottom="@dimen/button_height"
        android:visibility="visible">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adsView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/bottom_lin"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentBottom="true"
        android:layout_margin="@dimen/button_margin"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:visibility="visible">

        <Button
            android:id="@+id/clear_button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/btn_normal_green"
            android:gravity="center"
            android:text="Clean Junk"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_large" />
    </LinearLayout>
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:app1="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_container2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroud_layout"
    android:orientation="vertical"
    android:visibility="gone">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="192.0dip"
            android:layout_height="192.0dip"
            android:layout_centerInParent="true"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <com.ferdi.cleaner.views.HoloCircularProgressBar
                    android:id="@+id/clean_done_holoCircularProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    app1:marker_progress="0.0"
                    app1:marker_visible="false"
                    app1:progress="1"
                    app1:progress_background_color="@color/circle_progress_background"
                    app1:progress_color="@color/circle_progress"
                    app1:stroke_width="6.0dip" />

                <ImageView
                    android:id="@+id/clean_done_iv_bg"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="16.0dip"
                    android:background="@drawable/circle_floating_bg"
                    android:rotation="-30.0"
                    android:visibility="visible" />

                <ImageView
                    android:id="@+id/clean_done_iv_done"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="40.0dip"
                    android:src="@drawable/ic_cleaning"
                    android:visibility="visible" />
            </FrameLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:gravity="center">

                <ImageView
                    android:id="@+id/clean_up_rocket"
                    android:layout_width="112.0dip"
                    android:layout_height="112.0dip"
                    android:layout_gravity="center" />
            </LinearLayout>
        </FrameLayout>

        <TextView
            android:id="@+id/clean_up_done_tv_notice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16.0dip"
            android:layout_marginRight="16.0dip"
            android:layout_marginTop="32.0dip"
            android:gravity="center"
            android:text="@string/boost_notice"
            android:textColor="@color/white"
            android:textSize="12.0sp"
            android:visibility="gone" />

        <FrameLayout
            android:id="@+id/clean_up_done_icon_container"
            android:layout_width="144.0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingTop="32.0dip">

            <TextView
                android:id="@+id/clean_up_tv_done"
                android:layout_width="wrap_content"
                android:layout_height="48.0dip"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="32.0dip"
                android:background="@drawable/button_done_selector"
                android:gravity="center"
                android:minWidth="64.0dip"
                android:paddingBottom="8.0dip"
                android:paddingLeft="32.0dip"
                android:paddingRight="32.0dip"
                android:paddingTop="8.0dip"
                android:text="DONE"
                android:textColor="@color/phoneboost_text_top_color_white_blue"
                android:textSize="16.0sp"
                android:typeface="sans"
                android:visibility="gone" />
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>

my application stops and I do not understand why, I would like an idea of why it stops, in advance thank you very much

Body:
One issue that I definitely see is 'Android won't be able to identify app1 namespace'.

<com.ferdi.cleaner.views.HoloCircularProgressBar
                    android:id="@+id/clean_done_holoCircularProgressBar"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    app1:marker_progress="0.0"
                    app1:marker_visible="false"
                    app1:progress="1" app1:progress_background_color="@color/circle_progress_background"
                    app1:progress_color="@color/circle_progress"
                    app1:stroke_width="6.0dip" />

But since the inflateException mentions the issue with com.ferdi.cleaner.widget.textcounter.CounterView as well, there seems to be another issue with the attribute definition of the counter view. Please verify the same.


问:
Java: Anybody have an example project using google protocol buffers over a network?
I'm not sure how to send messages with google protocol buffers to start, so an example with how to build a message, send it, and the reconstruct the message would be awesome.</p>

答:
http://www.javarants.com/2008/12/27/using-jax-rs-with-protocol-buffers-for-high-performance-rest-apis/

Kind of complicated though :)

ContentLicense: CC BY-SA 2.5


问:
Json with '@attr' in name to java POJO
I have the following json schema. The schema has a @ character in the schema. How does this translate to a java pojo?

Sample json

```<code>{
"@type":"employee",
"name":"John Doe"
}
</code>```

答:
You can use the Jackson Library and do something like:</p>

```<code>public class Person {

    private final String type;

    private final String name;

    @JsonCreator
    public Person(@JsonProperty("@type") String type, @JsonProperty("name") String name) {
        this.type = type;
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }
}
</code>```

Title: Json with '@attr' in name to java POJO

Body:
I have the following json schema. The schema has a @ character in the schema. How does this translate to a java pojo?

Sample json

{
"@type":"employee",
"name":"John Doe"
}

Body:
You can use the Jackson Library and do something like:

public class Person {

    private final String type;

    private final String name;

    @JsonCreator
    public Person(@JsonProperty("@type") String type, @JsonProperty("name") String name) {
        this.type = type;
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }
}


问:
Aggregate n DataFrames into a single DataFrame - element-wise
What is the pythonic way to generate a pandas DataFrame using an aggregation function to combine several other DataFrames? (DataFrames are all of the same shape with same headers.)

Aggregation functions I might be interested in are mean, minimum, maximum and user defined functions taking two operands.

For example, this is the way I might generate a DataFrame (max) which has the maximum element from four other DataFrames (df1,df2,df3,df4). This is hardly a pattern (idiom) that I can apply in all situations and to calculate the mean would look quite different.

```<code>import pandas as pd

df1 = pd.DataFrame({'a' : [1,2,3], 'b': [3,9,5]})
df2 = pd.DataFrame({'a' : [6,2,7], 'b': [3,4,5]})
df3 = pd.DataFrame({'a' : [6,2,11], 'b': [3,4,5]})
df4 = pd.DataFrame({'a' : [6,2,7], 'b': [3,12,5]})
print(df1)
print(df2)
print(df3)
print(df4)

max1 = df1.where(df1 > df2, df2)
max2 = df3.where(df3 > df4, df4)
max = max1.where(max1 > max2, max2)
print(max)
</code>```

An example of a user defined function taking two operands which I will also want to apply, is the following which is essentially the same as max in this example but will be something more complicated in practice

```<code>def myagg(a, b):
    return a > b
</code>```

答:
Concatenate them all with pd.concat and then groupby to find max:

```<code>pd.concat([df1, df2, df3, df4], 1).groupby(level=0, axis=1).max()

    a   b
0   6   3
1   2  12
2  11   5
</code>```

Compare this to your method which also returns:

```<code>max

    a   b
0   6   3
1   2  12
2  11   5
</code>```

Note that the column names across every dataframe would have to be the same for this (as well as your current method) to work.

As piR mentioned, the describe function performs some general aggregations for you:

```<code>lodf = [df1, df2, df3, df4]

pd.concat(lodf, keys=range(len(lodf))) \
  .groupby(level=1).describe().swaplevel(0, 1, 1).sort_index(1)

    25%       50%       75%       count        max        mean        min  \
      a    b    a    b    a     b     a    b     a     b     a     b    a
0  4.75  3.0  6.0  3.0  6.0  3.00   4.0  4.0   6.0   3.0  4.75  3.00  1.0
1  2.00  4.0  2.0  6.5  2.0  9.75   4.0  4.0   2.0  12.0  2.00  7.25  2.0
2  6.00  5.0  7.0  5.0  8.0  5.00   4.0  4.0  11.0   5.0  7.00  5.00  3.0

             std
     b         a         b
0  3.0  2.500000  0.000000
1  4.0  0.000000  3.947573
2  5.0  3.265986  0.000000
</code>```

Body:
What is the pythonic way to generate a pandas DataFrame using an aggregation function to combine several other DataFrames? (DataFrames are all of the same shape with same headers.)

Aggregation functions I might be interested in are mean, minimum, maximum and user defined functions taking two operands.

For example, this is the way I might generate a DataFrame (max) which has the maximum element from four other DataFrames (df1,df2,df3,df4). This is hardly a pattern (idiom) that I can apply in all situations and to calculate the mean would look quite different.

import pandas as pd

df1 = pd.DataFrame({'a' : [1,2,3], 'b': [3,9,5]})
df2 = pd.DataFrame({'a' : [6,2,7], 'b': [3,4,5]})
df3 = pd.DataFrame({'a' : [6,2,11], 'b': [3,4,5]})
df4 = pd.DataFrame({'a' : [6,2,7], 'b': [3,12,5]})
print(df1)
print(df2)
print(df3)
print(df4)

max1 = df1.where(df1 > df2, df2)
max2 = df3.where(df3 > df4, df4)
max = max1.where(max1 > max2, max2)
print(max)

An example of a user defined function taking two operands which I will also want to apply, is the following which is essentially the same as max in this example but will be something more complicated in practice

def myagg(a, b):
    return a > b

Body:
Concatenate them all with pd.concat and then groupby to find max:

pd.concat([df1, df2, df3, df4], 1).groupby(level=0, axis=1).max()

    a   b
0   6   3
1   2  12
2  11   5

Compare this to your method which also returns:

max

    a   b
0   6   3
1   2  12
2  11   5

Note that the column names across every dataframe would have to be the same for this (as well as your current method) to work.

As piR mentioned, the describe function performs some general aggregations for you:

lodf = [df1, df2, df3, df4]

pd.concat(lodf, keys=range(len(lodf))) \
  .groupby(level=1).describe().swaplevel(0, 1, 1).sort_index(1)

    25%       50%       75%       count        max        mean        min  \
      a    b    a    b    a     b     a    b     a     b     a     b    a
0  4.75  3.0  6.0  3.0  6.0  3.00   4.0  4.0   6.0   3.0  4.75  3.00  1.0
1  2.00  4.0  2.0  6.5  2.0  9.75   4.0  4.0   2.0  12.0  2.00  7.25  2.0
2  6.00  5.0  7.0  5.0  8.0  5.00   4.0  4.0  11.0   5.0  7.00  5.00  3.0

             std
     b         a         b
0  3.0  2.500000  0.000000
1  4.0  0.000000  3.947573
2  5.0  3.265986  0.000000


问:
Concatenation with FILTER clause
This question follows this one.

I now have this code:

```<code>select
    min(purchaseDate) filter (where fruitType = 'apple') as appleFirstPurchaseDate,
    min(purchaseDate) filter (where fruitType = 'orange') as orangeFirstPurchaseDate,
    customer
from fruitPurchases
group by customer
</code>```

Which gives the following output:

```<code>appleFirstPurchaseDate  orangeFirstPurchaseDate
----------------------  -----------------------
       2017-05-03              2016-11-25
</code>```

You should also know that each piece of fruit has an ID. With this ID, I'd like to create two keys: one that concatenates the fruit ID with the appleFirstPurchaseDate and the other one that concatenates the fruit ID with the orangeFirstPurchaseDate

Therefore I tried this:

```<code>select
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'apple') as appleKey,
    min(purchaseDate) filter (where fruitType = 'apple') as apple,
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'orange') as orangeKey,
    min(purchaseDate) filter (where fruitType = 'orange') as orange,
    customer
from fruitPurchases
group by customer, fruitId
</code>```

But when a customer has already bought apples as well as oranges, it unfortunately gives me this:

```<code>  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03           [NULL]             [NULL]
   [NULL]                 [NULL]          322-2016-11-25      2016-11-25
</code>```

While I would like this:

```<code>  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03       322-2016-11-25        2016-11-25
</code>```

Last piece of information: an earlier FirstPurchaseDate does not imply a lower fruitID.

答:
This is a combination of a "greatest-per-group" problem, followed by "pivoting".

General assumptions:

All involved columns are defined NOT NULL - or there are problem with sorting and concatenation.

(customer, fruidType, purchaseDate) is unique - or you need to define rules how to break ties.

You want just two fruits in the result.

The basic problem in your query: you only want customer in the GROUP BY clause, since you want one row per customer. Not customer, fruitid which produces one row per combination of customer and fruitid.

But there is no built-in aggregate function to retrieve, in a single step,

the fruitID from the same row that also holds the earliest purchaseDate for each (customer, fruidType).

You could make your query work by appending the fruitID (instead of prepending) because the concatenated text would still sort with the earliest date first, but that's very ugly and needlessly slow:

```<code>SELECT customer
     , min(purchaseDate || '-' || fruitId) FILTER (WHERE fruitType = 'apple')  AS appleKey
                       , min(purchaseDate) FILTER (WHERE fruitType = 'apple')  AS apple
     , min(purchaseDate || '-' || fruitId) FILTER (WHERE fruitType = 'orange') AS orangeKey
                       , min(purchaseDate) FILTER (WHERE fruitType = 'orange') AS orange
     , customer
FROM   fruitPurchases
GROUP  BY customer;
</code>```

I wouldn't be caught using that.

There are the related window function first_value() and last_value(), but those don't aggregate. And you cannot use the FILTER clause, which is only for aggregate function. So you would need an extra query level, and it's simpler to just use the window function row_number() instead to mark the first row for each group in a subquery or CTE ...

@Gordon made it work with reversed frame definitions for the window function. Consider this simplified, completed, and optimized version:

```<code>SELECT DISTINCT ON (customer)
       customer
     , first_value(fruitId || '-' || purchaseDate) OVER a AS appleKey
     , first_value(purchaseDate)                   OVER a AS appleFirstPurchaseDate
     , first_value(fruitId || '-' || purchaseDate) OVER o AS orangeKey
     , first_value(purchaseDate)                   OVER o AS orangeFirstPurchaseDate
FROM  (SELECT * FROM fruitPurchases WHERE fruitType IN ('apple', 'orange')) sub
WINDOW a AS (PARTITION BY customer ORDER BY fruittype ASC , purchaseDate)
     , o AS (PARTITION BY customer ORDER BY fruittype DESC, purchaseDate);
</code>```

But this should be faster:

```<code>WITH cte AS (
   SELECT DISTINCT ON (customer, fruitType)
          customer, fruitType, fruitId || '-' || purchaseDate AS key, purchaseDate
   FROM   fruitPurchases
   WHERE  fruitType IN ('apple', 'orange')
   ORDER  BY customer, fruitType, purchaseDate
   )
SELECT customer
     , a.key          AS appleKey
     , a.purchaseDate AS appleFirstPurchaseDate
     , o.key          AS orangeKey
     , a.purchaseDate AS orangeFirstPurchaseDate
FROM   cte a
JOIN   cte o USING (customer)
WHERE  a.fruitType = 'apple'
AND    o.fruitType = 'orange';
</code>```

All it needs to excel is an index on (customer, fruitType, purchaseDate) - or on (customer, fruitType, purchaseDate, fruitId) if your table allows index-only scans. Details depend on undisclosed information. Related:

How does PostgreSQL perform ORDER BY if a b-tree index is built on that field?

The CTE computes the greatest per group with DISTINCT ON:

Select first row in each GROUP BY group?

Depending on actual data distribution there may be even faster techniques:

Optimize GROUP BY query to retrieve latest record per user

The outer SELECT is a simplistic pivoting technique. Works for any number of fruits.

The same using said window function row_number() in the CTE:

```<code>WITH cte AS (
   SELECT customer, fruitType, fruitId || '-' || purchaseDate AS key, purchaseDate
        , row_number() OVER (PARTITION BY customer, fruitType ORDER BY purchaseDate) AS rn
   FROM   fruitPurchases
   WHERE  fruitType IN ('apple', 'orange')
   )
SELECT customer
     , a.key          AS appleKey
     , a.purchaseDate AS appleFirstPurchaseDate
     , o.key          AS orangeKey
     , a.purchaseDate AS orangeFirstPurchaseDate
FROM   cte a
JOIN   cte o USING (customer, rn)
WHERE  a.rn = 1
AND    a.fruitType = 'apple'
AND    o.fruitType = 'orange';
</code>```

dbfiddle here

But I would seriously consider normalizing your DB design first, which would make the task simpler and faster.

Title: Concatenation with FILTER clause

Body:
This question follows this one.

I now have this code:

select
    min(purchaseDate) filter (where fruitType = 'apple') as appleFirstPurchaseDate,
    min(purchaseDate) filter (where fruitType = 'orange') as orangeFirstPurchaseDate,
    customer
from fruitPurchases
group by customer

Which gives the following output:

appleFirstPurchaseDate  orangeFirstPurchaseDate
----------------------  -----------------------
       2017-05-03              2016-11-25

You should also know that each piece of fruit has an ID. With this ID, I'd like to create two keys: one that concatenates the fruit ID with the appleFirstPurchaseDate and the other one that concatenates the fruit ID with the orangeFirstPurchaseDate

Therefore I tried this:

select
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'apple') as appleKey,
    min(purchaseDate) filter (where fruitType = 'apple') as apple,
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'orange') as orangeKey,
    min(purchaseDate) filter (where fruitType = 'orange') as orange,
    customer
from fruitPurchases
group by customer, fruitId

But when a customer has already bought apples as well as oranges, it unfortunately gives me this:

  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03           [NULL]             [NULL]
   [NULL]                 [NULL]          322-2016-11-25      2016-11-25

While I would like this:

  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03       322-2016-11-25        2016-11-25

Last piece of information: an earlier FirstPurchaseDate does not imply a lower fruitID.

Body:
This is a combination of a "greatest-per-group" problem, followed by "pivoting".

General assumptions:

All involved columns are defined NOT NULL - or there are problem with sorting and concatenation.

(customer, fruidType, purchaseDate) is unique - or you need to define rules how to break ties.

You want just two fruits in the result.

The basic problem in your query: you only want customer in the GROUP BY clause, since you want one row per customer. Not customer, fruitid which produces one row per combination of customer and fruitid.

But there is no built-in aggregate function to retrieve, in a single step,

the fruitID from the same row that also holds the earliest purchaseDate for each (customer, fruidType).

You could make your query work by appending the fruitID (instead of prepending) because the concatenated text would still sort with the earliest date first, but that's very ugly and needlessly slow:

SELECT customer
     , min(purchaseDate || '-' || fruitId) FILTER (WHERE fruitType = 'apple')  AS appleKey
                       , min(purchaseDate) FILTER (WHERE fruitType = 'apple')  AS apple
     , min(purchaseDate || '-' || fruitId) FILTER (WHERE fruitType = 'orange') AS orangeKey
                       , min(purchaseDate) FILTER (WHERE fruitType = 'orange') AS orange
     , customer
FROM   fruitPurchases
GROUP  BY customer;

I wouldn't be caught using that.

There are the related window function first_value() and last_value(), but those don't aggregate. And you cannot use the FILTER clause, which is only for aggregate function. So you would need an extra query level, and it's simpler to just use the window function row_number() instead to mark the first row for each group in a subquery or CTE ...

@Gordon made it work with reversed frame definitions for the window function. Consider this simplified, completed, and optimized version:

SELECT DISTINCT ON (customer)
       customer
     , first_value(fruitId || '-' || purchaseDate) OVER a AS appleKey
     , first_value(purchaseDate)                   OVER a AS appleFirstPurchaseDate
     , first_value(fruitId || '-' || purchaseDate) OVER o AS orangeKey
     , first_value(purchaseDate)                   OVER o AS orangeFirstPurchaseDate
FROM  (SELECT * FROM fruitPurchases WHERE fruitType IN ('apple', 'orange')) sub
WINDOW a AS (PARTITION BY customer ORDER BY fruittype ASC , purchaseDate)
     , o AS (PARTITION BY customer ORDER BY fruittype DESC, purchaseDate);

But this should be faster:

WITH cte AS (
   SELECT DISTINCT ON (customer, fruitType)
          customer, fruitType, fruitId || '-' || purchaseDate AS key, purchaseDate
   FROM   fruitPurchases
   WHERE  fruitType IN ('apple', 'orange')
   ORDER  BY customer, fruitType, purchaseDate
   )
SELECT customer
     , a.key          AS appleKey
     , a.purchaseDate AS appleFirstPurchaseDate
     , o.key          AS orangeKey
     , a.purchaseDate AS orangeFirstPurchaseDate
FROM   cte a
JOIN   cte o USING (customer)
WHERE  a.fruitType = 'apple'
AND    o.fruitType = 'orange';

All it needs to excel is an index on (customer, fruitType, purchaseDate) - or on (customer, fruitType, purchaseDate, fruitId) if your table allows index-only scans. Details depend on undisclosed information. Related:

How does PostgreSQL perform ORDER BY if a b-tree index is built on that field?

The CTE computes the greatest per group with DISTINCT ON:

Select first row in each GROUP BY group?

Depending on actual data distribution there may be even faster techniques:

Optimize GROUP BY query to retrieve latest record per user

The outer SELECT is a simplistic pivoting technique. Works for any number of fruits.

The same using said window function row_number() in the CTE:

WITH cte AS (
   SELECT customer, fruitType, fruitId || '-' || purchaseDate AS key, purchaseDate
        , row_number() OVER (PARTITION BY customer, fruitType ORDER BY purchaseDate) AS rn
   FROM   fruitPurchases
   WHERE  fruitType IN ('apple', 'orange')
   )
SELECT customer
     , a.key          AS appleKey
     , a.purchaseDate AS appleFirstPurchaseDate
     , o.key          AS orangeKey
     , a.purchaseDate AS orangeFirstPurchaseDate
FROM   cte a
JOIN   cte o USING (customer, rn)
WHERE  a.rn = 1
AND    a.fruitType = 'apple'
AND    o.fruitType = 'orange';

dbfiddle here

But I would seriously consider normalizing your DB design first, which would make the task simpler and faster.


问:
Set variable on shub deploy project
I'm trying to setup scrapy settings to work with test and production environment on local and also on scrapinghub.
And I would like to know if there is any way to set this variable (for example as the following) on shub deploy:

And then at settings.py:

```<code>if env == "test":
     var1 = some_ip
     var2 = username
elif env == "prod":
     var1 = some_ip
     var2 = username
</code>```

Or... maybe there is a cleaner way to this?

Thank you for reading!

PS: I want to automate the settings depending of the environment where is the spider/project located instead of changing the variables manually.

EDIT: Found a better solution. Check the answer.

答:
If you want to control it using environment variables you could do the following: export an environment variable DEPLOY_TARGET that would contain test or prod and execute the following command to deploy

```<code>shub deploy $DEPLOY_TARGET
</code>```

Title: Set variable on shub deploy project

Body:
I'm trying to setup scrapy settings to work with test and production environment on local and also on scrapinghub.
And I would like to know if there is any way to set this variable (for example as the following) on shub deploy:

And then at settings.py:

if env == "test":
     var1 = some_ip
     var2 = username
elif env == "prod":
     var1 = some_ip
     var2 = username

Or... maybe there is a cleaner way to this?

Thank you for reading!

PS: I want to automate the settings depending of the environment where is the spider/project located instead of changing the variables manually.

EDIT: Found a better solution. Check the answer.

Body:
If you want to control it using environment variables you could do the following: export an environment variable DEPLOY_TARGET that would contain test or prod and execute the following command to deploy

shub deploy $DEPLOY_TARGET


问:
Swift/MySQL/PHP 'Required Parameters Missing' Error
Here is my swift for registering the user:

```<code>//Information fields
@IBOutlet weak var user: UITextField!
@IBOutlet weak var pass: UITextField!
@IBOutlet weak var pass2: UITextField!
@IBOutlet weak var name: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var Message: UILabel!

//Register button
@IBAction func register(_ sender: Any) {

    let Parameters = ["username": user.text, "password": pass.text, "email": email.text, "name": name.text]

    let url = URL(string: "http://cgi.soic.indiana.edu/~lvweiss/prof4/register.php")!

    let session = URLSession.shared

    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: Parameters, options: .prettyPrinted)
    } catch let error {
        print(error.localizedDescription)
        Message.text = String(error.localizedDescription)
    }

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }

        guard let data = data else {
            return
        }

        do {
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print(json)
                }
        } catch let error {
            print(error.localizedDescription)
            self.Message.text = String(error.localizedDescription)
        }
    })
    task.resume()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
</code>```

Here is the PHP:

```<code><?php
require_once 'DbOperation.php';

$response = array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!verifyRequiredParams(array('username', 'password', 'email', 'name'))) {
    //getting values
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $name = $_POST['name'];

    //creating db operation object
    $db = new DbOperation();

    //adding user to database
    $result = $db->createUser($username, $password, $email, $name);

    //making the response accordingly
    if ($result == USER_CREATED) {
        $response['error'] = false;
        $response['message'] = 'User created successfully';
    } elseif ($result == USER_ALREADY_EXIST) {
        $response['error'] = true;
        $response['message'] = 'User already exist';
    } elseif ($result == USER_NOT_CREATED) {
        $response['error'] = true;
        $response['message'] = 'Some error occurred';
    }
} else {
    $response['error'] = true;
    $response['message'] = 'Required parameters are missing';
}
} else {
$response['error'] = true;
$response['message'] = 'Invalid request';
}

//function to validate the required parameter in request
function verifyRequiredParams($required_fields)
{

//Looping through all the parameters
foreach ($required_fields as $field) {
    //if any requred parameter is missing
    if (!isset($_POST[$field]) || strlen(trim($_POST[$field])) <= 0) {

        //returning true;
        return true;
    }
}
return false;
}

echo json_encode($response);
?>
</code>```

Here is the information I am trying to post to the database:
iOS Registration Fields:

And the error I am receiving from Xcode when hitting the register button:

2017-11-14 00:42:01.529344-0500 WeissProf4[8754:662299] [MC] Lazy loading
      NSBundle MobileCoreServices.framework

  2017-11-14 00:42:01.530670-0500 WeissProf4[8754:662299] [MC] Loaded MobileCoreServices.framework

  2017-11-14 00:42:01.550941-0500 WeissProf4[8754:662299] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/leviweiss/Library/Developer/CoreSimulator/Devices/C98EE410-1CA2-4B4B-9ED8-A4F112C629E2/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

  2017-11-14 00:42:03.468653-0500 WeissProf4[8754:662299] [MC] Reading from private effective user settings.

  2017-11-14 00:42:04.769899-0500 WeissProf4[8754:662505] [MC] Invalidating cache

  2017-11-14 00:42:05.281372-0500 WeissProf4[8754:662299] [MC] Reading from private effective user settings.

  ["message": Required parameters are missing, "error": 1]

I'm not sure what is going on, I know the PHP is successfully connecting to the DB and is able to post the required info (tested with Postman).  I am thinking it could be an error with how Swift deals with posting in PHP, although I am absolutely not a PHP expert.

答:
SOLUTION Swift4:

```<code>@IBAction func register(_ sender: Any) {

let request = NSMutableURLRequest(url: NSURL(string: "http://cgi.soic.indiana.edu/~lvweiss/prof4/register.php")! as URL)
request.httpMethod = "POST"
let postString = "username=\(user.text!)&password=\(pass.text!)&email=\(email.text!)&name=\(name.text!)"
request.httpBody = postString.data(using: String.Encoding.utf8)

let task = URLSession.shared.dataTask(with: request as URLRequest) {
    data, response, error in

    if error != nil {
        print("error=\(String(describing: error))")
        return
    }

    print("response = \(String(describing: response))")

    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print("responseString = \(String(describing: responseString))")
}
task.resume()
</code>```

}

Body:
Here is my swift for registering the user:

//Information fields
@IBOutlet weak var user: UITextField!
@IBOutlet weak var pass: UITextField!
@IBOutlet weak var pass2: UITextField!
@IBOutlet weak var name: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var Message: UILabel!

//Register button
@IBAction func register(_ sender: Any) {

    let Parameters = ["username": user.text, "password": pass.text, "email": email.text, "name": name.text]

    let url = URL(string: "http://cgi.soic.indiana.edu/~lvweiss/prof4/register.php")!

    let session = URLSession.shared

    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: Parameters, options: .prettyPrinted)
    } catch let error {
        print(error.localizedDescription)
        Message.text = String(error.localizedDescription)
    }

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }

        guard let data = data else {
            return
        }

        do {
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print(json)
                }
        } catch let error {
            print(error.localizedDescription)
            self.Message.text = String(error.localizedDescription)
        }
    })
    task.resume()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Here is the PHP:

<?php
require_once 'DbOperation.php';

$response = array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!verifyRequiredParams(array('username', 'password', 'email', 'name'))) {
    //getting values
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $name = $_POST['name'];

    //creating db operation object
    $db = new DbOperation();

    //adding user to database
    $result = $db->createUser($username, $password, $email, $name);

    //making the response accordingly
    if ($result == USER_CREATED) {
        $response['error'] = false;
        $response['message'] = 'User created successfully';
    } elseif ($result == USER_ALREADY_EXIST) {
        $response['error'] = true;
        $response['message'] = 'User already exist';
    } elseif ($result == USER_NOT_CREATED) {
        $response['error'] = true;
        $response['message'] = 'Some error occurred';
    }
} else {
    $response['error'] = true;
    $response['message'] = 'Required parameters are missing';
}
} else {
$response['error'] = true;
$response['message'] = 'Invalid request';
}

//function to validate the required parameter in request
function verifyRequiredParams($required_fields)
{

//Looping through all the parameters
foreach ($required_fields as $field) {
    //if any requred parameter is missing
    if (!isset($_POST[$field]) || strlen(trim($_POST[$field])) <= 0) {

        //returning true;
        return true;
    }
}
return false;
}

echo json_encode($response);
?>

Here is the information I am trying to post to the database:
iOS Registration Fields:

And the error I am receiving from Xcode when hitting the register button:

2017-11-14 00:42:01.529344-0500 WeissProf4[8754:662299] [MC] Lazy loading
      NSBundle MobileCoreServices.framework

  2017-11-14 00:42:01.530670-0500 WeissProf4[8754:662299] [MC] Loaded MobileCoreServices.framework

  2017-11-14 00:42:01.550941-0500 WeissProf4[8754:662299] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/leviweiss/Library/Developer/CoreSimulator/Devices/C98EE410-1CA2-4B4B-9ED8-A4F112C629E2/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

  2017-11-14 00:42:03.468653-0500 WeissProf4[8754:662299] [MC] Reading from private effective user settings.

  2017-11-14 00:42:04.769899-0500 WeissProf4[8754:662505] [MC] Invalidating cache

  2017-11-14 00:42:05.281372-0500 WeissProf4[8754:662299] [MC] Reading from private effective user settings.

  ["message": Required parameters are missing, "error": 1]

I'm not sure what is going on, I know the PHP is successfully connecting to the DB and is able to post the required info (tested with Postman).  I am thinking it could be an error with how Swift deals with posting in PHP, although I am absolutely not a PHP expert.

Body:
SOLUTION Swift4:

@IBAction func register(_ sender: Any) {

let request = NSMutableURLRequest(url: NSURL(string: "http://cgi.soic.indiana.edu/~lvweiss/prof4/register.php")! as URL)
request.httpMethod = "POST"
let postString = "username=\(user.text!)&password=\(pass.text!)&email=\(email.text!)&name=\(name.text!)"
request.httpBody = postString.data(using: String.Encoding.utf8)

let task = URLSession.shared.dataTask(with: request as URLRequest) {
    data, response, error in

    if error != nil {
        print("error=\(String(describing: error))")
        return
    }

    print("response = \(String(describing: response))")

    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print("responseString = \(String(describing: responseString))")
}
task.resume()

}


问:
I deleted a React component that was never used, but now the page won't render... huh?
I had a component in a ReactJS app I am working on that I sweeeearrr is not used anywhere. It even gave me the warning that "footer is defined but not used'. It isn't render anywhere and it never did anything, modify state... yet I'm getting an error message that says that the page cannot be rendered because the file is missing after I deleted it.

Is this simply a matter that I must be missing a connection somewhere?

答:
yet I'm getting an error message that says that the page cannot be rendered because the file is missing after I deleted it.

An unused import / require statement is left in there.

Solution

Search for filename in the entire code base and remove usage.

More

Something like TypeScript would have caught this at compile time.


问:
How to use periodic commit from scala to load csv file to neo4j?
I am trying to create a graph by loading a CSV file to neo4j from scala code.

I'm using a periodic commit which is resulting in the following error:

```<code>Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException:
Executing queries that use periodic commit in an open transaction is not possible.
</code>```

Any help on why I get this error and how to avoid it? I am using periodic commit as I would be loading over 100k nodes. Is there any alternative to periodic commit?

Code:

```<code>def createGraph(createStmt: ArrayBuffer[String]): Unit = {
val session = driver.session()
val greeting: String = session.writeTransaction(new TransactionWork[String]() {
  override def execute(transaction: Transaction): String = {
    var result1: StatementResult = transaction.run("USING PERIODIC COMMIT\n"+
                                                  "LOAD CSV WITH HEADERS FROM \"file:///path/to/file.csv\" AS csvLine\n"+
                                                  "CREATE (p:Person {id: toInt(csvLine.id), name: csvLine.name})")

    "Graph Created"
  }
})
</code>```

}

答:
The driver docs on transactions suggest state that "Auto-commit transactions are the only way to execute USING PERIODIC COMMIT Cypher statements."

Auto-commits are executed as session.run(...), without starting a new transaction with session.writeTransaction(...).

Body:
I am trying to create a graph by loading a CSV file to neo4j from scala code.

I'm using a periodic commit which is resulting in the following error:

Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException:
Executing queries that use periodic commit in an open transaction is not possible.

Any help on why I get this error and how to avoid it? I am using periodic commit as I would be loading over 100k nodes. Is there any alternative to periodic commit?

Code:

def createGraph(createStmt: ArrayBuffer[String]): Unit = {
val session = driver.session()
val greeting: String = session.writeTransaction(new TransactionWork[String]() {
  override def execute(transaction: Transaction): String = {
    var result1: StatementResult = transaction.run("USING PERIODIC COMMIT\n"+
                                                  "LOAD CSV WITH HEADERS FROM \"file:///path/to/file.csv\" AS csvLine\n"+
                                                  "CREATE (p:Person {id: toInt(csvLine.id), name: csvLine.name})")

    "Graph Created"
  }
})

}


问:
dplyr select/distinct keeps brining in a column I don't choose
I have a table with many columns in it. For example:

```<code>MRN     | Svc_Line...
--------|----------
123456  | Medical
123456  | Medical
987654  | Surgical
...
</code>```

I issue the following commands which all bring back an extra column:

```<code>dplyr::select(
distinct(
.data = tblPerf
, MRN
    )
)
</code>```

Brings back MRN and Svc_Line

```<code>dplyr::select(
.data = tblPerf
, MRN
)
</code>```

Brings back MRN and Svc_Line

```<code>dply::distinct(
.data = tblPerf
, MRN
)
</code>```

brings back MRN and Svc_Line

No matter what columns I try to bring back Svc_Line is always also brought back. It is a factor, not sure why this is happening. I have shutdown and restarted my R-Studio session

The table tblPerf was put together from another table, rad_data. The table rad_data has many variables that were created using mutate() based upon groupings of other columns. I then did the following:

```<code>tblPerf <- rad_data %>%
mutate(ord_per_pt_elos =
       round((enc_order_count/Performance), 4)) %>%
mutate(ord_pty_svcline_ord_elos =
       round((svcline_ord_per_pt/ord_pty_svc_elos), 4)) %>%
mutate(avg_ord_per_pt_elos = round(avg_ordperenc_ord_pty/ord_pty_elos, 4))
</code>```

And am then trying to select/distinct from that. I have since also done tblPerf <- tblPerf in hopes of getting rid of the grouping error. I am now getting the error of:

```<code>> tblPerf <- tblPerf
> dplyr::select(
+   .data = tblPerf
+   , MRN
+ )
Adding missing grouping variables: `Ord_Pty_Number`, `LIHN_Svc_Line`
# A tibble: 1,715 x 3
# Groups:   Ord_Pty_Number, LIHN_Svc_Line [217]
   Ord_Pty_Number LIHN_Svc_Line    MRN
            <chr>        <fctr>  <chr>
 1          12345       Medical 123456
</code>```

I did not have this issue yesterday

答:
You need to ungroup the dataframe.</p>

```<code>> tblPerf <- tblPerf %>% ungroup()
> dplyr::select(
+   .data = tblPerf
+   , MRN
+ )
</code>```

Body:
I have a table with many columns in it. For example:

MRN     | Svc_Line...
--------|----------
123456  | Medical
123456  | Medical
987654  | Surgical
...

I issue the following commands which all bring back an extra column:

dplyr::select(
distinct(
.data = tblPerf
, MRN
    )
)

Brings back MRN and Svc_Line

dplyr::select(
.data = tblPerf
, MRN
)

Brings back MRN and Svc_Line

dply::distinct(
.data = tblPerf
, MRN
)

brings back MRN and Svc_Line

No matter what columns I try to bring back Svc_Line is always also brought back. It is a factor, not sure why this is happening. I have shutdown and restarted my R-Studio session

The table tblPerf was put together from another table, rad_data. The table rad_data has many variables that were created using mutate() based upon groupings of other columns. I then did the following:

tblPerf <- rad_data %>%
mutate(ord_per_pt_elos =
       round((enc_order_count/Performance), 4)) %>%
mutate(ord_pty_svcline_ord_elos =
       round((svcline_ord_per_pt/ord_pty_svc_elos), 4)) %>%
mutate(avg_ord_per_pt_elos = round(avg_ordperenc_ord_pty/ord_pty_elos, 4))

And am then trying to select/distinct from that. I have since also done tblPerf <- tblPerf in hopes of getting rid of the grouping error. I am now getting the error of:

> tblPerf <- tblPerf
> dplyr::select(
+   .data = tblPerf
+   , MRN
+ )
Adding missing grouping variables: `Ord_Pty_Number`, `LIHN_Svc_Line`
# A tibble: 1,715 x 3
# Groups:   Ord_Pty_Number, LIHN_Svc_Line [217]
   Ord_Pty_Number LIHN_Svc_Line    MRN
            <chr>        <fctr>  <chr>
 1          12345       Medical 123456

I did not have this issue yesterday

Body:
You need to ungroup the dataframe.

> tblPerf <- tblPerf %>% ungroup()
> dplyr::select(
+   .data = tblPerf
+   , MRN
+ )


问:
Double sided bar chart in JavaFX
For a project I am working on, I would like to display data as a continually updating double sided bar chart (or double sided histogram) in JavaFX. I have successfully created a horizontal bar chart where the bars extend from only one side of axis, but not from both sides.

An example of what I want to do would be, on a chart, each row (or bar) represents a store, the left side represents what percentage is currently female and the right the percentage currently male. As customers enter and leave the store, this chart updates to show current gender percentage, with it always adding up to 100% unless there are no customers. In which case there is no bar. This means that, unless there are no customers, the bar will always be the same length, just shifted to each side. The whole chart shows maybe 5 different stores, so has five different bars with gender representations.

I don't have enough reputation to post an image so here is a  link to what I want it to look like.

This is a link that looks like what I can currently make.

Thank you!

答:
Originally I tried to use a BarChart object to graph a double sided bar chart, but after looking at this question I realized that a StackedBarChart had what I was looking for. This is the code I used to create a double sided histogram/bar chart for anyone else wondering how to do this:</p>

```<code>    import java.util.Arrays;
    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;

    public class HorizontalHistogramTest extends Application{

        final static String austria = "Austria";
        final static String brazil = "Brazil";
        final static String france = "France";
        final static String italy = "Italy";
        final static String usa = "USA";
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final StackedBarChart<Number, String> sbc =
        new StackedBarChart<Number,String>(yAxis, xAxis);
        final XYChart.Series<Number,String> series1 =
        new XYChart.Series<Number,String>();

@Override
public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    sbc.setTitle("Country Summary");
    xAxis.setLabel("Country");
    xAxis.setCategories(FXCollections.<String>observableArrayList(
            Arrays.asList(austria, brazil, france, italy, usa)));
    yAxis.setLabel("Percent");
    // plus value
    series1.getData().add(new XYChart.Data<Number,String>(10, austria));
    series1.getData().add(new XYChart.Data<Number,String>(50, brazil));
    series1.getData().add(new XYChart.Data<Number,String>(40, france));
    series1.getData().add(new XYChart.Data<Number,String>(50, italy));
    series1.getData().add(new XYChart.Data<Number,String>(60, usa));
    // minus value
    series1.getData().add(new XYChart.Data<Number,String>(-90, austria));
    series1.getData().add(new XYChart.Data<Number,String>(-50, brazil));
    series1.getData().add(new XYChart.Data<Number,String>(-60, france));
    series1.getData().add(new XYChart.Data<Number,String>(-50, italy));
    series1.getData().add(new XYChart.Data<Number,String>(-40, usa));

    Scene scene = new Scene(sbc, 800, 600);
    sbc.getData().addAll(series1);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
}
}
</code>```

Title: Double sided bar chart in JavaFX

Body:
Originally I tried to use a BarChart object to graph a double sided bar chart, but after looking at this question I realized that a StackedBarChart had what I was looking for. This is the code I used to create a double sided histogram/bar chart for anyone else wondering how to do this:

    import java.util.Arrays;
    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;

    public class HorizontalHistogramTest extends Application{

        final static String austria = "Austria";
        final static String brazil = "Brazil";
        final static String france = "France";
        final static String italy = "Italy";
        final static String usa = "USA";
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final StackedBarChart<Number, String> sbc =
        new StackedBarChart<Number,String>(yAxis, xAxis);
        final XYChart.Series<Number,String> series1 =
        new XYChart.Series<Number,String>();

@Override
public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    sbc.setTitle("Country Summary");
    xAxis.setLabel("Country");
    xAxis.setCategories(FXCollections.<String>observableArrayList(
            Arrays.asList(austria, brazil, france, italy, usa)));
    yAxis.setLabel("Percent");
    // plus value
    series1.getData().add(new XYChart.Data<Number,String>(10, austria));
    series1.getData().add(new XYChart.Data<Number,String>(50, brazil));
    series1.getData().add(new XYChart.Data<Number,String>(40, france));
    series1.getData().add(new XYChart.Data<Number,String>(50, italy));
    series1.getData().add(new XYChart.Data<Number,String>(60, usa));
    // minus value
    series1.getData().add(new XYChart.Data<Number,String>(-90, austria));
    series1.getData().add(new XYChart.Data<Number,String>(-50, brazil));
    series1.getData().add(new XYChart.Data<Number,String>(-60, france));
    series1.getData().add(new XYChart.Data<Number,String>(-50, italy));
    series1.getData().add(new XYChart.Data<Number,String>(-40, usa));

    Scene scene = new Scene(sbc, 800, 600);
    sbc.getData().addAll(series1);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
}
}


问:
How to create and use GeoSpatial indexes in Marklogic from Sparql
I have loaded the geospatial data from geonames.org into Marklogic using RDF import.

When using the Query Console to explore the data, I see the data has been loaded into an xml document and looks like this:

```<code><sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#lat</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">40.41476</sem:object>
</sem:triple>
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#long</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">-8.54304</sem:object>
</sem:triple>
</code>```

I am able to do a SPARQL DESCRIBE and see data. Here is an example.

```<code>@prefix geonames: <http://www.geonames.org/ontology#> .
@prefix xs: <http://www.w3.org/2001/XMLSchema#> .
@prefix p0: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
<http://sws.geonames.org/2736540/> geonames:parentCountry <http://sws.geonames.org/2264397/> ;
                                   geonames:countryCode "PT"^^xs:string ;
                                   p0:long "-8.54304"^^xs:string ;
                                   geonames:featureCode <http://www.geonames.org/ontology#P.PPL> ;
                                   geonames:parentADM1 <http://sws.geonames.org/2742610/> ;
                                   geonames:parentFeature <http://sws.geonames.org/2742610/> ;
                                   <http://www.w3.org/2000/01/rdf-schema#isDefinedBy> "http://sws.geonames.org/2736540/about.rdf"^^xs:string ;
                                   a geonames:Feature ;
                                   geonames:locationMap <http://www.geonames.org/2736540/pedreira-de-vilarinho.html> ;
                                   geonames:name "Pedreira de Vilarinho"^^xs:string ;
                                   geonames:nearbyFeatures <http://sws.geonames.org/2736540/nearby.rdf> ;
                                   geonames:featureClass geonames:P ;
                                   p0:lat "40.41476"^^xs:string .
</code>```

I want to query over this data using SPARQL QUERY as my Query Type in a way where I can take advantage of the geospatial indexes that MarkLogic can create.

I have been having trouble with two aspects of this.

How to correctly create the geospatial indexes for the wgs84_pos#lat and wgs84_pos#long predicates?

How do I access those indexes from SPARQL QUERY?

I would like to have a sparql query that would be able to find subjects within some range of a Point.

=====================================

Followup:

After following David Ennis's Answer (Which worked nicely, thanks!) I ended up with this sample Xquery that was able to select data out of documents via geosearch and then use those IRI's in a sparql values query.

Example:

```<code>xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
       at "/MarkLogic/semantics.xqy";

let $matches := cts:search(//rdf:RDF,
  cts:element-pair-geospatial-query (
    fn:QName("http://www.geonames.org/ontology#","Feature"),
    fn:QName("http://www.w3.org/2003/01/geo/wgs84_pos#", "lat"),
    fn:QName ("http://www.w3.org/2003/01/geo/wgs84_pos#","long"),
    cts:circle(10, cts:point(19.8,99.8))))

let $iris := sem:iri($matches//@rdf:about)

let $bindings := (fn:map(function($n) { map:entry("featureIRI", $n) }, $iris))

let $sparql := '
  PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
  SELECT *
  WHERE {
    ?featureIRI wgs:lat ?lat;
                wgs:long ?long.
  }
'

return sem:sparql-values($sparql, $bindings)
</code>```

This xquery queries the geospatial index, finds matching documents and then selects the IRI in the rdf:about attribute of the xml document.
It then maps over all of those IRIs and creates map entries that can be passed in the bindings parameter of the sem:sparql-values function.

答:
I do not believe you can do what you want via just native SPARQL. Geospacial queries in any SPARQL implementation are extensions like geoSPARQL, Apache Jena geospacial queries etc.

My suggested approach in MarkLogic:

Insert the geonames subjects into MarkLogic as unmanaged triples (an XML or JSON document with embedded triples for each one)

In the same document, include the geo-spacial data in one of the acceptable MarkLogic formats. This essentially adds geo-spacial metadata to the triple since it is in the same fragment.

Add geo-spacial path-range-indexes for the geospacial data.

Use SPARQL inside of MarkLogic with a cts query restriction.

The Building Blocks for above:

Understanding unmanaged triples

Understanding Geo-spacial Region Types

Understanding Geo-spacial Indexes

Understanding Geo-spacial Queries

Understanding Semantics with cts search

Another approach to the final query could be the Optic API but I do not see how it would negate the need to do steps 1-3

Body:
I have loaded the geospatial data from geonames.org into Marklogic using RDF import.

When using the Query Console to explore the data, I see the data has been loaded into an xml document and looks like this:

<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#lat</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">40.41476</sem:object>
</sem:triple>
<sem:triple>
<sem:subject>http://sws.geonames.org/2736540/</sem:subject>
<sem:predicate>http://www.w3.org/2003/01/geo/wgs84_pos#long</sem:predicate>
<sem:object datatype="http://www.w3.org/2001/XMLSchema#string">-8.54304</sem:object>
</sem:triple>

I am able to do a SPARQL DESCRIBE and see data. Here is an example.

@prefix geonames: <http://www.geonames.org/ontology#> .
@prefix xs: <http://www.w3.org/2001/XMLSchema#> .
@prefix p0: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
<http://sws.geonames.org/2736540/> geonames:parentCountry <http://sws.geonames.org/2264397/> ;
                                   geonames:countryCode "PT"^^xs:string ;
                                   p0:long "-8.54304"^^xs:string ;
                                   geonames:featureCode <http://www.geonames.org/ontology#P.PPL> ;
                                   geonames:parentADM1 <http://sws.geonames.org/2742610/> ;
                                   geonames:parentFeature <http://sws.geonames.org/2742610/> ;
                                   <http://www.w3.org/2000/01/rdf-schema#isDefinedBy> "http://sws.geonames.org/2736540/about.rdf"^^xs:string ;
                                   a geonames:Feature ;
                                   geonames:locationMap <http://www.geonames.org/2736540/pedreira-de-vilarinho.html> ;
                                   geonames:name "Pedreira de Vilarinho"^^xs:string ;
                                   geonames:nearbyFeatures <http://sws.geonames.org/2736540/nearby.rdf> ;
                                   geonames:featureClass geonames:P ;
                                   p0:lat "40.41476"^^xs:string .

I want to query over this data using SPARQL QUERY as my Query Type in a way where I can take advantage of the geospatial indexes that MarkLogic can create.

I have been having trouble with two aspects of this.

How to correctly create the geospatial indexes for the wgs84_pos#lat and wgs84_pos#long predicates?

How do I access those indexes from SPARQL QUERY?

I would like to have a sparql query that would be able to find subjects within some range of a Point.

=====================================

Followup:

After following David Ennis's Answer (Which worked nicely, thanks!) I ended up with this sample Xquery that was able to select data out of documents via geosearch and then use those IRI's in a sparql values query.

Example:

xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
       at "/MarkLogic/semantics.xqy";

let $matches := cts:search(//rdf:RDF,
  cts:element-pair-geospatial-query (
    fn:QName("http://www.geonames.org/ontology#","Feature"),
    fn:QName("http://www.w3.org/2003/01/geo/wgs84_pos#", "lat"),
    fn:QName ("http://www.w3.org/2003/01/geo/wgs84_pos#","long"),
    cts:circle(10, cts:point(19.8,99.8))))

let $iris := sem:iri($matches//@rdf:about)

let $bindings := (fn:map(function($n) { map:entry("featureIRI", $n) }, $iris))

let $sparql := '
  PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
  SELECT *
  WHERE {
    ?featureIRI wgs:lat ?lat;
                wgs:long ?long.
  }
'

return sem:sparql-values($sparql, $bindings)

This xquery queries the geospatial index, finds matching documents and then selects the IRI in the rdf:about attribute of the xml document.
It then maps over all of those IRIs and creates map entries that can be passed in the bindings parameter of the sem:sparql-values function.


问:
catching all invalid URLs
I recently upgraded a site and almost all URLs have changed. I have redirected all of them (or so I hope) but it may be possible that some of them have slipped by me. Is there a way to somehow catch all invalid URLs and send the user to a certain page and somehow know what URL the person came from so I could log this, and fix those? I'm thinking I could use .htaccess somehow but am not sure how. I am using PHP Thanks so much!</p>

答:
in .htaccess in the web root</p>

```<code>ErrorDocument 404 /yourFile.php
</code>```

Title: catching all invalid URLs

Body:
in .htaccess in the web root

ErrorDocument 404 /yourFile.php