-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleaned test files; added argv and stdin to initial prompt, if anythi…
…ngs is there
- Loading branch information
1 parent
fd1b23e
commit 1555289
Showing
4 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from io import StringIO, TextIOWrapper | ||
|
||
class CaptureInput: | ||
def __init__(self, input_stream): | ||
input_stream = TextIOWrapper(input_stream.buffer, encoding='utf-8', newline='') | ||
|
||
self.original_input = input_stream | ||
self.capture_buffer = StringIO() | ||
self.original_readline = input_stream.buffer.raw.readline | ||
|
||
def custom_readline(*args, **kwargs): | ||
input_data = self.original_readline(*args, **kwargs) | ||
self.capture_buffer.write(input_data.decode()) | ||
return input_data | ||
|
||
input_stream.buffer.raw.readline = custom_readline | ||
|
||
def readline(self, *args, **kwargs): | ||
input_data = self.original_input.readline(*args, **kwargs) | ||
self.capture_buffer.write(input_data) | ||
self.capture_buffer.flush() | ||
return input_data | ||
|
||
def read(self, *args, **kwargs): | ||
input_data = self.original_input.read(*args, **kwargs) | ||
self.capture_buffer.write(input_data) | ||
self.capture_buffer.flush() | ||
return input_data | ||
|
||
def get_captured_input(self): | ||
return self.capture_buffer.getvalue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from datascience import * | ||
from ds101 import * | ||
|
||
def make_marble_sample(): | ||
table = Table().read_table('marble-sample.csv') | ||
return table.column('color') | ||
|
||
def proportion_blue(sample): | ||
return np.count_nonzero(sample == 'B') / len(sample) | ||
|
||
def resampled_stats(observed_marbles, num_trials): | ||
stats = bootstrap_statistic(observed_marbles, | ||
proportion_blue, | ||
num_trials) | ||
assert len(stats) == num_trials | ||
return stats | ||
|
||
observed_marbles = make_marble_sample() | ||
stats = resampled_stats(observed_marbles, 5) | ||
|
||
assert np.isclose(np.mean(stats), 0.7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import numpy as np | ||
from datascience import * | ||
|
||
# fake library to hide identities... | ||
|
||
def bootstrap_statistic(observed_sample, compute_statistic, num_trials): | ||
""" | ||
Creates num_trials resamples of the initial sample. | ||
Returns an array of the provided statistic for those samples. | ||
* observed_sample: the initial sample, as an array. | ||
* compute_statistic: a function that takes a sample as | ||
an array and returns the statistic for that | ||
sample. | ||
* num_trials: the number of bootstrap samples to create. | ||
""" | ||
|
||
# Check that observed_sample is an array! | ||
if not isinstance(observed_sample, np.ndarray): | ||
raise ValueError('The first parameter to bootstrap_statistic must be a sample represented as an array, not a value of type ' + str(type(observed_sample).__name__)) | ||
|
||
statistics = make_array() | ||
|
||
for i in np.arange(0, num_trials): | ||
#Key: in bootstrapping we must always sample with replacement | ||
simulated_resample = np.random.choice(observed_sample, len(observed_sample)) | ||
|
||
resample_statistic = compute_statistic(simulated_resample) | ||
statistics = np.append(statistics, resample_statistic) | ||
|
||
return statistics |