forked from ourresearch/jump-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournal.py
1715 lines (1416 loc) · 72.7 KB
/
journal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
import datetime
import weakref
from collections import OrderedDict
from collections import defaultdict
from threading import Lock
import numpy as np
import scipy
from cached_property import cached_property
from scipy.optimize import curve_fit
from app import DEMO_PACKAGE_ID
from app import use_groups
from app import use_groups_free_instant
from app import use_groups_lookup
from util import format_currency
from util import format_percent
from util import format_with_commas
scipy_lock = Lock()
# from future of OA paper, modified to be just elsevier, all colours
default_download_by_age = [0.371269, 0.137739, 0.095896, 0.072885, 0.058849]
default_download_older_than_five_years = 1.0 - sum(default_download_by_age)
def display_cpu(value):
if value and str(value).lower() != "nan":
return value
else:
return "-"
class Journal(object):
years = list(range(0, 5))
def __init__(self, issn_l, scenario=None, scenario_data=None, package=None):
self.now = datetime.datetime.utcnow()
self.set_scenario(scenario)
self.set_scenario_data(scenario_data)
self.issn_l = issn_l
self.my_package = package
self.package_id = package.package_id
self.package_id_for_db = self.package_id
if self.package_id.startswith("demo") or self.my_package.is_demo:
self.package_id_for_db = DEMO_PACKAGE_ID
self.subscribed_bulk = False
self.subscribed_custom = False
self.use_default_download_curve = False
self.use_default_num_papers_curve = False
def set_scenario(self, scenario):
if scenario:
self.scenario = weakref.proxy(scenario)
self.settings = self.scenario.settings
else:
self.scenario = None
self.settings = None
def set_scenario_data(self, scenario_data):
self._scenario_data = scenario_data
@cached_property
def subscribed(self):
return self.subscribed_bulk or self.subscribed_custom
@cached_property
def my_scenario_data_row(self):
return self._scenario_data["unpaywall_downloads_dict"][self.issn_l] or {}
@cached_property
def title(self):
return self.journal_metadata.title
@cached_property
def publisher(self):
return self.journal_metadata.publisher
@cached_property
def publisher_short(self):
x_words = self.publisher.split()
if len(x_words) == 1:
return self.publisher[:8]
acronym = ""
for word in x_words:
acronym += word[0].upper()
short_name = ""
for word in x_words:
word = word.capitalize()
short_name += word[:3]
return short_name if len(short_name) <= 8 else acronym
@cached_property
def subject(self):
return self._scenario_data['concepts'].get(self.issn_l, {}).get("best", "")
@cached_property
def subject_top_three(self):
return self._scenario_data['concepts'].get(self.issn_l, {}).get("top_three", "")
@cached_property
def subjects_all(self):
return self._scenario_data["concepts"].get(self.issn_l, {}).get("all", [])
@cached_property
def journal_metadata(self):
return self.my_package.get_journal_metadata(self.issn_l)
@cached_property
def issns(self):
return self.journal_metadata.issns
@cached_property
def institution_id(self):
return self.scenario.institution_id
@cached_property
def institution_name(self):
return self.scenario.institution_name
@cached_property
def institution_short_name(self):
return self.scenario.institution_short_name
@cached_property
def cost_first_year_including_content_fee(self):
# return float(self.my_scenario_data_row.get("price", 0)) * (1 + self.settings.cost_content_fee_percent/float(100))
my_lookup = self._scenario_data["prices"]
if my_lookup.get(self.issn_l, None) is None:
print("no price for {}".format(self.issn_l))
return None
# print "my price", self.issn_l, float(my_lookup.get(self.issn_l)) * (1 + self.settings.cost_content_fee_percent/float(100))
return float(my_lookup.get(self.issn_l)) * (1 + self.settings.cost_content_fee_percent/float(100))
@cached_property
def papers_2018(self):
response = self.my_scenario_data_row.get("num_papers_2018", 0)
if not response:
return 0
return response
@cached_property
def num_citations_historical_by_year(self):
try:
my_dict = self._scenario_data[self.package_id_for_db]["citation_dict"].get(self.issn_l, {})
except KeyError:
# print "key error in num_citations_historical_by_year for {}".format(self.issn_l)
return [0 for year in self.years]
# the year is a string key alas
if my_dict and isinstance(list(my_dict.keys())[0], int):
return [my_dict.get(year, 0) for year in self.historical_years_by_year]
else:
return [my_dict.get(str(year), 0) for year in self.historical_years_by_year]
@cached_property
def num_citations(self):
return round(np.mean(self.num_citations_historical_by_year), 4)
@cached_property
def num_authorships_historical_by_year(self):
try:
my_dict = self._scenario_data[self.package_id_for_db]["authorship_dict"].get(self.issn_l, {})
except KeyError:
# print "key error in num_authorships_historical_by_year for {}".format(self.issn_l)
return [0 for year in self.years]
# the year is a string key alas
if my_dict and isinstance(list(my_dict.keys())[0], int):
return [my_dict.get(year, 0) for year in self.historical_years_by_year]
else:
return [my_dict.get(str(year), 0) for year in self.historical_years_by_year]
@cached_property
def num_authorships(self):
return round(np.mean(self.num_authorships_historical_by_year), 4)
@cached_property
def bronze_oa_embargo_months(self):
return self._scenario_data["embargo_dict"].get(self.issn_l, None)
def set_subscribe_bulk(self):
self.subscribed_bulk = True
# invalidate cache
for key in self.__dict__:
if "actual" in key:
del self.__dict__[key]
def set_unsubscribe_bulk(self):
self.subscribed_bulk = False
# invalidate cache
for key in self.__dict__:
if "actual" in key:
del self.__dict__[key]
def set_subscribe_custom(self):
self.subscribed_custom = True
# invalidate cache
for key in self.__dict__:
if "actual" in key:
del self.__dict__[key]
def set_unsubscribe_custom(self):
self.subscribed_custom = False
# invalidate cache
for key in self.__dict__:
if "actual" in key:
del self.__dict__[key]
@cached_property
def years_by_year(self):
return [self.now.year + year_index for year_index in self.years]
@cached_property
def historical_years_by_year(self):
# used for citation, authorship lookup
return list(range(self.now.year - 5, self.now.year))
@cached_property
def cost_actual_by_year(self):
if self.subscribed:
return self.subscription_cost_by_year
return self.ill_cost_by_year
@cached_property
def cost_actual(self):
if self.subscribed:
return self.subscription_cost
return self.ill_cost
@cached_property
def subscription_cost_by_year(self):
if self.cost_first_year_including_content_fee is not None:
response = [round(((1+self.settings.cost_alacart_increase/float(100))**year) * self.cost_first_year_including_content_fee )
for year in self.years]
else:
# will cause errors further down the line
response = [None for year in self.years]
return response
@cached_property
def subscription_cost(self):
return round(np.mean(self.subscription_cost_by_year), 4)
@cached_property
def cpu(self):
if not self.use_paywalled or self.use_paywalled < 1:
return None
return round(self.cost_subscription_minus_ill/self.use_paywalled, 6)
@cached_property
def old_school_cpu(self):
if not self.downloads_total or self.downloads_total < 1:
return None
return round(float(self.subscription_cost)/self.downloads_total, 6)
@cached_property
def use_weight_multiplier(self):
if not self.downloads_total:
return 1.0
return float(self.use_total) / self.downloads_total
@cached_property
def use_free_instant_by_year(self):
response = [0 for year in self.years]
for group in use_groups_free_instant:
for year in self.years:
response[year] += self.__getattribute__("use_{}_by_year".format(group))[year]
return response
@cached_property
def use_instant_by_year(self):
response = [0 for year in self.years]
for group in use_groups_free_instant:
for year in self.years:
response[year] += self.__getattribute__("use_{}_by_year".format(group))[year]
if self.subscribed:
group = "subscription"
for year in self.years:
response[year] += self.__getattribute__("use_{}_by_year".format(group))[year]
return response
@cached_property
def use_instant(self):
# return round(np.mean(self.use_instant_by_year), 4)
response = 0
for group in use_groups_free_instant:
response += self.__getattribute__("use_{}".format(group))
if self.subscribed:
group = "subscription"
response += self.__getattribute__("use_{}".format(group))
return response
@cached_property
def use_free_instant(self):
# return round(np.mean(self.use_free_instant_by_year), 4)
response = 0
for group in use_groups_free_instant:
response += self.__getattribute__("use_{}".format(group))
return response
@cached_property
def downloads_subscription_by_year(self):
return self.downloads_paywalled_by_year
@cached_property
def downloads_subscription(self):
return self.downloads_paywalled
@cached_property
def use_subscription(self):
return self.use_paywalled
@cached_property
def use_subscription_by_year(self):
return [self.use_paywalled_by_year[year] for year in self.years]
@cached_property
def downloads_social_network_multiplier(self):
if not self.settings.include_social_networks:
return 0.0
return self._scenario_data["social_networks"].get(self.issn_l, 0.06)
@cached_property
def downloads_social_networks_by_year(self):
if not self.downloads_social_network_multiplier:
return [0.0 for year in self.years]
response = [0.0 for year in self.years]
for year in self.years:
social_network = self.downloads_total_by_year[year] * self.downloads_social_network_multiplier
if social_network:
overlap_with_backfile = (social_network * self.downloads_backfile_by_year[year]) / self.downloads_total_by_year[year]
# print social_network, self.downloads_backfile_by_year[year], self.downloads_total_by_year[year], overlap_with_backfile
social_network = social_network - overlap_with_backfile
response[year] = social_network
# response = [self.downloads_total_by_year[year] * self.downloads_social_network_multiplier for year in self.years]
response = [min(response[year], self.downloads_total_by_year[year] - self.downloads_oa_by_year[year]) for year in self.years]
response = [max(response[year], 0) for year in self.years]
return response
@cached_property
def downloads_social_networks(self):
return round(np.mean(self.downloads_social_networks_by_year), 4)
@cached_property
def use_social_networks_by_year(self):
response = [max(0, round(self.downloads_social_networks_by_year[year] * self.use_weight_multiplier, 4)) for year in self.years]
response = [min(response[year], self.use_total_by_year[year] - self.use_oa_by_year[year]) for year in self.years]
response = [max(response[year], 0) for year in self.years]
return response
@cached_property
def use_social_networks(self):
# return round(self.downloads_social_networks * self.use_weight_multiplier, 4)
response = min(np.mean(self.use_social_networks_by_year), self.use_total - self.use_oa)
return response
@cached_property
def downloads_ill_by_year(self):
response = [self.settings.ill_request_percent_of_delayed/float(100) * self.downloads_paywalled_by_year[year] for year in self.years]
response = [num if num else 0 for num in response]
return response
@cached_property
def downloads_ill(self):
return round(np.mean(self.downloads_ill_by_year), 4)
@cached_property
def use_ill(self):
return self.settings.ill_request_percent_of_delayed/float(100) * self.use_paywalled
@cached_property
def use_ill_by_year(self):
return [self.settings.ill_request_percent_of_delayed/float(100) * self.use_paywalled_by_year[year] for year in self.years]
@cached_property
def downloads_other_delayed_by_year(self):
return [self.downloads_paywalled_by_year[year] - self.downloads_ill_by_year[year] for year in self.years]
@cached_property
def downloads_other_delayed(self):
return round(np.mean(self.downloads_other_delayed_by_year), 4)
@cached_property
def use_other_delayed(self):
return self.use_paywalled - self.use_ill
@cached_property
def use_other_delayed_by_year(self):
return [self.use_paywalled_by_year[year] - self.use_ill_by_year[year] for year in self.years]
@cached_property
def display_perpetual_access_years(self):
if not self.perpetual_access_years:
return ""
if min(self.perpetual_access_years) < min(self.year_by_perpetual_access_years):
return "<{}-{}".format(min(self.perpetual_access_years), max(self.perpetual_access_years))
return "{}-{}".format(min(self.perpetual_access_years), max(self.perpetual_access_years))
@cached_property
def has_perpetual_access(self):
# print "has_perpetual_access", self.perpetual_access_years
if not self.perpetual_access_years:
return False
return True
@cached_property
def year_by_perpetual_access_years(self):
return list(range(min(self.historical_years_by_year)-5, max(self.historical_years_by_year)+1))
@cached_property
def perpetual_access_years(self):
# if no perpetual access data for any journals in this scenario, then we are acting like it has perpetual access to everything
# else, for this journal
# if two dates, that is the perpetual access range
# if a start date and no end date, then has perpetual access till the model says it doesn't
# if no start date, then no perpetual access
# if not there, then no perpetual access
data_dict = self._scenario_data["perpetual_access"]
# if not data_dict:
# return self.year_by_perpetual_access_years
if self.issn_l not in data_dict:
return []
start_date = data_dict[self.issn_l]["start_date"]
end_date = data_dict[self.issn_l]["end_date"]
# if no dates, then no perpetual access
if not start_date:
start_date = datetime.datetime(1850, 1, 1) # far in the past
# if no end date, then has perpetual access till the model says it doesn't
if not end_date:
end_date = datetime.datetime(2042, 1, 2) # far in the future, let's really hope we have universal OA by then
# if two dates, that is the perpetual access range
response = []
for year in self.year_by_perpetual_access_years:
working_date = datetime.datetime(year, 1, 2).isoformat() # use January 2nd
try:
start_date = start_date.isoformat()
except:
pass
try:
end_date = end_date.isoformat()
except:
pass
in_range = working_date > start_date and working_date < end_date
if in_range:
# print year, "yes", data_dict[self.issn_l]
response.append(year)
else:
pass
# print year, "no", data_dict[self.issn_l]
return response
@cached_property
def downloads_backfile_by_year(self):
response = self.sum_obs_pub_matrix_by_obs(self.backfile_obs_pub)
response = [min(response[year], self.downloads_total_by_year[year] - self.downloads_oa_by_year[year]) for year in self.years]
return response
@cached_property
def downloads_obs_pub(self):
by_age = self.downloads_by_age
by_age_old = self.downloads_total_older_than_five_years/5.0
growth_scaling = self.growth_scaling_downloads
my_matrix = self.obs_pub_matrix(by_age, by_age_old, growth_scaling)
return my_matrix
@cached_property
def oa_obs_pub(self):
by_age = self.downloads_oa_by_age
if not self.downloads_by_age[4]:
by_age_old = (self.downloads_total_older_than_five_years/5.0)
else:
by_age_old = (self.downloads_total_older_than_five_years/5.0) * (self.downloads_oa_by_age[4]/(self.downloads_by_age[4]))
growth_scaling = self.growth_scaling_oa_downloads
my_matrix = self.obs_pub_matrix(by_age, by_age_old, growth_scaling)
return my_matrix
@cached_property
def backfile_raw_obs_pub(self):
response = {}
for obs_year in range(self.now.year, self.now.year + 5):
obs_key = "obs{}".format(obs_year)
response[obs_key] = {}
for pub_year in range(self.now.year - 10, self.now.year + 5):
pub_key = "pub{}".format(pub_year)
# modelling subscription ending in 2020, so no backfile beyond that
if pub_year in self.perpetual_access_years:
value = self.downloads_obs_pub[obs_key][pub_key] - self.oa_obs_pub[obs_key][pub_key]
elif pub_year-1 in self.perpetual_access_years:
value = 0.5*(self.downloads_obs_pub[obs_key][pub_key] - self.oa_obs_pub[obs_key][pub_key])
else:
value = 0
value = max(value, 0)
response[obs_key][pub_key] = int(round(value))
return response
@cached_property
def backfile_obs_pub(self):
response = {}
for obs_year in range(self.now.year, self.now.year + 5):
obs_key = "obs{}".format(obs_year)
response[obs_key] = {}
for pub_year in range(self.now.year - 10, self.now.year + 5):
pub_key = "pub{}".format(pub_year)
value = self.backfile_raw_obs_pub[obs_key][pub_key]
# value *= (self.settings.backfile_contribution / 100.0)
value = max(0, value)
response[obs_key][pub_key] = int(round(value))
return response
def obs_pub_matrix(self, by_age, by_age_old, growth_scaling):
response = {}
for obs_index, obs_year in enumerate(range(self.now.year, self.now.year + 5)):
response["obs{}".format(obs_year)] = {}
for pub_year in range(self.now.year - 10, self.now.year + 5):
age = obs_year - pub_year
value = 0
if age >= 0 and age <= 4:
value = int(round(by_age[age]))
elif age >= 5 and age <= 9:
value = int(round(by_age_old))
value *= growth_scaling[obs_index]
response["obs{}".format(obs_year)]["pub{}".format(pub_year)] = int(round(value))
return response
def display_obs_pub_matrix(self, my_obs_pub_matrix):
response = []
obs_keys_ordered = sorted(my_obs_pub_matrix.keys())
for obs_key in obs_keys_ordered:
sub_response = []
pub_row = my_obs_pub_matrix[obs_key]
pub_keys_ordered = sorted(pub_row.keys())
for pub_key in pub_keys_ordered:
sub_response.append(pub_row[pub_key])
response.append(sub_response)
return response
def sum_obs_pub_matrix_by_obs(self, my_obs_pub_matrix):
response = [0 for year in self.years]
for i, obs_year in enumerate(range(self.now.year, self.now.year + 5)):
obs_key = "obs{}".format(obs_year)
for pub_year in range(self.now.year - 10, self.now.year + 5):
pub_key = "pub{}".format(pub_year)
response[i] += my_obs_pub_matrix[obs_key][pub_key]
return response
@cached_property
def downloads_backfile(self):
return round(np.mean(self.downloads_backfile_by_year), 4)
@cached_property
def use_backfile_by_year(self):
response = [max(0, round(self.downloads_backfile_by_year[year] * self.use_weight_multiplier, 4)) for year in self.years]
response = [min(response[year], self.use_total_by_year[year] - self.use_oa_by_year[year]) for year in self.years]
return response
@cached_property
def use_backfile(self):
# response = [min(response[year], self.use_total_by_year[year] - self.use_oa_by_year[year]) for year in self.years]
response = min(np.mean(self.use_backfile_by_year), self.use_total - self.use_oa - self.use_social_networks)
return round(response, 4)
@cached_property
def raw_num_oa_historical_by_year(self):
return [self.num_green_historical_by_year[year]+self.num_bronze_historical_by_year[year]+self.num_hybrid_historical_by_year[year] for year in self.years]
@cached_property
def use_oa_plus_social_networks(self):
return self.use_oa + self.use_social_networks
@cached_property
def use_oa_plus_social_networks_by_year(self):
return [self.use_oa_by_year[year] + self.use_social_networks_by_year[year] for year in self.years]
@cached_property
def downloads_oa_by_year(self):
# if self.issn_l == "0271-678X":
# print "self.oa_obs_pub", self.oa_obs_pub
# print "self.sum_obs_pub_matrix_by_obs(self.oa_obs_pub)", self.sum_obs_pub_matrix_by_obs(self.oa_obs_pub)
return self.sum_obs_pub_matrix_by_obs(self.oa_obs_pub)
@cached_property
def downloads_oa_plus_social_networks_by_year(self):
return [self.downloads_oa_by_year[year] + self.downloads_social_networks_by_year[year] for year in self.years]
@cached_property
def use_oa(self):
# return round(self.downloads_oa * self.use_weight_multiplier, 4)
# return self.use_oa_green + self.use_oa_bronze + self.use_oa_hybrid
# if self.issn_l == "0271-678X":
# print "self.use_oa_by_year", self.use_oa_by_year
response = min(np.mean(self.use_oa_by_year), self.use_total)
return round(response, 4)
@cached_property
def use_oa_by_year(self):
# TODO fix
response = [max(0, self.downloads_oa_by_year[year] * self.use_weight_multiplier) for year in self.years]
response = [min(response[year], self.use_total_by_year[year]) for year in self.years]
return response
@cached_property
def use_oa_percent_by_year(self):
# print self.use_oa_by_year, self.use_total_by_year
response = [min(100, round(100.0*(self.use_oa_by_year[year]/(1.0+self.use_total_by_year[year])), 1)) for year in self.years]
return response
@cached_property
def downloads_total_by_year(self):
scaled = [self.downloads_scaled_by_counter_by_year[year] * self.growth_scaling_downloads[year] for year in self.years]
return scaled
@cached_property
def downloads_total(self):
return round(np.mean(self.downloads_total_by_year), 4)
# used to calculate use_weight_multiplier so it can't use it
@cached_property
def use_total_by_year(self):
return [self.downloads_total_by_year[year] + self.use_addition_from_weights*self.growth_scaling_downloads[year] for year in self.years]
@cached_property
def use_total(self):
response = round(np.mean(self.use_total_by_year), 4)
if response == 0:
response = 0.0001
return response
@cached_property
def raw_downloads_by_age(self):
# isn't replaced by default if too low or not monotonically decreasing
total_downloads_by_age_before_counter_correction = [self.my_scenario_data_row.get("downloads_{}y".format(age), 0) for age in self.years]
total_downloads_by_age_before_counter_correction = [val if val else 0 for val in total_downloads_by_age_before_counter_correction]
downloads_by_age = [num * self.downloads_counter_multiplier for num in total_downloads_by_age_before_counter_correction]
return downloads_by_age
@cached_property
def curve_fit_for_downloads(self):
x = np.array(self.years)
y = np.array(self.downloads_by_age_before_counter_correction)
initial_guess = (float(np.max(y)), 30.0, -1.0) # determined empirically
def func(x, a, b, c):
try:
response = b + a * scipy.special.expit( x / c )
except:
response = None
return response
try:
pars, pcov = curve_fit(func, x, y, initial_guess)
except:
return {}
y_fit = [func(a, pars[0], pars[1], pars[2]) for a in x]
residuals = y - y_fit
ss_res = np.sum(residuals**2) + 0.0001
ss_tot = np.sum((y - np.mean(y))**2) + 0.0001
r_squared = 1 - (ss_res / ss_tot)
return {"y_fit": y_fit,
"r_squared": r_squared,
"params": list(pars),
"input_y": list(y)}
@cached_property
def downloads_by_age_before_counter_correction(self):
downloads_by_age_before_counter_correction = [self.my_scenario_data_row.get("downloads_{}y".format(age), 0) for age in self.years]
downloads_by_age_before_counter_correction = [val if val else 0 for val in downloads_by_age_before_counter_correction]
return downloads_by_age_before_counter_correction
@cached_property
def downloads_by_age(self):
self.use_default_download_curve = False
# although the curve fit is on downloads, download number probably off if there are some years with no papers,
# so in those cases just use the default
nonzero_paper_years = [year for year in self.years if self.raw_num_papers_historical_by_year[year]]
if len(nonzero_paper_years) == 5:
scipy_lock.acquire()
my_curve_fit = self.curve_fit_for_downloads
scipy_lock.release()
if my_curve_fit and my_curve_fit["r_squared"] >= 0.75:
# print u"GREAT curve fit for {}, r_squared {}".format(self.issn_l, my_curve_fit.get("r_squared", "no r_squared"))
downloads_by_age_before_counter_correction_curve_to_use = my_curve_fit["y_fit"]
else:
# print u"bad curve fit for {}, r_squared {}".format(self.issn_l, my_curve_fit.get("r_squared", "no r_squared"))
self.use_default_download_curve = True
else:
self.use_default_download_curve = True
if self.use_default_download_curve:
sum_total_downloads_by_age_before_counter_correction = np.sum(self.downloads_by_age_before_counter_correction)
downloads_by_age_before_counter_correction_curve_to_use = [num*sum_total_downloads_by_age_before_counter_correction for num in default_download_by_age]
downloads_by_age = [num * self.downloads_counter_multiplier for num in downloads_by_age_before_counter_correction_curve_to_use]
downloads_by_age = [max(val, 0.0) for val in downloads_by_age]
return downloads_by_age
@cached_property
def downloads_total_older_than_five_years(self):
if self.use_default_download_curve:
return default_download_older_than_five_years * (self.downloads_total)
return self.downloads_total - np.sum(self.downloads_by_age)
@cached_property
def downloads_per_paper_by_age(self):
# TODO do separately for each type of OA
# print [[float(num), self.num_papers, self.num_oa_historical] for num in self.downloads_by_age]
if self.num_papers:
return [float(num)/self.num_papers for num in self.downloads_by_age]
return [0 for num in self.downloads_by_age]
@cached_property
def downloads_scaled_by_counter_by_year(self):
# TODO is flat right now
downloads_total_before_counter_correction_by_year = [max(1.0, self.my_scenario_data_row.get("downloads_total", 0.0) or 0.0) for year in self.years]
downloads_total_before_counter_correction_by_year = [val if val else 0.0 for val in downloads_total_before_counter_correction_by_year]
downloads_total_scaled_by_counter = [num * self.downloads_counter_multiplier for num in downloads_total_before_counter_correction_by_year]
return downloads_total_scaled_by_counter
@cached_property
def downloads_per_paper(self):
per_paper = float(self.downloads_scaled_by_counter_by_year)/self.num_papers
return per_paper
@cached_property
def proportion_oa_historical_by_year(self):
response = []
for year in self.years:
if self.raw_num_papers_historical_by_year[year]:
response.append(float(self.raw_num_oa_historical_by_year[year] or 0) / self.raw_num_papers_historical_by_year[year])
else:
response.append(None)
# if self.issn_l == "0031-9406":
# print "self.raw_num_oa_historical_by_year", self.raw_num_oa_historical_by_year
# print self.raw_num_papers_historical_by_year
# print "response", response
# print
return response
@cached_property
def num_oa_historical_by_year(self):
oa_proportion_reversed = self.proportion_oa_historical_by_year[::-1]
num_scaled_by_num_papers = []
for year in self.years:
if oa_proportion_reversed[year] and oa_proportion_reversed[year]:
num_scaled_by_num_papers.append(oa_proportion_reversed[year]*self.num_papers_by_year[year])
else:
num_scaled_by_num_papers.append(0)
# if self.issn_l == "0031-9406":
# print "self.num_papers_growth_from_2018_by_year", self.num_papers_growth_from_2018_by_year
# print oa_proportion_reversed, "oa_proportion_reversed"
# print num_scaled_by_num_papers, "num_scaled_by_num_papers"
# print self.num_papers_by_year, "num_papers_by_year"
# print
return [int(round(min(self.num_papers_by_year[year], num_scaled_by_num_papers[year]))) for year in self.years]
@cached_property
def downloads_oa_by_age(self):
response = [(float(self.downloads_per_paper_by_age[age])*self.num_oa_historical_by_year[age]) for age in self.years]
if self.bronze_oa_embargo_months:
for age in self.years:
if age*12 >= self.bronze_oa_embargo_months:
response[age] = self.downloads_by_age[age]
# if self.issn_l == "0031-9406":
# print "self.num_oa_historical_by_year", self.num_oa_historical_by_year
# print "downloads_per_paper_by_age", self.downloads_per_paper_by_age
# print "downloads_by_age", self.downloads_by_age
# print "downloads_by_age_before_counter_correction", self.downloads_by_age_before_counter_correction
# print "downloads_oa_by_age", response
return response
@cached_property
def downloads_oa_bronze_by_age(self):
response = [(float(self.downloads_per_paper_by_age[age])*self.num_bronze_by_year[age]) for age in self.years]
return response
@cached_property
def downloads_oa_green_by_age(self):
response = [(float(self.downloads_per_paper_by_age[age])*self.num_green_by_year[age]) for age in self.years]
return response
@cached_property
def num_hybrid_by_year(self):
num_reversed = self.num_hybrid_historical_by_year[::-1]
return [min(self.num_papers_by_year[year],
num_reversed[year]) for year in self.years]
@cached_property
def num_bronze_by_year(self):
num_reversed = self.num_bronze_historical_by_year[::-1]
return [min(self.num_papers_by_year[year] - self.num_hybrid_by_year[year],
num_reversed[year]) for year in self.years]
@cached_property
def num_green_by_year(self):
num_reversed = self.num_green_historical_by_year[::-1]
return [min(self.num_papers_by_year[year] - self.num_hybrid_by_year[year] - self.num_bronze_by_year[year],
num_reversed[year]) for year in self.years]
@cached_property
def downloads_oa_hybrid_by_age(self):
response = [(float(self.downloads_per_paper_by_age[age])*self.num_hybrid_by_year[age]) for age in self.years]
return response
@cached_property
def downloads_oa_peer_reviewed_by_age(self):
num_reversed = self.num_peer_reviewed_historical_by_year[::-1]
num_for_convolving = [min(self.num_papers_by_year[year], num_reversed[year]) for year in self.years]
response = [(float(self.downloads_per_paper_by_age[age])*num_for_convolving[age]) for age in self.years]
return response
@cached_property
def downloads_paywalled_by_year(self):
scaled = [self.downloads_total_by_year[year]
- (self.downloads_backfile_by_year[year] + self.downloads_oa_by_year[year] + self.downloads_social_networks_by_year[year])
for year in self.years]
scaled = [max(0, num) for num in scaled]
return scaled
@cached_property
def downloads_paywalled(self):
return round(np.mean(self.downloads_paywalled_by_year), 4)
@cached_property
def use_paywalled(self):
return max(0, self.use_total - self.use_free_instant)
@cached_property
def use_paywalled_by_year(self):
return [max(0, self.use_total_by_year[year] - self.use_free_instant_by_year[year]) for year in self.years]
@cached_property
def downloads_counter_multiplier_normalized(self):
return round(self.downloads_counter_multiplier / self.scenario.downloads_counter_multiplier, 4)
@cached_property
def use_weight_multiplier_normalized(self):
return round(self.use_weight_multiplier / self.scenario.use_weight_multiplier, 4)
@cached_property
def downloads_actual(self):
response = defaultdict(int)
for group in use_groups:
response[group] = round(np.mean(self.downloads_actual_by_year[group]), 4)
return response
@cached_property
def use_actual(self):
response = defaultdict(int)
for group in use_groups + ["oa_plus_social_networks"]:
response[group] = self.__getattribute__("use_{}".format(group))
if self.subscribed:
response["ill"] = 0
response["other_delayed"] = 0
else:
response["subscription"] = 0
response["oa_no_social_networks"] = response["oa"]
return response
@cached_property
def downloads_actual_by_year(self):
#initialize
my_dict = {}
# include the if to skip this if no useage
if self.downloads_total:
for group in use_groups:
my_dict[group] = self.__getattribute__("downloads_{}_by_year".format(group))
if self.subscribed:
my_dict["ill"] = [0 for year in self.years]
my_dict["other_delayed"] = [0 for year in self.years]
else:
my_dict["subscription"] = [0 for year in self.years]
return my_dict
@cached_property
def use_actual_by_year(self):
my_dict = {}
for group in use_groups:
# defaults
my_dict[group] = self.__getattribute__("use_{}_by_year".format(group))
if self.subscribed:
my_dict["ill"] = [0 for year in self.years]
my_dict["other_delayed"] = [0 for year in self.years]
else:
my_dict["subscription"] = [0 for year in self.years]
return my_dict
@cached_property
def downloads_total_before_counter_correction(self):
return max(1.0, self.my_scenario_data_row.get("downloads_total", 0.0))
@cached_property
def use_addition_from_weights(self):
# using the average on purpose... by year too rough
weights_addition = 0
# the if is to help speed it up
if self.num_citations or self.num_authorships:
weights_addition = float(self.settings.weight_citation) * self.num_citations
weights_addition += float(self.settings.weight_authorship) * self.num_authorships
weights_addition = round(weights_addition, 4)
return weights_addition
@cached_property
def downloads_counter_multiplier(self):
try:
counter_for_this_journal = self._scenario_data[self.package_id_for_db]["counter_dict"][self.issn_l]
counter_multiplier = float(counter_for_this_journal) / self.downloads_total_before_counter_correction
except:
counter_multiplier = float(0)
return counter_multiplier
@cached_property
def ill_cost(self):
return round(np.mean(self.ill_cost_by_year), 4)
@cached_property
def ill_cost_by_year(self):
return [round(self.downloads_ill_by_year[year] * self.settings.cost_ill, 4) for year in self.years]
@cached_property
def cost_subscription_minus_ill_by_year(self):
return [self.subscription_cost_by_year[year] - self.ill_cost_by_year[year] for year in self.years]
@cached_property
def cost_subscription_minus_ill(self):
return round(self.subscription_cost - self.ill_cost, 4)
@cached_property
def cpu_rank(self):
if self.cpu:
try:
return self.scenario.cpu_rank_lookup[self.issn_l]
except ReferenceError:
return None
return None
@cached_property
def old_school_cpu_rank(self):
if self.old_school_cpu:
return self.scenario.old_school_cpu_rank_lookup[self.issn_l]
return None
@cached_property
def cost_subscription_fuzzed(self):
return self.scenario.cost_subscription_fuzzed_lookup[self.issn_l]
@cached_property
def cost_subscription_minus_ill_fuzzed(self):