Last active
January 25, 2022 18:34
-
-
Save emailrhoads/7ef3fcdc8fa0fc00f99ab5d6e57f07c6 to your computer and use it in GitHub Desktop.
[export avm data to CSV] #crape
This file contains hidden or 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
| blrs = Crape::New::BatchLoanRun.all | |
| wanted_values = blrs.map do |r| | |
| avm_result = r.loan_run.avm_result | |
| { | |
| loan_number: r.loan_number, | |
| endpoint: avm_result.dig('endpoint'), | |
| fsd: avm_result.dig('avmResult', 'forecastStdDev'), | |
| marketValue: avm_result.dig('avmResult', 'marketValue'), | |
| lowValue: avm_result.dig('avmResult', 'lowValue'), | |
| highValue: avm_result.dig('avmResult', 'highValue'), | |
| } | |
| end | |
| CSV.open("avm_results.tsv", "w", write_headers: true, headers: wanted_values.first.keys, col_sep: "\t") do |csv| | |
| wanted_values.each do |h| | |
| csv << h.values | |
| end | |
| end; | |
| # Export BAF | |
| export = Enumerator.new do |lines| | |
| # Uses ActiveRecord Batch API to find records efficently | |
| Crape::New::LoanRun.find_each do |record| | |
| lines << record.slice('loan_number', 'normalized_address_line_text', 'normalized_city_name', 'normalized_property_postal_code', 'normalized_property_state', 'fips_state_numeric_code', 'fips_county_code', 'ffiec_census_tract') | |
| end | |
| end | |
| CSV.open("BulkLoans.20211001_20220124_results.tsv", "w", write_headers: true, headers: export.first.keys, col_sep: "\t") do |csv| | |
| export.each do |h| | |
| csv << h.values | |
| end | |
| end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment